Can you use "OR" statements in automations

New to Home Assistant and can’t seem to find an answer in the Automations guidance document nor in this Forum, so please be patient. I’m coming from SmartThings and want to replicate this automation:

  • IF 10:00 pm AND Side Door Contact Sensor OR Sgl Garage Door OR Dbl Garage Door OR Shed Door contact sensor open
  • THEN
    • Send text message (“A door is open”)
    • Turn On Driveway Lights Notification (Green/80%/Chase/2 hours)

I’m still wrestling with text messages but my immediate question is how to program the ORs in an automation.

Thanks.

My best advice to learn this is to use the GUI to make an automation with OR and AND conditions, like the ones you want and then switch to YAML view through the three dots in the upper right corner.
This will show you the nested conditions like you need to use them.

I believe the easiest is to put the doors in a group.
I believe there is a setting to make it go on if only one is open.

Then you just use time trigger and condition the group

Your trigger will be 10pm

your conditions will be Side Door Contact Sensor OR Sgl Garage Door OR Dbl Garage Door OR Shed Door contact sensor open.

that will look something like this:

triggers:
  - trigger: time
    at: '22:00:00'
conditions:
  - condition: or
    conditions:
      - condition: state
        entity_id:
          - Side Door Contact Sensor
          - Sgl Garage Door
          - Dbl Garage Door
          - Shed Door contact sensor
        state: open
actions:
  - Send text message (“A door is open”)
  - Turn On Driveway Lights Notification (Green/80%/Chase/2 hours)

obviously you will need to fix all of the entity_ids, state and the actions to actual code. But that should point you in the right direction.

That’s not going to work… the OR isn’t OR-ing the states of the sensors, it’s Or-ing the AND-ed states of the sensors with no other condition.

You need to have triggers that cover all those events and then mirror them in the conditions. To get an OR logic in the State condition you need to include the configuration variable match with the value any:

triggers:
  - trigger: time
    at: '22:00:00'
  - trigger: state
    entity_id:
      - binary_sensor.side_door_contact_sensor
      - binary_sensor.sgl_garage_door
      - binary_sensor.dbl_garage_door
      - binary_sensor.shed_door_contact_sensor
    to: "on"
conditions:
  - condition: time
    after: '22:00:00'
  - condition: state
    entity_id:
      - binary_sensor.side_door_contact_sensor
      - binary_sensor.sgl_garage_door
      - binary_sensor.dbl_garage_door
      - binary_sensor.shed_door_contact_sensor
    state: "on"
    match: any
actions:
# Your actions

Putting them in a group with “All entities” disabled would also work. You would use the same construction as above, but place the group entity in the State trigger and condition rather than the individual doors.

Just note, there is a slight difference in functionality between the two. In the automation above, each door opening generates it’s own trigger event. If someone opened the side door and left it open then opened the shed door, you would get a message for for the side door then one for the shed. Using a group you would only get a message for the side door.


FWIW, the update later this week should be adding to the “purpose specific” triggers and conditions feature available in Labs, so you would be able to just label the doors and use something like:

triggers:
  - trigger: door.opened
    target:
      label_id: exterior_doors
    options:
      behavior: any
  - trigger: time
    at: "22:00:00"
conditions:
  - condition: door.is_open
    target:
      label_id: exterior_doors
    options:
      behavior: any
  - condition: time
    after: "22:00:00"
1 Like

I guess we’ll have to wait to see what the OP’s reply is for the actual use case but the way I read this without adding assumption as to intent:

  • IF 10:00 pm AND Side Door Contact Sensor OR Sgl Garage Door OR Dbl Garage Door OR Shed Door contact sensor open

The OP says at 10pm (trigger) then check to see if any of the listed things are open (or condition for the doors) then do the actions.

I guess it could go the other way but that’s less likely since the OP didn’t say “after 10pm” they said “if 10pm” implying “if (it’s at) 10pm”.

:man_shrugging: