No. You need to define the day
list in each one of the three Template Sensors.
Here’s a revised version of your Template Sensors:
- platform: template
sensors:
raam_laatst_gedetecteerd:
value_template: >
{% set day = ["Zo", "Ma", "Di", "Wo", "Do", "Vr", "Za"] %}
{% set x = states.binary_sensor
| selectattr('attributes.device_class', 'eq', 'window')
| selectattr('state', 'eq', 'on')
| sort(reverse=true, attribute='last_changed') %}
{{ '' if x|count == 0 else
x[0].name | regex_replace(' raam', '', true) ~
' ({} {})'.format(day[x[0].last_changed.isoweekday()],
x[0].last_changed.timestamp() | timestamp_custom('%H:%M:%S')) }}
# {{ x|count }}
deur_laatst_gedetecteerd:
value_template: >
{% set day = ["Zo", "Ma", "Di", "Wo", "Do", "Vr", "Za"] %}
{% set x = states.binary_sensor
| selectattr('attributes.device_class', 'eq', 'door')
| selectattr('state', 'eq', 'on')
| sort(reverse=true, attribute='last_changed') %}
{{ '' if x|count == 0 else
x[0].name | regex_replace(' deur', '', true) ~
' ({} {})'.format(day[x[0].last_changed.isoweekday()],
x[0].last_changed.timestamp() | timestamp_custom('%H:%M:%S')) }}
# {{ x|count }}
beweging_laatst_gedetecteerd:
value_template: >
{% set day = ["Zo", "Ma", "Di", "Wo", "Do", "Vr", "Za"] %}
{% set x = states.binary_sensor
| selectattr('attributes.device_class', 'eq', 'motion')
| selectattr('state', 'eq', 'on')
| sort(reverse=true, attribute='last_changed') %}
{{ '' if x|count == 0 else
x[0].name | regex_replace('beweging ', '', true) ~
' ({} {})'.format(day[x[0].last_changed.isoweekday()],
x[0].last_changed.timestamp() | timestamp_custom('%H:%M:%S')) }}
# {{ x|count }}
However, I strongly recommend you create three groups, the first containing all binary_sensors for doors, the second for windows, and the third for all motion detectors. It will not only simplify the templates used in the Template Sensors but will operate more efficiently.
- platform: template
sensors:
raam_laatst_gedetecteerd:
value_template: >
{% set day = ["Zo", "Ma", "Di", "Wo", "Do", "Vr", "Za"] %}
{% set x = expand('group.raam') | selectattr('state', 'eq', 'on')
| sort(reverse=true, attribute='last_changed') %}
{{ '' if x|count == 0 else
x[0].name | regex_replace(' raam', '', true) ~
' ({} {})'.format(day[x[0].last_changed.isoweekday()],
x[0].last_changed.timestamp() | timestamp_custom('%H:%M:%S')) }}
# {{ x|count }}
deur_laatst_gedetecteerd:
value_template: >
{% set day = ["Zo", "Ma", "Di", "Wo", "Do", "Vr", "Za"] %}
{% set x = expand('group.deur') | selectattr('state', 'eq', 'on')
| sort(reverse=true, attribute='last_changed') %}
{{ '' if x|count == 0 else
x[0].name | regex_replace(' deur', '', true) ~
' ({} {})'.format(day[x[0].last_changed.isoweekday()],
x[0].last_changed.timestamp() | timestamp_custom('%H:%M:%S')) }}
# {{ x|count }}
beweging_laatst_gedetecteerd:
value_template: >
{% set day = ["Zo", "Ma", "Di", "Wo", "Do", "Vr", "Za"] %}
{% set x = expand('group.beweging') | selectattr('state', 'eq', 'on')
| sort(reverse=true, attribute='last_changed') %}
{{ '' if x|count == 0 else
x[0].name | regex_replace('beweging ', '', true) ~
' ({} {})'.format(day[x[0].last_changed.isoweekday()],
x[0].last_changed.timestamp() | timestamp_custom('%H:%M:%S')) }}
# {{ x|count }}
Why will it be more efficient? Because when you have a Template Sensor with a template containing states.binary_sensor
it will cause the Template Sensor to be updated when any binary_sensor in your system changes state. That means when a window is opened it will cause all three of the Template Sensors to be updated because all three of them use states.binary_sensor
in their template.
In contrast, if the Template Sensor’s template uses expand('group.raam')
then it will be updated only when one of the group’s members changes its state. That means when a window is opened, only one of the three Template Sensors, the one handling windows, is updated.