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

**URL:** <https://discourse.nodered.org/t/sample-flow-on-how-to-setup-environment-variable-in-a-sub-flow/25341>\
**Category:** Share Your Projects\
**Created:** [23 April 2020 03:52 UTC](https://discourse.nodered.org/t/sample-flow-on-how-to-setup-environment-variable-in-a-sub-flow/25341 "2020-04-23T03:52:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zainisma](https://avatars.discourse-cdn.com/v4/letter/z/aeb1de/32.png) [@zainisma](https://discourse.nodered.org/u/zainisma)\
**Post date:** [23 April 2020 03:52 UTC](https://discourse.nodered.org/t/sample-flow-on-how-to-setup-environment-variable-in-a-sub-flow/25341/1 "2020-04-23T03:52:01Z")

</div>

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)...

 ![Set_up_sub_flow_env_var_click_edit_prop](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/5/25e91501115b3153e7d545e0f08a1c5e35133033.png)

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`

 ![Set_up_sub_flow_env_var](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/e/ceb490fbb15d3212248e13fc31c77fd3fa27e817.png)

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...

 ![Set_up_sub_flow_env_var_click_add_btn](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/8/c855612a858dae482e25fe6667d340be079888f2.png)

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: -

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

return msg;

```

 ![Sub_flow_output_2_func_node_JS](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/e/1e204a27745a813a2d025b5e63a2f22c17841fc1.png)

1. another change node but this time msg.payload is "Set to" a J: expression that looks like this: -

```auto
$env('some_var')

```

 ![Sub_flow_output_3_chg_node_JSONata_exp](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/d/cde15d9311245be3228e939dd21e5744e9a172f5.png)

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

 ![This_is_the_Sub_flow](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/2/d2f842832a8e525317bee8290424d6f3119dbc5e.png)

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.

 ![Sub_flow_env_var_param](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/e/6e94ab92b5010baa9f7d9816b6fdc5bc1eeae455.png)

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)...

 ![FLOW_using_the_Sub_flow](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/3/2373b297b1fcf77f3095268c615208fe4ce81a16.png)

Here's the subflow...

```auto
[{"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...

```auto
[{"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.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [23 April 2020 04:55 UTC](https://discourse.nodered.org/t/sample-flow-on-how-to-setup-environment-variable-in-a-sub-flow/25341/2 "2020-04-23T04:55:42Z")

</div>

_Moved to sharing_

---

<div class="post-metadata">

**Author:** ![zainisma](https://avatars.discourse-cdn.com/v4/letter/z/aeb1de/32.png) [@zainisma](https://discourse.nodered.org/u/zainisma)\
**Post date:** [23 April 2020 06:11 UTC](https://discourse.nodered.org/t/sample-flow-on-how-to-setup-environment-variable-in-a-sub-flow/25341/3 "2020-04-23T06:11:04Z")

</div>

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

---

<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:** [22 June 2020 06:11 UTC](https://discourse.nodered.org/t/sample-flow-on-how-to-setup-environment-variable-in-a-sub-flow/25341/4 "2020-06-22T06:11:08Z")

</div>

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