Sample flow on how to setup Environment Variable in a sub-flow

Good day all,

I'm just sharing a simple flow on setting up an Environment Variable Parameter for a sub-flow. Where the sub-flow environment variable has been set up to be some_var

After creating your sub-flow... look for the "edit properties" button as shown below (circled in red)...

Upon clicking on the button, you would need to set up your environment variable as follows... In the example below my environment variable is some_var

Oops I forgot!!! Before you can get to see the two fields above, you'd first need to click on the +add button circled in red as shown below... Otherwise it'll just be blank...

Nothing special in the sub-flow, it has 1 input and 3 separate outputs that come from...

  1. a change node, that has a msg.payload being "Set to" an $ env variable some_var.

  2. a function node, that has the following code within: -

msg.payload = env.get("some_var");

return msg;

  1. another change node but this time msg.payload is "Set to" a J: expression that looks like this: -
$env('some_var')

Here is a screenshot of my sub-flow...

So now when you change the content within the Sub-Flow parameter (you get this by double clicking on your sub-flow node that is being used in your flow), that content shall be passed on to anywhere the environmental variable some_var is used.

You'll be able to see the "happy" string at all 3 outputs of the sub-flow (attached to debug nodes with node status enabled)...

Here's the subflow...

[{"id":"7eba2b65.f1ecd4","type":"change","z":"3a93e082.e06f8","name":"set msg.payload to sub-flow \"some_var\" parameter in a change node","rules":[{"t":"set","p":"payload","pt":"msg","to":"some_var","tot":"env"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":100,"wires":[[]]},{"id":"3cdbaf03.01d94","type":"function","z":"3a93e082.e06f8","name":"set msg.payload to sub-flow \"some_var\" parameter in a function node","func":"msg.payload = env.get(\"some_var\");\n\nreturn msg;","outputs":1,"noerr":0,"x":370,"y":160,"wires":[[]]},{"id":"d7956efa.6d90b","type":"change","z":"3a93e082.e06f8","name":"set msg.payload to sub-flow \"some_var\" parameter in a change node with JSONata expressions","rules":[{"t":"set","p":"payload","pt":"msg","to":"$env('some_var')","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":450,"y":220,"wires":[[]]}]

Here's the Flow that uses the sub-flow...

[{"id":"3a93e082.e06f8","type":"subflow","name":"Subflow 2","info":"","category":"","in":[{"x":40,"y":160,"wires":[{"id":"7eba2b65.f1ecd4"},{"id":"3cdbaf03.01d94"},{"id":"d7956efa.6d90b"}]}],"out":[{"x":680,"y":100,"wires":[{"id":"7eba2b65.f1ecd4","port":0}]},{"x":680,"y":160,"wires":[{"id":"3cdbaf03.01d94","port":0}]},{"x":680,"y":280,"wires":[{"id":"d7956efa.6d90b","port":0}]}],"env":[{"name":"some_var","type":"str","value":""}],"color":"#DDAA99"},{"id":"7eba2b65.f1ecd4","type":"change","z":"3a93e082.e06f8","name":"set msg.payload to sub-flow \"some_var\" parameter in a change node","rules":[{"t":"set","p":"payload","pt":"msg","to":"some_var","tot":"env"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":100,"wires":[[]]},{"id":"3cdbaf03.01d94","type":"function","z":"3a93e082.e06f8","name":"set msg.payload to sub-flow \"some_var\" parameter in a function node","func":"msg.payload = env.get(\"some_var\");\n\nreturn msg;","outputs":1,"noerr":0,"x":370,"y":160,"wires":[[]]},{"id":"d7956efa.6d90b","type":"change","z":"3a93e082.e06f8","name":"set msg.payload to sub-flow \"some_var\" parameter in a change node with JSONata expressions","rules":[{"t":"set","p":"payload","pt":"msg","to":"$env('some_var')","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":450,"y":220,"wires":[[]]},{"id":"ae95f225.51856","type":"subflow:3a93e082.e06f8","z":"dea2f3bb.0b89c","name":"test sub flow var","env":[{"name":"some_var","value":"happy","type":"str"}],"x":260,"y":400,"wires":[["e8220033.5a99e"],["536c2dc2.21f7a4"],["d730a55a.546418"]]},{"id":"e06bc9da.7e5ac8","type":"inject","z":"dea2f3bb.0b89c","name":"just a manual trigger","topic":"","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":250,"y":340,"wires":[["ae95f225.51856"]]},{"id":"e8220033.5a99e","type":"debug","z":"dea2f3bb.0b89c","name":"Change Node >>> Select $ env variable and type some_var in the field","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":670,"y":340,"wires":[]},{"id":"536c2dc2.21f7a4","type":"debug","z":"dea2f3bb.0b89c","name":"Function Node >>> let/var foo = env.get(\"some_var\");","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":620,"y":400,"wires":[]},{"id":"d730a55a.546418","type":"debug","z":"dea2f3bb.0b89c","name":"JSONata >>> $env('some_var')","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":550,"y":460,"wires":[]},{"id":"b4863e65.35379","type":"comment","z":"dea2f3bb.0b89c","name":"Subflow Environment Variable settings","info":" * Click edit properties button\n * At the bottom of Environment Variables Tab click the +add button\n * A new row with 2 fields (Name & Default Value) shall appear within the Environement Variables Tab\n * In the Name Field... type your variable name (example: 'some_var')\n * Now you can use add 'some_var' throughtout the subflow.\n * Any values typed into the 'some_var' parameter field, will be used wherever 'some_var' variable is used within the subflow.","x":190,"y":460,"wires":[]}]

Hope this helps on your Node-Red exploration journey.

1 Like

Moved to sharing

Thanks bakman2... didn't realize there was a sharing option

1 Like

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