How to set a trigger which only fires if event 2 occurs within X seconds of event 1?

I am trying to setup an automation where I have an event (Motion detection at front door)
which can then either be followed by a knock on the door (vibration sensor) or no knock.

I’m trying to figure out how I could trigger an automation for both situation.
i.e.
Automation 1 = Event Motion followed within 10secs by door vibration
Autiomation 2 = Event Motion followed within 10secs by NO door vibration

Thanks

trigger:
  platform: state 
  entity_id: sensor.front_door_motion
  to: 'on' 
action:
  - wait_template: "{{ is_state('sensor.vibration' , 'on') }}"
    timeout:
      seconds: 10
    continue_on_timeout: false
  - YOUR ACTIONS 
2 Likes

Great! thanks for quick reply :slight_smile: :slight_smile: :slight_smile:

There are a couple of changes coming (hopefully) soon that will make this very easy. It would look like this:

trigger:
- platform: state
  entity_id: sensor.front_door_motion
  to: 'on'
action:
- wait_for_trigger:
  - platform: state
    entity_id: sensor.vibration
    to: 'on'
  timeout: 10
- choose:
  - conditions:
    - condition: template
      value_template: "{{ wait.trigger is none }}"
    sequence:
    # Actions when vibration did NOT happen within 10 secs
    - ...
  default:
  # Actions when vibration DID happen within 10 secs
  - ...

For more details see https://github.com/home-assistant/core/pull/38075.

And, FWIW, the wait_template action will also create/update a variable named wait. See https://github.com/home-assistant/core/pull/38634.

I would assume these features will be in the 0.115 release.

1 Like

Hey @pnbruckner , thanks for the tip!
I’ll keep my eye open for that as it’s a lot more neat than what I came up with, which required @anon43302295’s solution for the Scenario 1 and the 2 automations (see below) for Scenario 2.
It works but it’s clunky…

- alias: Not a door knock helper
  trigger:
  - entity_id: binary_sensor.front_door_vibration
    platform: state 
    to: 'on' 
  action:
  # set the vibration state
  - service: input_boolean.turn_on
    data:
      entity_id: input_boolean.front_door_vibration_state
  
- alias: Not a door knock
  description: ''
  trigger:
  - entity_id: binary_sensor.front_ring_motion
    platform: state 
    to: 'on' 
  action:
  # clear the vibration state
  - service: input_boolean.turn_off
    data:
      entity_id: input_boolean.front_door_vibration_state
  - delay: 00:00:10
  - service_template: >
      {% if is_state('input_boolean.front_door_vibration_state', 'off') %}
         script.someone_is_lurking
      {% endif %}
  mode: single

If you’re using a service_template, then it should always generate a valid service, no matter what the conditions are. In your case, if the input_boolean does change to 'on', the automation will cause an error because it will try to call the service "" (i.e., an empty string), which will never exist.

Better to use a condition action:

  action:
  # clear the vibration state
  - service: input_boolean.turn_off
    data:
      entity_id: input_boolean.front_door_vibration_state
  - delay: 00:00:10
  - condition: state
    entity_id: input_boolean.front_door_vibration_state
    state: 'off'
  - service: script.someone_is_lurking

Also I would suggest changing the mode of the automation to restart, which will better handle the case if binary_sensor.front_ring_motion changes to 'on' again within the 10 second delay.

1 Like

hi there @pnbruckner, I’m trying to do something similair - turn on my smart switch (lamp) when I turn ON then OFF my shelly light switch within a 2 second space… would you know how I can substitute this code to do this, or what trigger setup I use?

Sorry, I’ve been kind of busy.

Since you did not provide details (like entity IDs), I’ll make an assumption that both the smart lamp switch and the shelly light switch correspond to HA switch entities. In that case, maybe something like this:

automation:
  - alias: Turn on smart lamp when shelly switch toggled on & off quickly
    id: shelly_to_smart_lamp
    mode: queued
    trigger:
      - platform: state
        entity_id: switch.shelly
        from: "off"
        to: "on"
    action:
      - wait_template: "{{ is_state('switch.shelly', 'off') }}"
        timeout: 2
      - condition: "{{ wait.completed }}"
      - service: switch.turn_on
        target:
          entity_id: switch.smart_lamp

Oh yes, you’re right. And yes, that resolved it! Thanks @pnbruckner

1 Like

Hi all, for others, this is the code I’ve used to get it working:

alias: switch on/off blinds toggle
description: ""
trigger:
  - platform: state
    entity_id:
      - switch.shelly_shsw_1_58bf25d86a80
    to: "on"
condition: []
action:
  - wait_for_trigger:
      - platform: state
        entity_id:
          - switch.shelly_shsw_1_e89f6d86802e
        to: "off"
        from: "on"
    timeout:
      hours: 0
      minutes: 0
      seconds: 2
      milliseconds: 0
    continue_on_timeout: false
  - service: cover.toggle
    data: {}
    target:
      entity_id: cover.blockoutblinds
mode: single