Greetings,
I’m trying to create a binary_sensor that listen for entities change that are included in a Group.
- trigger:
- platform: template
value_template: >-
{% set excluded = ['switch.caldaia_on_off'] %}
{% set entita = expand('group.cucina')
| rejectattr("state", "in", ["undefined", "unavailable"])
| rejectattr("entity_id", "in", excluded)
%}
{% set switch_on = entita | selectattr('state', 'eq', 'on') %}
{{ (switch_on | list | count > 0) }}
binary_sensor:
- name: Cucina attiva
unique_id: cucina_attiva
state: >-
{% set excluded = ['switch.caldaia_on_off'] %}
{% set entita = expand('group.cucina')
| rejectattr("state", "in", ["undefined", "unavailable"])
| rejectattr("entity_id", "in", excluded)
%}
{% set switch_on = entita | selectattr('state', 'eq', 'on') %}
{{ (switch_on | list | count > 0) }}
attributes:
fake_trigger_upd: >-
{{ not state_attr(this.entity_id, 'fake_trigger_upd')|bool(false) }}
availability: "{{ true }}"
It seems to works when at least one entity become Active but refuses to goes Off when all same entitites goes Off.
I tested the “{%…%}” in the Developer Model section and it works well following the behaviour I’m expecting.
I tried just leaving only one Switch inside that Group but nothing has changed.
I also added a “fake” attribute that changes state at every trigger but it doesn’t change its state when all group entities goes Off.
If I replace “platform: template” and “value_template: …” with the relative list of “platform: state” and “entity_id:…” all works fine listening correctly for both ON and OFF events of same entitites.
So I suppose there is something on the trigger that doesn’t work well using Trigger Template and/or “expand()” function.
I read THIS topic but it seems not solved as I demonstrated now.
The only workaround I found is avoid to expand the group in the Template Trigger and just list all group entities in a “platform: state” trigger, but this completly crazy…
UPDATE:
I noticed a strange behaviour while testing “{%…%}” in Development Model page.
If I try to execute this code:
{% set excluded = ['switch.caldaia_on_off'] %}
{% set entita = expand('group.cucina')
| rejectattr("state", "in", ["undefined", "unavailable"])
| rejectattr("entity_id", "in", excluded)
%}
{% set switch_on = entita | selectattr('state', 'eq', 'on') %}
{{ switch_on | map(attribute="entity_id") | list }}
{{ switch_on | list | count > 0 }}
{{ switch_on | map(attribute="entity_id") | list }}
the output is:
['light.dimmer_6_cucina_virtual', 'light.dimmer_6_cucina_reale']
False
[]
It seems the “switch_on” variable become Empty after a while…