Problem with automation AC off When window is close

Hi all, can you help me to solve the problem in automation?
So the idea is: if the AC is in fan_only no automation trigger. If the AC is in other states and window ( or windows ) are open ( o will be open) the automation should turn off the specific AC.

Here my automation:

alias: Villa-Force_to_Stop_Air_Cond_if_Window_Open
description: ''
trigger:
  - platform: state
    entity_id:
      - binary_sensor.window1
      - binary_sensor.window2
      - binary_sensor.window3
      - binary_sensor.window4
      - binary_sensor.window5_1
      - binary_sensor.window5_2
    to: 'on'
  - platform: state
    entity_id:
      - climate.1
      - climate.2
      - climate.3
      - climate.4
      - climate.5
condition:
  - condition: state
    entity_id: input_boolean.interlock_air_cond_villa
    state: 'on'
action:
  - service: climate.turn_off
    target: 
      entity_id >-
      {% if is_state('binary_sensor.window1', 'on') and not
      is_state('climate.1', 'fan_only') %} climate.1
      {% endif %}

  - service: climate.turn_off
    target: 
      entity_id >-
      {% if is_state('binary_sensor.window2', 'on') and not
      is_state('climate.2', 'fan_only') %}
           climate.2
      {% endif %}

  - service: climate.turn_off
    target: 
      entity_id >-
      {% if is_state('binary_sensor.window3', 'on') and not
      is_state('climate.3', 'fan_only') %}
           climate.3
      {% endif %}

  - service: climate.turn_off
    target: 
      entity_id >-
      {% if is_state('binary_sensor.window4', 'on') and not
      is_state('climate.4', 'fan_only') %}
           climate.4
      {% endif %}

  - service: climate.turn_off
    target: 
      entity_id >-
      {% if is_state('binary_sensor.window5_1', 'on') or
      is_state('binary_sensor.window5_2', 'on') and not
      is_state('climate.5', 'fan_only') %}
           climate.5
      {% endif %}

mode: single
initial_state: true

________
what i noticed is that only the first climate1 seems to work. while sometimes the 2nd or 3rd or 4th or 5th also works ....
maybe there is a smarter way to write the code?
Thanks for your help / suggestion

It is very difficult to make sense of what you have written. Could you please edit and format your post as explained here.

Thanks Tom, the code is now formatted correctly!

Your actions will probably be filling your logs with errors about null entity ids.

e.g. Consider this:

action:
  - service: climate.turn_off
    target: 
      entity_id >-
      {% if is_state('binary_sensor.window1', 'on') and not
      is_state('climate.1', 'fan_only') %} climate.1
      {% endif %}

When the window is off or the climate is in fan only mode what your template resolves to is this:

action:
  - service: climate.turn_off
    target: 
      entity_id # nothing here (and you are missing the colon after entity_id)

If you are going to call the service you must supply an entity id. If you don’t then it will generate an error and the actions will stop. This is why you were seeing the occasional 1st or 2nd action work. They only work when the conditions are true. When they are false they error and stop. I assume the colon went missing when you created this example post.

A better way to approach this is with a choose action which will skip the whole action if your conditions are not met:

action:
  - choose:
      - conditions:
          - "{{ is_state('binary_sensor.window1', 'on') and not is_state('climate.1', 'fan_only') }}"
        sequence:
          - service: climate.turn_off
            target: 
              entity_id: climate.1
  - choose:
      - conditions:
          - "{{ is_state('binary_sensor.window2', 'on') and not is_state('climate.2', 'fan_only') }}"
        sequence:
          - service: climate.turn_off
            target: 
              entity_id: climate.2
  - choose:
      - conditions: etc...

Each sequence is only executed if the condition before it is true. Otherwise the whole sequence is skipped and the automation moves on to the next action.

You can also do this with the if/then action (not template):

action:
  - if:
      - "{{ is_state('binary_sensor.window1', 'on') and not is_state('climate.1', 'fan_only') }}"
    then:
      - service: climate.turn_off
        target: 
          entity_id: climate.1
  - if:
      - "{{ is_state('binary_sensor.window2', 'on') and not is_state('climate.2', 'fan_only') }}"
    then:
      - service: climate.turn_off
        target: 
          entity_id: climate.2

Which you use is up to you. They are both equivalent. Choose has some advantages over if/then for more complicated logic. But in this case it makes no difference.

Note: I have used the shorthand template condition.

This:

  - if:
      - "{{ is_state('binary_sensor.window1', 'on') and not is_state('climate.1', 'fan_only') }}"

Is equivalent to:

  - if:
      - condition: template
        value_template:  "{{ is_state('binary_sensor.window1', 'on') and not is_state('climate.1', 'fan_only') }}"

You don’t actually need a template (though it is more compact). This is the same:

  - if:
      - condition: state
        entity_id:  binary_sensor.window1
        state: 'on'
      - condition: not
        conditions:
          - condition: state
            entity_id: climate.1
            state: fan_only

Thanks Tom, this is perfect.

what about the mobile notification or persistent notification?

if i try to implement the mobile notification in order to understand which device was forced off,
here below my code:

      - service: notify.mobile_app_myphone
        data_template:
          title: My title
          message: |
            AC {{ trigger.to_state.attributes.friendly_name }} is forced OFF !!!
            <br>Date/Time: {{now().strftime("%a %b %d %H:%M:%S %p")}}
          data:
            color: grey

but if I add the above code inside each “choose” I get notifications for all Air Conditioners, this is not good…

thanks for your support

Also that message won’t make sense if the trigger was a window opening. e.g.

AC Window 4 forced off !!!
Date/Time: Fri Jun 24 18:47:50 PM

It’s a tricky one. Let me think about it.

EDIT: you could use an input text and append each climate device that was turned off:

action:
  - service: input_text.set_value
    target:
      entity_id: input_text.climate_holder
    data:
      value: ''
  - if:
      - "{{ is_state('binary_sensor.window1', 'on') and not is_state('climate.1', 'fan_only') }}"
    then:
      - service: climate.turn_off
        target: 
          entity_id: climate.1
      - service: input_text.set_value
        target:
          entity_id: input_text.climate_holder
        data:
          value: "{{ states('input_text.climate_holder') ~ 'Climate 1 ' }}"
  - if:
      - "{{ is_state('binary_sensor.window2', 'on') and not is_state('climate.2', 'fan_only') }}"
    then:
      - service: climate.turn_off
        target: 
          entity_id: climate.2
      - service: input_text.set_value
        target:
          entity_id: input_text.climate_holder
        data:
          value: "{{ states('input_text.climate_holder') ~ 'Climate 2 ' }}"
.
.
.
  - if:
      - "{{ is_state('binary_sensor.window5', 'on') and not is_state('climate.5', 'fan_only') }}"
    then:
      - service: climate.turn_off
        target: 
          entity_id: climate.5
      - service: input_text.set_value
        target:
          entity_id: input_text.climate_holder
        data:
          value: "{{ states('input_text.climate_holder') ~ 'Climate 5 ' }}"
  - service: notify.mobile_app_myphone
    data_template:
      title: My title
      message: |
        The following ACs were forced off: {{ states('input_text.climate_holder') }}
        <br>Date/Time: {{now().strftime("%a %b %d %H:%M:%S %p")}}
      data:
        color: grey

ok Thanks tom!!!

Edited post above.

To be honest…I don’t like in this way with txt file… Maybe if I change not is_state with states: cool, heat, etc without put off… I will solve the problem. I will check!

thanks a lot

I have recently updated my Blueprint for Home Assistant climate entities. Although this topic has been solved, I would like to introduce you to the method I have used. Check out the code:

Thanks Rocks for your advise.
Let me share my project just to have a valid alternative to our blueprint:

alias: ''
description: ''
trigger:
  - platform: state
    entity_id:
      - binary_sensor.window1
      - binary_sensor.window2
      - binary_sensor.window3
      - binary_sensor.window4
      - binary_sensor.window5
    to: 'on'
  - platform: state
    entity_id:
      - climate.ac1
      - climate.ac2
      - climate.ac3
      - climate.ac4
      - climate.ac5
    to:
      - dry
      - cool
      - heat
      - heat_cool
condition:
  - condition: state
    entity_id: input_boolean.interlock_air_cond
    state: 'on'
action:
#same area
  - if:
      - >-
        {{ is_state('binary_sensor.window1', 'on') or
        is_state('binary_sensor.window2', 'on') }}
    then:
      - service: climate.turn_off
        target:
          entity_id:
            - climate.ac1
            - climate.ac2
        data: {}
#indipendent rooms
  - if:
      - >-
        {{ is_state('binary_sensor.window3', 'on')
        }}
    then:
      - service: climate.turn_off
        target:
          entity_id: climate.ac3
        data: {}
  - if:
      - '{{ is_state(''binary_sensor.window4'', ''on'') }}'
    then:
      - service: climate.turn_off
        target:
          entity_id: climate.ac4
        data: {}
  - if:
      - '{{ is_state(''binary_sensor.window5'', ''on'') }}'
    then:
      - service: climate.turn_off
        target:
          entity_id: climate.ac5
        data: {}
mode: single
initial_state: true


#to recap:
if I have the windows open and I turn on ACx it turns off.
if I turn on only the fan it stays on
if while I use AC and open the window the air conditioner is switched off according to the area of interest.
Thank you all