# Using subflow environment variable to set mqtt topic

**URL:** <https://discourse.nodered.org/t/using-subflow-environment-variable-to-set-mqtt-topic/15331>\
**Category:** General\
**Created:** [9 September 2019 20:53 UTC](https://discourse.nodered.org/t/using-subflow-environment-variable-to-set-mqtt-topic/15331 "2019-09-09T20:53:40Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![paul-bivol](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-bivol/32/12227_2.png) [@paul-bivol](https://discourse.nodered.org/u/paul-bivol)\
**Post date:** [9 September 2019 20:53 UTC](https://discourse.nodered.org/t/using-subflow-environment-variable-to-set-mqtt-topic/15331/1 "2019-09-09T20:53:40Z")

</div>

Hello!  
I want to create a subflow that can turn off a bulb when a certain message is received and then poll its state to make sure the bulb is off. After confirmation it should repeat the input message. The purpose of this is to daisy-chain a bunch of these (one for each bulb) and watch bulbs being turned off one after another.  
It would be great if I could make subscribing to the mqtt topic for the bulb configurable though the subflow environment variables so I could just instantiate the subflow once for each bulb.  
I've tried the solution proposed in [https://cookbook.nodered.org/mqtt/subscribe-to-topic](https://cookbook.nodered.org/mqtt/subscribe-to-topic) where the mqtt topic field is set to $(MY\_TOPIC) but it doesn't work.  
After some debugging it looks like the variable is never expanded. I have replaced mqtt node with a change node for debugging purposes and it looks like the variable is indeed expanded in this case.  
Is this a bug or a feature of the mqtt node?  
Is my approach on this issue just wrong?

Thank you

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [9 September 2019 21:02 UTC](https://discourse.nodered.org/t/using-subflow-environment-variable-to-set-mqtt-topic/15331/2 "2019-09-09T21:02:48Z")

</div>

The environment variables are expanded at deploy time as that is when the configuration of each node is read. They are not dynamically read during execution.

---

<div class="post-metadata">

**Author:** ![paul-bivol](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-bivol/32/12227_2.png) [@paul-bivol](https://discourse.nodered.org/u/paul-bivol)\
**Post date:** [9 September 2019 21:08 UTC](https://discourse.nodered.org/t/using-subflow-environment-variable-to-set-mqtt-topic/15331/3 "2019-09-09T21:08:52Z")

</div>

That would be fine. Each subflow instance has a different topic set in the variable and I am not expecting that to change unless I change the flow and re-deploy.  
Since it is properly expanded in a change node why isn't it expanded in the topic field of a mqtt node?

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [9 September 2019 22:35 UTC](https://discourse.nodered.org/t/using-subflow-environment-variable-to-set-mqtt-topic/15331/4 "2019-09-09T22:35:32Z")

</div>

In that case it should work OK. `$(MYTOPIC1)` in the first. `$(MYTOPIC2)` in the next etc

---

<div class="post-metadata">

**Author:** ![tinamore](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tinamore/32/15693_2.png) [@tinamore](https://discourse.nodered.org/u/tinamore)\
**Post date:** [13 September 2019 15:30 UTC](https://discourse.nodered.org/t/using-subflow-environment-variable-to-set-mqtt-topic/15331/5 "2019-09-13T15:30:41Z")

</div>

I have same problem.  
I set Topic in mqtt node: cmnd/$(MYTOPIC2)/power2  
and i set environment variable MYTOPIC2  
But when deploy, NOT working...

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [13 September 2019 15:54 UTC](https://discourse.nodered.org/t/using-subflow-environment-variable-to-set-mqtt-topic/15331/6 "2019-09-13T15:54:07Z")

</div>

Hi, What does the 2nd para of the docs say ? [https://nodered.org/docs/user-guide/environment-variables](https://nodered.org/docs/user-guide/environment-variables)

---

<div class="post-metadata">

**Author:** ![tinamore](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tinamore/32/15693_2.png) [@tinamore](https://discourse.nodered.org/u/tinamore)\
**Post date:** [13 September 2019 16:08 UTC](https://discourse.nodered.org/t/using-subflow-environment-variable-to-set-mqtt-topic/15331/7 "2019-09-13T16:08:31Z")

</div>

OK, I understand. Thank you very much

```auto
This only works if it replaces the entire property - it cannot be used to substitute just part of the value. For example, it is not possible to use CLIENT-${HOST}.

```

But it seems this is a limitation. In the subflow there are many mqtt subscribe nodes. For example I use Tasmota for esp8266:

```auto
stat / ROOM1 / power2
tele / ROOM1 / LWT
stat / ROOM1 / RESULT
tele / ROOM1 / STATE

```

Can you give me any solution to replace all ROOM1 in all topic mqtt subscribe node

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [13 September 2019 16:26 UTC](https://discourse.nodered.org/t/using-subflow-environment-variable-to-set-mqtt-topic/15331/8 "2019-09-13T16:26:27Z")

</div>

The trick is to use additional subflow properties:

 ![Node-RED](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/1/13f000a6c8cdb95984ef153ab8e3e5656238fb8f.png)

Here you can see we've defined the `ROOM` env var. We then define four further env vars, one for each of the topics you want to use. In the subflow properties table, I've set their type to be Env Var - note the `$` next to each one. That field type allows you to compose a value in the way you need.

You can then use `${TOPIC1}` etc in your MQTT nodes.

---

<div class="post-metadata">

**Author:** ![tinamore](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tinamore/32/15693_2.png) [@tinamore](https://discourse.nodered.org/u/tinamore)\
**Post date:** [13 September 2019 17:06 UTC](https://discourse.nodered.org/t/using-subflow-environment-variable-to-set-mqtt-topic/15331/9 "2019-09-13T17:06:06Z")

</div>

> [@knolleary](#):
>
> Here you can see we've defined the `ROOM` env var. We then define four further env vars, one for each of the topics you want to use. In the subflow properties table, I've set their type to be Env Var - note the `$` next to each one. That field type allows you to compose a value in the way you need.

Great. I have done it successfully.  
Thanks for the support.  
Thanks for what you did with node-red.  
Wishing you success and happiness.

---

<div class="post-metadata">

**Author:** ![paul-bivol](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-bivol/32/12227_2.png) [@paul-bivol](https://discourse.nodered.org/u/paul-bivol)\
**Post date:** [16 September 2019 20:15 UTC](https://discourse.nodered.org/t/using-subflow-environment-variable-to-set-mqtt-topic/15331/10 "2019-09-16T20:15:22Z")

</div>

It still does not work for mqtt input nodes.  
I get "topic not defined" in the debug window.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [16 September 2019 20:49 UTC](https://discourse.nodered.org/t/using-subflow-environment-variable-to-set-mqtt-topic/15331/11 "2019-09-16T20:49:33Z")

</div>

Hi @paul-bivol - what version of Node-RED are you using?

Can you export your MQTT node and share it here?

---

<div class="post-metadata">

**Author:** ![paul-bivol](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-bivol/32/12227_2.png) [@paul-bivol](https://discourse.nodered.org/u/paul-bivol)\
**Post date:** [16 September 2019 21:40 UTC](https://discourse.nodered.org/t/using-subflow-environment-variable-to-set-mqtt-topic/15331/12 "2019-09-16T21:40:11Z")

</div>

I'm running v0.20.8.  
[subflow.txt](https://discourse.nodered.org/uploads/short-url/jYFjN5jDDcpXBaBZa9Wy7815FhL.txt) (5.0 KB)  
Thank you for looking into this.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [16 September 2019 22:21 UTC](https://discourse.nodered.org/t/using-subflow-environment-variable-to-set-mqtt-topic/15331/13 "2019-09-16T22:21:16Z")

</div>

Hi @paul-bivol

In your subflow properties, change the `$(ROOM)` to `${ROOM}` and it will work.

`$()` is deprecated in node properties, and in the `$env` inputs, you must use `${}`.

---

<div class="post-metadata">

**Author:** ![nlecaude](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nlecaude/32/25_2.png) [@nlecaude](https://discourse.nodered.org/u/nlecaude)\
**Post date:** [17 September 2019 01:35 UTC](https://discourse.nodered.org/t/using-subflow-environment-variable-to-set-mqtt-topic/15331/14 "2019-09-17T01:35:33Z")

</div>

> [@knolleary](#):
>
> `$()` is deprecated in node properties

Is it deprecated in favor of `${}` or is it deprecated for node properties in general?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [17 September 2019 06:00 UTC](https://discourse.nodered.org/t/using-subflow-environment-variable-to-set-mqtt-topic/15331/15 "2019-09-17T06:00:51Z")

</div>

> [@nlecaude](#):
>
> Is it deprecated in favor of `${}`

Yes. `${}` is the preferred syntax. We won't remove `$()`, but nor will we expand its use.

---

<div class="post-metadata">

**Author:** ![paul-bivol](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-bivol/32/12227_2.png) [@paul-bivol](https://discourse.nodered.org/u/paul-bivol)\
**Post date:** [17 September 2019 09:10 UTC](https://discourse.nodered.org/t/using-subflow-environment-variable-to-set-mqtt-topic/15331/16 "2019-09-17T09:10:49Z")

</div>

Awesome! It works now. Thank you very much for your help.

---

<div class="post-metadata">

**Author:** ![robert\_64](https://avatars.discourse-cdn.com/v4/letter/r/9e8a1a/32.png) [@robert\_64](https://discourse.nodered.org/u/robert_64)\
**Post date:** [6 April 2021 11:48 UTC](https://discourse.nodered.org/t/using-subflow-environment-variable-to-set-mqtt-topic/15331/17 "2021-04-06T11:48:34Z")

</div>

This works fine with Topic, but I can't make it work with the Server.  
Added ${MQTT\_HOST} in the Server field in a subflow. Added it also in the properties. All exactly as with Topic, but no output. Anyone has an idea?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [6 April 2021 12:13 UTC](https://discourse.nodered.org/t/using-subflow-environment-variable-to-set-mqtt-topic/15331/18 "2021-04-06T12:13:09Z")

</div>

You need to make sure the MQTT Broker config node is 'owned' by the subflow and not a global config node.

In the edit dialog for the config node, at the very bottom is a select box - make sure you have the subflow selected, rather than 'on all flows'.

---

<div class="post-metadata">

**Author:** ![jopl](https://avatars.discourse-cdn.com/v4/letter/j/74df32/32.png) [@jopl](https://discourse.nodered.org/u/jopl)\
**Post date:** [9 November 2022 09:45 UTC](https://discourse.nodered.org/t/using-subflow-environment-variable-to-set-mqtt-topic/15331/22 "2022-11-09T09:45:31Z")

</div>

Dear all NodeRed enthusiastics,  
I notified I am not able to use environment variable in another environment variable definition in flow properties similar to example shown above. I don't know why, but the first variable (writen in format ${ID} in next variable definition) is empty string, despite ID variable was defined in special line above definition as string ... Would you be so kind and could you advise me, what I am doing wrong? Thx in advance. Regards, Josef.

---

<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:** [9 November 2022 10:36 UTC](https://discourse.nodered.org/t/using-subflow-environment-variable-to-set-mqtt-topic/15331/23 "2022-11-09T10:36:56Z")

</div>

Welcome to the forum @jopl

You question does not appear to be related to the two year old topic that you added it to, so please start a new topic with at title that matches your problem. When you do that please show exactly what you are doing that does not work, rather than just describing it in words.

[Next page](https://discourse.nodered.org/t/using-subflow-environment-variable-to-set-mqtt-topic/15331.md?page=2)
