Make conditions within Choose Action OR

Hi all
Can the conditions within the action ‘Choose’ be made OR instead of AND?
What is the syntax for that?

This is the current YAML:

alias: Evening Check
description: >-
  When I put my phone on charge, at home, after 2100, (and am therefore probably
  going to bed), check doors are locked etc
trigger:
  - type: plugged_in
    platform: device
    device_id: e47b3d0477733e994567b62fb08d7b7f
    entity_id: binary_sensor.galaxy_s10_is_charging
    domain: binary_sensor
condition:
  - condition: time
    after: '21:00'
    before: '03:00'
  - condition: zone
    entity_id: person.nick_elliott
    zone: zone.home
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: binary_sensor.conexis_l1_the_current_status_of_the_door
            state: open
          - condition: state
            entity_id: binary_sensor.conexis_l1_the_current_status_of_the_door_2
            state: open
          - condition: state
            entity_id: binary_sensor.conexis_l1_the_current_status_of_the_door_3
            state: open
        sequence:
          - service: notify.mobile_app_galaxy_s10
            data:
              message: Door(s) are unlocked!
              title: Home Assistant
    default: []
mode: single

I tried adding the line - condition: or after the line - choose:, and although HASS didn’t complain when I saved the YAML, when I next edited the automation, that line had gone again.

Thanks!

this doesn’t work?

      - conditions:
          - condition: or
            conditions:
              - condition: state
                entity_id: binary_sensor.conexis_l1_the_current_status_of_the_door
                state: open
              - condition: state
                entity_id: binary_sensor.conexis_l1_the_current_status_of_the_door_2
                state: open
              - condition: state
                entity_id: binary_sensor.conexis_l1_the_current_status_of_the_door_3
                state: open

It’s in the documents.

https://www.home-assistant.io/docs/scripts/conditions/#or-condition

A binary_sensor’s state value can be on or off. Your State Condition is configured to detect open which is neither on or off.

Open/Closed are how on/off are displayed in the Lovelace UI when the binary_sensor’s device_class is door or window.


Here’s another way to achieve your goal:

  - choose:
      - conditions:
          - condition: template
            value_template: >
               {{ expand('binary_sensor.conexis_l1_the_current_status_of_the_door',
                    'binary_sensor.conexis_l1_the_current_status_of_the_door_2',
                    'binary_sensor.conexis_l1_the_current_status_of_the_door_3')
                  | selectattr ('state', 'eq', 'on') | list | count > 0 }}
        sequence:

Unfortunately, it seems not.

I get:

  - choose:
    - conditions:    *duplicate key*
      - condition: or
      conditions:   *duplicate key*
        - condition: state   *Missing property "entity_id"*
        entity_id: binary_sensor.conexis_l1_the_current_status_of_the_door   *bad indentation of a mapping entry*
        state: open
      - condition: state  *can not read a block mapping entry; a multiline key may not be an implicit key*
        entity_id: binary_sensor.conexis_l1_the_current_status_of_the_door_2  *bad indentation of a mapping entry*
        state: open
      - condition: state
        entity_id: binary_sensor.conexis_l1_the_current_status_of_the_door_3  *bad indentation of a mapping entry*
        state: open  *bad indentation of a mapping entry*

(the Emphasis bits are the errors Studio Code Server reports in red when I try editing automations.yaml to match your suggestion).

So would on be equivalent to the door being open or closed?

I don’t understand the code in your example at all! I will have to read up on that.

I did see that page, but the examples don’t quite match the existing syntax in the Conditions section of the action section.

To be honest, I’m a bit confused by the syntax anyway. What’s the difference between the lines that start with a hyphen and those that don’t?

Hold on - I didn’t realise white space and indenting was critical in yaml. I’ve tidied it up, using tab instead of space and fiddled with the indenting, and now the errors have gone. Not sure about Taras’s comment regarding on/off instead of open/closed, but perhaps I am getting closer!

Binary sensors can only have the state “on” or “off”. Everything else is only translated in the frontend depending on the device_class.

{{ expand('binary_sensor.conexis_l1_the_current_status_of_the_door',
          'binary_sensor.conexis_l1_the_current_status_of_the_door_2',
          'binary_sensor.conexis_l1_the_current_status_of_the_door_3')
   | selectattr ('state', 'eq', 'on') | list | count > 0 }}

  1. It uses the expand function to get all of the available information for your three binary_sensors.
  2. It then uses the selectattr filter to select only the binary_sensors that are currently on.
  3. The selected binary_sensors (if any) are put into a list.
  4. The quantity of items in the list is determined with the count filter.
  5. If the quantity is greater than zero, the template reports true (otherwise false).

If it reports true then it means one or more binary_sensors is on. It’s performing an implicit logical OR operation.


The device_class of binary_sensor.front_door is door so its state in the Lovelace UI is displayed as Open/Closed.

Screenshot from 2021-12-07 18-35-00

However, it’s a binary_sensor so its true state is always on/off as shown in Developer Tools > States.

So when referencing a binary_sensor’s state in a State Trigger or State Condition or Template Condition or etc, you use its true state values (on/off) not the ones displayed in the Lovelace UI due to its choice of device_class (Open/Closed for door).

You are getting the ‘duplicate key’ error because your indentation is wrong, causing the condition:'s to be read incorrectly.

This is a good blog post about the basics of YAML:

http://thomasloven.com/blog/2018/08/YAML-For-Nonprogrammers/