Entity name used in trigger and condition?

I’m trying to create an automation that doesn’t require me to hard code every sensor entity name into the trigger.

I have ecobee thermostats, and each thermostat also has an additional occupancy sensor attached to it.

So for my thermostat named upstairs there are two binary sensors

  1. binary_sensor.upstairs_occupancy
  2. binary_sensor.upstairs_sensor_occupancy

And I have another thermostat downstairs so I would have two more binary_sensors similarly named. I also have a few other sensors that are named binary_sensor.ABC_occupancy, binary_sensor.DEF_occupancy and binary_sensor.GHI_occupancy

What I want to happen is if more than ONE (1) sensor whose name matches binary_sensor.XXX_occupancy is on/true then run the automation.

This smells like a template trigger to fire anytime a binary sensor whose name ends with _occupancy, and a condition template counting the number of those same entity name matches.

Can you suggest what this automation would look like that doesn’t require hard coding of the sensor names?

Pinging @123 as it looks like you’ve seen this request before.

FAQ #16

The template would use search as a test to select all binary_sensors containing “_occupancy” in their name. However I would create a Template Sensor that reports the quantity that are on and then use a State Trigger to detect when the sensor’s value changes.

Let me know if you need help composing creating the Template Sensor.