<table style="width: 100%;">
<thead>
<tr>
<th>Topic</th>
<th>Online</th>
<th>QoS</th>
<th>Retain</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="row in msg.payload">
<td>{{row.topic.replace("/$online","")}}</td>
<td>{{row.payload}}</td>
<td>{{row.qos}}</td>
<td>{{row.retain}}</td>
</tr>
</tbody>
</table>
Hey guys,I want to display a list of (connected & online) devices in the Node-Red dashboard.The devices are ESPs that use MQTT to transfer data.If I subscribe to this topic "homie/+/$online" I get messages like these, one for each device:{ topic: "homie/esp04/$online",payload: "false", qos: 1, retain: true,_msgid: "785e78c4.4ddf28" }{ topic: "homie/esp05/$online",payload: "true", qos: 1, retain: true,_msgid: "26973b83.d4e664" }Now, I'd like to display the status of each device in the Dashboard in a list.But if I use a text node I only get the status of the most recent message.How would I turn this into a list?Can I do this directly via a dashboard template node?Or should I use a function node to and a global(?) array to store the status?What do you think?Cheersl.
Typically, I would use the ng-repeat directive in a ui_template node to render this array as an html table:
Hey guys,
With the mqtt node I get automatic messages from all ESPs running homie.
This is similar to the suggestion from David, but I didn't want to hardwire my devices so I can add or remove devices withouut changing the flow. Hence I used the wildcard.
The function node stores the data in the context and sends it on to the template node like Colin suggested.
The result now looks like this:
This is exactly what I was looking for. Thanks.
Here is the source:
[{"id":"86ef05b.41e0af8","type":"mqtt in","z":"b3c692d7.9e1bf","name":"","topic":"axure/+/$online","qos":"2","broker":"20b4bf75.197a","x":140,"y":100,"wires":[["30d90fac.5b27f","2be3948.a1bd06c"]]},{"id":"30d90fac.5b27f","type":"function","z":"b3c692d7.9e1bf","name":"store status","func":"var deviceStatus = context.get('deviceStatus') || {};\n\nvar parts = msg.topic.split(\"/\");\ndeviceStatus[parts[1]] = (msg.payload == \"true\") ? \"online\" : \"offline\";\ncontext.set(\"deviceStatus\", deviceStatus);\n\nmsg.topic = \"deviceStatus\";\nmsg.payload = deviceStatus;\nreturn msg;","outputs":1,"noerr":0,"x":330,"y":100,"wires":[["bb837253.0a60c","c2c8fbdb.e9b3b8"]]},{"id":"2be3948.a1bd06c","type":"debug","z":"b3c692d7.9e1bf","name":"","active":true,"console":"false","complete":"true","x":310,"y":140,"wires":[]},{"id":"2326e758.3682b8","type":"comment","z":"b3c692d7.9e1bf","name":"monitor online status","info":"","x":140,"y":60,"wires":[]},{"id":"bb837253.0a60c","type":"ui_template","z":"b3c692d7.9e1bf","group":"e536ee0f.6b586","name":"Device Status Table","order":0,"width":0,"height":0,"format":"\n<table style=\"width: 50%\">\n <thead>\n <tr>\n <th style=\"text-align: left\">Device</th>\n <th style=\"text-align: left; width: 10%\">Online</th>\n </tr>\n </thead>\n <tbody>\n <tr ng-repeat=\"(key, value) in msg.payload\">\n <td>{{key}}</td>\n <td ng-switch on=\"value\">\n <span ng-switch-when=\"online\"><ng-md-icon icon=\"check_circle\" style=\"color: green\" size=\"20\"></ng-md-icon></span>\n <span ng-switch-when=\"offline\"><ng-md-icon icon=\"error\" style=\"color: red\" size=\"20\"></ng-md-icon></span>\n </td>\n </tr>\n </tbody>\n</table>","storeOutMessages":true,"fwdInMessages":true,"x":550,"y":100,"wires":[[]]},{"id":"c2c8fbdb.e9b3b8","type":"debug","z":"b3c692d7.9e1bf","name":"","active":true,"console":"false","complete":"true","x":510,"y":140,"wires":[]},{"id":"20b4bf75.197a","type":"mqtt-broker","z":"","broker":"192.168.42.1","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willRetain":"false","willPayload":"","birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":""},{"id":"e536ee0f.6b586","type":"ui_group","z":"","name":"Axure Dashboard","tab":"3a8ca5f4.05522a","disp":true,"width":"8"},{"id":"3a8ca5f4.05522a","type":"ui_tab","z":"","name":"Home","icon":"dashboard"}]
Cheers
l.
In the function nodes I store each message in an flow context array.
The trigger node creates and updated the table also every 5 seconds.
The resulting table now looks like this:
Offline devices are grey, and low and critical battery statuses are indicated.
Cheers
l.