Here’s why your Trigger-based Template Binary Sensor fails to work the way you think it should.
A Template Trigger will trigger when the result produced by its template changes from false
to true
. It does not trigger when it changes from true
to false
.
Given that information, when will this template’s result change from false
to true
?
- trigger:
- platform: template
value_template: >-
{{ expand('group.cucina_attiva')
| rejectattr("state", "in", ["undefined", "unavailable"])
| rejectattr("entity_id", "in", ['switch.caldaia_on_off'])
| selectattr('state', 'eq', 'on')
| list | count > 0 }}
It will happen when none of the lights are on and then one of them is turned on. When that happens the number of illuminated lights is greater than 0 and the template’s result will change from false
to true
. That’s the only time this template will trigger the Template Trigger.
When all lights are turned off, the quantity of illuminated lights is no longer greater than 0 so the template’s result changes from true
to false
. That change does not trigger the Template Trigger. That’s why your Trigger-based Template Binary Sensor’s state changes to on
but never changes to off
.
The fact is that there is no need to create a Trigger-based Template Binary Sensor for your stated application. It adds additional complexity, provides no advantages over a Template Binary Sensor (in this case), and even introduces a major functional disadvantage.
I highly recommend you use the Template Binary Sensor I posted earlier.
The Time Pattern Trigger is effectively bypassing the Template Trigger and forcing the state
template to be evaluated every 30 seconds.
There’s no need for the Time Pattern Trigger or the Template Trigger. A Template Binary Sensor, having no explicit triggers, will report the correct state using its template and nothing else. It works efficiently because it only updates when necessary (when one of its monitored entities changes state) and not unnecessarily according to a time pattern.