How to automate including/excluding temperature sensors from a group

I have a climate control that takes as its input a helper group that aggregates the results (takes the lowest value) from several temperature sensors around the house (a fairly common practice).

However, if I don’t have guests staying, I want to exclude that room from the aggregate, since I don’t care if the guest room is cold if we don’t have guests.
For now, I just want a simple binary toggle to enable/disable “guest mode”. Longer term, maybe I’ll do something snazzier wth occupancy sensors.

I am using a sensor group, but it doesn’t seem possible to use automation to edit the members of such a group. The set action on groups is only applicable to “old style” groups as per the docs.

Any ideas here? I’ve been using my best Google-fu all evening and can’f figure it out.

Were You thinking about a template sensor and a little bit of code?

For example:

          {{ [
            states('sensor.temp_1') | float(999),
            states('sensor.temp_2') | float(999),
            (
              states('sensor.temp_3') | float(999)
              if is_state('input_boolean.enable', 'on')
              else 999
            )
          ] | min }}
2 Likes

There are a couple options. If you’re going to be switching multiple sensors in and out of the “group”, the better option is probably to use Labels and the Spook custom integration with a Template Sensor . Otherwise, you would use a Template Sensor based on associated Input booleans or Binary sensors as mentioned above.

sudo_nitz has posted one option, but there are many ways to do it. I would probably build the list of sensors first since that will make it relatively easy to move sensors into conditional lists. You can even add a given sensor to multiple conditional lists:

{% set base = ['sensor.temp_1', 'sensor.temp_2'] %}
{% set guest = ['sensor.temp_guest_room'] if is_state('input_boolean.guest_mode', 'on') else [] %}
{% set kitchen = ['sensor.temp_kitchen'] if is_state('binary_sensor.kitchen_occupancy', 'on') else [] %}
{{ (base + guest + kitchen) | select('has_value') 
| map('states') | map('float') | min }}

If you decide to go with Labels, the Template sensor would use basically the same expression in its State template, but it would get the list of sensors from the Label:

{{ label_entities('your_label') | select('has_value') 
| map('states') | map('float') | min }}

FWIW, using Labels will put you in a good position to use these “groups” with the purpose-specific triggers and conditions as they are expanded and rolled out.

2 Likes

Using labels is cunning and elegant. I love it!
I’ve managed to get it working well.

I just have a couple of stretch goals I wonder if you can help me out with:

  1. I’d like to add an availability template to the sensor so that it changes to unavailable if any of the labelled entities themselves are unavailable. I’m a novice to templating and struggling a bit with the sytax.
  2. Right now, the templated sensor is only triggered by the underlying entities changing. This means that when my automation removes or adds the appropriate label to an entity, the template sensor doesn’t reflect the change right away. It usually takes a few minutes until one of the underlying entities gets updated in order to trigger re-evaluation of the template. Do you know a way to get the template to trigger re-evaluation as soon as my automation runs?

Many thanks :slight_smile:

{{ label_entities('your_label') | reject('has_value') | list | count > 0 }}

Try adding a homeassistant.reload_config_entry to your automation after the label management action. Target the action on the template sensor entity.

1 Like

You are a star! Thanks so much.
Both worked great. I just had to tweak the availability template slightly. You version returned false when there were no unavailable sensors, I needed the opposite:

{# Count how may of the included entities have no value. If more than zero, mark sensor unavailable  #}
{{ label_entities('downstairs_temp_sensor') | reject('has_value') | list | count == 0 }}