# How to read parent flow variable in a subflow?

**URL:** <https://discourse.nodered.org/t/how-to-read-parent-flow-variable-in-a-subflow/93039>\
**Category:** General\
**Created:** [10 November 2024 19:09 UTC](https://discourse.nodered.org/t/how-to-read-parent-flow-variable-in-a-subflow/93039 "2024-11-10T19:09:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kerb](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kerb/32/96293_2.png) [@Kerb](https://discourse.nodered.org/u/Kerb)\
**Post date:** [10 November 2024 19:09 UTC](https://discourse.nodered.org/t/how-to-read-parent-flow-variable-in-a-subflow/93039/1 "2024-11-10T19:09:32Z")

</div>

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 ?

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/5/65a3a4015565b659daebf5ab3267648943cda2b2.png)

Node-RED version: v3.0.2

```auto
[
    {
        "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": [
            []
        ]
    }
]

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [10 November 2024 20:43 UTC](https://discourse.nodered.org/t/how-to-read-parent-flow-variable-in-a-subflow/93039/2 "2024-11-10T20:43:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kerb](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kerb/32/96293_2.png) [@Kerb](https://discourse.nodered.org/u/Kerb)\
**Post date:** [10 November 2024 21:05 UTC](https://discourse.nodered.org/t/how-to-read-parent-flow-variable-in-a-subflow/93039/3 "2024-11-10T21:05:18Z")

</div>

Thanks E1cid,

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/5/d5f8314f81445ae1b2c08750f4c8e8d4af24cf2c.png)

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

---

<div class="post-metadata">

**Author:** ![GogoVega](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gogovega/32/71313_2.png) [@GogoVega](https://discourse.nodered.org/u/GogoVega)\
**Post date:** [10 November 2024 21:40 UTC](https://discourse.nodered.org/t/how-to-read-parent-flow-variable-in-a-subflow/93039/4 "2024-11-10T21:40:40Z")

</div>

Objects have special behavior:

```js
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

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [10 November 2024 22:24 UTC](https://discourse.nodered.org/t/how-to-read-parent-flow-variable-in-a-subflow/93039/5 "2024-11-10T22:24:57Z")

</div>

> [@Kerb](#):
>
> Additional question, what the meaning of "Deep copy value" check box ?

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [24 November 2024 22:25 UTC](https://discourse.nodered.org/t/how-to-read-parent-flow-variable-in-a-subflow/93039/6 "2024-11-24T22:25:40Z")

</div>

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