Question about subflows

Hi, I wonder if a running instance of a subflow has some unique id?

I need to temporary save a variable in (i.e. in global context) unique to a subsystem instance. If there is, is there then any way to "grab" that id from inside the subflow?

So basically, this is how the subflow works:

  • I have several instances of the subflow running
  • any instance can at any time send a request to a single external process for analyzing
  • when the result then is returned, I need to match this to the correct subflow instance, the issuing instance of the request

If the subflow instance would have a unique id I could basically store the variable like "global.set(subflow_id, my_variable);" and then when the result is returned check and match it

Don't think it is possible, but I could be wrong.

Easiest way to do this, is to add an environment variable like "subflowId" and set this up in the UI properties
Per subflow instance you enter the id and can do whatever you want with it.

You can get this variable by using something like msg.subflowId = env.get("subflowId") in your subflow.

subflow > edit properties eg.

You can create a subflow property that you can then give it a unique value when you set up the subflow instance, then in the subflow you can use the property by accessing it as an enviromental variable
eg.

[{"id":"20de693b.4818b6","type":"subflow","name":"Subflow 1","info":"","category":"","in":[{"x":50,"y":30,"wires":[{"id":"7259a3b5.931d6c"}]}],"out":[{"x":280,"y":60,"wires":[{"id":"7259a3b5.931d6c","port":0}]}],"env":[{"name":"sub_id","type":"str","value":"","ui":{"type":"input","opts":{"types":["str"]}}}],"color":"#DDAA99"},{"id":"7259a3b5.931d6c","type":"function","z":"20de693b.4818b6","name":"","func":"global.set(env.get(\"sub_id\"), msg.payload);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":180,"y":160,"wires":[[]]},{"id":"282feb47.86fdcc","type":"inject","z":"b779de97.b1b46","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":200,"y":4880,"wires":[["8a8601e9.736cb8"]]},{"id":"8a8601e9.736cb8","type":"subflow:20de693b.4818b6","z":"b779de97.b1b46","name":"","env":[{"name":"sub_id","value":"atest","type":"str"}],"x":400,"y":4880,"wires":[[]]}]

[edit] Snap.

@E1cid and @bakman2 , thank you both very much, following your suggestions I finally made it work. Only drawback is that you have to edit each instance, I was targeting a generic solution to have it more "automatic"

If only the subflow instances could have their own id like a node has it's own node_id. Or even better, if subflow instances could have their own local context everything would have been much easier

They do have their own context... flow context.

Oh, maybe I have missed that!? Does it mean that each instance of a subflow has it's own flow context even if they are placed on the same flow tab?

Yes.

https://nodered.org/docs/user-guide/context#context-scopes

Oh, perfect, that is eactly what I was looking for, thanks!

In the same way as the nodes inside a subflow can access the context of the parent flow by prepending $parent is there an opposite way as well? So that nodes in the parent flow can access the flow context inside the subflow?

No - there is no way to drill in to a subflow's context.

Drill in - that was the wording I was looking for. I'm working on a complex flow where this ability would be very useful. I have subflow instances that are either free or busy and I need to signal that to nodes outside the subflow. Currently using a rather "non-sophisticated method" involving many extra nodes outside of the subflow(s). Would such a feature break some general rules or design pattern?

Would such a feature break some general rules or design pattern?

It would be fairly brittle as it would have to rely in the instance id of the subflow - and as soon as you copy/paste the node things may change.

Have you considered using the Subflow Status node to set the state of the subflow that can be monitored with a Status node outside of the subflow?

Right, that might be the way, thanks, will try that!!

EDIT: Confirming, works brilliant!

1 Like

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