A very standard ui-table situation

Dear All,

I have a very standard situation where I have multiple temperature/humidity sensors which are distributed in our apartment. I just want to feed the payloads into a ui-table. Important to mention that the sensors do only report when changes in temperature/humidity are registered. And this is what I want to have, just to update the table (and ony the respective cells) if required.

However, my basic problem starts earlier: How can I get both sensors in the example below feed into ui-table ? Please look at my example flow. Is it really required to generate an 'arrayed' payload first ?

[{"id":"5d3de8db.f734e8","type":"inject","z":"2beb2f2e.99868","name":"Output_Sensor_1","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"humidity\":42.9,\"temperature\":21.74}","payloadType":"json","x":180,"y":1390,"wires":[["89f58166.2e746"]]},{"id":"65e29898.2f6a28","type":"inject","z":"2beb2f2e.99868","name":"Output_Sensor_2","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"humidity\":53.8,\"temperature\":23.41}","payloadType":"json","x":180,"y":1440,"wires":[["47ab2e50.f98bd"]]},{"id":"89f58166.2e746","type":"change","z":"2beb2f2e.99868","name":"","rules":[{"t":"set","p":"payload.room","pt":"msg","to":"Room 1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":430,"y":1390,"wires":[["c622219b.20832"]]},{"id":"47ab2e50.f98bd","type":"change","z":"2beb2f2e.99868","name":"","rules":[{"t":"set","p":"payload.room","pt":"msg","to":"Room 2","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":430,"y":1440,"wires":[["c622219b.20832"]]},{"id":"c622219b.20832","type":"function","z":"2beb2f2e.99868","name":"manipulate payload","func":"var t = Math.round(msg.payload.temperature*10)/10; // Temperature\nvar h = Math.round(msg.payload.humidity); // Humidity\nvar r = msg.payload.room; // Room\n\nvar msg1 = { payload: [ { room:r, temp: t, humid: h } ]};\nreturn msg1;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":680,"y":1410,"wires":[["8bc1cbe9.84e9c8"]]},{"id":"8bc1cbe9.84e9c8","type":"ui_table","z":"2beb2f2e.99868","group":"4b41a1fd.e0159","name":"mytable","order":1,"width":"6","height":"4","columns":[{"field":"room","title":"Room","width":"","align":"left","formatter":"plaintext","formatterParams":{"target":"_blank"}},{"field":"temp","title":"Temperature","width":"","align":"left","formatter":"plaintext","formatterParams":{"target":"_blank"}},{"field":"humid","title":"Humidity","width":"","align":"left","formatter":"plaintext","formatterParams":{"target":"_blank"}}],"outputs":0,"cts":false,"x":880,"y":1410,"wires":[]},{"id":"9107512f.eb64","type":"comment","z":"2beb2f2e.99868","name":"Outputs of Sensor1 and Sensor2 not simultaneous","info":"","x":260,"y":1340,"wires":[]},{"id":"4b41a1fd.e0159","type":"ui_group","name":"Group 5","tab":"29ec6908.552b36","order":5,"disp":true,"width":6},{"id":"29ec6908.552b36","type":"ui_tab","name":"Table_UI_TEST","icon":"track_changes","order":8,"disabled":false,"hidden":false}]

Thanks in advance

Not really, but it is easier to generate an array with the full table. Alternatively you could use tabulator commands to update individual rows. The ui_table documentation explains how to issue tabulator commands to the node.

If you want to go to the easier route here is how it could be done.

1- Generate a data structure that is easier to work with. I would create an object like the one below. It is straightforward to update this data structure with your data.

r-1

2- Add a function node to modify your data structure to the array format required by the ui_table node. It is quite easy with jsonata but you can go with javascript if you want to. You should end up with an array like this one.

r-2

Here is how you table will display in the dashboard:

r-3

Code:

[{"id":"cbe143b3.14b06","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"ff82eac1.97cf48","type":"inject","z":"cbe143b3.14b06","name":"Output_Sensor_1","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"humidity\":42.9,\"temperature\":21.74}","payloadType":"json","x":300,"y":200,"wires":[["f9a24427.b1da88"]]},{"id":"68e050b9.9ee73","type":"inject","z":"cbe143b3.14b06","name":"Output_Sensor_2","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"humidity\":53.8,\"temperature\":23.41}","payloadType":"json","x":300,"y":250,"wires":[["cbe45d87.fcb53"]]},{"id":"cbe45d87.fcb53","type":"change","z":"cbe143b3.14b06","name":"","rules":[{"t":"set","p":"payload.room","pt":"msg","to":"Room2","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":550,"y":250,"wires":[["e7a74899.5e6338"]]},{"id":"e7a74899.5e6338","type":"function","z":"cbe143b3.14b06","name":"manipulate payload","func":"var t = Math.round(msg.payload.temperature*10)/10; // Temperature\nvar h = Math.round(msg.payload.humidity); // Humidity\nvar r = msg.payload.room; // Room\n\nlet room = global.get(\"room\") || {} ;\n\nroom[r] = { \"temp\" : t, \"humid\" : h , \"room\" : r};\n\nglobal.set(\"room\" , room);\n\nmsg.payload = room;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":790,"y":220,"wires":[["a92778e.fc58388"]]},{"id":"3e2b2225.92171e","type":"ui_table","z":"cbe143b3.14b06","group":"46394dc6.651624","name":"mytable","order":1,"width":"6","height":"4","columns":[{"field":"room","title":"Room","width":"","align":"left","formatter":"plaintext","formatterParams":{"target":"_blank"}},{"field":"temp","title":"Temperature","width":"","align":"left","formatter":"plaintext","formatterParams":{"target":"_blank"}},{"field":"humid","title":"Humidity","width":"","align":"left","formatter":"plaintext","formatterParams":{"target":"_blank"}}],"outputs":0,"cts":false,"x":1180,"y":220,"wires":[]},{"id":"7388db54.963164","type":"comment","z":"cbe143b3.14b06","name":"Outputs of Sensor1 and Sensor2 not simultaneous","info":"","x":380,"y":150,"wires":[]},{"id":"ea3afeb8.59b73","type":"debug","z":"cbe143b3.14b06","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1150,"y":160,"wires":[]},{"id":"f9a24427.b1da88","type":"change","z":"cbe143b3.14b06","name":"","rules":[{"t":"set","p":"payload.room","pt":"msg","to":"Room1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":540,"y":200,"wires":[["e7a74899.5e6338"]]},{"id":"a92778e.fc58388","type":"change","z":"cbe143b3.14b06","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.*","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":980,"y":220,"wires":[["3e2b2225.92171e","ea3afeb8.59b73"]]},{"id":"46394dc6.651624","type":"ui_group","name":"Group 5","tab":"54a84fff.34561","order":5,"disp":true,"width":6},{"id":"54a84fff.34561","type":"ui_tab","name":"Table_UI_TEST","icon":"track_changes","order":8,"disabled":false,"hidden":false}]

Dear Andrei,

good approach using global context. Thanks.
I will continue based on that (eventually keeping all in one function node instead of using JSONata).
Nevertheless, the JSONata command is a nice one :slight_smile: -> I understand what it does but I don't understand why :slight_smile:

Thanks

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.