Turning on an automation if devices are triggered in a specific order

Hii,

I want to turn on a light if “detector 1” detects motion and then followed by “detector 2” detects motion.

I only want to turn on the light if the detectors detect in this order. I assume the solution will be simple enough, but I couldn’t figure out how to do it🙃.

For added functionality, it would be great if the light is turned only if detector 2 starts motion detection within 10 seconds of detector 1 starting motion detection. Hope I’ve made some sense here 🫠.

Any ideas.
Thanks!!

1 Like

You could have a look at the automation action Wait for Trigger in the documentation.

Hi, this is an example of such an automation with a “wait for trigger”.
If within 20secs after motion in the hall there is motion in the office, only then the light goes on. (in a dimmed state)

alias: "Office: Motion at night => Light on"
description: ""
trigger:
  - type: motion
    platform: device
    device_id: <device_id>
    entity_id: binary_sensor.hall_motion_occupancy
    domain: binary_sensor
condition:
  - condition: and
    conditions:
      - condition: or
        conditions:
          - condition: state
            entity_id: input_boolean.bedtime
            state: "on"
          - condition: state
            entity_id: input_boolean.sleep
            state: "on"
      - condition: state
        entity_id: light.office
        state: "off"
      - condition: state
        entity_id: sun.sun
        state: below_horizon
    enabled: true
action:
  - wait_for_trigger:
      - type: motion
        platform: device
        device_id: <device_id>
        entity_id: binary_sensor.office_motion_occupancy
        domain: binary_sensor
    continue_on_timeout: false
    timeout:
      hours: 0
      minutes: 0
      seconds: 20
      milliseconds: 0
  - service: light.turn_on
    data:
      brightness_pct: 20
    target:
      entity_id: light.office
  - delay:
      hours: 0
      minutes: 3
      seconds: 0
      milliseconds: 0
  - service: light.turn_off
    data: {}
    target:
      entity_id: light.office
mode: single

1 Like

Does your automation work as intended? This is exactly what I want to do too.

Can you explain how the wait for trigger works.
So I made an automation using your code, and for the action I selected wait for trigger of the second motion sensor. Then I also added the turn on light in the action.

But how can this work? It waits for a trigger as a action and while also turning the light on…

Cant seem to find it. The resource I did find however is very brief

If during the “wait_for_trigger” timeframe the second motion sensor detects motion, the automation continues with the next action.

The actions in the actions section of an automation are carried out in sequence. So, if you have a Wait for Trigger action followed by a Call Service (turn on light) action then, the light will not be turned on until the Wait for Trigger action has received it’s trigger event.

There are also options to specify what to do if the trigger doesn’t occur in a given time period.

OK got it. Thanks
The sequential is whete I was confused as usually the actions are run all at once.

So what is the timeout. Is it how long the wait for trigger waits for until the 2nd motion sensor triggers. And if the time elapses, then the automation stops?
Amd also what is the “continue in timeout” checkbox

They may look like they all run at once because, in most cases, the actions being performed are almost instantanious but, they actually always run in sequence.

Firstly, if no timeout time is set then HA will wait forever for the trigger event to occur. The subsequent actions in the actions section may never get executed.

If a timeout time is specified then HA will do somrthing when either the trigger event being awaited occurs or the timeout time elapses.

Then, either:

If the trigger event being awaited occurs before the timeout time elapses then the next action in the sequesnce of acions will be executed.

If the timout elapses before the trigger event awaited occurs then what happens depends on the setting of the Continue on Timeout toggle.

If the toggle is off (so don’t continue) then the sequence of actions will stop - no more actions in the sequence will be executed.

If, on the other hand, the Continue on Timeout toggle in on then processing will continue through the remaining actions in the sequence.

Also, if HA is reastarted or the automation is reloaded then the automation will be aborted. This will most likely mean that the automation will stop waiting for a trigger and stop entirely.

1 Like

Thanks a lot!
You explained it really well.
It works exactly as stated and my automations work as intended now. :slightly_smiling_face:

1 Like

@Mari143: please take the time to mark the answer as solution, you do that by selecting the three dots under the post:

image

Then select the check box:

image

By doing so this can be useful to other users as well and prevents that someone else steps in to try to help you.

I dont see any check box when I click the 3 dots.

I think it requires a certain trust level for users to mark something down as a solution. And I havent reached there yet🙃

1 Like

No, I think it’s because you didn’t put your question in any category.
You can still change this by editing your first post (the title) and then mark as solved.

1 Like