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

3

6

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?

Pontine answered 1/6, 2017 at 3:53 Comment(0)
Z
4

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)

Zonked answered 1/6, 2017 at 7:46 Comment(3)
I see... But where can I add a value to the variable ENV_VAR_NAME?Pontine
Depends what node-red is running, but you can set them on the cmd line before starting node-redZonked
Do you mean setting as a system variable on a terminal before starting the Node-RED? Could you give me more details? I tried setting system variables but it didn't work :/Pontine
U
1

Maybe this helps: https://nodered.org/docs/writing-functions#global-context

Global context

There is also a global context available that is shared by, and accessible to all nodes. For example to make the variable foo available globally across the canvas:

global.set("foo","bar");  // this is now available to other nodes

And can then be read using .get

var myfoo = global.get("foo");  // this should now be "bar"
Usn answered 16/9, 2017 at 14:29 Comment(1)
As discussed in the other answer the global context is not useful for this questionZonked
P
0

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.

Photographic answered 26/11, 2017 at 0:30 Comment(2)
HOSTNAME is a special case as for some reason NodeJS removes this from the list of environment variables exposed at start up. Setting anything else via the settings.js file is not required and actually harder to do in PaaS environmentsZonked
You can use this method to add any variable. Add (above the module.exports lines): <process.env.SECRETMSG = 'Nick and Dave are awsome!';<then add <env:process.env <to the < functionGlobalContext: <and you can then access a function node with: var env = global.get('env'); msg.payload = env.SECRETMSG; return msg;Photographic

© 2022 - 2024 — McMap. All rights reserved.