How to isolate temporary data used in a subflow?

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?

From the doc: Working with context : Node-RED

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)


but .. how set node-only values?

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.

1 Like

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.


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

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.

Now you explained, it's obvious also to me :slight_smile:

1 Like

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