List Devices in Workflow

Hi again,

I’m using a workflow to determine whether my REST-based devices are still connected to Losant, based on the last time they sent a “heartbeat” attribute report to Losant. If the heartbeat was too long ago, I consider them disconnected and create an event to report the problem.

Because a disconnected device can’t report itself disconnected (over REST, at least), I trigger my watchdog workflow with a timer. When the timer fires, I examine each device matching my filter criteria (right now, just a tag filter on the device recipe). Currently, I’m able to do this as follows: when the timer expires, the workflow calls an AWS Lambda function that fetches a list of the devices then POSTs the list to a webhook that triggers another part of the same workflow.

The Lambda function is basically a means of transferring the result of GET /devices on the Losant API to the workflow. The piece that would allow me to eliminate the Lambda function is a workflow node that emits a list of devices.

The workflow ID is 578d165826434a01005416b0 in application 577d2bdc7b3f830100d937ae, for a more concrete example.

Of course, complementing this request would be a workflow node that performs a for-each on a specified array in the payload. :slight_smile: For each element in the array, it could place that current element at a specified payload path then emit the current payload to its output. It would thus emit one copy of the payload for each element in the array, with each emitted payload containing a reference to the current array element. Because workflows already have conditionals and a way to “jump” by connecting the graph in a loop, the for-each node wouldn’t offer anything that is not already possible, but it would save some space and reduce the number of moving parts in the workflow.

For the most part, it seems that the workflows are geared towards dealing with one payload at a time, end-to-end, so perhaps my need for a loop suggests that I’m drifting off base.

The HTTP Data node can request GET /devices and parse the JSON, so you get a list of devices in payload.

Losant Workflow doesn’t have a ForEach, unlike Yahoo Pipes.
One workaround is round robin:

  1. get value: load the deviceId processed last time, as lastId
  2. raw function: sort the device array by ascending deviceId
  3. raw function: choose the first deviceId that is greater than lastId; if all deviceIds are less than lastId, choose the smallest deviceId
  4. store value: save the chosen deviceId as lastId

Therefore, each workflow execution will process one device, but every device gets served eventually.

Good idea to use the HTTP node. It does involve two of them (one for authorization, and one to perform the request), but I can remove the Lambda function that way for now.

The round-robin workaround is interesting. I already do have a loop in the workflow, created by setting an iterator variable in the payload then feeding it back through a conditional to check the value of the iterator. It would just be convenient to have a loop block :slight_smile:

Totally agree on the loop. We’ve seen so many use-cases for it. We’ve been experimenting with some UX and do hope to have something soon.

As for getting /devices, the HTTP node is a good way to go. Tokens do not currently expire, so you could generate an API token externally and then simply use it inside the workflow (put it in globals or something), which would eliminate the need for the extra authentication http node.