# Subflows - passing payload or other properties via environment variable?

**URL:** <https://discourse.nodered.org/t/subflows-passing-payload-or-other-properties-via-environment-variable/97756>\
**Category:** General\
**Created:** [23 June 2025 11:58 UTC](https://discourse.nodered.org/t/subflows-passing-payload-or-other-properties-via-environment-variable/97756 "2025-06-23T11:58:41Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![SiDtheTurtle](https://avatars.discourse-cdn.com/v4/letter/s/d07c76/32.png) [@SiDtheTurtle](https://discourse.nodered.org/u/SiDtheTurtle)\
**Post date:** [23 June 2025 11:58 UTC](https://discourse.nodered.org/t/subflows-passing-payload-or-other-properties-via-environment-variable/97756/1 "2025-06-23T11:58:41Z")

</div>

Hi folks! Here's my scenario: I'm building automations in Home Assistant, I want to create a reusable function so that any time I want to send a mobile notification, I can use the same set of nodes.

I can build this easily using a link out- create a flow that does all this work, and just use link out to link to it. It seems that on paper, reusing nodes as functions feels more like the job of a subflow, so I ported everything to a subflow. I then set environment variables such that I can pass the title of the notification, the message etc. So far so good. But I can't inject something like msg.payload into an environment variable. Why is that? Is it because it's set at compile time or similar? Basically whatever I try it doesn't seem I can set it dynamically.

Of course msg.payload flows down to the subflow anyway, so I can just call it from the other end, but from a documentation point of view it'd be nicer to have a single notification subflow node, with all the environment variables set. Otherwise I need at least 2: one to set msg.payload, then one to access the subflow.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [23 June 2025 13:46 UTC](https://discourse.nodered.org/t/subflows-passing-payload-or-other-properties-via-environment-variable/97756/2 "2025-06-23T13:46:17Z")

</div>

You pass data to a subflow or a linked flow as a message. Why do you feel that environment variables are more appropriate than msg.subject, msg.to, msg.payload (etc)?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 June 2025 14:24 UTC](https://discourse.nodered.org/t/subflows-passing-payload-or-other-properties-via-environment-variable/97756/3 "2025-06-23T14:24:01Z")

</div>

I presume that @SiDtheTurtle is talking about the use of subflow properties, which are pseudo env vars.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [23 June 2025 14:30 UTC](https://discourse.nodered.org/t/subflows-passing-payload-or-other-properties-via-environment-variable/97756/4 "2025-06-23T14:30:32Z")

</div>

Quite likely, but I do wonder why s/he assumes they are more appropriate than message properties. Of course, he might be right!

I don't use subflows, only because they occupy the top of the editor palette column.  
I doubt if this is a good reason to spurn them, so I'm interested to learn more.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 June 2025 14:37 UTC](https://discourse.nodered.org/t/subflows-passing-payload-or-other-properties-via-environment-variable/97756/5 "2025-06-23T14:37:58Z")

</div>

@SiDtheTurtle, subflow properties are useful where each instance of a subflow requires a set of fixed values. If you want to pass in values that vary each time you pass a message to the subflow then use message properties, such as msg.payload.

---

<div class="post-metadata">

**Author:** ![SiDtheTurtle](https://avatars.discourse-cdn.com/v4/letter/s/d07c76/32.png) [@SiDtheTurtle](https://discourse.nodered.org/u/SiDtheTurtle)\
**Post date:** [23 June 2025 14:40 UTC](https://discourse.nodered.org/t/subflows-passing-payload-or-other-properties-via-environment-variable/97756/6 "2025-06-23T14:40:44Z")

</div>

All good points. I guess it works, it's just I'm sending half the inputs via environment variables (sorry, yes I mean subflow properties), half via messages.

Let's take an example of the front door being opened. So I set the notification title environment variable to 'Doors' and the msg.notification\_message (or whatever) to "{{entity name}} has been opened!". Of course when I'm setting the notification message I could set say msg.title to 'Doors', not use any environment variables and it's be cleaner. I guess I was just hoping I could have a single node (made up of a sub node) that I could drop in, set some environment variables and it'd work.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 June 2025 16:10 UTC](https://discourse.nodered.org/t/subflows-passing-payload-or-other-properties-via-environment-variable/97756/7 "2025-06-23T16:10:06Z")

</div>

Do you have another instance of the subflow where the title is, for example, Windows?

Can you give some examples of what entity name is?

---

<div class="post-metadata">

**Author:** ![SiDtheTurtle](https://avatars.discourse-cdn.com/v4/letter/s/d07c76/32.png) [@SiDtheTurtle](https://discourse.nodered.org/u/SiDtheTurtle)\
**Post date:** [24 June 2025 07:05 UTC](https://discourse.nodered.org/t/subflows-passing-payload-or-other-properties-via-environment-variable/97756/8 "2025-06-24T07:05:51Z")

</div>

Here's an example that might help. It's very Home Assistant specific, but I guess the question is generic:

I have two hose valves that control the soaker hose for the hedge and the pool top up. I turn these on by hand (or app), but often forget to switch them off. So I built a simple flow that triggers if the valve has been on for a set time, and switches them off:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/8/386a14fdd0c4edf5ed2df7ce15072100e0cfe465.png)

For both hoses, I want to send a mobile notification. In the teal green node (a sub flow), I set the environment variables as follows:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/8/b8fc0d42a0cefbc01d1c58a1de05373fa98aa358.png)

Those three elements that say 'Water' are fed into the sub flow and used to build the JSON message that's pushed to the HA app. That's static, easy enough. The message however, needs to be dynamic, so I build that in the gold change node in the middle:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/7/f703ce7445be2b8f5a32f56e021a463d00cf8620.png)

So now some of the fields are set by environment variable, some by msg.message. But not always, on other flows the message is static so I can use an environment variable. So in my subflow, I have a convoluted set of switches and functions to check if the environment variable has been set, if not, check the message, if not, fail.

My goal is to have a generic sub flow I can drop in everywhere, seeing as I see mobile notifications being core to my development.

I guess you could argue I need to set up all of these parameters as tags, labels etc. at source (Home Assistant), then pull them through in the payload and decode it in the sub flow?

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [24 June 2025 09:21 UTC](https://discourse.nodered.org/t/subflows-passing-payload-or-other-properties-via-environment-variable/97756/9 "2025-06-24T09:21:54Z")

</div>

Hey @SiDtheTurtle.

Looking at your last post.

The second picture where you have the names.... Water, Water, Water... as the title, message and channel.

In the next picture you are showing a `change` node.

You lost me where the `change` node is.  
Is it in the subflow?

The line is using `context`.  
Shouldn't it be `env` if it is in the subflow?

# SORRY

I'm kinda confused reading it a third time.

If you have made a `subflow`, I am not sure it is ..... _needed_ to have the `change` node just before the message goes into it.  
Just do that in the `subflow`

For now, I'd suggest you extract all the nodes from the `subflow` and make sure it works at this level.  
Yes, `env` is a bit difficult to get around.

But I'm not seeing what you want to do exactly.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [24 June 2025 09:30 UTC](https://discourse.nodered.org/t/subflows-passing-payload-or-other-properties-via-environment-variable/97756/10 "2025-06-24T09:30:34Z")

</div>

Here is a test flow that may help you with using `subflows` and context stuff.

I had a real bad time at the start and this kind of helped.

```auto
[{"id":"480c6735eef22428","type":"subflow","name":"Get parent flow context example","info":"","category":"","in":[{"x":60,"y":80,"wires":[{"id":"0baab8933b6c881b"}]}],"out":[{"x":410,"y":80,"wires":[{"id":"0baab8933b6c881b","port":0}]}],"env":[],"meta":{},"color":"#DDAA99"},{"id":"0baab8933b6c881b","type":"function","z":"480c6735eef22428","name":"parent context get","func":"msg.payload = flow.get(\"$parent.example\");\nreturn msg;\n// flow.set(\"$parent.Timer\"+x, x);","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":230,"y":80,"wires":[[]]},{"id":"bc62956f2461c1bb","type":"inject","z":"7e987ddf260bdf0d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"A","payloadType":"str","x":110,"y":1950,"wires":[["4bc259bbc93e8c1d"]]},{"id":"1da3c7e9fc2e9311","type":"inject","z":"7e987ddf260bdf0d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"B","payloadType":"str","x":110,"y":1990,"wires":[["4bc259bbc93e8c1d"]]},{"id":"f9e8ec3151fa7e4b","type":"inject","z":"7e987ddf260bdf0d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":110,"y":2090,"wires":[["9b854c5131989160","8e7dfd53955cbfad"]]},{"id":"4bc259bbc93e8c1d","type":"change","z":"7e987ddf260bdf0d","name":"","rules":[{"t":"set","p":"example","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":1970,"wires":[[]]},{"id":"ed2b683cd58e129f","type":"debug","z":"7e987ddf260bdf0d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":630,"y":2090,"wires":[]},{"id":"9b854c5131989160","type":"subflow:480c6735eef22428","z":"7e987ddf260bdf0d","name":"","x":350,"y":2090,"wires":[["ed2b683cd58e129f"]]},{"id":"ef3e11816c8dc078","type":"debug","z":"7e987ddf260bdf0d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":480,"y":2150,"wires":[]},{"id":"8e7dfd53955cbfad","type":"change","z":"7e987ddf260bdf0d","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"example","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":2150,"wires":[["ef3e11816c8dc078"]]}]

```

 ![Screenshot from 2025-06-24 19-29-18](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/c/5c896e5fbf3d8b50c81cb738d65442c8f8b14cdc.png)

Hope it helps.

You just press the `A` or `B` node and then the `timestamp` node and look at the two debug nodes.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [26 June 2025 10:09 UTC](https://discourse.nodered.org/t/subflows-passing-payload-or-other-properties-via-environment-variable/97756/11 "2025-06-26T10:09:39Z")

</div>

So for that example (with the hose valves), ideally what would you like to specify in the subflow properties?

---

<div class="post-metadata">

**Author:** ![ThingsTinkerer](https://avatars.discourse-cdn.com/v4/letter/t/91b2a8/32.png) [@ThingsTinkerer](https://discourse.nodered.org/u/ThingsTinkerer)\
**Post date:** [26 June 2025 12:28 UTC](https://discourse.nodered.org/t/subflows-passing-payload-or-other-properties-via-environment-variable/97756/12 "2025-06-26T12:28:05Z")

</div>

This is how a lot of I/O nodes work, ref http and all sorts of other protocols. You can set URL in node property (env) as a static value, all incoming messages will use that address. Or set it dynamically in msg.url.

Your issue may be that msg props are not as obvious and less strict than the config menu of a node / subflow? I agree it would be nice to have a clear view of args, like a function signature. If done right, this info should at least be available in documentation of the node / subflow (when people care enough to explain it there).

I write a lot of subflows as easy-to-use tools. What I often end up doing for maximal flexibility is to support both dynamic msg props AND subflow config env variables. Like this:

```javascript
msg.url ??= env.get("url");

```

If value is set dynamically in msg, use that. Otherwise use whatever set in config.

---

<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:** [24 September 2025 12:29 UTC](https://discourse.nodered.org/t/subflows-passing-payload-or-other-properties-via-environment-variable/97756/13 "2025-09-24T12:29:00Z")

</div>

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