# Use dynamic context based variable in change node for setting a variable

**URL:** https://discourse.nodered.org/t/use-dynamic-context-based-variable-in-change-node-for-setting-a-variable/87519
**Category:** General
**Tags:** function-node, switch-node, subflow
**Created:** [26 April 2024 13:26 UTC](https://discourse.nodered.org/t/use-dynamic-context-based-variable-in-change-node-for-setting-a-variable/87519 "2024-04-26T13:26:47Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![psmtvdw](https://avatars.discourse-cdn.com/v4/letter/p/e480ec/32.png) [@psmtvdw](https://discourse.nodered.org/u/psmtvdw)
#### Post date: [26 April 2024 13:26 UTC](https://discourse.nodered.org/t/use-dynamic-context-based-variable-in-change-node-for-setting-a-variable/87519/1 "2024-04-26T13:26:47Z")

</div>

I have a subflow, inside this subflow I want to use the value of an env variable of this subflow in a change node to write a global variable

This is how it looks like (and works succesfully) in a function node:

```auto
global.set("status.devices" + env.get("deviceId"), "nok")
return msg;

```

Can I achieve the same in a change node?  
This is the syntax I was trying to use but is not working:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/9/49662f16add759c3f679e8be9adfda6154928916.png)

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [26 April 2024 13:49 UTC](https://discourse.nodered.org/t/use-dynamic-context-based-variable-in-change-node-for-setting-a-variable/87519/2 "2024-04-26T13:49:01Z")

</div>

There are several ways here is one.

```auto
[{"id":"73173a06beaebc00","type":"change","z":"d1395164b4eec73e","name":"","rules":[{"t":"set","p":"devices","pt":"msg","to":"\"deviceId\" & $env(\"deviceId\")","tot":"jsonata"},{"t":"set","p":"status[msg.devices]","pt":"global","to":"nok","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":540,"y":7820,"wires":[[]]}]

```

 ![chrome_screenshot_26 Apr 2024 14_47_42 BST](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/1/c1978fcdaf91aeb49a000fe40a5e09d25d6ac870.png)

---

<div class="post-metadata">

### Author: ![psmtvdw](https://avatars.discourse-cdn.com/v4/letter/p/e480ec/32.png) [@psmtvdw](https://discourse.nodered.org/u/psmtvdw)
#### Post date: [26 April 2024 14:28 UTC](https://discourse.nodered.org/t/use-dynamic-context-based-variable-in-change-node-for-setting-a-variable/87519/3 "2024-04-26T14:28:48Z")

</div>

ok, I get the idea of using jsonata as an itermediate step.  
Example on how this would work om my exact case:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/5/65bba5e9094ba95d34d71d094604f557a13865be.png)  
example flow with 2 subflows, different deviceId for each:

```auto
[{"id":"282de603a3e98524","type":"subflow","name":"Subflow 2","info":"","category":"","in":[{"x":50,"y":30,"wires":[{"id":"73173a06beaebc00"}]}],"out":[],"env":[{"name":"deviceId","type":"str","value":""}],"meta":{},"color":"#DDAA99"},{"id":"73173a06beaebc00","type":"change","z":"282de603a3e98524","name":"","rules":[{"t":"set","p":"deviceId","pt":"msg","to":"$env(\"deviceId\")","tot":"jsonata"},{"t":"set","p":"status.devices[msg.deviceId]","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":220,"y":80,"wires":[[]]},{"id":"41263f249259654e","type":"subflow:282de603a3e98524","z":"8617b96f8c8e93ed","name":"deviceA","env":[{"name":"deviceId","value":"deviceA","type":"str"}],"x":470,"y":100,"wires":[]},{"id":"a181053111630f15","type":"inject","z":"8617b96f8c8e93ed","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"ok","payloadType":"str","x":210,"y":180,"wires":[["4418dafc915d302c"]]},{"id":"4418dafc915d302c","type":"subflow:282de603a3e98524","z":"8617b96f8c8e93ed","name":"deviceB","env":[{"name":"deviceId","value":"deviceB","type":"str"}],"x":470,"y":140,"wires":[]},{"id":"9ad689075a9cce3b","type":"inject","z":"8617b96f8c8e93ed","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"nok","payloadType":"str","x":200,"y":220,"wires":[["4418dafc915d302c"]]},{"id":"2be500a165e57375","type":"inject","z":"8617b96f8c8e93ed","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"ok","payloadType":"str","x":210,"y":100,"wires":[["41263f249259654e"]]},{"id":"c1ad625858cd8838","type":"inject","z":"8617b96f8c8e93ed","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"nok","payloadType":"str","x":200,"y":140,"wires":[["41263f249259654e"]]}]

```

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [26 April 2024 14:34 UTC](https://discourse.nodered.org/t/use-dynamic-context-based-variable-in-change-node-for-setting-a-variable/87519/4 "2024-04-26T14:34:14Z")

</div>

That would work if the property was `status.devices.<id>` and would not require JSONata, as you can use the env $ type input in the change node.  
but

> [@psmtvdw](#):
>
> ```auto
> global.set("status.devices" + env.get("deviceId"), "nok")
> return msg;
> 
> ```

the property is `status.devices<id>` there is no dot between devices and id.

---

<div class="post-metadata">

### Author: ![psmtvdw](https://avatars.discourse-cdn.com/v4/letter/p/e480ec/32.png) [@psmtvdw](https://discourse.nodered.org/u/psmtvdw)
#### Post date: [29 April 2024 06:02 UTC](https://discourse.nodered.org/t/use-dynamic-context-based-variable-in-change-node-for-setting-a-variable/87519/5 "2024-04-29T06:02:44Z")

</div>

Yes, you're right, sorry for the confusion, but there was a mistake in my initial code. There should have been a dot.

> [@psmtvdw](#):
>
> ```auto
> global.set("status.devices" + env.get("deviceId"), "nok")
> return msg;
> 
> ```

should have been

```auto
global.set("status.devices." + env.get("deviceId"), "nok")
return msg;

```

And in that case using $env instead of jsonata is the better solution

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [13 May 2024 06:03 UTC](https://discourse.nodered.org/t/use-dynamic-context-based-variable-in-change-node-for-setting-a-variable/87519/6 "2024-05-13T06:03:35Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
