Skip to content

Instantly share code, notes, and snippets.

@juggledad
Last active November 29, 2021 10:57
Show Gist options
  • Save juggledad/66a6acd17ed49cb5cd464d58627b7d78 to your computer and use it in GitHub Desktop.
Save juggledad/66a6acd17ed49cb5cd464d58627b7d78 to your computer and use it in GitHub Desktop.
DEMO: multiple devices (pickable via a dropdown) feeding single gauge

Prerequisites: this flow uses node-red-contrib-random

This is a simple demo showing how you could have a single dashboard page with one gauge and a dropdown that is used to select which device is sending data to the gauge.

The three devices are simulated with inject nodes connected to random nodes connected to a change node that sets msg.topic to the unique device name. The random node for device 1 generates numbers between 1 and 10, the random node for device 2 generates numbers between 11 and 21, the random node for device 3 generates numbers between 20 and 31.

The msg from all three devices are then passed to a switch node that checks the msg.topic against a flow variable that is initialized to device1 and is changed by selecting a device from the dropdown. Only msgs that match are passed on.

The gauge displays numbers between 0-10 will display in the green color range, 11-20 in the yellow range and 21-30 in the red color range to give a visual clue to which device is reporting the data along with the label of the gauge changing to the device number.

multi-device-gauge-demo

[{"id":"13170589.bf6fd2","type":"tab","label":"Multi device-gauge ","disabled":false,"info":""},{"id":"7b531c11.f67024","type":"inject","z":"13170589.bf6fd2","name":"Device 1","topic":"","payload":"","payloadType":"date","repeat":"1","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":300,"wires":[["a83780c5.b055e8"]]},{"id":"a308ede3.ab1868","type":"inject","z":"13170589.bf6fd2","name":"Device 2","topic":"","payload":"","payloadType":"date","repeat":"2","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":340,"wires":[["160d3204.08c70e"]]},{"id":"1c3da3c1.1f22a4","type":"inject","z":"13170589.bf6fd2","name":"Device 3","topic":"","payload":"","payloadType":"date","repeat":"3","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":380,"wires":[["eb872888.48fd4"]]},{"id":"738911c4.73b9d","type":"change","z":"13170589.bf6fd2","name":"topic = \"device1\"","rules":[{"t":"set","p":"topic","pt":"msg","to":"device1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":470,"y":300,"wires":[["b9a84f61.ebdef8"]]},{"id":"3fb16384.6742dc","type":"change","z":"13170589.bf6fd2","name":"topic = \"device2\"","rules":[{"t":"set","p":"topic","pt":"msg","to":"device2","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":470,"y":340,"wires":[["b9a84f61.ebdef8"]]},{"id":"a24359b7.98ef48","type":"change","z":"13170589.bf6fd2","name":"topic = \"device3\"","rules":[{"t":"set","p":"topic","pt":"msg","to":"device3","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":470,"y":380,"wires":[["b9a84f61.ebdef8"]]},{"id":"44db16e6.dc6e18","type":"ui_dropdown","z":"13170589.bf6fd2","name":"","label":"","tooltip":"","place":"Select option","group":"48cfdbc6.f90534","order":2,"width":0,"height":0,"passthru":true,"options":[{"label":"Device 1","value":"device1","type":"str"},{"label":"Device 2","value":"device2","type":"str"},{"label":"Device 3","value":"device3","type":"str"}],"payload":"","topic":"","x":110,"y":480,"wires":[["a63e3ed2.d8549"]]},{"id":"a63e3ed2.d8549","type":"change","z":"13170589.bf6fd2","name":"topic = msg.payload","rules":[{"t":"set","p":"device","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":304,"y":479,"wires":[[]]},{"id":"cf58110c.4440c","type":"ui_gauge","z":"13170589.bf6fd2","name":"","group":"48cfdbc6.f90534","order":1,"width":0,"height":0,"gtype":"gage","title":"{{msg.topic}}","label":"units","format":"{{value}}","min":0,"max":"30","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":930,"y":340,"wires":[]},{"id":"b9a84f61.ebdef8","type":"switch","z":"13170589.bf6fd2","name":"","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"device","vt":"flow"},{"t":"eq","v":"device","vt":"flow"},{"t":"eq","v":"device","vt":"flow"}],"checkall":"true","repair":false,"outputs":3,"x":650,"y":340,"wires":[["cf58110c.4440c","f6cd1dc7.a4f808"],["cf58110c.4440c","7d69424f.e79c24"],["cf58110c.4440c","5551adfd.26e7c4"]]},{"id":"b552a78.116b258","type":"inject","z":"13170589.bf6fd2","name":"initilize the flow variable \"device\"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":200,"y":220,"wires":[["1fd4d6db.4555a1"]]},{"id":"1fd4d6db.4555a1","type":"change","z":"13170589.bf6fd2","name":"topic = \"device1\"","rules":[{"t":"set","p":"device","pt":"flow","to":"device1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":470,"y":220,"wires":[[]]},{"id":"5551adfd.26e7c4","type":"debug","z":"13170589.bf6fd2","name":"DEBUG: Device 3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":730,"y":500,"wires":[]},{"id":"7d69424f.e79c24","type":"debug","z":"13170589.bf6fd2","name":"DEBUG: Device 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":730,"y":460,"wires":[]},{"id":"f6cd1dc7.a4f808","type":"debug","z":"13170589.bf6fd2","name":"DEBUG: Device 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":730,"y":420,"wires":[]},{"id":"a83780c5.b055e8","type":"random","z":"13170589.bf6fd2","name":"random 1-10","low":"1","high":"10","inte":"true","property":"payload","x":270,"y":300,"wires":[["738911c4.73b9d"]]},{"id":"160d3204.08c70e","type":"random","z":"13170589.bf6fd2","name":"random 11-20","low":"11","high":"20","inte":"true","property":"payload","x":280,"y":340,"wires":[["3fb16384.6742dc"]]},{"id":"eb872888.48fd4","type":"random","z":"13170589.bf6fd2","name":"random 21-30","low":"21","high":"30","inte":"true","property":"payload","x":280,"y":380,"wires":[["a24359b7.98ef48"]]},{"id":"a6e5289c.ef0c8","type":"comment","z":"13170589.bf6fd2","name":"initilize flow variable at deploy","info":"","x":160,"y":180,"wires":[]},{"id":"10781d93.540e2a","type":"comment","z":"13170589.bf6fd2","name":"This section simulates data coming from three devices","info":"","x":240,"y":260,"wires":[]},{"id":"fa1f44f4.f1c948","type":"comment","z":"13170589.bf6fd2","name":"the switch determines which data goes to the gauge","info":"","x":790,"y":300,"wires":[]},{"id":"29195282.9ba90e","type":"comment","z":"13170589.bf6fd2","name":"Dropdown to pick device to monitor","info":"","x":180,"y":440,"wires":[]},{"id":"f98827b5.2c2f6","type":"comment","z":"13170589.bf6fd2","name":"This flow demonstrate how three devices could send data to a single gauge","info":"","x":310,"y":40,"wires":[]},{"id":"8cc5fca3.17512","type":"comment","z":"13170589.bf6fd2","name":"The devices send random data in groups to trigger the gauge's color","info":"","x":280,"y":80,"wires":[]},{"id":"7bb7e4fc.66bf0c","type":"comment","z":"13170589.bf6fd2","name":"Device 1 : 1-10 (green) - Device 2 : 11-20 (yellow) - Device 3 : 21-30 (red)","info":"","x":300,"y":120,"wires":[]},{"id":"48cfdbc6.f90534","type":"ui_group","z":"","name":"Dynamic device gauge Picker","tab":"10fce37c.85a72d","order":2,"disp":true,"width":"6","collapse":false},{"id":"10fce37c.85a72d","type":"ui_tab","name":"Tab 3","icon":"dashboard","order":1}]
@lorenzozaccomer
Copy link

Hello, it doesn't work when I change the inject time of the message (for example 1 minute), if I select for example the first device and after the second device, the information will not update with the last data of the last one until I received the information from the second device.

How I can solve it?
Thanks

@juggledad
Copy link
Author

HI, if you want to do that then you would have to expand the flows. i would store the latest value of each device in a flow variable. then in the dropdown flow you would look at the selection, get the flow variable for that device and send it thru the switch that feeds the gauge. That should work.

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