Automation - best way to trigger automation by entities defined in a group

Hi all:

I’d like to make an automation for controlling thermostats in each room based on whether window is open/closed in that room. I already created a group of binary_sensors to track window open/close in each room. However I’m struggling to find a smart way to create the trigger. So far I have tried:
(1) State change of the group binary_sensor: it works to trigger the automation however I don’t get trigger.entity_id for which individual window/door that changed state
(2) Trigger with template like below. This only get triggered when number of open windows changes from 0 to any number (when the state of template becomes true). However I’d like to trigger whenever the number of open window changes

trigger:
  - platform: template
    value_template: >-
      {{ expand('binary_sensor.group_windows_doors')
           | selectattr('state', 'eq', 'on')
           | list
           | count  }}
    id: window_door

(3) ChatGPT suggested to use state trigger but use template for entity_id like, but I can’t seem to get it to work.

entity_id: >
  {{ expand('binary_sensor.group_windows_doors') | map(attribute='entity_id') | join(', ') }}

So I’m running out of ideas - any suggestion?

This is a known deficiency of groups (and labels): using them as triggers in automations is extremely difficult.

Generally the way to solve this is to create a trigger-based template sensor (in YAML, it isn’t available through the UI) whose state is the number of matching entities, and then you can have an attribute that is populated with the entity that was most recently matched (or unmatched). Or depending on your goal, you can have the attribute contain a list of all the matching entities.

Then in your automation you would trigger on any state change of the template sensor (when the count changes), and you can pick up the entity id of the offending entity from the attribute of the template sensor.

Thanks! My current workaround is just to put all contact sensors under entity_id element