Using environment variables

I try to use my own environment variables in sub-flows. That's my goal and I fail.
So I go back to a very simple 'flow' with nothing but a Inject node and a debug node.

I set up the Inject node and replace the default 'timestamp' by 'Environment' ('Umgebungsvariable' in my German setup) and ask for "USER". The answer I get is 'root' and that's what I expect because "USER" is set in Linux and I am 'root' here.

I replace 'USER' by 'NR_NODE_ID' and I get a long Hex-Value.

Now I ask for 'PORT' or 'uiPort' or 'flowFile' or anything else available in 'settings.js' and here I get nothing than empty strings: "".

My understanding is that settings in 'settings.js' under 'module.exports' are treated as 'Environment Variables'.

Can anybody help me here? Because as long as this simple stuff is not working it makes no sense to play with 'env.get("something")....

That is very bad practice and will likely cause you other issues later on.

What set these env variables? They need to have been set before Node-RED starts for them to be visible to Node-RED.

No, they exist on an object called RED which is available in a function node. They are only available as env variables if they were set as such prior to the launch of Node-RED.

I think it should be RED.util.getSetting() but I have to admit that I can't get that to work at all right now.

Seems that, while RED.util.getSetting() certainly exists when called from a function node (you can look it up with Object.keys(RED.util), it does not actually work. @knolleary or @Steve-Mcl - is that supposed to work in a function node? Sorry, I can never remember and I can't seem to look it up.

I suspect that the environment variable $USER is inherited by Node-red from the shell that starts it up. Other Bash variables such as $PWD are available too.

The syntax for setting an environment variable in settings.js is like this - making IP and HOSTNAME available withing Node-red:

 hostname = require('os').hostname();
 const execSync = require('child_process').execSync;
 const ipaddress = execSync('/home/pi/bin/ipaddress', { encoding: 'utf-8' });
 process.env.IP = ipaddress;
 process.env.HOSTNAME = hostname;

I don't know why I have require('os').hostname() for one OS command but execSync('/home/pi/bin/ipaddress', { encoding: 'utf-8' }) for another!

@TotallyInformation: I know about the risk to run programs as 'root'. My test- Node-Red is running inside a proxmox - LXC, so the risk is limited.

Thank you all for your input, but I still do not know how to introduce an environment variable.

To make long story short:

I want to have a flow with two nodes:

  • Inject node with msg.payload = $MY_ENV_VAR
  • A debug node to show the value of MY_ENV_VAR

Then I want to assign "Hello World" to MY_EǸV_VAR and want to see "Hello World" in the right hand debug area if I press the Inject.

Pretty simple. Isn't it ? :grinning:


only the items listed in the code completion are relevant to the function node


Also listed here: @node-red/util_util - Documentation

Anything else should be considered private.

Can you explain why you want to do this with an environment variable rather than a context variable?

A simple request, not a veiled criticism :smiley:

Yes. No problem.
As mentioned in the initial post the goal is to work with Subflows. And my understandig is that these Subflows are managed by Environment Variables.

Subflow Instance properties
Since 0.20, Subflows can be configured with instance properties. These appear as environment variables within the Subflow and can be customised for individual instances of the subflow.

That's the reason why.

Thanks :smiley:

I've never used subflows hence I didn't know about that.

@all: I think I got it.
I have to define the environment variable when creating the subflow.
If the subflow is done, I can use it. And when using it I can adapt the values of the environment variables. They appear as properties in the subflow-instance. And that's the way to customize the subflow, (if necessary).

Another way is to transfer information via 'payload'.
But this is much more complicated. The way to use environment variables is much smarter and in line with the general handling of all other nodes.

Btw: my understanding is a little bit different from the way they are used here. But perhaps it is consistent within node red....

Thank's a lot.

Can you post an example of the way you can adapt the values of the environment variables in the subflow to help educate me and anyone else new to subflows?

So far I have managed to define environment variables in the subflow's config and to access them (and $parent flow variables) in the subflow code.

Just to wrap up the question about access to settings.js properties inside node-red. If you are not writing a custom node, your only choice is to make a change to settings.js like this (which I do anyway always):

const nrsettings = {
  // All the existing settings go in here rather than exporting the object directly
  // ...

// Splitting the export this way allows us to dynamically override settings if we want to
// Or, as in this case, to pass a setting into Node-RED
nrsettings.functionGlobalContext._port = nrsettings.uiPort

module.exports = nrsettings

My project is for Smart Home.
The current task was to display and track gauges for nominal and actual temperature and flash a 'LED' if any window in the room is open. An open window reduces the nominal temperature.

And I want to track the state of the window. I want to know if it is open and I want to know how long it is open. The state will be sent to a log file and I can get an information via email about the change of the state. But I don't want to get an email all the time, so I can switch off email support.

For a room, let's say the office, I have one thermostat and two windows.

This is the flow I started with

For all windows (10) in all rooms I have a very similar flow. They only differ in the name of the room, window or door, and in a 'Subject' for the email.

So I select some nodes and transfer these nodes into a Subflow.

If I double click the subflow I get this:

When selecting the properties I can add environment variables:

Now I go back to my application and select 'Subflow 1'.

I modify my three values

and do the same for the other window.

That's it.

Thanks, a careful detailed explanation. I'm studying it.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.