Using subflow environment variable to set mqtt topic

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 ?

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.


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.

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?

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

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.

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.