That’s an anti-pattern … which is software terminology for “bad practice”.
Convert this “triggerless automation” into a script and call it from the other automation (or simply merge it into the other automation if it’s the only one that calls it).
FWIW, the automation contains a curious usage of the parallel statement. Typically it’s used to parallelize multiple actions yet here it’s used for a single action.
Tip
Avoid creating a variable whose name is the same as a script action or Jinja filter. For example, delay is an existing script action.
Don’t get it it. It does not trigger a single action. It triggers two different calls for one echo-dot in my living room and one in my bedroom (depending of the presence in the respective room)
PS: No - sorry - you’re right - to be honest I don’t know why it came out that way. I wanted to have the echo dot calls in the parallell statement and not the “repeat until” … looks like i mixed something up …
A few more questions. Is it possible to fill the condition entity_id, which checks if the window contact is still open, also with a variable (I want to use this automation at the end for all windows in my house) - I couldn’t get this to work.
Second question - maybe you see something obvious, but I can’t pinpoint the problem. When I close the window before the “initial_delay_minutes” is over, it still tells me to close the window once, even though it is already closed.
Thx for every help!
alias: "ALEXA: Heizung Schlafzimmerfenster"
description: ""
trigger:
- platform: state
entity_id:
- binary_sensor.ha_contactgroup_schlafzimmerfenster
from: "off"
to: "on"
for:
hours: 0
minutes: 0
seconds: 30
condition:
- condition: state
entity_id: input_boolean.helper_global_heating
state: "on"
- condition: state
entity_id: input_boolean.helper_alexa_for_airing
state: "off"
- condition: state
entity_id: input_boolean.alexa_helper_alexa_for_airing_at_night
state: "off"
action:
- variables:
window: Schlafzimmerfenster
room: schlafzimmer
initial_delay_minutes: |
{% set float_temp = states('sensor.mean_temperature_outdoor')|float %}
{% if float_temp <= 0 %} 5
{% elif 0 < float_temp <= 10 %} 10
{% elif 10 < float_temp < 20 %} 15
{% elif "unknown" %} 5
{% endif %}
stillnotclosed_delay_minutes: 5
- service: climate.turn_off
data: {}
target:
entity_id: climate.{{room}}
- delay: 00:{{ initial_delay_minutes | int }}:00
alias: Verzögern für {{initial_delay_minutes}} Minuten
- if:
- condition: state
entity_id: binary_sensor.ha_contactgroup_schlafzimmerfenster
state: "on"
- condition: state
entity_id: input_boolean.helper_schlafzimmerfenster_blocker_for_heating
state: "off"
then:
- alias: "{{window}} schließen!"
parallel:
- alias: Echo-Dot Wohnzimmer
if:
- condition: state
entity_id: input_boolean.helper_livingroom_occupied
state: "on"
then:
- service: script.1632671670004
data: {}
- service: notify.alexa_media
data:
message: "{{ window }} schließen!"
data:
type: announce
method: all
target:
- media_player.echo_dot_wohnzimmer
- alias: Echo-Dot Schlafzimmer
if:
- condition: state
entity_id: input_boolean.helper_masters_bedroom_occupied
state: "on"
- condition: or
conditions:
- condition: time
after: "10:00:00"
before: "20:00:00"
- condition: state
entity_id: switch.shelly_schreibtisch
state: "on"
then:
- service: media_player.volume_set
data:
volume_level: 0.3
target:
entity_id: media_player.echo_dot_schlafzimmer
- service: notify.alexa_media
data:
message: "{{ window }} schließen!"
data:
type: announce
method: all
target:
- media_player.echo_dot_schlafzimmer
- repeat:
sequence:
- if:
- condition: state
entity_id: binary_sensor.ha_contactgroup_schlafzimmerfenster
state: "on"
- condition: state
entity_id: input_boolean.helper_schlafzimmerfenster_blocker_for_heating
state: "off"
then:
- delay: 00:{{ stillnotclosed_delay_minutes | int }}:00
alias: Verzögern für {{ stillnotclosed_delay_minutes }} Minuten
- parallel:
- alias: Echo-Dot Wohnzimmer
if:
- condition: state
entity_id: input_boolean.helper_livingroom_occupied
state: "on"
then:
- service: script.1632671670004
data: {}
- service: notify.alexa_media
data:
message: "{{ window }} ist noch immer geöffnet!"
data:
type: announce
method: all
target:
- media_player.echo_dot_wohnzimmer
- alias: Echo-Dot Schlafzimmer
if:
- condition: state
entity_id: input_boolean.helper_masters_bedroom_occupied
state: "on"
- condition: or
conditions:
- condition: time
after: "10:00:00"
before: "20:00:00"
- condition: state
entity_id: switch.shelly_schreibtisch
state: "on"
then:
- service: media_player.volume_set
data:
volume_level: 0.3
target:
entity_id: media_player.echo_dot_schlafzimmer
- service: notify.alexa_media
data:
message: "{{ window }} ist noch immer geöffnet!"
data:
type: announce
method: all
target:
- media_player.echo_dot_schlafzimmer
until:
- condition: state
entity_id: binary_sensor.ha_contactgroup_schlafzimmerfenster
state: "off"
- condition: state
entity_id: input_boolean.helper_schlafzimmerfenster_blocker_for_heating
state: "off"
- service: climate.set_hvac_mode
data:
hvac_mode: auto
target:
entity_id: climate.{{room}}
mode: single
Sounds legit - and with a template condition it’s possible to use a variable as entity_id? How is the format … like normal variable surrounded with “{{ }}” ?
mhh … -To be honest currently I don’t have any ideas to “workaround” this - any ideas from you guys?
Maybe something like another automation which cancels this one when the window/door get’s closed - but this would be the “anti-pattern” again which has been mentioned by @123 earlier right?
Like other variables, the trigger variable should not be put in quotes inside function like states(), state_att(), etc. However, there is no need to recalculate the value like that since that information already exists within the trigger variable’s object. Either {{trigger.to_state.attributes.friendly_name}} or {{trigger.to_state.name}} will return the friendly name of the entity responsible for triggering the automation… the latter will return the entity ID in cases where a friendly name wasn’t defined.
Just FYI, the trigger variable only has a value inside a running automation or trigger-based template sensor, so you cannot test them in the Template editor unless you define the variable object for yourself.
this works !! - is there also a way to get the area the entity is in into a variable?
I also thought on this - but this also didn’t work - I guess because when Alexa tells me to close the window the “next run” is already initiated therefore it still tells to close the windows once after already closed. Therefore I worked around this issue with another condition within the loop itself which again checks if window is open or not. This condition looks like this then →
repeat:
sequence:
- delay: 00:{{ stillnotclosed_delay_minutes | int }}:00
alias: Verzögern für {{ stillnotclosed_delay_minutes }} Minuten
- if:
- condition: template
value_template: "{{ is_state(trigger.entity_id , 'on') }}"
then:
- service: script.1632671670004
data: {}
- service: notify.alexa_media
data:
message: " {{ window }} ist noch immer geöffnet!"
data:
type: announce
method: all
target:
- media_player.echo_dot_wohnzimmer
- alias: Echo-Dot Schlafzimmer
if:
- condition: state
entity_id: input_boolean.helper_masters_bedroom_occupied
state: "on"
- condition: or
conditions:
- condition: time
after: "10:00:00"
before: "20:00:00"
- condition: state
entity_id: switch.shelly_schreibtisch
state: "on"
then:
- service: media_player.volume_set
data:
volume_level: 0.3
target:
entity_id: media_player.echo_dot_schlafzimmer
- service: notify.alexa_media
data:
message: " {{ window }} ist noch immer geöffnet!!"
data:
type: announce
method: all
target:
- media_player.echo_dot_schlafzimmer
while:
- condition: template
value_template: "{{ is_state(trigger.entity_id , 'on') }}"
I understood it technically - but it still does - as soon as I remove the additional condition to check if the window is still open, in the while loop - it says “close window” again, even after the window is already closed. I don’t know why - maybe I have a wrong sequence or something.