Need help setting up Sub Flow Environment Variable

Good day all,

First up, my Node Red Environment stats, Node Red v1.0.4, running on Windows 10, with node.js v12.16.1 and npm v6.13.4

I am at a complete lost on trying to do something simple with my first sub flow.

Here is my sub flow (which will cache data if Node Red Catches a Communication Error and resume sending out the cached data when communication is re-established)

For each Sub Flow that I use in my flow, I'd like each one of them to have its own flow.cached_data_temp_repository. This is to avoid writing the wrong cached data into the wrong database table if I only had one common flow.cached_data_temp_repository for all of my 10 separate database tables.

I came across this settings in the sub flow "edit properties" tab

How can I make use of this feature, so that all 10 of my sub-flows will have its own individual flow.cached_data_temp_repository respectively... like this

sub-flow no.1 will write to flow.cached_data_temp_repository_no1
sub flow no.2 will write to flow.cached_data_temp_repository_no2
sub flow no.3 will write to flow.cached_data_temp_repository_no3
... etc

Could I use the Environment Variables feature to assign different flow.cached_data_temp_repository_noX for each of my sub-flows to achieve this?

IF YES, how do I setup my function node and my change node to make use of this input that I have defined in my Environment Variables?

Here is my function node...

Here is one of my change node

Here is the other change Node

Below is my sub flow as you see in the screenshots above

[{"id":"1799862e.08b0aa","type":"switch","z":"926f6f3.0148b9","name":"","property":"network_link","propertyType":"flow","rules":[{"t":"eq","v":"Network OK","vt":"str"},{"t":"neq","v":"Network OK","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":130,"y":220,"wires":[["720d7cd9.f1f934"],["46285185.bb43e"]]},{"id":"720d7cd9.f1f934","type":"switch","z":"926f6f3.0148b9","name":"","property":"payload","propertyType":"msg","rules":[{"t":"hask","v":"Timestamp","vt":"str"}],"checkall":"false","repair":false,"outputs":1,"x":270,"y":200,"wires":[["cd164951.37ec98"]]},{"id":"cd164951.37ec98","type":"trigger","z":"926f6f3.0148b9","op1":"true","op2":"false","op1type":"bool","op2type":"bool","duration":"1","extend":true,"units":"s","reset":"","bytopic":"all","name":"","x":400,"y":200,"wires":[["f4aaac97.6ee29"]]},{"id":"f4aaac97.6ee29","type":"rbe","z":"926f6f3.0148b9","name":"","func":"rbe","gap":"","start":"","inout":"out","property":"payload","x":410,"y":160,"wires":[["2d6e622a.77c7ae"]]},{"id":"2d6e622a.77c7ae","type":"switch","z":"926f6f3.0148b9","name":"","property":"payload","propertyType":"msg","rules":[{"t":"true"}],"checkall":"false","repair":false,"outputs":1,"x":410,"y":120,"wires":[["ff8a264c.9d7e08"]]},{"id":"ff8a264c.9d7e08","type":"change","z":"926f6f3.0148b9","name":"feed latest cache","rules":[{"t":"set","p":"payload","pt":"msg","to":"what_Var_do_I_put_IN_HERE?","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":630,"y":120,"wires":[["b02dfcc5.e375d"]]},{"id":"b02dfcc5.e375d","type":"function","z":"926f6f3.0148b9","name":"send one object out at a time from array","func":"var cached_data_array = msg.payload;\nvar msg1 = {};\n\nfor(var i=0; i < cached_data_array.length ;i++)\n{\nmsg.payload = cached_data_array[i];\nmsg1.payload = ((i+1) === cached_data_array.length)? \"cache_data_sent\" : null;\n\nnode.send([msg, msg1]);\n}\n\n\n\n\n","outputs":2,"noerr":0,"x":720,"y":160,"wires":[["1ce9caa7.7f85d5"],["db84beee.a6391"]]},{"id":"1ce9caa7.7f85d5","type":"delay","z":"926f6f3.0148b9","name":"","pauseType":"rate","timeout":"1","timeoutUnits":"seconds","rate":"3","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":850,"y":100,"wires":[[]]},{"id":"db84beee.a6391","type":"switch","z":"926f6f3.0148b9","name":"Check cache sent completed","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"cache_data_sent","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":680,"y":220,"wires":[["5f2c1b31.22edd4"]]},{"id":"5f2c1b31.22edd4","type":"change","z":"926f6f3.0148b9","name":"reset flow.latest_cache to \"empty\"","rules":[{"t":"set","p":"what_Var_do_I_put_IN_HERE?","pt":"flow","to":"[]","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":700,"y":260,"wires":[[]]},{"id":"46285185.bb43e","type":"function","z":"926f6f3.0148b9","name":"Write data to flow.latest_cache","func":"var feed = msg.payload;\nvar cn = env.get(\"what_Var_do_I_put_IN_HERE?\");\n\nvar data_cache = flow.get(cn);\n\ndata_cache.push(feed);\n\nmsg.payload = data_cache;\n\nflow.set(cn,data_cache);\n\nreturn msg;\n\n/*\nif (Array.isArray(array) && array.length) {\n    // code\n}\n*/","outputs":1,"noerr":0,"x":350,"y":240,"wires":[[]]}]

I am really looking forward to get any feedback from the Node Red experts here on resolving this simple problem of mine.

When inside a subflow, the flow context is scoped to that instance of the subflow. So they can all use the same context property name without overwriting each other.

Meaning even if I keep the exact same name such as flow.my_data_cache for all 10 of my sub flows they will not get all mixed up?

Owh! OK! That's good to know.... AWESOME!

I've been giving it a try but it's as though the subflows are not writing to the flow.latest_cache.

I tried going to my sub flows tab and looking up the "Context Data" Tab, and clicked on the refresh button for "Flow"... Unfortunately nothing is showing up.

Any thoughts on how I can verify that data is actually being cached into my sub flow flow.latest_cache for each of my sub flows?

The context sidebar doesn't work so well with subflows at the moment.

When you have the subflow open in a tab, then you are looking at the template of the subflow, not an individual instance. So the context sidebar doesn't know what instance to show you the data for.

Can you add a temporary flow in the subflow to write its contents to a debug node?

Thanks for the tip! I'll try that

Sorry for the delayed update.

Anyway, problem identified.

Just FYI the settings.js file have all context being written into a file instead of memory. So, my sub-flows are using the flow.latest_cache for the very first time, hence it just could'nt find the files to reference in my context folder. Which means, I didn't properly declare or setup a default value for my flow.latest_cache when it is run for the very first time.

So I added the following initialization function into my sub-flow to deal with this mistake.

/*Check if flow.latest_cache_done is undefined*/
if(typeof flow.get("latest_cache_done") === undefined){
    flow.set("latest_cache_done",true); //IF it is still undefined, flow.latest_cache_done shall be set to TRUE
}


/*Check if flow.latest_cache is undefined*/
if (typeof flow.get("latest_cache") === undefined ){  
/*IF flow.latest_cache is undefined (or encountered/used for the very first time)*/
    flow.set("latest_cache",[]);// declare it as an empty object "[]"
/*IF flow.latest_cache has array values/ cached data
BUT flow.latest_cache_done is TRUE (meaning that array/cache values has been transferred)*/    
}else if ((typeof flow.get("latest_cache").length !== 0) && flow.get("latest_cache_done") === true){
    flow.set("latest_cache",[]);// We can re-initialize the flow.latest_cache back to being empty "[]"
}else{
/*However, IF the flow.latest_cache_done equals FALSE, that means the cached data has NOT been transferred.
DO NOT re-initialize flow.latest_cache and retain its cached data values for next transfer*/
    flow.get("latest_cache");
}

Here is my updated data cache sub-flow with the flow initialization being done once at deploy.