Add a sensor for each area to get area states

It could be useful to have a sensor for area entities states. It can allow you to determine easily if there are motions detected on a specific area, or how many light are on, or the average temperature of all temperatures sensors.

I tried to trigger my automation by another way and it could help me.
For example with an automation for turning on lights on motions :

Start my automation when motion (binary_sensor with device_class = motion) are detected on an area. Then, turn on lights relate to this area.

Actually, I’ve created these sensor by it’s quite cpu-consuming :

- binary_sensor:

    - unique_id: jardin_status
      name: "Présence jardin"
      device_class: occupancy
      # icon: fas:key
      state: "{{ this.attributes.motion | default('Value when missing') }}"
      attributes:
        type:             "area_status"
        light:            "{{ expand(area_entities('jardin') | select('match', 'light.')) | selectattr('state', 'eq', 'on') | list | length }}"
        switch:           "{{ expand(area_entities('jardin') | select('match', 'switch.')) | selectattr('state', 'eq', 'on') | list | length }}"
        climate:          "{{ expand(area_entities('jardin') | select('match', 'climate.')) | selectattr('attributes.preset_mode', 'eq', 'comfort') | selectattr('state', 'eq', 'heat') | list | length }}"
        tv:               "{{ expand(area_entities('jardin') | select('match', 'media_player.')) | selectattr('attributes.templates.device_class', 'eq', 'tv') | selectattr('state', 'eq', 'playing') | list | length }}"
        speaker:          "{{ expand(area_entities('jardin') | select('match', 'media_player.')) | selectattr('attributes.templates.device_class', 'eq', 'speaker') | selectattr('state', 'eq', 'playing') | list | length }}"
        motion:           "{{ expand(area_entities('jardin') | select('match', 'binary_sensor.')) | selectattr('attributes.device_class', 'eq', 'motion') | selectattr('state', 'eq', 'on') | list | length }}"
        door:             "{{ expand(area_entities('jardin') | select('match', 'binary_sensor.')) | selectattr('attributes.device_class', 'eq', 'door') | selectattr('state', 'eq', 'on') | list | length }}"
        safety:           "{{ expand(area_entities('jardin') | select('match', 'binary_sensor.')) | selectattr('attributes.device_class', 'eq', 'safety') | selectattr('state', 'eq', 'on') | list | length }}"
        window:           "{{ expand(area_entities('jardin') | select('match', 'binary_sensor.')) | selectattr('attributes.device_class', 'eq', 'window') | selectattr('state', 'eq', 'on') | list | length }}"
        cover:            "{{ expand(area_entities('jardin') | select('match', 'cover.')) | map(attribute='state') | list | length }}"
        # plant:       "{{ expand(area_entities('jardin') | select('match', 'input_select.lumieres_.')) | map(attribute='state') | first | default('unknown') }}"
        light_mode:       "{{ expand(area_entities('jardin') | select('match', 'input_select.lumieres_.')) | map(attribute='state') | first | default('unknown') }}"
        last_scene:       "{{ expand(area_entities('jardin') | select('match', 'scene.')) | rejectattr('state', 'eq', 'unknown') | sort(reverse=true, attribute='state') | map(attribute='attributes.name') | first | default('unknown') }}"
        scene_jour:       "{{ expand(area_entities('jardin') | select('match', 'input_text.scene_jour_')) | map(attribute='state') | first | default('unknown') }}"
        scene_sunset:     "{{ expand(area_entities('jardin') | select('match', 'input_text.scene_sunset_')) | map(attribute='state') | first | default('unknown') }}"
        scene_soir:       "{{ expand(area_entities('jardin') | select('match', 'input_text.scene_soir_')) | map(attribute='state') | first | default('unknown') }}"
        scene_nuit:       "{{ expand(area_entities('jardin') | select('match', 'input_text.scene_nuit_')) | map(attribute='state') | first | default('unknown') }}"
        temperature: >-
          {% set array = expand(area_entities('jardin') | select('match', 'sensor.')) | rejectattr('attributes.device_class', 'undefined') | selectattr('attributes.device_class', 'eq', 'temperature') | map(attribute="state") | reject("eq", "unavailable") | map('float') | list | default([0]) %}
          {{ 'unavailable' if array | length == 0 else '{:.1f}'.format(array | sum / array | length) }}
        humidity: >-
          {% set array = expand(area_entities('jardin') | select('match', 'sensor.')) | rejectattr('attributes.device_class', 'undefined') | selectattr('attributes.device_class', 'eq', 'humidity') | map(attribute="state") | reject("eq", "unavailable") | map('float') | list | default([0]) %}
          {{ 'unavailable' if array | length == 0 else '{:.1f}'.format(array | sum / array | length) }}
        illuminance: >-
          {% set array = expand(area_entities('jardin') | select('match', 'sensor.')) | rejectattr('attributes.device_class', 'undefined') | selectattr('attributes.device_class', 'eq', 'illuminance') | map(attribute="state") | reject("eq", "unavailable") | map('float') | list | default([0]) %}
          {{ 'unavailable' if array | length == 0 else '{:.1f}'.format(array | sum / array | length) }}

The main goal is to trigger entities by their device_class and not by their names.

With an area’s sensor. It could be more simple to detect if there is motions on an area.