Node-red read sensors from HA and out put average data

Hello,
I tried to read the 3 DTH11 from HA and wants to get average temp/humid from the 3 sensors. How can I use the function to get each data?

[{"id":"f734f8c6ce29227a","type":"poll-state","z":"8c081f22d984df18","name":"","server":"a1278bc0.5f1838","version":2,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"updateinterval":"5","updateIntervalType":"num","updateIntervalUnits":"seconds","outputinitially":false,"outputonchanged":false,"entity_id":"sensor.f1_temp1","state_type":"num","halt_if":"0","halt_if_type":"num","halt_if_compare":"gt","outputs":2,"x":170,"y":100,"wires":[["318eb3d8c75cc3a9"],[]],"outputLabels":["temp1",""]},{"id":"7dfe251d455d4cad","type":"poll-state","z":"8c081f22d984df18","name":"","server":"a1278bc0.5f1838","version":2,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"updateinterval":"5","updateIntervalType":"num","updateIntervalUnits":"seconds","outputinitially":false,"outputonchanged":false,"entity_id":"sensor.f1_humid1","state_type":"num","halt_if":"0","halt_if_type":"num","halt_if_compare":"gt","outputs":2,"x":180,"y":160,"wires":[[],[]],"outputLabels":["humid1",""]},{"id":"42683a8f42c8ebcd","type":"poll-state","z":"8c081f22d984df18","name":"","server":"a1278bc0.5f1838","version":2,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"updateinterval":"5","updateIntervalType":"num","updateIntervalUnits":"seconds","outputinitially":false,"outputonchanged":false,"entity_id":"sensor.f1_temp2","state_type":"num","halt_if":"0","halt_if_type":"num","halt_if_compare":"gt","outputs":2,"x":170,"y":220,"wires":[["318eb3d8c75cc3a9"],[]],"outputLabels":["temp2",""]},{"id":"f724dcfd126c6a42","type":"poll-state","z":"8c081f22d984df18","name":"","server":"a1278bc0.5f1838","version":2,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"updateinterval":"5","updateIntervalType":"num","updateIntervalUnits":"seconds","outputinitially":false,"outputonchanged":false,"entity_id":"sensor.f1_humid2","state_type":"num","halt_if":"0","halt_if_type":"num","halt_if_compare":"gt","outputs":2,"x":180,"y":280,"wires":[[],[]],"outputLabels":["humid2",""]},{"id":"beadeb789874a2b7","type":"poll-state","z":"8c081f22d984df18","name":"","server":"a1278bc0.5f1838","version":2,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"updateinterval":"5","updateIntervalType":"num","updateIntervalUnits":"seconds","outputinitially":false,"outputonchanged":false,"entity_id":"sensor.f1_temp3","state_type":"num","halt_if":"0","halt_if_type":"num","halt_if_compare":"gt","outputs":2,"x":170,"y":340,"wires":[["318eb3d8c75cc3a9"],[]],"outputLabels":["temp3",""]},{"id":"d9d63bdc65fe7219","type":"poll-state","z":"8c081f22d984df18","name":"","server":"a1278bc0.5f1838","version":2,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"updateinterval":"5","updateIntervalType":"num","updateIntervalUnits":"seconds","outputinitially":false,"outputonchanged":false,"entity_id":"sensor.f1_humid3","state_type":"num","halt_if":"0","halt_if_type":"num","halt_if_compare":"gt","outputs":2,"x":180,"y":400,"wires":[[],[]],"outputLabels":["humid3",""]},{"id":"318eb3d8c75cc3a9","type":"function","z":"8c081f22d984df18","name":"Average_Temp","func":"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":600,"y":260,"wires":[[]]},{"id":"a1278bc0.5f1838","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30}]

Home assistant states are stored and updated in nodered when they update in HA. Using that global store we don’t have to poll the entities.

[{"id":"f4101a27494f96f6","type":"inject","z":"f80b6c338afd5483","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":320,"y":380,"wires":[["1f84fcfc79acc87e"]]},{"id":"f116848b2a227090","type":"debug","z":"f80b6c338afd5483","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":790,"y":380,"wires":[]},{"id":"1f84fcfc79acc87e","type":"function","z":"f80b6c338afd5483","name":"","func":"var temp1 = global.get('homeassistant.homeAssistant.states[\"sensor.flue_temp_2\"].state');\nvar temp2 = global.get('homeassistant.homeAssistant.states[\"sensor.fire_box_2\"].state');\nvar temp3 = global.get('homeassistant.homeAssistant.states[\"sensor.hot_water\"].state');\n\nvar hum1 = global.get('homeassistant.homeAssistant.states[\"sensor.flue_temp_2\"].state');\nvar hum2 = global.get('homeassistant.homeAssistant.states[\"sensor.fire_box_2\"].state');\nvar hum3 = global.get('homeassistant.homeAssistant.states[\"sensor.hot_water\"].state');\n\n\nlet t1 = parseFloat(temp1);\nlet t2 = parseFloat(temp2);\nlet t3 = parseFloat(temp3);\n\nlet h1 = parseFloat(hum1);\nlet h2 = parseFloat(hum2);\nlet h3 = parseFloat(hum3);\n\n\nmsg.temperature = (t1 + t2 + t3) / 3;\nmsg.humidity = (h1 + h2 + h3) / 3;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":540,"y":380,"wires":[["f116848b2a227090"]]}]
2 Likes

Simple and fast; works perfectly. Thank you so much.