Example reference to Device in Function node

How to use Javascript / Function node to do conditional assignments to a set of Devices instead of the graphical way?

Specifically, how to refer to a specific device in Javascript? Can you show a simple example?

It’s pretty tedious graphically. I’d like to do this in script. Thanks!

@Bryan_Chase,

I’m a little unsure of what your workflow is trying to accomplish. Could you elaborate more on the purpose of this workflow?

The Switch Node may help out clean up the workflow a bit. But, with more context, we can talk about better ways your goal could be accomplished.

I’m not sure what you mean by “refer to a specific device in Javascript”. What would you reference? What would you like to do in the Function with the Device?

My workflow is reading from an API and putting several data structures into the various brandches of the playload. Based on 2 separate payload fields (IDs of 2 different types of sensors), it needs to update 1 of several devices with information (with a 3rd field: signal strength).

In the SS I showed, I’ve connected up only 14 total sensors using the UI. Just to get that it was a lot of (too much) clicking and dragging of lines. It won’t scale when we need to go to 40 or 400 sensors. I’d rather do this in text, so I can use a text editor and do quick search/replace. I’d rather not connect up a structure 5 or 20 times that size. If one of the devices changes, then I’d have to click in each box, choose a different device and save it. That’s not manageable.

So, how do I write JS in a Function Node to set state in a Device? I see hints of it on other Forum posts but, how do I need to define a new device and it’ll match up the ID? Or how should I do it?

@Bryan_Chase,

@JuliaKempf working on a complete example for you.

For now, however, Device Tags are the way I’d recommend solving this problem in a scalable way. You can easily store deviceId or deviceType as a custom tag on your device.

In your workflow, you can use the Device: Get Node to query devices based on their tag. Then, after you query the device, you can report its state. It would take the workflow you have now down to something that resembles this:

This is the screenshot that’s in the docs. Notice the usage of templates in the node configuration. Feel free to shoot over any more questions you may have.

Hi @Bryan_Chase,

I built out an example workflow for this, let’s dive in!

I have multiple alarm devices that report state through a Webhook:

Now, my device’s data comes to my workflow with an identifier value that corresponds to one of my devices.

Screen Shot 2020-01-28 at 3.57.18 PM

Since my devices are named Alarm 1 and Alarm 2, and this identifier value is a string of numbers and letters, I make use of tags for these devices.

Over on the device page, I create a new tag identifier and insert the value from my physical device that corresponds to my Losant device. I do this for each alarm device.

Then, over in my workflow, I include a Device: Get Node and set the query method to “Match any tags.”

I then configure the query and reference the identifier value that exists on the payload:

And finally, you can see in the Debug Log that the node has retrieved the correct device. You can now update your device state using the available deviceId value.

Please let me know if I can explain any further!

Thanks,
Julia

Julia,

Thanks for the reply. I’m only now getting back to this. My question is more about how to use the FUNCTION block to do this. For example, I want to update devices using a FUNCTION block. For example, using your device identifier:

Hi @Bryan_Chase,

It is not currently possible to set Device State from the Function Node. The Function Node is intended for custom Javascript that operates on the current payload, it is not able to report state or update devices. Building on the walkthrough above, I would still recommend using the Device: Get Node, and then following with the Device State Node.

Thanks!
Julia

Hi Julia,

Thanks for the reply. I do like the way you’ve done it using the Get Node and whatnot. I’ll try that style. I still like the idea of updating Device State from a function node, if that can be considered for the future.

Thanks for the great support!

1 Like