Hi,
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.
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:
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.
@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....
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
@jbudd
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.
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.