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

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:

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:

There are several ways here is one.

[{"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":[[]]}]

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


example flow with 2 subflows, different deviceId for each:

[{"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"]]}]

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

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

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

should have been

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

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

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