Setting a Node Property from Global or Flow Variable

I know that I can get / set flow, env and global variables inside a function node, but how can I use them inside another node property ?

For example, lets say I have a node and I want to set it's name property from a flow variable named 'left' how would I do that ??

I have tried:

{{flow.left}}
{{$flowContext("left")}}

But neither appear to work, can this be done ??

In general, you cannot reference context properties directly in a node property unless the node makes it explicitly clear in its help that you can do so.

More typically, if the node allows you to dynamically set a property it will be done via message properties that you pass the node. In which case, you can use a Change node to copy a context property to a message property before passing it in.

I have done this in the following way more or less, setting the name of the node programely is difficult,
(you have to update the flow.js file and deploy it) but maybe this can help you.


What i do here is,

  • I set the name of the comment node,
  • deploy, and then use this name property to inject this value into the flow.
  • so i can see what i am injecting for manually testing.

[{"id":"688cf92c.1f9a48","type":"trigger","z":"e97aed1b.db4d9","op1":"true","op2":"false","op1type":"bool","op2type":"bool","duration":"200","extend":false,"units":"ms","reset":"","bytopic":"all","name":"for puls","x":384,"y":72,"wires":[["feabb847.8fa378"]]},{"id":"feabb847.8fa378","type":"function","z":"e97aed1b.db4d9","name":"main func","func":"if(msg.topic === \"setStatus\") return [null,null];\n\nvar Door = 0;\nvar pin = global.get(\"manual\"); // number\n\nif (msg.flow[pin].t !== \"switch\") {\n    Door = msg.payload;\n    if(msg.payload === true || msg.payload === false) {\n       node.warn(\"Doors need to be opend and closed!!!\");\n       return null;\n    }\n}\n\nif( Door === \"OPEN\" || Door === \"CLOSED\" ) { \n    return {manually:true,i:pin,state:Door === \"OPEN\"?false:true}; // doors\n} \nif(msg.payload === \"OPEN\" || msg.payload === \"CLOSED\") {\n   msg.payload = msg.payload === \"CLOSED\"? false:true;\n}           \nreturn{  i       :pin,\n         state   :msg.payload,\n         manually:true};\n\n\n","outputs":1,"noerr":0,"x":552,"y":108,"wires":[[]],"outputLabels":["Doors"]},{"id":"2901597f.23a516","type":"switch","z":"e97aed1b.db4d9","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"pushed","vt":"str"},{"t":"eq","v":"pushed-released","vt":"str"},{"t":"eq","v":"CLOSED","vt":"str"},{"t":"eq","v":"OPEN","vt":"str"},{"t":"eq","v":"setStatus","vt":"str"}],"checkall":"false","repair":false,"outputs":5,"x":206,"y":120,"wires":[["688cf92c.1f9a48"],["b999acdc.acb2c"],["3d798d5c.84e7a2"],["3d798d5c.84e7a2"],["e4f73ac0.49bb38"]]},{"id":"b999acdc.acb2c","type":"trigger","z":"e97aed1b.db4d9","op1":"true","op2":"false","op1type":"bool","op2type":"bool","duration":"1000","extend":false,"units":"ms","reset":"","bytopic":"all","name":"hold 1 sec","x":384,"y":108,"wires":[["feabb847.8fa378"]]},{"id":"3d798d5c.84e7a2","type":"trigger","z":"e97aed1b.db4d9","op1":"","op2":"","op1type":"pay","op2type":"nul","duration":"1","extend":false,"units":"ms","reset":"","bytopic":"all","name":"dummy","x":384,"y":144,"wires":[["feabb847.8fa378"]]},{"id":"e4f73ac0.49bb38","type":"http request","z":"e97aed1b.db4d9","name":"get number from comment field","method":"GET","ret":"obj","paytoqs":false,"url":"http://openhab:1880/flows","tls":"","persist":false,"proxy":"","authType":"","x":166,"y":204,"wires":[["a15d0cf2.06521"]]},{"id":"a15d0cf2.06521","type":"change","z":"e97aed1b.db4d9","name":"set global to number","rules":[{"t":"set","p":"input","pt":"msg","to":"payload.(\t$number($[id=\"f987b28a.65576\"].name\t))","tot":"jsonata"},{"t":"set","p":"manual","pt":"global","to":"input","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":424,"y":204,"wires":[["8a1db4ab.f16868"]]},{"id":"8a1db4ab.f16868","type":"function","z":"e97aed1b.db4d9","name":"Info on manual","func":"var Door = false;\nvar comment = [], p;\n\n\nif (msg.flow[msg.input].t === \"switch\") {\n    p = msg.flow[msg.input].puls;\n} else {\n    p =msg.flow[msg.input].OPEN;\n    Door = true;\n}\nfor (var name in p){\n    comment.push(name + \" - \");\n}\n\nmsg = {};\nreturn {payload:Door? \"Type: Door - Output \" + comment : \"Type: Switch - Outpu \" + comment};\n\n\n","outputs":1,"noerr":0,"x":632,"y":204,"wires":[[]]}]

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