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

Viewed 21442

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?

3 Answers

I know this is an old post but the current method proviced be Nick O'Leary in the node-red google group is as follows:

  1. Edit your node-red settings file (/home/pi/.node-red/settings.js) and add the following (above the module.exports lines):

    process.env.HOSTNAME = require('os').hostname();

  2. restart Node-RED. Et Voila - $(HOSTNAME) now works.

You can set any env var you want under the process.env object.

Lets say you wanted to add FOO and set it to 'just another bar', you would add the following: process.env.FOO = 'just another bar'; and you can now use it.

Related