Sequence automation conditions

Trying to figure out an automation that would run if a sequence of events happened.
Someone walks down the steps (tripwire)
door opened
Crosses tripwire
Then automation turns light on.

The above would happen one after the other in the above order. Using triggers and conditions I can only figure out how to do it if all of them happened at once like this:

automation:
- alias: Entering House
  trigger:
    platform: state
    entity_id: sensor.tripwire1
    to: 'on'
  action:
    service: light.turn_on
    entity_id: light.kitchen
    data:
      brightness: 128
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: 'sensor.door2'
        state: 'open'
      - condition: state
        entity_id: 'sensor.tripwire2'
        state: 'on'

The above I have just made up to explain what I am trying to do, so I could have syntax slightly wrong (not tested the above) but its the logic I am trying to get. The above obviously wouldn’t work because its expecting the above conditions to both be true (AND), no good using OR because then if one of them triggered the action would happen.

Basically I’m trying to figure out if someone is coming in or out of the house.

Any help appreciated.

You can probably do this in YAML using input_booleans to maintain state across a few different automations and then have a final automation that only fires if the input_booleans are all on, and then reset them.

Another approach would be to code this as an App in AppDaemon - maintaining state across events is a lot easier in an App.

2 Likes

Please don’t even think about doing this in HA native. That would be a real pain but not undo able! That’s a classic for AppDaemon!

~Cheers

2 Likes

Thanks I was already intending to try out Appdaemon and HADashboard now a bigger reason to move Appdeamon up the list.

2 Likes

i recently did this, with one condition less: after a button is pressed this automation waits 30 seconds to see if motion is detected and only then turns on the nightstand.

alias: Alles ausschalten
trigger:
  - platform: event
    event_type: button_pressed
    event_data: {'state': 'off',  'entity_id': 'switch.stube_schalter_1_links'}
action:
  # script to turn everything off
  - service: script.turn_on
    entity_id: script.stube_alles_aus
  # wait 30 seconds to see if there’s motion
  - wait_template: "{{ states.binary_sensor.bewegungsmelder_schlafzimmer.state == 'on'  }}"
    timeout: 00:00:30
  # double check motion (and time), since after 30 seconds the action sequence continues
  - condition: state
    entity_id: binary_sensor.bewegungsmelder_schlafzimmer
    state: 'on'
  - condition: time
    after: '20:00:00'
  - service: switch.turn_on
    entity_id: switch.sonofftwo

i guess you could trigger the automation via the first trip wire and then use the wait template to check the door sensor and then wait for the second trip wire before turning on the light.

2 Likes

I can’t wait to try out AppDaemon for this scenario!

I often find myself wishing there was a “sequence” concept in HA. I currently simulate a “trigger sequence” by using input_selects with several “states”, using automations to traverse the states.

For example, I want to know whether I’m actually inside my house, as opposed to “outside the front door”. Circular zone/regions aren’t good at making that distinction, so I incorporated my door sensor to make a conclusion:

  1. The moment I go from “Away” to “Home”, I set the input_select to “Outside”.
  2. If I’m “Outside” and my door opens, then I set the input_select to “Entering”.
  3. If I’m “Entering” and then my door closes, then I set the input_select to “Inside”.

Lot of assumptions there. It works 90% of the time, but there’s a bunch of edge cases (guests open the door, or I open door but close it without entering, etc). It gets complex FAST when trying to account for those edge cases.

AppDaemon looks like a much better way to go rather than emulating a state machine with input_selects.

BUT…

Given that it seems like a pretty common need, would it maybe be worth it to add a new event type into HA?

For example:

# configuration.yaml
# (this is a proposal, doesn't actually exist!)
automation:
   trigger:
     platform: event_sequence
     sequence:
     - entity_id: device_tracker.my_phone
       from: 'not_home'
       to: 'home'
     - entity_id: sensor.front_door
       from: 'CLOSED'
       to: 'OPEN'
     - entity_id: sensor.front_door
       from: 'OPEN'
       to: 'CLOSED'
   action:
   - service: homeassistant.turn_on
     entity_id: group.house_lights

I’m looking to try my hand at contributing to HA and thought introducing the concept of a “sequence” that can be defined using yaml might be valuable to most non-developer folks.

2 Likes

Hello everyone,
Anybody have a sample appdaemon app that does a similar thing?
Or an outline of how to do this.
I am imagining:

  • Once the tripwire(Motion sensor outside) trips set a variable to true and call a function to set it back to false in say 30 seconds

  • Door opens and do the same with another variable

  • Now when the internal motion sensor is triggered in 30 seconds switch on the light

  • Reset all the vars back.
    Any other suggestions?

Interestingly I just solved similar via multiple triggers with template conditions comparing status and last update time. (Eg: Gone to bed if late and downstairs vacant and landing sensor tiggered after hall…)
I need to give appdameon a go too…

would you mind sharing your code for this? I’m trying to understand how to use templates but struggling badly

No problem… if you can make sense of below…
basically:

  • trigger on restart or quiet zones
  • condition on quiet zones and upstairs ocuppied after downstairs
  • then action.

note: there is an exceptional condition (the last or) - basically vera does my pir updates, and they get stuck in a state. this workaround covers it until I get to move the alarm over to HA directly.

- alias: gone to bed
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: input_boolean.justrestarted
        to: 'off'
        from: 'on'
        for:
          minutes: 21
      - platform: state
        entity_id: input_select.alarmf1_occupied
        to: 'not_home'
        from: 'home'
        for:
          minutes: 5
      - platform: state
        entity_id: input_boolean.bedtime
        from: 'off'
        to: 'on'
    condition:
      condition: and
      conditions:
        - condition: state
          entity_id: input_boolean.disable_home_away
          state: 'off'
        - condition: state
          entity_id: input_boolean.gone_to_bed
          state: 'off'
        - condition: state
          entity_id: input_boolean.bedtime
          state: 'on'
        - condition: state
          entity_id: group.outside_livingroom_pir
          state: 'off'
          for:
            minutes: 5
            seconds: 5
        - condition: state
          entity_id: input_boolean.livingroom_occupied
          state: 'off'
        - condition: state
          entity_id: input_select.alarmf1_occupied
          state: 'not_home'
          for:
            minutes: 5
        - condition: template
          value_template: "{{ states.input_select.alarmf1_occupied.last_changed > states.input_select.alarmf0_occupied.last_changed }}"
        # exceptional condition
        - condition: or
          conditions:
            - condition: state
              entity_id: input_select.alarmf0_occupied
              state: 'not_home'
            # catch pirs not updated.
            - condition: template
              value_template: "{{ (as_timestamp(now()) - as_timestamp(states.input_select.alarmf0_occupied.last_changed )) > 600 }}"
    action:
      - service: script.gone_to_bed