unipi1.1. evok to MQTT using Node-Red
-
I am using this NODE-RED flow to read all unipi data into my MQTT server.
It initializes all states for my unipi control panel. Then it uses websocket for real time unipi events but with one exception.```
1-wire sensors are read every 5 min (bulk request) which gives me better statistics and charts.
I hope this will give interested persons some ideas but also I would appreciate suggestions to make it better.[{"id":"f8678f60.d5327","type":"tab","label":"unipi to MQTT","disabled":false,"info":""},{"id":"425e99ca.4a2418","type":"mqtt out","z":"f8678f60.d5327","name":"Relay, input, ai, ao Status","topic":"","qos":"0","retain":"","broker":"f75fab70.b2cd18","x":830,"y":580,"wires":[]},{"id":"29777893.b1a018","type":"function","z":"f8678f60.d5327","name":"format ai","func":"var t_loc = \"home/ug/Buero/unipi/1/\";\nmsg.topic = t_loc + msg.payload.dev + \"/\" + msg.payload.circuit +\"/\";\nif(msg.payload.dev == \"ai\"){\n msg.payload = msg.payload.value.toFixed(4); \n}\nelse{\n msg.payload = msg.payload.value;\n}\nreturn msg;","outputs":1,"noerr":0,"x":640,"y":580,"wires":[["425e99ca.4a2418"]]},{"id":"5ede4ca2.f90cb4","type":"websocket in","z":"f8678f60.d5327","name":"","server":"","client":"990d9908.58cdd8","x":140,"y":580,"wires":[["46654644.1df548"]]},{"id":"a864b8fe.d45638","type":"function","z":"f8678f60.d5327","name":"no temp","func":"if(msg.payload.dev != \"temp\"){\n return msg; \n}\n","outputs":1,"noerr":0,"x":500,"y":580,"wires":[["29777893.b1a018"]]},{"id":"f08bf24b.e7a51","type":"function","z":"f8678f60.d5327","name":"temp-1 DS18B20","func":"var t_loc = \"home/ug/Buero/unipi/1/temp/1/\";\nt_loc = t_loc + msg.payload.circuit +\"/\";\nmsg.topic = t_loc;\nvar output = msg.payload.value;\nmsg.payload = output.toFixed(2);\nreturn msg;","outputs":1,"noerr":0,"x":1050,"y":160,"wires":[["3be383a8.548c6c"]]},{"id":"3be383a8.548c6c","type":"mqtt out","z":"f8678f60.d5327","name":"unipi 1 temp 1","topic":"","qos":"","retain":"","broker":"f75fab70.b2cd18","x":1240,"y":160,"wires":[]},{"id":"b3ec1ef1.54292","type":"mqtt out","z":"f8678f60.d5327","name":"unipi 1 temp 2","topic":"","qos":"","retain":"","broker":"f75fab70.b2cd18","x":1240,"y":200,"wires":[]},{"id":"c71ef89e.5b24c8","type":"function","z":"f8678f60.d5327","name":"temp-2 DS18B20","func":"var t_loc = \"home/ug/Buero/unipi/1/temp/2/\";\nt_loc = t_loc + msg.payload.circuit +\"/\";\nmsg.topic = t_loc;\nvar output = msg.payload.value;\nmsg.payload = output.toFixed(2);\nreturn msg;","outputs":1,"noerr":0,"x":1050,"y":200,"wires":[["b3ec1ef1.54292"]]},{"id":"bca1e190.e9d98","type":"function","z":"f8678f60.d5327","name":"temp-3 DS18B20","func":"var t_loc = \"home/ug/Buero/unipi/1/temp/3/\";\nt_loc = t_loc + msg.payload.circuit +\"/\";\nmsg.topic = t_loc;\nvar output = msg.payload.value;\nmsg.payload = output.toFixed(2);\nreturn msg;","outputs":1,"noerr":0,"x":1050,"y":240,"wires":[["f029bb17.3d1618"]]},{"id":"f029bb17.3d1618","type":"mqtt out","z":"f8678f60.d5327","name":"unipi 1 temp 3","topic":"","qos":"","retain":"","broker":"f75fab70.b2cd18","x":1240,"y":240,"wires":[]},{"id":"5ad4d62d.5e80c8","type":"function","z":"f8678f60.d5327","name":"temp-4 DS18B20","func":"var t_loc = \"home/ug/Buero/unipi/1/temp/4/\";\nt_loc = t_loc + msg.payload.circuit +\"/\";\nmsg.topic = t_loc;\nvar output = msg.payload.value;\nmsg.payload = output.toFixed(2);\nreturn msg;","outputs":1,"noerr":0,"x":1050,"y":280,"wires":[["f9333318.bb5a2"]]},{"id":"b4c4344.57a9dc8","type":"function","z":"f8678f60.d5327","name":"humidity DS2438","func":"var vdd = parseFloat(msg.payload.vdd);\nvar vad = parseFloat(msg.payload.vad);\nvar temp = parseFloat(msg.payload.temp);\nvar stdhum = ((vad / vdd) - 0.16) / 0.0062; \nvar truehum = (stdhum / (1.0546 - (0.00216 * temp)));\nmsg.payload = truehum.toFixed(2);\nmsg.payload = msg.payload.trim();\nif (msg.payload > 100){\n msg.payload = global.get('ghum');\n}\n//\nvar t_loc = \"home/ug/Buero/unipi/1/humidity/1/\";\nt_loc = t_loc + msg.payload.circuit +\"/\";\nmsg.topic = t_loc;\n//\nreturn msg;","outputs":1,"noerr":0,"x":1050,"y":420,"wires":[["30b11170.1e2a1e"]]},{"id":"7e250c5f.fd3ee4","type":"function","z":"f8678f60.d5327","name":"temp 6 DS2438","func":"var t_loc = \"home/ug/Buero/unipi/1/temp/6/\";\nt_loc = t_loc + msg.payload.circuit +\"/\";\nmsg.topic = t_loc;\nvar output = parseFloat(msg.payload.temp);\nmsg.payload = output.toFixed(2);\nreturn msg;","outputs":1,"noerr":0,"x":1060,"y":460,"wires":[["45936118.dcbbe"]]},{"id":"f9333318.bb5a2","type":"mqtt out","z":"f8678f60.d5327","name":"unipi 1 temp 4","topic":"","qos":"","retain":"","broker":"f75fab70.b2cd18","x":1240,"y":280,"wires":[]},{"id":"30b11170.1e2a1e","type":"mqtt out","z":"f8678f60.d5327","name":"unipi 1 humidity 1","topic":"","qos":"","retain":"","broker":"f75fab70.b2cd18","x":1250,"y":420,"wires":[]},{"id":"45936118.dcbbe","type":"mqtt out","z":"f8678f60.d5327","name":"unipi 1 temp 6","topic":"","qos":"","retain":"","broker":"f75fab70.b2cd18","x":1240,"y":460,"wires":[]},{"id":"e5025120.5b355","type":"function","z":"f8678f60.d5327","name":"temp-5 DS2438","func":"var t_loc = \"home/ug/Buero/unipi/1/temp/5/\";\nt_loc = t_loc + msg.payload.circuit +\"/\";\nmsg.topic = t_loc;\nvar output = parseFloat(msg.payload.temp);\nmsg.payload = output.toFixed(2);\nreturn msg;","outputs":1,"noerr":0,"x":1060,"y":380,"wires":[["9ed8047c.f3f058"]]},{"id":"9ed8047c.f3f058","type":"mqtt out","z":"f8678f60.d5327","name":"unipi 1 temp 5","topic":"","qos":"","retain":"","broker":"f75fab70.b2cd18","x":1240,"y":380,"wires":[]},{"id":"4c1b3270.369abc","type":"function","z":"f8678f60.d5327","name":"light DS2438","func":"var t_loc = \"home/ug/Buero/unipi/1/light/1/\";\nt_loc = t_loc + msg.payload.circuit +\"/\";\nmsg.topic = t_loc;\nvar intensity = parseFloat(msg.payload.vis);\nif(intensity <= 0){\n intensity = 0.00;\n}\nelse{\n intensity = intensity * 100/0.25;\n}\nmsg.payload = intensity.toFixed(2);\nmsg.payload = msg.payload.trim();\n//if (msg.payload >= 100){\n// msg.payload = global.get('glight');\n//}\nreturn msg;","outputs":1,"noerr":0,"x":1070,"y":500,"wires":[["15aedf8f.c552f"]]},{"id":"15aedf8f.c552f","type":"mqtt out","z":"f8678f60.d5327","name":"unipi 1 light 1","topic":"","qos":"","retain":"","broker":"f75fab70.b2cd18","x":1230,"y":500,"wires":[]},{"id":"ef3a8f6d.2f621","type":"function","z":"f8678f60.d5327","name":"Pressure DS18B20","func":"var t_loc = \"home/ug/Buero/unipi/1/pressure/1/\";\nt_loc = t_loc + msg.payload.circuit +\"/\";\nmsg.topic = t_loc;\nvar p1 = parseFloat(msg.payload.value);\nvar p2 = (p1*3.2)+700;\nvar prel = p2/Math.pow((1-482/44330), 5.255);\nmsg.payload = prel.toFixed(2);\nreturn msg;","outputs":1,"noerr":0,"x":1050,"y":320,"wires":[["d8a2d644.9554c8"]]},{"id":"d8a2d644.9554c8","type":"mqtt out","z":"f8678f60.d5327","name":"unipi 1 pressure 1","topic":"","qos":"","retain":"","broker":"f75fab70.b2cd18","x":1250,"y":320,"wires":[]},{"id":"97eb1941.4ecc08","type":"function","z":"f8678f60.d5327","name":"","func":"if (msg.payload.circuit === \"28B01DC107000045\"){\n return [msg,null,null,null,null,null,null]; \n} \nelse if (msg.payload.circuit === \"282836C1070000DD\"){\n return [null,msg,null,null,null,null,null]; \n} \nelse if (msg.payload.circuit === \"2892844D07000012\"){\n return [null,null,msg,null,null,null,null]; \n} \nelse if (msg.payload.circuit === \"281893C00700005E\"){\n return [null,null,null,msg,null,null,null]; \n} \nelse if (msg.payload.circuit === \"28A2DC8400000366\"){\n return [null,null,null,null,msg,null,null]; \n} \nelse if (msg.payload.circuit === \"266B64140200002D\"){\n return [null,null,null,null,null,msg,null]; \n}\nelse if (msg.payload.circuit === \"2616BF0A020000FC\"){\n return [null,null,null,null,null,null,msg]; \n}","outputs":7,"noerr":0,"x":770,"y":300,"wires":[["f08bf24b.e7a51"],["c71ef89e.5b24c8"],["bca1e190.e9d98"],["5ad4d62d.5e80c8"],["ef3a8f6d.2f621"],["e5025120.5b355","b4c4344.57a9dc8"],["7e250c5f.fd3ee4","4c1b3270.369abc"]]},{"id":"46654644.1df548","type":"function","z":"f8678f60.d5327","name":"parse JSON","func":"return{payload : JSON.parse(msg.payload)};","outputs":1,"noerr":0,"x":350,"y":580,"wires":[["a864b8fe.d45638"]]},{"id":"b6b92a45.1c8d28","type":"function","z":"f8678f60.d5327","name":"","func":"msg.url = \"http://10.0.0.52:8080/bulk\";\nmsg.headers =JSON.stringify({\"content-type\":\"application/json\"});\nmsg.payload = JSON.stringify({\"group_queries\": [{\"device_types\": [\"temp\",\"relay\",\"input\"]}]});\nreturn msg;\n\n","outputs":1,"noerr":0,"x":250,"y":80,"wires":[["bf0c4b0b.b56638"]]},{"id":"8c0f4577.83b128","type":"inject","z":"f8678f60.d5327","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"onceDelay":"0.3","x":110,"y":80,"wires":[["b6b92a45.1c8d28"]]},{"id":"bf0c4b0b.b56638","type":"http request","z":"f8678f60.d5327","name":"unipi 1","method":"POST","ret":"obj","url":"","tls":"","x":370,"y":80,"wires":[["b6115323.a444e"]]},{"id":"91f83346.6450f","type":"split","z":"f8678f60.d5327","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":610,"y":80,"wires":[["6012b09e.6dac6"]]},{"id":"6012b09e.6dac6","type":"function","z":"f8678f60.d5327","name":"","func":"if (msg.payload.dev === \"relay\"){\n return [msg,null,null]; \n}\nelse if (msg.payload.dev === \"input\"){\n return [null,msg,null]; \n}\nelse if (msg.payload.dev === \"temp\"){\n return [null,null,msg]; \n}","outputs":3,"noerr":0,"x":730,"y":80,"wires":[["b8044d09.5d792"],["417274e8.91ca7c"],["97eb1941.4ecc08"]]},{"id":"b6115323.a444e","type":"function","z":"f8678f60.d5327","name":"","func":"return{payload : msg.payload.data.group_queries[0]};","outputs":1,"noerr":0,"x":490,"y":80,"wires":[["91f83346.6450f"]]},{"id":"8a9fbef6.b5fcd","type":"mqtt out","z":"f8678f60.d5327","name":"MQTT relay Status","topic":"","qos":"","retain":"","broker":"813194e1.4ab8a8","x":1050,"y":60,"wires":[]},{"id":"b8044d09.5d792","type":"function","z":"f8678f60.d5327","name":"relay","func":"msg.topic = \"home/ug/Buero/unipi/1/relay/\" + msg.payload.circuit +\"/\";\nmsg.payload = msg.payload.value;\nreturn msg;","outputs":1,"noerr":0,"x":890,"y":60,"wires":[["8a9fbef6.b5fcd"]]},{"id":"417274e8.91ca7c","type":"function","z":"f8678f60.d5327","name":"input","func":"msg.topic = \"home/ug/Buero/unipi/1/input/\" + msg.payload.circuit +\"/\";\nmsg.payload = msg.payload.value;\nreturn msg;","outputs":1,"noerr":0,"x":890,"y":100,"wires":[["8696be09.54cce"]]},{"id":"8696be09.54cce","type":"mqtt out","z":"f8678f60.d5327","name":"MQTT input Status","topic":"","qos":"","retain":"","broker":"813194e1.4ab8a8","x":1050,"y":100,"wires":[]},{"id":"c2c30d37.de6ae","type":"inject","z":"f8678f60.d5327","name":"","topic":"","payload":"","payloadType":"date","repeat":"300","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":300,"wires":[["f5f849f5.62eb48"]]},{"id":"f5f849f5.62eb48","type":"function","z":"f8678f60.d5327","name":"","func":"msg.url = \"http://10.0.0.52:8080/bulk\";\nmsg.headers =JSON.stringify({\"content-type\":\"application/json\"});\nmsg.payload = JSON.stringify({\"group_queries\": [{\"device_types\": [\"temp\"]}]});\nreturn msg;\n\n","outputs":1,"noerr":0,"x":250,"y":300,"wires":[["6f854ab0.64d634"]]},{"id":"6f854ab0.64d634","type":"http request","z":"f8678f60.d5327","name":"unipi 1","method":"POST","ret":"obj","url":"","tls":"","x":370,"y":300,"wires":[["7fbfe0ea.7be2f"]]},{"id":"f3df64d3.5075e8","type":"split","z":"f8678f60.d5327","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":610,"y":300,"wires":[["97eb1941.4ecc08"]]},{"id":"7fbfe0ea.7be2f","type":"function","z":"f8678f60.d5327","name":"","func":"return{payload : msg.payload.data.group_queries[0]};","outputs":1,"noerr":0,"x":490,"y":300,"wires":[["f3df64d3.5075e8"]]},{"id":"597a13df.071aec","type":"comment","z":"f8678f60.d5327","name":"Initialisation (bulk request)","info":"","x":130,"y":40,"wires":[]},{"id":"c29cc9fa.d7b108","type":"comment","z":"f8678f60.d5327","name":"read 1-Wire every 5 minutes (bulk request)","info":"","x":180,"y":260,"wires":[]},{"id":"60c51296.d7872c","type":"comment","z":"f8678f60.d5327","name":"websocket receive realtime events of unipi","info":"","x":180,"y":540,"wires":[]},{"id":"f75fab70.b2cd18","type":"mqtt-broker","z":"","broker":"10.0.0.43","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"990d9908.58cdd8","type":"websocket-client","z":"","path":"ws://10.0.0.52:8080/ws","tls":"","wholemsg":"false"},{"id":"813194e1.4ab8a8","type":"mqtt-broker","z":"","broker":"10.0.0.43","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","willTopic":"","willQos":"0","willPayload":""}]
-