Skip to content

Instantly share code, notes, and snippets.

@mharizanov
Created November 6, 2014 13:54
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mharizanov/8ba7f90f3ea8d92b1e01 to your computer and use it in GitHub Desktop.
Save mharizanov/8ba7f90f3ea8d92b1e01 to your computer and use it in GitHub Desktop.
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"]]}]
@KirillDrozdov
Copy link

Hi, Matias, I can't export your flow, could please check it?

@Diez66
Copy link

Diez66 commented Apr 4, 2020

Problem solved a long time ago, for me, it was a new problem today and you solved it for me, Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment