Automation to turn off air condition when windows are open

hello,
I have two air condition units and when I turn them on I check for windows status.
If some of the windows are open, I play a notification via TTS at alexa and then turn them off.

Here is the code am using to achieve it:

alias: Air condition state and windows open
description: ''
trigger:
  - platform: state
    entity_id:
      - climate.attic_left
      - climate.attic_right
    to: cool
    from: 'off'
condition:
  - condition: state
    entity_id: group.attic_windows_status
    state: 'on'
action:
  - data:
      target:
        - media_player.attic
        - media_player.bedroom
        - media_player.living_room
      data:
        type: tts
      message: >-
        Attention! The air condition has been turned on but the windows at attic
        are still open. Please close them otherwise it will turn off
        automatically within 60 seconds.
    service: notify.alexa_media
  - delay:
      hours: 0
      minutes: 1
      seconds: 0
      milliseconds: 0
  - service: climate.turn_off
    data: {}
    target:
      entity_id:
        - climate.attic_left
        - climate.attic_right
mode: single

My problem is that it doesn’t work all the time.
I can’t figure out why. Any ideas please?

what is ‘it’ … is the airo status not properly registered or the room-opening not properly registered and doesn’t it play tts or ? Please try to be specific where it hangs, e.g. check in the automation trace what happened and where it stopped. Try triggering it multiple times

ok! Here it is
It stops at condition

image

I revise my automation and using the following codes seems to work better

alias: Air condition state cool and the windows opened
description: the windows are opened and someone has turned on the air condition
trigger:
  - platform: state
    entity_id:
      - climate.attic_left
      - climate.attic_right
    to: cool
    from: 'off'
  - platform: state
    entity_id:
      - group.attic_windows_status
    to: 'on'
condition:
  - condition: state
    entity_id:
      - binary_sensor.door_and_window_sensor_2618
      - binary_sensor.door_and_window_sensor_7f69
      - binary_sensor.door_and_window_sensor_7ef0
      - binary_sensor.door_and_window_sensor_2e34
    match: any
    state: 'on'
  - condition: state
    entity_id:
      - climate.attic_left
      - climate.attic_right
    match: any
    state: cool
action:
  - data:
      target:
        - media_player.attic
        - media_player.bedroom
        - media_player.living_room
      data:
        type: tts
      message: >-
        Attention! The air condition has been turned on but the windows at attic
        are still open. Please close them otherwise it will turn off
        automatically within 60 seconds.
    service: notify.alexa_media
  - delay:
      hours: 0
      minutes: 1
      seconds: 0
      milliseconds: 0
  - service: climate.turn_off
    data: {}
    target:
      entity_id:
        - climate.attic_left
        - climate.attic_right
mode: single

Now it doesn’t matter if I open windows first or air condition.
No matter what the trigger is, it will go through from both triggers and eventually if the a/c is on and windows are opened, or someone opens the windows while the a/c is on, the a/c will turned off automatically within 60 seconds.

Please if you can see an error, or a scenario that it will make it not to work let me know

If the triggering works/tested fine for you, then imho the only thing you need to add is an AND statement in the condition.
Because you now added the windows as a trigger you need to verify if cooling and windows open…which may sound weird because it triggered on window open…but it is needed as the condition does not know why it triggered…either window or cooling, hence it needs to check both conditions.
I would also remove the ‘from’ state, if the target is ‘cool’ then it does not matter where it came from. Adding a from reduces it to only trigger then, e.g. not when it goes from any other possible state (? on/unknown) to cool.

Something like below, use the gui and in conditions select AND

alias: Air condition state and windows open
description: ''
trigger:
  - platform: state
    entity_id:
      - climate.attic_left
      - climate.attic_right
    to: cool
  - platform: state
    entity_id:
      - group.attic_windows_status
    to: 'on'
condition:
  - condition: state
    entity_id: group.attic_windows_status
    state: 'on'
  - condition: and
    conditions:
    - condition: state
      entity_id:
        - climate.attic_left
        - climate.attic_right
      match: any
      state: cool
1 Like

Followed both of your recommendations. It seems it works as expected
thank you

Great…please mark for solution, helps the comm.