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
Right. That’s exactly what this does. The automations for 1 & 2 are simple (and not listed here).
This code should point you in the right direction. You’ll need to change the exact automation conditions to work with your motion sensor. So on motion, you’d do everything that’s in my automation except the last step (don’t call script.return_front_porch_delayed). Instead, have a separate automation that triggers when motion stops and call script.return_front_porch. You’d have no need for return_front_porch_delayed
I have Danfoss radiator thermostats and I was trying to do something like “if nobody home, decrease temperature to x”. At first stage I was trying to record current temperature to slider. I think I’ve done everything what you’ve suggested - I’ve created slider and script which should record current radiator setting.
above part is not working. My previous post does not contain code for a whole thing just yet … Idea is to store temp value in slider at startup (and it will get updated each time I change temperature setting by hand) and if everyone will leave the house decrease radiator temperature by 4’C, then if anyone get back home (actually 1km from home) restore temperature to value from slider.
Availability of the temperature at startup might be the case. I will try to delay it or just show current temperature elsewhere to make sure that this part is working.
If you “remember” it every time you change it, then when it decreases by 4C, it’d remember that. You’ll need to be careful about what trigger you tie the “remember” script to.
I’ve been using this code for a while and haven’t run into any issues. Could it be particular to Zwave? (Or could the behavior have changed since I started using the code…?)
I thinked first that it was due to phue.py library, but I had the same behavior with yeelight bulbs.
The light.turn_on function was probably updated. Too bad, this trick was useful.
Here was my approach to brighten stairway light then return to previous state :
##############################
- alias: Light Stairway Bright
trigger:
platform: state
entity_id: binary_sensor.motion_sensor_158d000130d5ed
from: 'off'
to: 'on'
action:
- service: light.turn_on
entity_id: light.stairway
data_template:
brightness: '{{ ((states.light.stairway.attributes.brightness or 0) + 150) | min(255) }}'
##############################
- alias: Light Stairway Back
trigger:
platform: state
entity_id: binary_sensor.motion_sensor_158d000130d5ed
from: 'on'
to: 'off'
action:
- service: light.turn_on
entity_id: light.stairway
data_template:
brightness: '{{ ((states.light.stairway.attributes.brightness or 0) - 150) | max(0) }}'
That’s unfortunate. I just ran a test and brightness-0 still performs an “off” for zwave lights. But (lazy), I’m still at .45. I’ll make an image of my card and upgrade and check things out at .47…
@ih8gates How would I adapt this to not change the brightness of a dimmer if it happens to already be on? My use case is the dining room chandelier where I want it to turn on dimply (30%) when someone triggers a motion/door sensor for 5-10 min and then turn off when no motion is detected. However, I don’t want it to adjust the level to 30% in the event that I’m having dinner in the room and the light is already on. Make sense?
Yup. Sorry - I had been experimenting with a check to see if the light had been changed by this motion sensing, or if it had just been turned on because of time-of-day, etc. So it’s not necessary here.
@ih8gates did you upgrade to the newest version 0.51.2? I’m seeing that your script appears to no longer save the last light level anymore. Can you confirm?