Environmental variables are used on operating systems (Linux,Windows etc) to store user and system data.
Environmental variables are stored as key value pairs of the form name=value.
A common environmental variable on Linux and Windows is the use home directory.
On windows this is HOMEPATH and on Linux HOME.
To view the contents you use the echo command as show below:
echo %HOMEPATH% and echo $HOME
On Linux environmental variables are case sensitive.
All systems allow you to create your own environmental variables.
on Linux you use export
export test=myvariable
more details for linux are here
Details for window are here
The set command can also be used on both Windows an Linux to view all environmental variables.
Important Note: You cannot set environmental variables in a flow.
Using Environmental Variables in Node-Red
Environmental variables are available in many nodes like the inject node (shown below) and change node (shown below)
They can also be used in function nodes using env.get as shown in the sample code below:
let home=env.get("HOME"); msg.home=home; return msg;
Predefined Environmental Variables
Version 2.2 included support for predefined environmental variables that allow access to node properties.
Flow and Group Environmental Variables
These are actually part of the node-red environment and not the operating system. They are easier to set up and use.
To set up flow environmental variables.
- Step 1- Click in the flow tab
- Step2 – Click on the environmental variable icon
- Step 3 – Click on the add button to add a new variable
To set up group environmental variables you will first need to create a group of nodes.
If you are unfamiliar with groups then take a look at this grouping nodes video
- Step 1- Click in the group to highlight it.
- Step2 – Click on the environmental variable icon
- Step 3 – Click on the add button to add a new variable
Summary
System and user environmental variables can be used inside node-red and in addition node-red lets you set node-red specific environmental variables for use inside node-red.
Environmental variables are often used for flow initialisation.
Related Tutorials and Resources:
- Using the Node-Red Function Node- Beginners Guide
- Storing Data in Node-Red Variables
- Understanding and Using The Node-Red Message Object
- Node-Red Initialising Flow Data on Startup
- Working With Time- Node-Red Programming
- Node-Red Message and Object Cloning
An idea how a suflow can retrieve the name used in the instance node?