Monday, June 11, 2018

A simple use of the join node

This is a very simple flow built on the top of a previous post .

It uses a Node-RED join node to group sequences os messages received from sensors. The output of the join node will give us an array with a fixed number of elements. This number of elements is configurable.




We will select the manual mode and set the number of messages to, let´s say five.




Here is like the flow looks like:



and this is what the flow shows in the debug node after you press the inject buttons a number of times:



As we expected the outcome is an array of five elements inside the message payload.


This array will allow us to perform some interesting calculations, like averaging values, checking the number of readings from each sensor, spot readings above the average, triggering other events, etc.


Flow:

[{"id":"369eef7d.03645","type":"tab","label":"Flow 18","disabled":false,"info":""},{"id":"ccb23d2.10b15c","type":"inject","z":"369eef7d.03645","name":"","topic":"sensor1","payload":"9","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":156,"y":107,"wires":[["1fe33550.256e1b"]]},{"id":"1fe33550.256e1b","type":"function","z":"369eef7d.03645","name":"Format sensor output","func":"let value = Math.floor(Math.random() * msg.payload + 1)\nmsg.payload = {[msg.topic] : value};\n//delete msg.topic;\nreturn msg;","outputs":1,"noerr":0,"x":386.0000686645508,"y":185.99999904632568,"wires":[["d25aaeae.def1f"]]},{"id":"b06bf348.e7bac","type":"inject","z":"369eef7d.03645","name":"","topic":"sensor2","payload":"9","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":153.89999771118164,"y":160.99999523162842,"wires":[["1fe33550.256e1b"]]},{"id":"ce089477.49c8a8","type":"inject","z":"369eef7d.03645","name":"","topic":"sensor3","payload":"9","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":153.89999771118164,"y":212.99999523162842,"wires":[["1fe33550.256e1b"]]},{"id":"a543c32d.d11d7","type":"inject","z":"369eef7d.03645","name":"","topic":"sensor4","payload":"9","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":149.89999771118164,"y":263.9999952316284,"wires":[["1fe33550.256e1b"]]},{"id":"f18969a7.7a9c78","type":"debug","z":"369eef7d.03645","name":"All events","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":706.8000106811523,"y":186.00000381469727,"wires":[]},{"id":"d25aaeae.def1f","type":"join","z":"369eef7d.03645","name":"Join 5","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"5","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"num","reduceFixup":"","x":568.9001045227051,"y":185.00000286102295,"wires":[["f18969a7.7a9c78"]]}]