OR in Triggers

Is it possible to use OR in a trigger or something similar?

I want to use CHOOSE in my automation conditions the service call would depend on what triggered the automation.

For example

The below will turn on one person’s bedside lamp if their phone is placed on the charger first. I currently run two separate automations but wondered if I could consolidate them in one by OR in trigger then CHOOSE in conditions.

- alias: First to Bed
  id: first_to_bed
  mode: single
  trigger:
  - platform: state
    entity_id: sensor.iphone_r_battery_state
    to: Charging
    from:
    - Not Charging
    - Full
  condition:
    condition: and
    conditions:
    - condition: time
      after: '20:00:00'
      before: '03:00:00'
    - condition: state
      entity_id: input_boolean.holiday_mode
      state: 'off'
    - condition: template
      value_template: '{{ is_state("sensor.iphone_m_battery_state", "Not Charging")
        }}'
    - condition: template
      value_template: '{{ is_state("person.m", "home") }}'
  action:
  - service: script.r_first_to_bed

Triggers are always OR logic.

  - platform: state
    entity_id: sensor.iphone_r_battery_state
    id: iphone_r
    to: Charging
    from:
    - Not Charging
    - Full
  - platform: state
    entity_id: sensor.iphone_m_battery_state
    id: iphone_m
    to: Charging
    from:
    - Not Charging
    - Full

Then remove the template condition

    - condition: template
      value_template: '{{ is_state("sensor.iphone_m_battery_state", "Not Charging")
        }}'
    - condition: template
      value_template: '{{ is_state("person.m", "home") }}'

Then in the choose you choose based on the trigger id, and do your checks (which you can do with state checking, not sure why you have used templates.

action:
  choose:
    - conditions:
        - condition: trigger
          id: iphone_r
        - condition: state
          entity_id: sensor.iphone_m_battery_state
          state: "Not Charging"
        - condition: state
          entity_id: person.m
          state: home
      sequence:
        - service: script.r_first_to_bed

Hopefully that gives you enough information for you to see how you would then use option 2 in the choose to react to the other person getting into bed first.

As an aside - how does person m not being home have any bearing on whether or not person r is the first to bed?

1 Like

That’s great, thanks for confirming

I think (around 3 years ago so can’t be 100% certain now) originally I added that condition to run a third automation if that condition was not met. Confusing, I know, and that’s why I’m trying to tidy up all my older scripts and automations now.

1 Like