Wait for and combine multiple inputs to a node

I sometimes need to have multiple inputs to a node, however due to the asynchronous nature of node-red flows, this isn't quite trivial. Here is a template that can help solve this issue

[{"id":"715ae02.ffcb82","type":"function","name":"Wait for all tasks to finish","func":"context.data = context.data || new Object();\n\nswitch (msg.topic) {\n    case \"task1\":\n        context.data.task1 = msg.payload;\n        msg = null;\n        break;\n    case \"task2\":\n        context.data.task2 = msg.payload;\n        msg = null;\n        break;\n    case \"task3\":\n        context.data.task3 = msg.payload;\n        msg = null;\n        break;\n        \n    default:\n        msg = null;\n    \tbreak;\n\n}\n\nif(context.data.task1 != null && context.data.task2 != null && context.data.task3 != null) {\n\tmsg2 = new Object();\n    msg2 = context.data;\n    context.data=null;\n\treturn msg2;\n} else return msg;","outputs":1,"x":695,"y":212,"z":"4d5a619c.fa15f","wires":[["1db3a013.cd5468"]]},{"id":"33b2850d.f24e6a","type":"delay","name":"Random delay","pauseType":"random","timeout":"5","timeoutUnits":"seconds","rate":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":437,"y":121,"z":"4d5a619c.fa15f","wires":[["715ae02.ffcb82"]]},{"id":"1db3a013.cd5468","type":"debug","name":"","active":true,"console":"false","complete":"true","x":902,"y":211,"z":"4d5a619c.fa15f","wires":[]},{"id":"35eb6e77.0f77da","type":"delay","name":"Random delay","pauseType":"random","timeout":"5","timeoutUnits":"seconds","rate":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":441,"y":217,"z":"4d5a619c.fa15f","wires":[["715ae02.ffcb82"]]},{"id":"c8ab7682.98ada","type":"delay","name":"Random delay","pauseType":"random","timeout":"5","timeoutUnits":"seconds","rate":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":438,"y":301,"z":"4d5a619c.fa15f","wires":[["715ae02.ffcb82"]]},{"id":"1ccffde6.02aa52","type":"function","name":"Task1","func":"msg.topic=\"task1\";\nmsg.payload=\"Task1's payload\"\nreturn msg;","outputs":1,"x":269,"y":121,"z":"4d5a619c.fa15f","wires":[["33b2850d.f24e6a"]]},{"id":"fe4cbb39.4f93c8","type":"function","name":"Task2","func":"msg.topic=\"task2\";\nmsg.payload=\"Task2's payload\"\nreturn msg;","outputs":1,"x":272,"y":216,"z":"4d5a619c.fa15f","wires":[["35eb6e77.0f77da"]]},{"id":"c3766a5d.afab88","type":"function","name":"Task3","func":"msg.topic=\"task3\";\nmsg.payload=\"Task3's payload\"\nreturn msg;","outputs":1,"x":267,"y":300,"z":"4d5a619c.fa15f","wires":[["c8ab7682.98ada"]]},{"id":"2dc185ba.57852a","type":"inject","name":"Start","topic":"","payload":"","payloadType":"none","repeat":"","crontab":"","once":false,"x":103,"y":205,"z":"4d5a619c.fa15f","wires":[["1ccffde6.02aa52","fe4cbb39.4f93c8","c3766a5d.afab88"]]}]
mharizanov

Flow Info

created 2 years, 8 months ago

Node Types

Core
  • debug (x1)
  • delay (x3)
  • function (x4)
  • inject (x1)

Tags

Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option