Multiple subflows with context variables in a tab

Hello,
I have a little problem understanding & figuring out how I can have multiple subflows on the same tab flow where each subflow needs to have it's own variables in context

As example, I have this test flow below. The only thing the subflow does is to set a flow context variable with the timestamp. But if I put more subflows in the same way into a single tab, there is a conflict

Is there another way how each subflow can store individual variables in context instead of using $parent.timestamp as in the example? Maybe like adding the unique node_id to the variable names somehow could be a solution ($parent.timestamp + node_id)?

Best regards, Walter

image

[{"id":"598467b2.d6e028","type":"subflow","name":"My timestamp","info":"","category":"","in":[{"x":220,"y":80,"wires":[{"id":"2f923787.1c4268"}]}],"out":[{"x":620,"y":80,"wires":[{"id":"2f923787.1c4268","port":0}]}],"env":[],"meta":{},"color":"#DDAA99"},{"id":"2f923787.1c4268","type":"change","z":"598467b2.d6e028","name":"","rules":[{"t":"set","p":"$parent.timestamp","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":420,"y":80,"wires":[[]]},{"id":"ebd92723.099598","type":"inject","z":"472fc173.1567","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":340,"y":1380,"wires":[["aba3db61.9fd578"]]},{"id":"7789d431.011d4c","type":"debug","z":"472fc173.1567","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":790,"y":1380,"wires":[]},{"id":"aba3db61.9fd578","type":"subflow:598467b2.d6e028","z":"472fc173.1567","name":"","env":[],"x":560,"y":1380,"wires":[["7789d431.011d4c"]]}]

I believe you can use flow context in a subflow and then each subflow will have its own copy.

I forgot mention that I also need to make the variables accessible to other nodes in the same tab

In that case you could use a subflow property to specify an id which you add as a suffix to the parent flow context var to identify which subflow it relates to.

Yes, what I thought, but I would like to avoid having to put that in manually, I would like to automate, make it a no-brainer. That's why I thought of using the node-id but I dunno know how??

If it were automatic how would you know how to address the var when used in the parent?

I plan to keep an array or so in the parent with those

You can feed in a unique id say in msg.topic or _msgid, and then merge the context store with a new object using JSONata, I found that it only works with globals
e.g.

[{"id":"598467b2.d6e028","type":"subflow","name":"My timestamp","info":"","category":"","in":[{"x":220,"y":80,"wires":[{"id":"2f923787.1c4268"}]}],"out":[{"x":620,"y":80,"wires":[{"id":"2f923787.1c4268","port":0}]}],"env":[],"color":"#DDAA99"},{"id":"2f923787.1c4268","type":"change","z":"598467b2.d6e028","name":"","rules":[{"t":"set","p":"timestamp","pt":"global","to":"$merge([$globalContext(\"timestamp\"),{$$.topic:$$.payload}])","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":80,"wires":[[]]},{"id":"a6f9391f.533ff","type":"subflow:598467b2.d6e028","z":"b779de97.b1b46","name":"","x":600,"y":2700,"wires":[["7789d431.011d4c"]]},{"id":"ebd92723.099598","type":"inject","z":"b779de97.b1b46","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"uniqueid1","payload":"","payloadType":"date","x":400,"y":2640,"wires":[["a6f9391f.533ff"]]},{"id":"7789d431.011d4c","type":"debug","z":"b779de97.b1b46","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":700,"y":2660,"wires":[]}]

Yes, I think that could work fine, I can use globals instead since they are unique,
Thank you!!

Are you sure you really need to use context? Alternatively you could pass the information out of the subflow, then it would be automatic. I tend to consider that I have failed if I have to use flow or global context.

No, not sure :wink: Not yet, still too much of an experimental work in progress,,,

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