Groups, Templates and Sensors

I am just looking for the best way to simplify my automations based on a number of different sensors.

I have couple of different sets of sensors and essentially I want to take action based on the lowest value of any sensor in that group. Rather than getting into a mess putting lots of ORs in my conditions what is the best way to have a “lowest bedroom temperature” sensor which is actually the lowest value of any bedroom sensor which I can then reference in my automations rather than the the actual sensors themselves?

Is there anything I can do to nest this so for example create a group of bedroom temperature sensors and then create psuedo sensors on top (eg. create a min and an average sensor) so that if I update the list of bedroom sensors both the min and average ones automatically pick that up?

There are a few methods to accomplish what you are trying to do.
I use state-based template sensor(s). When you base a template sensor on a group, it will update when the state of any member of the group updates.

This is the sensor configuration I use with a group that includes all the thermometers I have inside.

### configuration.yaml

template:
  sensor:
    - name: "average_household_temp"
      state_class: measurement
      device_class: temperature
      unit_of_measurement: "°F"
      icon: mdi:home-thermometer-outline
      state: >
        {% set temps = expand('group.indoor_thermometers')| rejectattr('state', 'in', ['unavailable', 'unknown', 'NaN'])| map(attribute='state') | map('float', 0) | list %}
        {{ (temps | sum / temps | count) | round(0, 0) }}
      availability: >
        {% set sensors = expand('group.indoor_thermometers') %}
        {{ sensors | selectattr('state','in', ['unknown','unavailable']) | list | length == 0 }}
      attributes:
        min: >
          {{ expand('group.indoor_thermometers') | rejectattr('state', 'in', ['unavailable', 'unknown', 'NaN'])| map(attribute='state') | map('float', 0) | list | min}}
        max: >
          {{ expand('group.indoor_thermometers') | rejectattr('state', 'in', ['unavailable', 'unknown', 'NaN'])| map(attribute='state') | map('float', 0) | list | max }}
    
1 Like

Good example for jameson_uk and also a good example for justifying this Feature Request:

If it were implemented, you would be able to reduce the Template Sensor to this:

template:
  sensor:
    - name: "average_household_temp"
      state_class: measurement
      device_class: temperature
      unit_of_measurement: "°F"
      icon: mdi:home-thermometer-outline
      variables:
        items: "{{ expand('group.indoor_thermometers')| rejectattr('state', 'in', ['unavailable', 'unknown', 'NaN'])| map(attribute='state') | map('float', 0) | list }}"
      state: "{{ (items | sum / items | count) | round(0, 0) }}"
      availability: "{{ items | length == 0 }}"
      attributes:
        min: "{{ items | min }}"
        max: "{{ items | max }}"

Yeah, I’ve up-voted every “add variables support to template sensors” FR post I’ve ever come across.

1 Like