This gets asked a lot so here is an example for automating a light to come on with movement and go off after there is no movement for a short time.
trigger:
- platform: state
entity_id: binary_sensor.your_motion_sensor_here # change this
to: 'on'
- platform: state
entity_id: binary_sensor.your_motion_sensor_here # change this
to: 'off'
for:
minutes: 2 # adjust as necessary
action:
- service: "light.turn_{{ trigger.to_state.state }}"
target:
entity_id: light.your_light_here # change this
mode: single
Since not all motion sensor return to an ‘off’ state, you might end up with using a delay.
In that case, you need to put the automation in restart mode, so it will reset the delay when re-triggered.
f.e. my mqtt motion detector:
alias: Hallway Downstairs Light on Motion
description: Off when no motion for 3 mins
trigger:
- alias: When motion detected hallway downstairs Pir] was trigged by MQTT
platform: mqtt
topic: Tasmota/tele/RESULT
payload: D4A0EF
value_template: '{{ value_json.RfReceived.Data}}'
condition: []
action:
- service: light.turn_on
target:
entity_id: light.hallway_downstairs
- delay:
hours: 0
minutes: 3
seconds: 0
milliseconds: 0
- service: light.turn_off
data: {}
target:
entity_id: light.hallway_downstairs
mode: restart