Using subflow environment variable to set mqtt topic

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

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.

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?

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

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

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

OK, I understand. Thank you very much

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:

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

The trick is to use additional subflow properties:

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.

1 Like

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

1 Like

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

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

Can you export your MQTT node and share it here?

I'm running v0.20.8.
subflow.txt (5.0 KB)
Thank you for looking into this.

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 ${}.

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

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

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