Is it possible to declare global variables in Node-RED and use them on the node configuration?

Rafael Muynarsk picture Rafael Muynarsk · Jun 1, 2017 · Viewed 15.6k times · Source

Supposing that I'm receiving information from many devices with the MQTT protocol and the following diagram is a simplified version of the block of one device:

device one

So let's also suppose that all other devices have exactly the same diagram, except for the topic name that is going to change to device2 for the second device, device3 for the third device and so on, as follows:

device two

The problem is that I want a way of changing names inside the node's configuration without having to do it one by one. Like declaring a global variable that can be used not just in the function but in the nodes themselves. For example, the last picture could use something like: MYVARIABLE_temperatureA and MYVARIABLE_temperatureB as topics.

So, is it possible to do something like this using the Node-RED? Or the solution lies just in creating a customized node that has a specific field for placing values?

Answer

hardillb picture hardillb · Jun 1, 2017

Not using the global context, but you can use environment variables using the following syntax:

$(ENV_VAR_NAME)

So you could prefix the configuration variables with a Environment variable then change those to update the nodes (with a restart of Node-RED)