Guide to create sensor that will be a average of other sensors

Here is a template that check calculate the average of the sensors.
It sums them up separately for each sensor (in case one of them is unavailable).
Just add or remove the {%%} blocks to suit the number of sensor you want to average.

- platform: template
  sensors:
    average_home_humidity:
      friendly_name: "Average Home Humidity"
      unit_of_measurement: "%"
      attribute_templates:
        area: "Hose"
      value_template: >
        {% set total = 0 %}
        {% set valid_sensors = 0 %}

        {% if is_number(states('sensor.bedroom_humidity')) %}
          {% set total = total + states('sensor.bedroom_humidity') | float %}
          {% set valid_sensors = valid_sensors + 1 %}
        {% endif %}

        {% if is_number(states('sensor.kitchen_humidity')) %}
          {% set total = total + states('sensor.emma_humidity') | float %}
          {% set valid_sensors = valid_sensors + 1 %}
        {% endif %}

        {% if is_number(states('sensor.living_room_humidity')) %}
          {% set total = total + states('sensor.living_room_humidity') | float %}
          {% set valid_sensors = valid_sensors + 1 %}
        {% endif %}

        {% if is_number(states('sensor.work_room_humidity')) %}
          {% set total = total + states('sensor.work_room_humidity') | float %}
          {% set valid_sensors = valid_sensors + 1 %}
        {% endif %}

        {% if valid_sensors > 0 %}
          {{ (total / valid_sensors) | round(1) }}
        {% else %}
          {{ 0 }}
        {% endif %}

I use the Combine the state of several sensors helper for average sensors

1 Like

Does this take into account the case where one of them becomes unavailable?

Yes it does. See: Min/Max - Home Assistant

1 Like

I think I’m getting a higher refresh rate when using template version.
Min/max are performed every 5 minutes and the template is updated every time one of the sensors is updated.

Where do you see this?

{% set sensors = 
  [
    'sensor.bedroom_humidity',
    'sensor.kitchen_humidity',
    'sensor.living_room_humidity',
    'sensor.work_room_humidity'
  ]
%}
{{
  sensors
    | map('states')
    | select('is_number')
    | map('float')
    | average(default=0)
}}
2 Likes

My mistake it is only in the basic graph, in the “show more” they are identical


0% humidity?
You should probably change it to not update to 0%

1 Like

i will probably use the @Holdestmade Built-in option.