Brighten front porch lights, then return to recorded dim level

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
17 Likes

This is something I’ve been working on too - my desired intention:

  1. Turn light on dim at dusk
  2. Turn the light off at 10pm
  3. Go full brightness for x minutes when motion detected
  4. Return to the previous level after motion detected
1 Like

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

1 Like

Hello,

idea seems to be awesome!

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.

automation:
  - alias: "settemp"
    trigger:
      platform: event
      event_type: homeassistant_start
    action:
      service: script.record_radiator_livingroom

script:
  record_radiator_livingroom:
    alias: Record Livingroom Radiator Temperature to slider
    sequence:
      - service: input_slider.select_value
        data_template:
          entity_id: input_slider.livingroom_radiator
          value: '{{ states.climate.danfoss_z_thermostat_014g0013_heating_1_8_1.attributes.temperature }}'

unfortunately this does not work. If I set temperature manually, like

value: 21

slider gets it after restart. If I’m trying to grab temperature from current thermostat setting after HA restart I’ve got 0 there.

Do you have any suggestions what I could try?

What part’s not working? I’m not clear what you’re going for. The only time that your automation will fire is when you start HA.

In my example, motion sets the script to run.

Also - it may be that at startup that the thermostat temperature is not available yet. You might need to wait for that to be valued.

value: '{{ states.climate.danfoss_z_thermostat_014g0013_heating_1_8_1.attributes.temperature }}'

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.

Hi everyone,
Am I missing something or setting light.turn_on with a brightness of zero will not turning off the light.

- service: light.turn_on
  data:
    entity_id: light.office
    brightness: 0

will set the light with a brightness of 1 on HA v0.46.0 with philips hue bulds
Any ideas ?

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) }}'

It appears the behavior of light.turn_on with brightness = 0 has changed. I’m on 0.47.1 and it doesn’t turn off lights connected via Wink or Hue.

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…

Under .47, it looks like Zwave is still behaving the same. Brightness=0 turns it off.

@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?

Just add a condition to the automation. Where the “only at night” part is, add something that’s essentially “only if the light is off”.

1 Like

@ih8gates I’m a little confused as to what the does. I have a log error that states it can’t find this. Should I remove it?

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.

1 Like

@ih8gates did you ever build this part out? Where would I add this piece as I have a similar use case. Thanks again for all your help!

I never did. You could just create a boolean and then turn it on/off with the same actions that adjust the lights. Then address it with conditions.

But I didn’t really have a use-case for that. Since I’m returning it to its previous state, I didn’t really need to know how it’d been turned on.

@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?