Automation using two consecutive triggers

I’m trying to create an automation that follows this scenario:

  • Door opens (binary sensor)
  • Door closes
  • If during these two actions a light was on, turn it off.

I could just as well make a single trigger on “Door closes”, but I also want this scenario:

  • Door opens
  • If during this one action a light was off, turn it on.

That way, if the close would just trigger with the condition of a light being on, it would turn off again.

In other words:
Can I have two consecutive triggers that need to happen in some specific order for an automation to trigger?

You can use a binary sensor.

Trigger : door opens
Actions

  • set binary sensor to on
  • condition : light is off
  • turn light on

Trigger : door closes
Condition : binary sensor on
Actions :

  • binary sensor off
  • condition: light is on
  • turn light off

And if you want a maximum delay between open and close, you will add a third automation to reset the binary sensor:
trigger : binary sensor on for XXX minutes
action : binary sensor off

Alright, yea that looks like something I need, thanks.
But how can I create such a “dummy” binary sensor that is not really attached to any platform?

template sensors:

  - platform: template
    sensors:
      my_dummy_sensor:
        value_template: "{{ states.light.livingroom }}"

thats a template sensor that will mimic a light on/off.

See the post I made here that goes more in depth: Adding entries in top row

I fixed it by making a MQTT sensor and switch.

For future google visitors:

binary_sensor:   
  - platform: mqtt
    state_topic: "door"

switch:    
  - platform: mqtt
    name: door
    command_topic: "door"
    state_topic: "door"

automation:
  - alias: "Turn on light when coming home using door"
    trigger:
      - platform: state
        entity_id: binary_sensor.door_window_sensor_158d0001d85c26
        from: 'off'
        to: 'on'
    condition:
        condition: state
        entity_id: light.hallway
        state: 'off'
    action:
      - service: light.turn_on
        entity_id: light.hallway

  - alias: "Turn off the lights when leaving home using door"
    trigger:
      - platform: state
        entity_id: binary_sensor.door_window_sensor_158d0001d85c26
        from: 'on'
        to: 'off'
    condition:
      - condition: state
        entity_id: light.hallway
        state: 'on'
      - condition: state
        entity_id: binary_sensor.mqtt_binary_sensor
        state: 'on'
    action:
      - service: light.turn_off
        entity_id: light.hallway
      - service: switch.turn_off
        entity_id: switch.door

  - alias: "Turn on door-switch when opening the door without light"
    trigger:
      - platform: state
        entity_id: binary_sensor.door_window_sensor_158d0001d85c26
        from: 'off'
        to: 'on'
    condition:
      condition: state
      entity_id: light.hallway
      state: 'on'
    action:
      service: switch.turn_on
      entity_id: switch.door

  - alias: "turn off door-switch"
    trigger:
      platform: state
      entity_id: binary_sensor.mqtt_binary_sensor
      to: 'on'
      for: '00:00:30'
    action:
      service: switch.turn_off
      entity_id: switch.door

Sorry, I meant ‘input boolean’. :roll_eyes:
Any input_XXX acts just like a variable so you can use them to store your status. This way, you know if you’ve started an automation prior to the trigger or not.

Ah yes that is exactly what I was looking for. I was already confused that such a thing wasn’t a part of HASS and I had to do it using my MQTT work-around. Turns out I just didn’t know what to search for.

Thanks!

I’ve also seen a user that made a variable custom component, but I can’t find it now. (storing value across restarts &so)

From your automation, I guess you have the xiaomi sensors ? (158dXXXX) You have immediate notification of the open/close status ? I’m wondering, my kit is still in transit… :wink:

Even though you seem to have it solved already, an alternative, simplified, approach would be the to use the light.toggle service. Since you’re doing nothing else than turning on or off, toggeling the lights would already do what you want.
For more complex automations though the usage of the input_boolean as a helper of course is a good solution. Just wanted to point out another way for doing this. :slight_smile:

Ah right, I was looking for something like that too.

And yes, they are xiaomi sensors with immediate notifications :slight_smile:
I have this automation and a notify automation attached to it.

The thing is that there was a “complex” condition in place here for deciding if I was coming home or going out, but looking at 2 consecutive actions. So a simple toggle wouldn’t suffice :sweat_smile:

Variable custom component: https://github.com/rogro82/hass-variables

Ah great, this can come useful. It’s easier than input_XXX and has more functions.

@Michiel_Magendans, thanks for the information. Can’t wait to test this, but chinese new year seems to delay my order…