I’m trying to setup a sensor to show the count of my open windows. I’ve setup a group with all my window binary sensors and am trying to count the ones that are “on” with the following sensor. I put this together after going through several iterations of examples on the forum but none are working for me. This current example will work with a count of up to “1” but even with more than one open it still just shows “1”.
Any help would be greatly appreciated as these templates are rather confusing to me.
window_count:
value_template: >-
{{ states | selectattr('entity_id', 'in', state_attr('group.windows', 'entity_id')) | selectattr('state', 'eq', 'on') | list | count | int }}
Add the entity_id field with all the entities from the group.
window_count:
entity_id:
- sensor.1
- sensor.2
- etc
value_template: >-
{{ states | selectattr('entity_id', 'in', state_attr('group.windows', 'entity_id')) | selectattr('state', 'eq', 'on') | list | count | int }}
This will cause your template to update when any sensor in the group changes. Otherwise it will only update when the GROUP state changes (which will be rare).
There’s a useful enhancement in 0.110 that changes the way the expand function is handled in Template Sensors. Instead of monitoring the state of the group (the way it’s been done in all prior releases), it now monitors the states of the group’s members.
It means that in 0.110, this Template Sensor will update whenever one of the members of group.windows changes state.
Figured it out. I actually had a window sensor that had the magnet fall off while I was changing this so one of them was on. It’s working as expected. Thanks for all your help!