Average temperature based on multiple temperature sensors

Hello,

Using following template sensor:

  {% set t1 = states('sensor.wifi_temperature_humidity_sensor_temperature') %}
  {% set t2 = states('sensor.hue_keuken_sensor_temperature') %}
  {% set t3 = states('sensor.hue_tv_sensor_temperature') %}
  {% set t4 = states('sensor.daikin_airco_leefruimte_climatecontrol_room_temperature') %}
  {% if t1 != 'unavailable' and t2 != 'unavailable' and t3 != 'unavailable' and t4 != 'unavailable' %}
    {{ ((t1 | float + t2 | float ) / 2) | round(2) }}
  {% elif t1 == 'unavailable' and t2 != 'unavailable' %}
    {{ (( t2 | float )) | round(2) }}
  {% elif t1 != 'unavailable' and t2 == 'unavailable' %}
    {{ (( t1 | float )) | round(2) }}
  {% else %}
    {{ states('sensor.average_living_temp') }}
  {% endif %}

How can I update the template to include T3 and T4?

You can make it much more compact and efficient if you use the built-in filters instead of a long series of if statements.

  • The test has_value can be used in a select filter so sensors that are ā€œunavailableā€ or ā€œunknownā€ will be ignored.
  • The filter map can be used to iterate other filter functions like states and float across items in a list.
{% set sensor_list = ['sensor.wifi_temperature_humidity_sensor_temperature',
'sensor.hue_keuken_sensor_temperature', 'sensor.hue_tv_sensor_temperature',
'sensor.daikin_airco_leefruimte_climatecontrol_room_temperature'] %}
{{ sensor_list | select('has_value') | map('states') | map('float') 
| average | default(states('sensor.average_living_temp'), 1) | round(2) }}

Hi Ward0. I donā€™t know if this will be of any use to you? When I created a ā€œHome Average Temperature Sensorā€ I placed all potential sensors into a group. Whenever the sensor is updated (e.g. using the time pattern automation trigger), anomalous readings from malfunctioning devices are filtered out, the contributing devices are counted, and an average applied. E.g.

{% set temperatureSensors = [
  states('sensor.temp_1') | float(default=0),
  states('sensor.temp_2') | float(default=0),
  etc. ] %}

or, set temperatureSensors = states.sensor | selectattr(ā€˜attributes.device_classā€™, ā€˜eqā€™, ā€˜temperatureā€™).

{% set valid_temperatureSensors = temperatureSensors | select('gt', 0) | list %}
{% set averageTemperature = (valid_temperatureSensors | sum / valid_temperatureSensors | length) %}

{{ averageTemperature | round(1) }}

In my version, ā€œunavailableā€ and ā€œunknownā€ will default to 0, then only states greater than (ā€œgtā€) 0 are selected. You could also use filters and tests like ā€œis_numberā€ or ā€œhas_valueā€ or ā€œrejectattr(ā€˜stateā€™, ā€˜eqā€™, [ā€˜unavailableā€™, ā€˜unknownā€™])ā€.

SUM adds all the states together. LENGTH is the number of states added together, which is used to produce the average.

Best wishes, Anton x

1 Like

Why not just use the min/max integration?

It takes care of all the unavailable logic for you and only averages the available sensors. You can even set it up from the UI:

I use the blueprint Advanced Heating Control V5. I could not use the min/max helper for this blueprint.

Thank you! Works perfectly.

In your first post, you said you were using a Template Sensor.

If youā€™re using a Template Sensor with the blueprint, then itā€™s possible to replace it with a Min/Max Sensor.

1 Like