another questions
In my rooms I have various devices, and a lot of them measure the temperature.
I would like to combine all of them to a group calculating the average of all it’s elements and would like to present this as Room temperature.
I’m searching for a solution to my temperature averaging issue. I have 4 room sensors which I can get the average temperatures from to control my central heating system to turn on or off. However when one of the sensors either goes offline or errors to a “0” reading, then the average is also affected as the sum total is still divided by the original total number of sensors, rather than the total number of working or plausible values (my sensors can go offline but still displaying a reading of “0” which is still a value albeit false in my case.
I tried to adapt the above code but is still reads a low average rather than what would be the higher value if it was only divided by the 3 plausible readings.
From above the correct Upstairs average (that I’m looking for) would be the sum ((19+18.7+20.9)/3) = 19.53 rather than the currently displayed 14.65
Anyone have a suggested way to only count sensors that are online and working with plausible values?
Is what I use. It will ignore any sensors that are not outputting a numeric value, and it will include them again once they start working again.
To get around the 0 issue, the easiest solution would be to create a template sensor for any sensor that exhibits this behaviour, and output the value of the source sensor if the value is over 0, otherwise output the existing value.
I have been using this code which uses the “Average” platform:
# Temperature Average
- platform: average
name: 'Upstairs Avg Temperature'
entities:
- sensor.luminance_sensor_3_temperature
- sensor.luminance_sensor_4_temperature
- sensor.luminance_sensor_5_temperature
- sensor.luminance_sensor_6_temperature
But as my erroneous sensor is still outputting a value of “0” it is still a value so counted. Let me change the platform to your suggested “min_max” to see if that can get around the 0 value.
I got something working with your example and substituted the room next door to the troublesome sensor so if it goes to “0” then the reading used is close to that bedroom temp and keeps the averages in the ball park.
Only thing that I noticed is the average is not exactly the same if I manually add them up and divide by 4. I manually calculate 19.03 but the sensor average is calculated as 19.23. I’m not sure if this is anything to do with the “float(0)” in the sensor template?
template:
sensor:
- name: "Sensor 5 Temperature"
unit_of_measurement: "C"
unique_id: Sensor 5 Temperature
state: >
{% set src = states('sensor.luminance_sensor_5_temperature')|float(0) %}
{{ src if src > 0 else states('sensor.luminance_sensor_3_temperature') }}
The template already rejects sensors with states that are “unavailable” and “unknown”. If you know that your sensor’s state is always ‘0.0’ when it is malfunctioning you can just add that to the list in the rejectattr filter. Also, we can now use Jinja’s average filter instead of setting up the math:
I finally got something working that now only averages out the non 0.0 temperatures. Now my heating timer boolean can get switched on when the average house temperatures drop below a set value and this is now not skewed when one sensor goes down.