# Example reference to Device in Function node

**URL:** <https://forums.losant.com/t/example-reference-to-device-in-function-node/2579>\
**Category:** Feature Request\
**Tags:** workflow\
**Created:** [January 28, 2020, 7:10am UTC](https://forums.losant.com/t/example-reference-to-device-in-function-node/2579 "2020-01-28T07:10:30Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bryan\_Chase](https://avatars.discourse-cdn.com/v4/letter/b/5f8ce5/32.png) [@Bryan\_Chase](https://forums.losant.com/u/Bryan_Chase)\
**Post date:** [January 28, 2020, 7:10am UTC](https://forums.losant.com/t/example-reference-to-device-in-function-node/2579/1 "2020-01-28T07:10:30Z")

</div>

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!

 ![image](https://us1.discourse-cdn.com/flex015/uploads/getstructure/original/2X/5/5f0cbd671baadaa00661a0992f17f6487ecee2cf.png)

---

<div class="post-metadata">

**Author:** ![anaptfox](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/anaptfox/32/873_2.png) [@anaptfox](https://forums.losant.com/u/anaptfox)\
**Post date:** [January 28, 2020, 2:08pm UTC](https://forums.losant.com/t/example-reference-to-device-in-function-node/2579/2 "2020-01-28T14:08:49Z")

</div>

@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?

> [@Bryan\_Chase](#):
>
> How to use Javascript / Function node to do conditional assignments to a set of Devices instead of the graphical way?

The [Switch Node](https://docs.losant.com/workflows/logic/switch/) may help out clean up the workflow a bit. But, with more context, we can talk about better ways your goal could be accomplished.

> [@Bryan\_Chase](#):
>
> Specifically, how to refer to a specific device in Javascript? Can you show a simple example?

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?

---

<div class="post-metadata">

**Author:** ![Bryan\_Chase](https://avatars.discourse-cdn.com/v4/letter/b/5f8ce5/32.png) [@Bryan\_Chase](https://forums.losant.com/u/Bryan_Chase)\
**Post date:** [January 28, 2020, 5:08pm UTC](https://forums.losant.com/t/example-reference-to-device-in-function-node/2579/3 "2020-01-28T17:08:32Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![anaptfox](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/anaptfox/32/873_2.png) [@anaptfox](https://forums.losant.com/u/anaptfox)\
**Post date:** [January 28, 2020, 6:10pm UTC](https://forums.losant.com/t/example-reference-to-device-in-function-node/2579/7 "2020-01-28T18:10:24Z")

</div>

@Bryan_Chase,

@JuliaKempf working on a complete example for you.

For now, however, [Device Tags](http://docs.losant.com/devices/overview/#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](https://docs.losant.com/workflows/data/get-device/) 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:

 ![image](https://us1.discourse-cdn.com/flex015/uploads/getstructure/original/2X/7/7394ea545b1698d0711ec734179155d704c4a5c3.png)

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.

---

<div class="post-metadata">

**Author:** ![JuliaKempf](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/juliakempf/32/2446_2.png) [@JuliaKempf](https://forums.losant.com/u/JuliaKempf)\
**Post date:** [January 28, 2020, 9:14pm UTC](https://forums.losant.com/t/example-reference-to-device-in-function-node/2579/8 "2020-01-28T21:14:13Z")

</div>

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:

 ![Screen Shot 2020-01-28 at 3.42.27 PM](https://us1.discourse-cdn.com/flex015/uploads/getstructure/original/2X/6/648c2cc33f92680fdf6f9d719f4007858eaeb309.png)

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](https://us1.discourse-cdn.com/flex015/uploads/getstructure/original/2X/d/d256f3ce0bd659ec60f79e0a331d1b5222ccd02d.png)

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.

 ![Screen Shot 2020-01-28 at 3.58.43 PM](https://us1.discourse-cdn.com/flex015/uploads/getstructure/original/2X/3/3c33377584ac2285a1e55c84181dad51bb12e365.png)

Then, over in my workflow, I include a [Device: Get Node](https://docs.losant.com/workflows/data/get-device/) and set the query method to “Match any tags.”

 ![Screen Shot 2020-01-28 at 4.01.45 PM](https://us1.discourse-cdn.com/flex015/uploads/getstructure/original/2X/a/aef1a21e9efa479ba384f741c5e3b8e32401df87.png)

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

 ![Screen Shot 2020-01-28 at 4.03.12 PM](https://us1.discourse-cdn.com/flex015/uploads/getstructure/original/2X/2/21e786d0d58d2a6b25aefa1a46267f72b0b144d0.png)

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.

 ![Screen Shot 2020-01-28 at 4.10.09 PM](https://us1.discourse-cdn.com/flex015/uploads/getstructure/original/2X/9/9e3c80909578fec82627dda71cac645c8c65773b.png)

Please let me know if I can explain any further!

Thanks,  
Julia

---

<div class="post-metadata">

**Author:** ![Bryan\_Chase](https://avatars.discourse-cdn.com/v4/letter/b/5f8ce5/32.png) [@Bryan\_Chase](https://forums.losant.com/u/Bryan_Chase)\
**Post date:** [February 7, 2020, 1:51am UTC](https://forums.losant.com/t/example-reference-to-device-in-function-node/2579/10 "2020-02-07T01:51:36Z")

</div>

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:

 ![image](https://us1.discourse-cdn.com/flex015/uploads/getstructure/original/2X/0/06df218b953d4d5fe6541b8b9dd0d659291fa669.png)

---

<div class="post-metadata">

**Author:** ![JuliaKempf](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/juliakempf/32/2446_2.png) [@JuliaKempf](https://forums.losant.com/u/JuliaKempf)\
**Post date:** [February 7, 2020, 3:17pm UTC](https://forums.losant.com/t/example-reference-to-device-in-function-node/2579/12 "2020-02-07T15:17:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Bryan\_Chase](https://avatars.discourse-cdn.com/v4/letter/b/5f8ce5/32.png) [@Bryan\_Chase](https://forums.losant.com/u/Bryan_Chase)\
**Post date:** [February 7, 2020, 5:49pm UTC](https://forums.losant.com/t/example-reference-to-device-in-function-node/2579/14 "2020-02-07T17:49:06Z")

</div>

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!
