Templating with entity_id wildcard

thank you.

Is there a way to adapt this kind of code :

{{ states | selectattr('entity_id','in', ['binary_sensor.kitchen_occupied','binary_sensor.living_room_occupied','binary_sensor.conservatory_occupied', 'binary_sensor.upstairs_hall_occupied', 'binary_sensor.bathroom_occupied','binary_sensor.bedroom_occupied'] ) |selectattr('state','eq','on') | list | count }}

without listing all entities needed.

Try this:

{{ states | selectattr('entity_id', 'search', '^(binary_sensor|sensor).*occupied') | selectattr('state', 'eq','on') | list | count }}

You can probably remove the sensor portion of the search - I added it in there for kicks.