I have a blueprint for switchting off an entity after selected value of minutes, but not so really happy with the solution.
Do you see how to improve the blueprint ?
Use case: The light should be switched off after defined time since last detected motion (since motion sensor swichted from “on” to “off”), but also, the light should be switched off after some time, if the light was switched on manualy, and therfore the motion sensor has not detected motion.
blueprint:
name: Entity (light/switch) OFF after n minutes after last motion detected
domain: automation
input:
motion_entity_group:
name: Motion Sensor Group
selector:
entity:
domain: group
light_target:
name: Entity to be switched
description: The light, switch, scene to turn on (or script to run) when the automation is triggered.
selector:
entity:
no_motion_wait:
name: Wait time
description: Time to leave the light on after last motion is detected.
default: 3
selector:
number:
min: 0
max: 60
step: 1
unit_of_measurement: minutes
no_motion_wait_at_night:
name: Wait time at night
description: Time to leave the light on after last motion is detected at night.
default: 1
selector:
number:
min: 0
max: 20
step: 1
unit_of_measurement: minutes
mode: restart
max_exceeded: silent
#variables:
# wait_duration_in_minutes: !input no_motion_wait
# wait_duration_in_seconds: '{{ wait_duration_in_minutes * 60}}'
trigger:
- platform: state
entity_id: !input 'light_target'
to: 'on'
from: 'off'
condition: []
action:
- choose:
- conditions:
- condition: state
entity_id: !input motion_entity_group
state: 'on'
sequence:
- choose:
- conditions:
- condition: time
after: '00:00'
before: '06:00'
sequence:
- wait_for_trigger:
- platform: state
entity_id: !input motion_entity_group
from: 'on'
to: 'off'
for:
minutes: !input no_motion_wait_at_night
default:
- wait_for_trigger:
- platform: state
entity_id: !input motion_entity_group
from: 'on'
to: 'off'
for:
minutes: !input no_motion_wait
default:
- choose:
- conditions:
- condition: time
after: '00:00'
before: '06:00'
sequence:
- wait_for_trigger:
- platform: state
entity_id: !input motion_entity_group
from: 'off'
to: 'on'
timeout:
minutes: !input no_motion_wait_at_night
default:
- wait_for_trigger:
- platform: state
entity_id: !input motion_entity_group
from: 'off'
to: 'on'
timeout:
minutes: !input no_motion_wait
#if motion detected, we have to wait again
- choose:
- conditions:
- condition: state
entity_id: !input motion_entity_group
state: 'on'
sequence:
- choose:
- conditions:
- condition: time
after: '00:00'
before: '06:00'
sequence:
- wait_for_trigger:
- platform: state
entity_id: !input motion_entity_group
from: 'on'
to: 'off'
for:
minutes: !input no_motion_wait_at_night
default:
- wait_for_trigger:
- platform: state
entity_id: !input motion_entity_group
from: 'on'
to: 'off'
for:
minutes: !input no_motion_wait
default: []
- delay: '00:00:01'
- service: homeassistant.turn_off
data: {}
entity_id: !input 'light_target'