SOLVED: Using choose for OR IF instead of ELSE IF

You were right, it was just because I was missing one choose statement. Now the automation works correctly when corrected as below. Thanks for doing the proofreading I failed at:


- id: someone_through_gate
  alias: someone_through_gate
  initial_state: 'true'
  trigger:
    platform: state
    entity_id: binary_sensor.person_inside_gate
    to: 'on'
  action:
    - choose:
        - conditions:
            - condition: and
              conditions:
              - condition: template
                value_template: '{{ (as_timestamp(now()) - as_timestamp(states.automation.person_in_front.attributes.last_triggered | default(0)) | int > 600)}}'
              - condition: state
                entity_id: device_tracker.sean_s_iphone_12
                state: 'home'
          sequence:
            service: notify.mobile_app_sean_s_iphone_12
            data:
              title: "Person Coming Through Gate"
              message: "Looks like someone is going to or coming from the front gate to the front door"
              data:
                push:
                  category: camera
                  thread-id: "security"
                entity_id: camera.front_stream
                clickAction: /lovelace/4
    - choose:
        - conditions:
            - condition: and
              conditions:
              - condition: template
                value_template: '{{ (as_timestamp(now()) - as_timestamp(states.automation.person_in_front.attributes.last_triggered | default(0)) | int > 600)}}'
              - condition: state
                entity_id: device_tracker.alena_s_iphone
                state: 'home'
          sequence:
            service: notify.mobile_app_alena_s_iphone
            data:
              title: "Person Coming Through Gate"
              message: "Looks like someone is going to or coming from the front gate to the front door"
              data:
                push:
                  category: camera
                  thread-id: "security"
                entity_id: camera.front_stream
                clickAction: /lovelace/4
    - choose:
        - conditions:
            - condition: and
              conditions:
              - condition: template
                value_template: '{{ (as_timestamp(now()) - as_timestamp(states.automation.person_in_front.attributes.last_triggered | default(0)) | int > 600)}}'
              - condition: state
                entity_id: device_tracker.sean_s_iphone_12
                state: 'home'
          sequence:
            - service: mqtt.publish
              data:
                topic: !secret genesys_5490_notify_topic
                payload: !secret genesys_5490_someone_through_gate_notification

In all three choose statements, the first condition is the same:

              - condition: template
                value_template: '{{ (as_timestamp(now()) - as_timestamp(states.automation.person_in_front.attributes.last_triggered | default(0)) | int > 600)}}'

Why not just move that test to the automation’s main condition?

BTW, the first and third choose have identical conditions. They both check if device_tracker.sean_s_iphone_12 is home. When the conditions are met, the first one will always be chosen and never the third one.

Because I didn’t know that it was possible to mix a central condition and a choose condition :wink: .

I explained why the last 2 have the same conditions above already. I plan to add more. I’m just building out the automation and making sure it works first.

1 Like

In case anyone wants to see the final automation, I needed to also switch the template to an input_boolean because it would change before it got to the value template and so wouldn’t work for telling how long it had been since the last run.

- id: notify_home_aqi_low
  alias: notify_home_aqi_low
  initial_state: 'true'
  trigger:
    platform: numeric_state
    entity_id: sensor.home_air_quality
    below: 45
    for: 
      minutes: 5
  condition:
    condition: state
    entity_id: input_select.last_inside_aqi_notification
    state: 'Bad'
  action:
    - choose:
        - conditions:
            - condition: state
              entity_id: device_tracker.sean_s_iphone_12
              state: 'home'
          sequence:
            - service: notify.mobile_app_sean_s_iphone_12
              data:
                title: "Home indoor air quality is good again"
                message: "The indoor Air Quality Index at home is currently {{states('sensor.home_air_quality')}}."             
                data:
                  push:
                    thread-id: "weather"

    - choose:
        - conditions: 
            - condition: state
              entity_id: device_tracker.alena_s_iphone
              state: 'home'
          sequence:
            - service: notify.mobile_app_alena_s_iphone
              data:
                title: "Home indoor air quality is good again"
                message: "The indoor Air Quality Index at home is currently {{states('sensor.home_air_quality')}}."             
                data:
                  push:
                    thread-id: "weather"
    - choose:
        - conditions: 
            - condition: state
              entity_id: device_tracker.sean_s_iphone_12
              state: 'home'
          sequence:
            - service: mqtt.publish
              data:
                topic: !secret genesys_5490_notify_topic
                payload: !secret genesys_5490_inside_aqi_good
    - service: input_select.select_option
      data:
        entity_id: input_select.last_inside_aqi_notification
        option: 'Good'