Skip to content

Commit 4aee6fe

Browse files
authored
Merge pull request #285 from drmibell/master
Update environment-variables.md
2 parents 90f7268 + 1dbbb28 commit 4aee6fe

File tree

1 file changed

+52
-29
lines changed

1 file changed

+52
-29
lines changed

docs/user-guide/environment-variables.md

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -60,24 +60,14 @@ function:
6060
let foo = env.get("FOO");
6161
```
6262

63-
### Built-In Environment Variables
64-
65-
*Since Node-RED 2.2*
63+
### Template node
6664

67-
Node-RED maintains its own set of environment variables for exposing information about the node itself. This information helps "locate" the node in your [workspace](/docs/user-guide/editor/workspace/index.md). Nodes in your workspace exist as part of a [flow](/docs/user-guide/editor/workspace/flows.md). Likewise, a node may (or may not) be part of a [group](/docs/user-guide/editor/workspace/groups.md). Nodes, flows and groups are each given unique IDs that are generated by Node-RED.
68-
69-
Nodes, flows and groups all support the `name` property, which you can change when [editing properties](/docs/user-guide/editor/workspace/nodes.md#editing-node-properties).
65+
*Since [Node-RED 3.0](https://nodered.org/blog/2022/07/14/version-3-0-released#environment-variables-in-the-template-node)*
7066

71-
The following environment variables can be used to access this contextual information for the node.
72-
73-
- `NR_NODE_ID`
74-
- `NR_NODE_NAME`
75-
- `NR_GROUP_ID`
76-
- `NR_GROUP_NAME`
77-
- `NR_FLOW_ID`
78-
- `NR_FLOW_NAME`
79-
80-
Note that while the IDs generated by Node-RED are guaranteed to be unique, the names are not. If a node, flow or group is not given a name, then the corresponding environment variable will return an empty string. If a node is not part of a group, its group id environment variable will also return an empty string.
67+
The `template` node can access environment variables using the syntax:
68+
```
69+
My favourite colour is {{env.COLOUR}}
70+
```
8171

8272
### Subflow Instance properties
8373

@@ -90,28 +80,61 @@ a subflow could be created to access the API and handle the response, using an
9080
environment variable to identify which type of record should be accessed. Individual
9181
instances of the Subflow can then be customised for those particular types.
9282

93-
### Flow/Group level environment variable
9483

95-
Since 2.21, environment variables can be set for Flow or Group on their settings panel.
9684

97-
### Predefined environment variable
85+
### Flow/Group level environment variables
9886

99-
Since 2.22, following predefined environment variables has been added that provides access to information about the node, group and flow.
87+
*Since [Node-RED 2.1](https://nodered.org/blog/2021/10/21/version-2-1-released#flowgroup-level-environment-variables)*
88+
89+
Environment variables can be set at the flow or group level. This is done in the appropriate tab in the edit dialog for the flow or group.
90+
91+
### Accessing nested environment variables
92+
93+
When accessing an environment variable in a subflow, Node-RED will search the
94+
subflow properties, then the flow containing the subflow (which could be a subflow
95+
itself).
96+
97+
In some cases it is useful to access the 'parent' levels environment variables
98+
without reference the 'local' value. This can be achieved by prefixing the
99+
variable name with `$parent.`.
100+
101+
102+
### Running as a service
103+
104+
When Node-RED is running as a service having been installed using the provided [script](https://nodered.org/docs/getting-started/raspberrypi), it will not have access to environment variables that are defined only in the calling process. In this instance, environment variables can be defined in the settings file by adding:
105+
106+
```
107+
process.env.FOO='World';
108+
```
109+
110+
placed outside the `module.exports` section. Alternatively, variables can be defined as part of the `systemd` service by placing statements of the form
111+
112+
```
113+
ENV_VAR='foobar'
114+
```
115+
in a file named `environment` within the Node-RED user directory, `~/.node-red`.
116+
117+
118+
### Built-In Environment Variables
119+
120+
*Since Node-RED 2.2*
121+
122+
Node-RED defines a set of environment variables for exposing information about the nodes, flows and groups.
123+
124+
This information helps "locate" the node in your [workspace](/docs/user-guide/editor/workspace/index.md). Nodes in your workspace exist as part of a [flow](/docs/user-guide/editor/workspace/flows.md). Likewise, a node may (or may not) be part of a [group](/docs/user-guide/editor/workspace/groups.md). Nodes, flows and groups are each given unique IDs that are generated by Node-RED.
125+
126+
Nodes, flows and groups all support the `name` property, which you can change when [editing properties](/docs/user-guide/editor/workspace/nodes.md#editing-node-properties).
127+
128+
The following environment variables can be used to access this information for a given node:
100129

101130
- `NR_NODE_ID` - the ID of the node
102131
- `NR_NODE_NAME` - the Name of the node
103-
- `NR_NODE_NAME` - the Path of the node
104-
105-
The node's path represents a node's hierarchy in a flow. It is `/`
106-
delimited IDs of the flow, enclosing subflows, and the node.
132+
- `NR_NODE_NAME` - the Path of the node. This represents a node's position in a flow. It is `/` delimited IDs of the flow, enclosing subflows, and the node.
107133
- `NR_GROUP_ID` - the ID of the containing group
108134
- `NR_GROUP_NAME` - the Name of the containing group
109135
- `NR_FLOW_ID` - the ID of the flow the node is on
110136
- `NR_FLOW_NAME` - the Name of the flow the node is on
111137

112-
### Accessing nested environment variable
138+
Note that while the IDs generated by Node-RED are guaranteed to be unique, the names are not. If a node, flow or group does not have a given name, the corresponding environment variable will be an empty string. If a node is not part of a group, its group id environment variable will also return an empty string.
139+
113140

114-
Environment variable definition is searched from the bottom to the top
115-
of the node's definition hierarchy. `$ parent.` prefix can be added to
116-
environment variable name (e.g. `$parent.NR_GROUP_NAME`) to move the
117-
start position of this search up one level in the hierarchy.

0 commit comments

Comments
 (0)