Sum of Temp sensors and average measurement

Hi, i’ve try to do a simple array of numbers (temperature from 3 sensors) and an average of this 3, but i don’t know why the average is wrong.

This is my code

[{"id":"87042901f1358814","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"dca3cca7.f1566","type":"inject","z":"87042901f1358814","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":80,"y":520,"wires":[["960b7ca3e71bc732","6d65c3dc8b03afb7","785eeec847419e54"]]},{"id":"960b7ca3e71bc732","type":"api-current-state","z":"87042901f1358814","name":"","server":"5342fb6.0acd404","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"sensor.temp_bagno_temperature","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":0,"forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":340,"y":440,"wires":[["3255d1fa.3cf55e"]]},{"id":"6d65c3dc8b03afb7","type":"api-current-state","z":"87042901f1358814","name":"","server":"5342fb6.0acd404","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"sensor.wellbeing_temp","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":0,"forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":370,"y":520,"wires":[["3255d1fa.3cf55e"]]},{"id":"3255d1fa.3cf55e","type":"join","z":"87042901f1358814","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"payload","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"3","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":630,"y":520,"wires":[["77c2f63818d5ef4c"]]},{"id":"6c921b40.2a0964","type":"debug","z":"87042901f1358814","name":"","active":true,"tosidebar":true,"console":true,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1000,"y":520,"wires":[]},{"id":"785eeec847419e54","type":"api-current-state","z":"87042901f1358814","name":"","server":"5342fb6.0acd404","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"climate.riscaldamento","state_type":"num","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"$entity().attributes.current_temperature","valueType":"jsonata"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":0,"forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":370,"y":600,"wires":[["3255d1fa.3cf55e"]]},{"id":"77c2f63818d5ef4c","type":"function","z":"87042901f1358814","name":"","func":"function sumArray(array) {\nvar sum = 0;\n  for (var index = 0; index < array.length; index++){ // 3 values in a for loop\n        if(!isNaN(array[index])){ // check if value is a number\n            sum += array[index]   // Add number on each iteration\n        }\n      }\n  return sum;\n}\n\nmsg.payload = sumArray(msg.payload)  // send value to function\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":800,"y":520,"wires":[["6c921b40.2a0964"]]},{"id":"5342fb6.0acd404","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":false,"cacheJson":true,"heartbeat":false,"heartbeatInterval":"30"}]

So are you just trying to get an average? If so perhaps you can try this HACS integration GitHub - Limych/ha-average: Average Sensor for Home Assistant

2 Likes

All you have shown is the raw data.

Show what the sensor code that you are trying to use to extract the data and then average that data that doesn’t work.

1 Like

Really appreciate. Thank you!

i ding using this node-red-contrib-calc (node) - Node-RED node extremely useful

Have you tried using the standard home assistant min_max platform

Min/Max - Home Assistant (home-assistant.io)

Here is one of my examples:

  - platform: min_max
    name: "Avg temp"
    type: "mean"
    round_digits: 1
    entity_ids:
      - sensor.temp9_temperature
      - sensor.temp10_temperature

It is quite simple and powerfull. Not only doing average (mean) but also min, max, median and last:

Try it :slight_smile:

1 Like