Looping through devices in a workflow

#1

I am trying to loop through multiple peripheral devices in a workflow to determine if an attribute has changed, and if so, issue a command to the controller device. The problem is that the devices are not static.

I have one device (type Controller) that contains the number of peripheral devices as an attribute (nAreas) which I can acquire using a Gauge Query on the controller device. All of the Area devices use a common tag (deviceType) set to “Area” so I can get device information for each of the Area type devices using a Gauge Query specifying the tag deviceType=“Area”, and the array is added to the payload. So far so good.

But now I want to loop through each Area device, get it’s attribute “level” and see if it’s changed. And if so, issue a command to the device.

Any advice on how to handle looping like this in a workflow?

0 Likes

#2

We’ve got a high priority task to release a loop construct, which will make things much easier. For now you can create a loop in a workflow manually. The easiest way is to put an index on the payload, increment it, and check it using a conditional. Here’s a screenshot of a basic loop.

It’s a little ugly because we’re attaching the output of one node to the input of a previous node, but that’s what creates the loop. The math node increments the index {{ data.index }} + 1 and saves it back on data.index.

You may want to use a function node to easily pull the current item in the array out and place it on the payload. Unfortunately handlebars does not make it easy to access an item in an array when the index you want is on another variable.

payload.data.currentDevice = payload.data.items[payload.data.index];
0 Likes