Automation - Alexa tells to close window after an configurable amount of time with switched off climate entity

Hi Guys,

I want to create an automation which tells by alexa announcement to close windows after an defined amount of delay (based on the outside temperature).

This is my current automation-config:

alias: "ALEXA: Heizung Schlafzimmerfenster"
description: ""
trigger: []
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:
      fenster: Schlafzimmerfenster
  - if:
      - condition: numeric_state
        entity_id: sensor.mean_temperature_outdoor
        below: 0
    then:
      - variables:
          delay: 5
    alias: Wenn unter 0°C dann setze Variable auf 5 Minuten Verzögerung
  - alias: >-
      Wenn unter 10°C aber über 0°C dann setze Variable auf 10 Minuten
      Verzögerung
    if:
      - condition: numeric_state
        entity_id: sensor.mean_temperature_outdoor
        below: 10
        above: 0
    then:
      - variables:
          delay: 10
  - alias: >-
      Wenn unter 20°C aber über 10°C dann setze Variable auf 15 Minuten
      Verzögerung
    if:
      - condition: numeric_state
        entity_id: sensor.mean_temperature_outdoor
        below: 20
        above: 10
    then:
      - variables:
          delay: 1
  - alias: Wenn über 20°C ist keine Verzögerung notwendig
    if:
      - condition: numeric_state
        entity_id: sensor.mean_temperature_outdoor
        above: 20
    then:
      - stop: Keine Verzögerung notwendig - es hat über 20°C draußen!
  - delay: 00:{{ delay | int }}:00
    alias: Verzögern für {{ delay }} 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: "{{ fenster }} 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: "{{ fenster }} schließen!"
              data:
                type: announce
                method: all
              target:
                - media_player.echo_dot_schlafzimmer
    alias: "{{ fenster }} schließen!"
  - alias: "{{ fenster }} ist noch immer geöffnet!"
    parallel:
      - repeat:
          sequence:
            - delay:
                hours: 0
                minutes: 0
                seconds: 30
                milliseconds: 0
            - 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: "{{ fenster }} 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: "{{ fenster }} schließen!"
                    data:
                      type: announce
                      method: all
                    target:
                      - media_player.echo_dot_schlafzimmer
          until:
            - condition: state
              entity_id: binary_sensor.ha_contactgroup_schlafzimmerfenster
              state: "off"
mode: single

Unfortunatley this does not work because it tells me that the “delay” variable is not defined although I define it via “if-then”.

image

Can you please have a look and tell me where is my fault.

PS: Don’t wonder that there is no trigger - this automation get’s triggered by another automation.

Thx in advance
Florian

PS: for sure open for any idea of improvement of the whole automation :slight_smile:

Variables defined inside if statements, only have their scope inside the if, so they are ‘forgotten’ once you go outside it.

That’s pretty uncomfortable for my automation. Any ideas to elegantly fix that?

Cheers

Set up the delay variable with your other variables… that way it’s available throughout the action block.

action:
  - variables:
      fenster: Schlafzimmerfenster
      delay: |
        {% set temp = states('sensor.mean_temperature_outdoor')|float %}
        {% if temp <= 0 %} 5
        {% elif 0 < temp <= 10 %} 10
        {% elif 10 < temp < 20 %} 1
        {% endif %}
...

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.

Thx for that hint - I’ll try it that way!

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 …

This is the new attempt.

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

You need to use a template condition.

condition: template
value_template: "{{ is_state(trigger.entity_id, 'on') }}"

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?

Thx in advance!

Had some trouble to understand this - now I understood that it uses the entity_id which is defined via the trigger!

Thx

Btw: Am I right when I say that something like
{{ state_attr('binary_sensor.ha_contactgroup_schlafzimmerfenster', 'friendly_name') }}

will not work as
{{ state_attr('trigger.entity_id', 'friendly_name') }}

It always tells me None as result of this template.

Thx in advance

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.

You may wish to consider redesigning it to use repeat - while. Its condition is evaluated before the sequence is run.

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') }}"

already answered there →

Just for reference – complete Automation which works perfectly (for me) now:

alias: "ALEXA: Heizung Schlafzimmerfenster"
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.ha_contactgroup_schlafzimmerfenster
    to: "on"
    for:
      hours: 0
      minutes: 0
      seconds: 0
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: |
        {{trigger.to_state.attributes.friendly_name}}
      room: |
        {{ area_name(trigger.entity_id) }}
      initial_delay_minutes: |
        {% set float_temp = states('sensor.mean_temperature_outdoor')|float %}
        {% if float_temp <= 0 %} 5
        {% elif 0 < float_temp <= 10 %} 1
        {% elif 10 < float_temp < 20 %} 1
        {% elif "unknown" %} 5
        {% endif %}
      stillnotclosed_delay_minutes: 1
  - 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: template
        value_template: "{{ is_state(trigger.entity_id , 'on') }}"
      - condition: state
        entity_id: input_boolean.helper_schlafzimmerfenster_blocker_for_heating
        state: "off"
    then:
      - if:
          - condition: template
            value_template: "{{ is_state(trigger.entity_id , '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:
        - 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') }}"
  - service: climate.set_hvac_mode
    data:
      hvac_mode: auto
    target:
      entity_id: climate.{{room}}
mode: single

A repeat - while won’t iterate even once if its condition(s) evaluates to false.

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.

Probably.

Anyway, what you have now works to your satisfaction.