Is there any way to use variables instead of strings in edit fields of a node?
To make it concrete:
In case of e.g. MQTT I have some devices of the same type. All devices have a couple of properties.
In order to access the same property of different devices I have to use the same 'Topic' with one difference, the address of the device. So I wonder if it is possible to replace the cryptic address (e.g. 0001204302303 ) by a more intuitive name like 'tempOffice'.
In general: is it possible to use variables for fields in nodes, for example the 'Name' field. No matter if it is wise here or not.
Using Variables in Nodes properties, is well supported, and is dependant on the node supporting it.
Example - the inject node supporting the
The name of a node however, CAN NOT be taken from a variable. in all my time using Node RED, the name of a node in your flow, is always a static value.
Other properties, again... Node Dependant, does support variables.
Regarding the MQTT in node, it does offer dynamic subscriptions, which let you unsubscribe from one topic and subscribe to another.
But in your case of multiple devices of the same type, do they let you change the mqtt topic?
If so use a topic such as
peskysensor/<sensor id> and subscribe to
Edit - changed "#" to "+" in the hope of reducing unnecessary message delivery.
If they don't, I guess a workaround might be to subscribe to
+, use a switch node to check if msg.topic matches the regex for 13 digits and republish to
Of course your downstream flow has to handle messages from different devices.
I talk about something like this:
The 'Topic' doesn't offer it in a generic way.
If there is no dropdown - to switch to using a context variable, then no.
But as @jbudd said - you can use the dynamic topic features.
Done right, you can combine a change node with your MQTT node to achieve it
Thank's a lot.
@jbudd: Not really what I was looking for, but very interesting for this use case.
I think it will make my life (the 'node-red-life') a little easier.
Most mqtt devices i have used (shelly, tasmota etc) it is possible to edit the device name/id (0001204302303 ) to a more human readable one. Have you looked at editing the topic device name in the mqqt devices own config?
You can still do it.
use that in an
inject node (as below, topic and action) - set it to inject after 1s
Set the MQTT to
dynamic subscription and connect them together
This will get you as close as possible to using a variable
@E1cid: It is not possible in my MQTT device. This makes it very uncomfortable to handle it and to track devices e.g. with MQTT explorers. Human readable information is available on the subscriber side, but it is not forwarded.
@markus-j-davis: The MQTT node has no input, so I don't know how to connect them.
ALL: The wildcard of MQTT is at the end a fairly nice solution. Together with a function node to filter the device coming out of the 'topic' it will be a really good solution. 'good' because it can end up in a structured piece of code and all links between intransparent numbers and transparent names are at one place.
Thank's a lot!!
You seem to have a work around now, but for info, the MQTT nodes reveal an input when set to dynamic.
Be aware that subscribing to a wildcard topic might result in a lot of extra message processing.
What are these sensor devices? A link would be interesting.
... Yes. Really a lot. And these data come in if any value changes.
So I changed my mind: marcus' hint works fine. And in combination with a subflow and drop down menus it's perfect. There is still only one location to map devices an functions and a very good usability.
Learn from the best.
This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.