Using Environmental Variables

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:

environmental-variables-1

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.

e-v-set

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)

environmental-variable-inject

environmental-variable-change

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;

ev-function

Predefined Environmental Variables

Version 2.2 included support for predefined environmental variables that allow access to node properties.

predefined-node-red-environmental-variables

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

environment-variable-flow

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 – Double click on the group to open its settings and Click on the environmental variable icon
  • Step 3 – Click on the add button to add a new variable

environment-variable-group

Global Enviromental Variables

Available in v3.1. These are configured in the settings menu.

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:

Click to rate this post!
[Total: 3 Average: 5]

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *