Is it possible to trigger a flow and wait for it to complete?

Quick summary : I'd like to trigger a flow from within a function node, then wait for it to complete, either by receiving a message or detecting a change in a context variable.

It may be that the new async mode in v1.0 will help with this, and if so I'm happy to try out the Beta.

More detail : I'm building a number of identical devices based on the ESP8266, and when each powers up it will send its MAC address to a Node-RED based controller. The controller will look up the MAC in a table, and reply with the device's "ID", effectively its MQTT topic. This means I don't have to hard code each device's ID, and if one fails I can substitute another with just a quick edit to the table data file.

However, if a device's message arrives before the table has been loaded into the global context (which might happen after a power outage) I'd like to wait in the lookup function until it's available.

I've tried various flow loops and async functions, but so far can't come up with a workable solution. I've even had occasional infinite loops which needed a controller reboot to get out of :open_mouth:

Any suggestions (including updating to v1.0) would be very welcome.

m

Have you looked at the 'queue' contrib nodes that are available.
https://flows.nodered.org/?term=queue&num_pages=1

They halt the flow until a process has completed.

Thanks Paul. I'll have a look at those, and it may be possible to do some sort of looped flow which waits for a message from the table loading flow. It would be nice to do it all inside the function, but that may not be possible (I've just started learning Node-RED).

m

If it helps, take a look at how this was achieved by @dceejay in this example;
https://flows.nodered.org/flow/cea8afa28b7a93ebdc0f

Ah, that looks interesting. I didn't know about queues as part of Node.js - I can see I've still a lot to learn about. I'll try to adapt the way this works to use the message from the table loading.

m

1 Like

Thanks! This proved to be a quick and easy solution. I used the "node-red-contrib-queue-gate" node to queue up the device messages, and sent an "open" message from the table load flow. Works a treat :smiley:

Simples!!

(I'll also take a closer look at the @dceejay flow though, as it looks like I can learn a lot from it.)

m

Good choice!

2 Likes