# How to isolate temporary data used in a subflow?

**URL:** <https://discourse.nodered.org/t/how-to-isolate-temporary-data-used-in-a-subflow/83663>\
**Category:** General\
**Created:** [13 December 2023 08:44 UTC](https://discourse.nodered.org/t/how-to-isolate-temporary-data-used-in-a-subflow/83663 "2023-12-13T08:44:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![realtebo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/realtebo/32/85732_2.png) [@realtebo](https://discourse.nodered.org/u/realtebo)\
**Post date:** [13 December 2023 08:44 UTC](https://discourse.nodered.org/t/how-to-isolate-temporary-data-used-in-a-subflow/83663/1 "2023-12-13T08:44:21Z")

</div>

Coming from traditional programming, I am creating subflows to allow 'reusability' .

In main flow, I need to run 3 'instances' (is it the right term in node-red?) of a subflow

each subflow handle a msg,payload with an array of data

i am able to parse all data, aggregate and emit as a modified payload from the subflow

To do this, I am activley using flow.\* context variables to save data between manipulations

The problem I noticied is that the flow.\* variables are shared between all instances of same subflow, so using 1 instance works. using 3 instances causes that my data is messed up, mixed, between data coming from different instances in different time slice.

I was thinking that flow.\* variables was 'local'. Instead they are something like statics vars, shared between all instances.

To be more clear, and sorry because English is not my native language,

- first instance of a flow set flow.temp = 1
- second instance of a flow set flow.temp =2

I need, or I would like, to preserve flow.temp isolated between two instances.

Is it possible?

---

<div class="post-metadata">

**Author:** ![realtebo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/realtebo/32/85732_2.png) [@realtebo](https://discourse.nodered.org/u/realtebo)\
**Post date:** [13 December 2023 08:58 UTC](https://discourse.nodered.org/t/how-to-isolate-temporary-data-used-in-a-subflow/83663/2 "2023-12-13T08:58:42Z")

</div>

From the doc: [Working with context : Node-RED](https://nodered.org/docs/user-guide/context)

> ### Context scopes
> 
> The ‘scope’ of a particular context value determines who it is shared with. There are three context scope levels:
> 
> - Node - only visible to the node that set the value
> - Flow - visible to all nodes on the same flow (or tab in the editor)
> - Global - visible to all nodes

Also, from same page

In a 'change' node, I can set global and flow, (and msg of course)

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/a/ea16da426d80d5a52e3daad80d631caa5f539d00.png)

_but .. how set node-only values?_

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [13 December 2023 09:57 UTC](https://discourse.nodered.org/t/how-to-isolate-temporary-data-used-in-a-subflow/83663/3 "2023-12-13T09:57:19Z")

</div>

Since `node` context applies only to the node that created it, it doesn't make sense in a change node. If you were able to create a node connect in a change node, that very change node would be the only node able to access that data. No other node in your flow could access it.

You could use a `function` node to store and retrieve the data.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [13 December 2023 10:27 UTC](https://discourse.nodered.org/t/how-to-isolate-temporary-data-used-in-a-subflow/83663/4 "2023-12-13T10:27:41Z")

</div>

> [@realtebo](#):
>
> The problem I noticied is that the flow.\* variables are shared between all instances of same subflow, so using 1 instance works. using 3 instances causes that my data is messed up, mixed, between data coming from different instances in different time slice.

That should not happen, flow context within a subflow should be local to the subflow. Are you using a very early version of node-red?

The flow below should show that they are separate. Each time the subflow is given a message the payload is added to an array in flow context within the subflow.

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

```auto
[{"id":"47d3f2b2fd096b55","type":"subflow","name":"Subflow 1","info":"","in":[{"x":50,"y":30,"wires":[{"id":"20c389309476ef4c"}]}],"out":[{"x":380,"y":80,"wires":[{"id":"20c389309476ef4c","port":0}]}]},{"id":"20c389309476ef4c","type":"function","z":"47d3f2b2fd096b55","name":"function 3","func":"let data = flow.get(\"data\") || []\ndata.push(msg.payload)\nflow.set(\"data\", data)\nmsg.payload = data\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":200,"y":80,"wires":[[]]},{"id":"7004899a06a4faab","type":"subflow:47d3f2b2fd096b55","z":"bdd7be38.d3b55","name":"","x":420,"y":3040,"wires":[["26545cda72f1cafc"]]},{"id":"e7c47b6ed31e0d67","type":"inject","z":"bdd7be38.d3b55","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":260,"y":3040,"wires":[["7004899a06a4faab"]]},{"id":"26545cda72f1cafc","type":"debug","z":"bdd7be38.d3b55","name":"debug 2466","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":630,"y":3040,"wires":[]},{"id":"c9949f18f2df4793","type":"subflow:47d3f2b2fd096b55","z":"bdd7be38.d3b55","name":"","x":420,"y":3120,"wires":[["8226716e3302dc2c"]]},{"id":"d22feef13062ee10","type":"inject","z":"bdd7be38.d3b55","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":260,"y":3120,"wires":[["c9949f18f2df4793"]]},{"id":"8226716e3302dc2c","type":"debug","z":"bdd7be38.d3b55","name":"debug 2467","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":630,"y":3120,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![realtebo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/realtebo/32/85732_2.png) [@realtebo](https://discourse.nodered.org/u/realtebo)\
**Post date:** [13 December 2023 11:20 UTC](https://discourse.nodered.org/t/how-to-isolate-temporary-data-used-in-a-subflow/83663/5 "2023-12-13T11:20:58Z")

</div>

Thanks for reply

I am using v3.1.3 on a raspberry pi 2 model b, installed using official setup script and not the packaged version

Your workflow is working as expected. I'll retry in my workflow and eventually reply in this thread.

---

<div class="post-metadata">

**Author:** ![realtebo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/realtebo/32/85732_2.png) [@realtebo](https://discourse.nodered.org/u/realtebo)\
**Post date:** [13 December 2023 13:27 UTC](https://discourse.nodered.org/t/how-to-isolate-temporary-data-used-in-a-subflow/83663/6 "2023-12-13T13:27:11Z")

</div>

Now you explained, it's obvious also to me 🙂

---

<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:** [27 December 2023 13:27 UTC](https://discourse.nodered.org/t/how-to-isolate-temporary-data-used-in-a-subflow/83663/7 "2023-12-27T13:27:29Z")

</div>

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