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
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
- ...
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_booleandoes 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.
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?
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