How to read parent flow variable in a subflow?

Hello,
In my level 1 NR-flow, I am repeating the same node multiple times, so I figured out, it would be easier to maintain using a subflow.

My problem is now that I can't read flow variable from the parent flow (level 1) in the subflow.
I have read different posts on the subject, but my attempts are not successful.
The output msg is not set with the correct values.

Any advice ?

Node-RED version: v3.0.2

[
    {
        "id": "9e1d5c1629033e70",
        "type": "subflow",
        "name": "V3V State",
        "info": "",
        "category": "",
        "in": [
            {
                "x": 520,
                "y": 160,
                "wires": [
                    {
                        "id": "16fbf363e7292075"
                    }
                ]
            }
        ],
        "out": [
            {
                "x": 890,
                "y": 160,
                "wires": [
                    {
                        "id": "16fbf363e7292075",
                        "port": 0
                    }
                ]
            }
        ],
        "env": [],
        "meta": {},
        "color": "#DDAA99"
    },
    {
        "id": "16fbf363e7292075",
        "type": "change",
        "z": "9e1d5c1629033e70",
        "name": "State msg",
        "rules": [
            {
                "t": "set",
                "p": "topic",
                "pt": "msg",
                "to": "state",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "timestamp",
                "pt": "msg",
                "to": "$parent.timestamp",
                "tot": "env"
            },
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "$parent.position",
                "tot": "env"
            },
            {
                "t": "set",
                "p": "motion",
                "pt": "msg",
                "to": "$parent.motion",
                "tot": "env"
            },
            {
                "t": "set",
                "p": "control_mode",
                "pt": "msg",
                "to": "$parent.control_mode",
                "tot": "env"
            },
            {
                "t": "set",
                "p": "reliability",
                "pt": "msg",
                "to": "$parent.reliability",
                "tot": "env"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 700,
        "y": 160,
        "wires": [
            []
        ]
    }
]

The problem is you are using $parent in an environmental var, not a flow var.
$ is and environmental type input, If you want a flow var from the flow tab scope, then select flow type input

1 Like

Thanks E1cid,

I have changed the input to flow var scope as you suggest, and it seems to works.

Additional question, what the meaning of "Deep copy value" check box ?

Objects have special behavior:

const originalObject = {
    name: 'John',
    age: 30,
};
const modifiedObject = originalObject;

console.log(originalObject.age); // 30

modifiedObject.age = 40;
console.log(originalObject.age, modifiedObject.age);
// Expected 30 & 40 but obtains 40 & 40

A deep copy means the new var is referencing new memory and not the same memory as the copied var. Without deep copy changing the OG var would also change the new var, as they would be referencing the same memory. If you want a further understanding search Javaascript object by reference which should give you plenty of reading material

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.