There have been a couple conversations about remembering the dim level & on-off state of a light so that you can return to it after changing it. Here’s something I cooked up that’s been working for a couple of days.
Behavior:
I generally keep my front porch lights at about 50% brightness. I set up some code so that when motion is sensed on my front porch, I brighten the lights to 100%, then return them to their previous dim level after 5 minutes. Or turn back off if they were off when the motion occurred.
This needs some finessing (like, don’t turn off the lights if someone’s still on the porch), but I haven’t built that out yet. In fact, my front porch motion is a bit different in that it’s triggered by my Arlo camera via IFTTT. I’ll leave it to you to pick the good parts of this and adapt to your needs.
The technique I came up with was to create an input_slider that is used to store the state of the light. Zero means “off”, otherwise it’s a brightness level. I’m exploiting the fact that setting light.turn_on to a brightness of zero acts like a light.turn_off. So I imagine that could break at some point. (I was trying to do this with a service template, but then the data needed to change (and you can’t pass a brightness to light.turn_off as it errors)),
Here we go:
input_slider:
porch_brightness:
name: Front Porch Brightness
initial: 25
min: 0
max: 255
step: 1
script:
record_front_porch:
alias: Record front porch status to slider
sequence:
# don't reset brightness if we've already got a timer waiting to reset front porch
- condition: state
entity_id: script.return_front_porch_delayed
state: 'off'
- service: input_slider.select_value
data_template:
entity_id: input_slider.porch_brightness
value: '{% if states.light.front_porch_light_level_32_0.state == "off" %}0{% else %}{{states.light.front_porch_light_level_32_0.attributes.brightness}}{% endif %}'
return_front_porch:
alias: Return front porch to recorded value
sequence:
- service: light.turn_on
entity_id: light.front_porch_light_level_32_0
data_template:
brightness: '{{states.input_slider.porch_brightness.state | int}}'
- service: input_boolean.turn_off
entity_id: input_boolean.front_porch_motion_light_active
return_front_porch_delayed:
alias: Wait 5 min and then return front porch to recorded value
sequence:
- delay:
minutes: 5
- service: script.return_front_porch
automation:
- alias: Motion ON front porch
trigger:
# if motion is ON - this is triggered via IFTTT and Arlo
- platform: state
entity_id: input_boolean.motion_front_porch
to: 'on'
from: 'off'
action:
# some notifications
- service: notify.scott_notifier
data:
message: "Motion front porch"
title: "Front Porch"
data:
priority: 0
- service: notify.kodi
data:
message: "Motion front porch"
title: "Front Porch"
# only at night
- condition: state
entity_id: sun.sun
state: 'below_horizon'
#remember the state of front porch
- service: script.record_front_porch
# turn the light on
- service: light.turn_on
entity_id: light.front_porch_light_level_32_0
data:
brightness: 255
# stop delay script if it's already running
- service: script.turn_off
entity_id: script.return_front_porch_delayed
# script with a delay that'll reset light in 5 min
- service: script.return_front_porch_delayed