How can I trigger an automation based on which of two binary sensors triggers first?

Hello,
I need to run an automation based on which one of my two binary sensors has triggered (went to “ON”) first.
How can I do this?

Thanks in advance
Aaron

1 Like

If you create an automation with a State Trigger, monitoring the two binary_sensors, it will be triggered by whichever of the binary_sensors is first to change to on.

alias: example
trigger:
  - platform: state
    entity_id:
      - binary_sensor.kitchen
      - binary_sensor.hallway
    from: 'off'
    to: 'on'
condition: []
action:
  ... your actions go here ...
1 Like

Thanks @123, but I need to have some kind of condition in which order the sensors triggered
(If sensor1 triggers and then sensor2, then do something different than if it’s the other way round)

Best Regards
Aaron

How about this? Use each sensor as a trigger in the automation, and then (assuming you’re using the UI) click on the three dots and select “Edit ID”. This produces a new field in the trigger, and you can specify it any way you want. (I frequently name them as what I’m supposed to do in the Action section, like “turn off toggle” or whatever.)

Then, when you get down to the action section, select “choose”. For the first section, choose “triggered by” and pick the trigger you want from the drop down, then fill out the rest. Add a section option to your “choose” (the top will now say “Choose from two options”) and add “triggered by” and your second action. Smart Home Junkie on YouTube has a very clear video on how to do this. It’s called “How to use Trigger IDs”.

I hope this is what you’re looking for!

There’s a few ways to accomplish this, but here’s an example using the “choose” functionality:

alias: "Which sensor first"
trigger:
  - platform: state
    entity_id:
      - binary_sensor.sensor1
      - binary_sensor.sensor2
    to: 'on'
condition: []
action:
  - choose:
    - conditions:
      - condition: state
        entity_id: binary_sensor.sensor1
        state: 'on'
      - condition: state
        entity_id: binary_sensor.sensor1
        state: 'off'
      sequence:
      - service: script.do_action_1
  - choose:
    - conditions:
      - condition: state
        entity_id: binary_sensor.sensor1
        state: 'off'
      - condition: state
        entity_id: binary_sensor.sensor1
        state: 'on'
      sequence:
      - service: script.do_action_2

So the script do_action_1 will only execute when sensor 1 turns on if sensor 2 is off (i.e., only when sensor 1 was first to trigger) and do_action_2 will only execute when sensor 2 turns on if sensor 1 is off. No action will happen if both are on, though you could add that.

It seems what you want is that when one trigger fires, you only want to execute some action when the second one triggers. If so, trigger on either and wait on the other using a wait for trigger. You’ll need to label your two triggers with IDs or access the trigger.entity_id variable in a choose. If we know your sensors and there’s a naming convention you’ve used, it can probably be simplified even more.

So it’s not merely which binary sensor activates first, it’s the order of activation of both binary sensors.

You’ll need to do what parautenbach has suggested; employ wait_for_trigger in the automation’s action.