Subflow: Environment variable affecting all instances instead of one

I have a Node-RED flow that includes multiple instances of a subflow. Within the subflow, I define a boolean environment variable, DEBUG_VERBOSE, which defaults to false.

Inside the subflow, I use the following code:

const DEBUG = env.get('DEBUG_VERBOSE');
if (DEBUG) node.warn("some text");

However, when I set DEBUG_VERBOSE to true in just one instance, I notice that warnings appear from all instances. Why is this happening?

The environment variable is global for all subflow instances. If you want to manage the variables with each subflow instance seperately/individually, use a flow variable instead. A flow variable should be local to each subflow instance.

See also: Flow variables in subflows

Hi @hominidae,

Are you sure this applies to environment variables set through the subflow properties?

  1. In my case, one of the environment variables I pass is a light sensor name, and the lights turn on/off correctly. If the variables were shared, all instances would control the same light.
  2. That would defeat the purpose of configuring environment properties within the subflow—how would you track the actual values if each instance allows different settings?

Here’s a working example (interestingly, "DEBUG" behaves as expected in this case):

example:

[
    {
        "id": "c77464f0692082d8",
        "type": "subflow",
        "name": "envVariable",
        "info": "",
        "category": "",
        "in": [
            {
                "x": 120,
                "y": 60,
                "wires": [
                    {
                        "id": "ae2c437f642028e2"
                    }
                ]
            }
        ],
        "out": [
            {
                "x": 380,
                "y": 60,
                "wires": [
                    {
                        "id": "ae2c437f642028e2",
                        "port": 0
                    }
                ]
            }
        ],
        "env": [
            {
                "name": "testName",
                "type": "str",
                "value": "test1"
            },
            {
                "name": "DEBUG_VERBOSE",
                "type": "bool",
                "value": "false"
            }
        ],
        "meta": {},
        "color": "#DDAA99"
    },
    {
        "id": "ae2c437f642028e2",
        "type": "function",
        "z": "c77464f0692082d8",
        "name": "test",
        "func": "node.warn(env.get(\"testName\"));\nconst DEBUG = env.get(\"DEBUG_VERBOSE\");\nif (DEBUG) node.warn(\"DEBUG is TRUE\");\nreturn msg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 250,
        "y": 60,
        "wires": [
            []
        ]
    },
    {
        "id": "4284f5b11bac1fb9",
        "type": "tab",
        "label": "test env variables",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "087aa76bd5d5b795",
        "type": "subflow:c77464f0692082d8",
        "z": "4284f5b11bac1fb9",
        "name": "",
        "x": 330,
        "y": 80,
        "wires": [
            []
        ]
    },
    {
        "id": "3a284023c415c461",
        "type": "inject",
        "z": "4284f5b11bac1fb9",
        "name": "all",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 90,
        "y": 40,
        "wires": [
            [
                "087aa76bd5d5b795",
                "32cd43170e7b4147",
                "9d12c18e69614587"
            ]
        ]
    },
    {
        "id": "32cd43170e7b4147",
        "type": "subflow:c77464f0692082d8",
        "z": "4284f5b11bac1fb9",
        "name": "",
        "env": [
            {
                "name": "testName",
                "value": "test2",
                "type": "str"
            },
            {
                "name": "DEBUG_VERBOSE",
                "value": "true",
                "type": "bool"
            }
        ],
        "x": 330,
        "y": 140,
        "wires": [
            []
        ]
    },
    {
        "id": "9d12c18e69614587",
        "type": "subflow:c77464f0692082d8",
        "z": "4284f5b11bac1fb9",
        "name": "",
        "env": [
            {
                "name": "testName",
                "value": "test3",
                "type": "str"
            }
        ],
        "x": 330,
        "y": 200,
        "wires": [
            []
        ]
    },
    {
        "id": "5bb8a70d185d4f03",
        "type": "inject",
        "z": "4284f5b11bac1fb9",
        "name": "1",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 90,
        "y": 80,
        "wires": [
            [
                "087aa76bd5d5b795"
            ]
        ]
    },
    {
        "id": "df967d6c47cead50",
        "type": "inject",
        "z": "4284f5b11bac1fb9",
        "name": "2",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 90,
        "y": 140,
        "wires": [
            [
                "32cd43170e7b4147"
            ]
        ]
    },
    {
        "id": "9e83536746611a40",
        "type": "inject",
        "z": "4284f5b11bac1fb9",
        "name": "3",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 90,
        "y": 200,
        "wires": [
            [
                "9d12c18e69614587"
            ]
        ]
    }
]

well, I am not sure but this is how your first examples behaves, isn't it?
See: Using environment variables : Node-RED
If the Env variable you define has the same value for each instance, each subflow will use the same value individually.
I can't check your other flow atm, but here - as you described it - you are using clear, distinct names and not a boolean.

In my example I have both a string and a boolean in the env variable. the boolean is exactly the same way I use it in my regular flow. Except in the example, it works as expected.

@yanivf can you confirm what version of Node-RED you are using?

hi @knolleary ,
My node-red is home-assistant add-on version 19.0.2 (inside node-red it says v4.0.9)

OK, your test flow works here, at least for NR v3.1.5 (which was what I had available here at the time being).
Do you only have defined the bool variable in the subflow that does not work? Maybe a long haul guess...

The flow you shared is behaving for me in NR 4.0.9 as well.

What do you see when you run it?