Binary Sensor Template to count Group State

I have a six occupancy binary sensors in a group and I want to be able to tell if all of them are on, for use in automations.

I’m after some help, please, with a template sensor to achieve this but can’t find anything suitable in the forum. Obviously, with a group, if one is on the group state is on but I need to be able to automate only when all are on.

{{ expand(state_attr('binary_sensor.your_occupancy_group', 'entity_id')) | selectattr('state', 'eq', 'on') | list | count  == 6 }}
1 Like

Thanks for the help Tom.

1 Like

To tweek @tom_l 's solution just a bit and make the output self maintaining.

{{ expand(state_attr('binary_sensor.your_occupancy_group', 'entity_id')) | selectattr('state', 'eq', 'off') | list | count  == 0 }}

With the above template, one can add or remove members from the group & the it will still produce the results intended.
There may likely exist an even more elegant statement, though I’ve not discovered it yet.
EDIT: I inverted both tests. & took several attempts to get the formatted code block to render correctly.

1 Like

You can use the “all entities” option to have binary sensor groups behave in the manner you describe (only on when all members of the group are on).