Dynamically create average temperature entity per area

I have multiple temperature sensors in each room/area and I would like to display (and use in other automations/mqtt) average temperature per area. Currently I’m using Helpers in UI (combine multiple sensors option) to get an average and to have history of it however this is not scalable (adding/removing sensor and/or is a lot of clicks for such simple thing). Is there a possibility to dynamically get average temperature of area as separate entity without using Helper? I found that I can use template with state something like this as a state:

template:
  - sensor:
      - name: "Bedroom Average Temperature"
        unit_of_measurement: "°C"
        state:"{{ area_entities('bedroom')  | select('is_state_attr', 'device_class', 'temperature') | map('states') | select('is_number') | map('float') | average }}"

however this still require one template sensor for each area. Can the configuration be somehow deduplicated?

I would like to have dynamically created entities, something like for-loop of areas and template for each in the loop.

It’s not possible via configuration only, unless you look for a custom integration or make one yourself.

I would assume your areas don’t change dynamically, so just set them up as a once-off.

You could do someting like this:

template:
  - sensor:
      - default_entity_id: sensor.bedroom_average_temperature
        variables:
          area: bedroom
        <<: &config
          name: "{{ area | default | replace('_', ' ') | title }} Average Temperature"
          unit_of_measurement: "°C"
          state: "{{ area_entities(area) | select('is_state_attr', 'device_class', 'temperature') | map('states') | select('is_number') | map('float') | average }}"
      - default_entity_id: sensor.kitchen_average_temperature
        variables:
          area: kitchen
        <<: *config
      - default_entity_id: sensor.living_room_average_temperature
        variables:
          area: living_room
        <<: *config
2 Likes

This is how I would do it. Although I’d change variables to be a single line

        variables: {area: bedroom}

You can even use the template editor to write the config for you.

template:
  - sensor:
      - default_entity_id: sensor.bedroom_average_temperature
        variables:
          area: bedroom
        <<: &config
          name: "{{ area | default | replace('_', ' ') | title }} Average Temperature"
          unit_of_measurement: "°C"
          state: "{{ area_entities(area) | select('is_state_attr', 'device_class', 'temperature') | map('states') | select('is_number') | map('float') | average }}"
{%- set remaining_areas = ['kitchen', 'living_room'] %}
{%- for area in remaining_areas %}
      - default_entity_id: sensor.{{ area }}_average_temperature
        variables: {area: {{ area }}}
        <<: *config
{%- endfor %}

Then just copy/paste the results into your configuration with this code commented out if you ever need to make future changes.
1 Like

A further option, there are some HACS addons such as GitHub - Limych/ha-average: Average Sensor for Home Assistant (General ability to average sensors) or GitHub - c-st/auto_areas: 🤖 A custom component for Home Assistant which automates your areas. (Areas, aggregate readings) which tackle this.

Integrations. Add-ons are something else.