Hey guys,
I’m new in HA…
I’m struggling with maybe a simple problem. I want to reduce the redundancy in my automation for my entities. So I wrote two times something like this:
{{ expand('sensor.sensor_fenster_klein_badezimmer_contact_state',
'sensor.sensor_fenster_gaste_wc_contact_state',
'sensor.sensor_fenster_gastezimmer_contact_state',
'sensor.sensor_fenster_kuche_contact_state',
'sensor.sensor_fenster_wohnzimmer_contact_state',
'sensor.sensor_haustur_contact_state',
'sensor.sensor_hwr_tur_contact_state',
'sensor.sensor_terrassentur_esszimmer_contact_state',
'sensor.sensor_terrassentur_wohnzimmer_contact_state',
'sensor.sensor_fenster_hwr_contact_state')
| selectattr('state', 'search',
'(Tilted|Open)') }}
one time a condition and one time the same in an action. So how can I prevent myself from repeating?
I just tried to use group helper and got a new entity “sensor.alle_fenster_turkontakte”:
But when I change my code to
{{ expand('sensor.alle_fenster_turkontakte')
| selectattr('state', 'search',
'(Tilted|Open)') }}
nothing happens. So I think ‘sensor.alle_fenster_turkontakte’ is not set / available?!
But what is the recommended way for this issue?
By the way this is my whole YAML of the automation with the redundancy:
alias: Bei Alarmanlage an, Fenster/Türen offen prüfen
description: ""
trigger:
- platform: state
entity_id:
- scene.homepilot_alarmanlage_an
from: null
to: null
id: Alarmanlage_An
- platform: state
entity_id:
- scene.homepilot_gute_nacht
from: null
to: null
id: Gute_Nacht
condition:
- condition: template
value_template: "{{ expand('sensor.sensor_fenster_klein_badezimmer_contact_state', \n 'sensor.sensor_fenster_gaste_wc_contact_state',\n 'sensor.sensor_fenster_gastezimmer_contact_state',\n 'sensor.sensor_fenster_kuche_contact_state',\n 'sensor.sensor_fenster_wohnzimmer_contact_state',\n 'sensor.sensor_haustur_contact_state',\n 'sensor.sensor_hwr_tur_contact_state',\n 'sensor.sensor_terrassentur_esszimmer_contact_state',\n 'sensor.sensor_terrassentur_wohnzimmer_contact_state',\n 'sensor.sensor_fenster_hwr_contact_state')\n\t| selectattr('state', 'search',\n '(Tilted|Open)') | list | count > 0 }}"
action:
- service: notify.notify
data:
title: Fenster / Türen offen
message: >-
Folgende Fenster / Türen sind noch offen:
{% for e in
expand('sensor.sensor_fenster_klein_badezimmer_contact_state',
'sensor.sensor_fenster_gaste_wc_contact_state',
'sensor.sensor_fenster_gastezimmer_contact_state',
'sensor.sensor_fenster_kuche_contact_state',
'sensor.sensor_fenster_wohnzimmer_contact_state',
'sensor.sensor_haustur_contact_state',
'sensor.sensor_hwr_tur_contact_state',
'sensor.sensor_terrassentur_esszimmer_contact_state',
'sensor.sensor_terrassentur_wohnzimmer_contact_state',
'sensor.sensor_fenster_hwr_contact_state') | selectattr('state',
'search', '(Tilted|Open)') %} - {{ e.name }} {% endfor %}
- service: scene.turn_on
metadata: {}
target:
entity_id: scene.homepilot_alarmanlage_aus
mode: single
Thanks in advance!