# Motion Automations

I’ve moved my home automation logic from the ISY994i to Home Assistant but it took me a while to get the responses to activity outside the house right. What I want is for motion to trigger a response (i.e. lights come on) which is simple. Then I want the system to wait until the motion sensors have been off for a while before reverting the initial response. This turned out to be harder than I expected.

Edit: The scheme described below to split the logic into two automations proved messy and brittle though I’m not entirely sure why. After fiddling and asking around in the Discord channels, I switched back to using the original wait_for_trigger scheme below and it’s now working. A glitch in the matrix of some kind… TL;DR, use the initial scheme below, not the one later in this post.

My initial automations to respond to motion sensors going on looked like the one below. It’s triggered when the sensor goes on, remembers the current state of the lights, turns those lights on, waits for the sensor to be off for five minutes, then returns the lights to their initial state. The “wait” step didn’t work.

- alias: Front Motion
description: ''
trigger:
- platform: state
entity_id: binary_sensor.front_motion
from: 'off'
to: 'on'
condition: []
action:
- service: scene.create
data:
scene_id: before_front_motion
snapshot_entities:
- light.porch_dimmer
- light.driveway_dimmer
- type: turn_on
entity_id: light.porch_dimmer
domain: light
brightness_pct: 100
- type: turn_on
entity_id: light.driveway_dimmer
domain: light
brightness_pct: 100
- wait_for_trigger:
platform: state
entity_id: binary_sensor.front_motion
from: 'on'
to: 'off'
for: 00:05:00
- scene: scene.before_front_motion


I’ve not confirmed this but I suspect there is something different between the trigger for the automation and the wait_for_trigger action. This automation keeps getting stuck at that action.

I ended up breaking this into two automations that turn each other on and off. The first one starts like the original but where the wait was, it instead turns the second automation on and itself off.

- alias: Front Motion
description: ''
trigger:
- platform: state
entity_id: binary_sensor.front_motion
from: 'off'
to: 'on'
condition: []
action:
- service: scene.create
data:
scene_id: before_front_motion
snapshot_entities:
- light.porch_dimmer
- light.driveway_dimmer
- type: turn_on
entity_id: light.porch_dimmer
domain: light
brightness_pct: 100
- type: turn_on
entity_id: light.driveway_dimmer
domain: light
brightness_pct: 100
- service: automation.turn_on
data: {}
entity_id: automation.front_motion_after
- service: automation.turn_off
data: {}
entity_id: automation.front_motion


The second one triggers on the sensor being off for five minutes then reverts to the saved state, turns the first automation on and itself off.

- alias: Front Motion (After)
description: ''
trigger:
platform: state
entity_id: binary_sensor.front_motion
from: 'on'
to: 'off'
for: 00:05:00
condition: []
action:
- scene: scene.before_front_motion
- service: automation.turn_on
data: {}
entity_id: automation.front_motion
- service: automation.turn_off
data: {}
entity_id: automation.front_motion_after


This appears to be working as expected. <fingers-crossed> I’d love to figure out how to accomplish this with a single automation though.