Help with or conditions in automation

Hi!
I am trying to make a automation that sends notification when a window or door is open during arming of house alarm.
The trigger and the action is ok, as i use the same code in other automations, but the conditions does not work, so i obviously doing something wrong…
All windows and doors has binary door/window sensors, and my goal is alert when one of them is in state “on”.
I am sure that i could use a better/shorter method, but this is what i have tried and it does not work:

  condition:
  - condition: or
    conditions:
    - condition: state
      entity_id: binary_sensor.openclose_23
      state: 'on'
  - condition: or
    conditions:
    - condition: state
      entity_id: binary_sensor.openclose_31
      state: 'on'
    - condition: or
      conditions:
      - condition: state
        entity_id: sensor.terrasse_door_stue
        state: 'on'
      - condition: or
        conditions:
        - condition: state
          entity_id: binary_sensor.openclose_20
          state: 'on'
        - condition: or
          conditions:
          - condition: state
            entity_id: binary_sensor.openclose_33
            state: 'on'

Someone that could point me in a right direction?

does it show anything like that in docs?
does this work?

condition:
- condition: or
  conditions:
  - condition: state
    entity_id: binary_sensor.openclose_23
    state: 'on'
  - condition: state
    entity_id: binary_sensor.openclose_31
    state: 'on'
  - condition: state
    entity_id: sensor.terrasse_door_stue
    state: 'on'
  - condition: state
    entity_id: binary_sensor.openclose_20
    state: 'on'
  - condition: state
    entity_id: binary_sensor.openclose_33
    state: 'on'

You can read this and do something like

condition:
- condition: template
  value_template: >
    {{
      is_state('binary_sensor.openclose_23', 'on') or
      is_state('binary_sensor.openclose_31', 'on') or
      is_state('sensor.terrasse_door_stue', 'on') or
      is_state('binary_sensor.openclose_20', 'on') or
      is_state('binary_sensor.openclose_33', 'on')
    }}

or this

condition:
- condition: template
  value_template: >
    {% set sensors = [
        'binary_sensor.openclose_20',
        'binary_sensor.openclose_23',
        'binary_sensor.openclose_31',
        'binary_sensor.openclose_33',
        'sensor.terrasse_door_stue'
      ]
    %}
    {% for sensor in sensors if is_state(sensor, 'on') %}
      {% if loop.first %}
        True
      {% endif %}
    {% endfor %}

Hi!
Yes, i think i found out the same, just have one or on top and add the entities and states under, i am trying this out now.

Yes that was my mistake, now it works!
Now i think i have to extend the functionality and figure out how i can include what sensor(s) that is on (what door/window is open) in the notification message.
Maybe i have to use a script and set a variable, i have to search here or google :slight_smile:

Thanks!

1 Like

Glad that it works for you

I’d strongly advice to start from reading official docs. The reason is HA evolves constantly and rapidly so every solution becomes obsolete or even doesn’t work anymore within months. On the other hand, docs are more or less up to date and contain some working examples.
There is also a cookbook.

For your goal have a look at Automation Tempting (this bit in particular) and Templating.

No global variables here unless you use custom integration that allows it.

2 Likes