Automation Logic: Motion Lights w/ dim before turning off

I have some Philips Hue strip lights above my kitchen cabinets that get triggered from 2 different motion sensors, one is a WeMo sensor and the other a Philips. I have no problems with this portion of my automation.
If it is between the hours of 0645 and 2230, then the lights come up to 100%, and between 2230 and 0645 they come up to 50%.
After being illuminated for 4 minutes and 30 seconds, they drop down to 50%, and then after another 30 seconds turn off.

My logic is failing me when the lights have been on for 4:30 without motion, and drop to the 50%. If there is motion during the 30 seconds before they turn off, the lights will return to 100% but also still turn off at 30 seconds (because the off script technically triggers at 4:30).

How do I need to change my logic, or maybe other solutions to reset the automation timer?

Thank you in advance!

automation:

######                                         ######
##         Kitchen Over Cabinet On Full            ##
######                                         ######
- alias: Kitchen Over Cabinet On Daytime
  trigger:
    - platform: state
      entity_id: binary_sensor.kitchen_motion
      to: 'on'
    - platform: state
      entity_id: binary_sensor.dining_room_motion
      to: 'on'
  condition:
    condition: time
    after: '06:45:00'
    before: '22:30:00'
  action:
    service: scene.turn_on
    entity_id: scene.kitchen_over_cabinet_full

######                                         ######
##         Kitchen Over Cabinet On Half            ##
######                                         ######
- alias: Kitchen Over Cabinet On Nighttime
  trigger:
    - platform: state
      entity_id: binary_sensor.kitchen_motion
      to: 'on'
    - platform: state
      entity_id: binary_sensor.dining_room_motion
      to: 'on'
  condition:
    condition: time
    after: '22:30:00'
    before: '06:45:00'
  action:
    service: scene.turn_on
    entity_id: scene.kitchen_over_cabinet_half

######                                         ######
##          Kitchen Over Cabinet Off               ##
######                                         ######
- alias: Kitchen Over Cabinet Off
  trigger:
    - platform: state
      entity_id: binary_sensor.kitchen_motion
      to: 'off'
      for:
        minutes: 4
        seconds: 30
    - platform: state
      entity_id: binary_sensor.dining_room_motion
      to: 'off'
      for:
        minutes: 4
        seconds: 30
  action:
    - service: scene.turn_on
      entity_id: scene.kitchen_over_cabinet_half
    - delay: 00:00:30
    - service: scene.turn_on
      entity_id: scene.kitchen_over_cabinet_off

scenes:

- name: Kitchen Over Cabinet Full
  entities:
    light.over_cabinet_1:
      state: on
      brightness: 255
- name: Kitchen Over Cabinet Half
  entities:
    light.over_cabinet_1:
      state: on
      brightness: 127
- name: Kitchen Over Cabinet Off
  entities:
    light.over_cabinet_1:
      state: off

EDIT: fixed preformatted text

Seems you will need to change the action with the 30 second delay into a script that you call instead.

Then when you sense motion turn off that script to cancel the delay using - service: script.turn_off

Just cancel the script every time you sense motion no matter what, if it isn’t running just nothing will happen and if that script is running then it will cancel that script and the lights won’t turn off.

edit: or maybe just use automation.turn_off (though I have no experience with this) instead of changing the action to a script.

1 Like

Thank you for the direction @stunts1337!

I have not gotten into scripts yet, so I will go visit those pages, and research a little more on automation.turn_off.

See my edit if you don’t want to get into scripts just yet, but I do suggest you check them out. Pretty useful stuff, no harder to use than a scene really.

You may use the “transition” attribute with Hue lights. Provide a time (in seconds) to go from current brightness to target brightness.

I tried both suggestions: automation.turn_off and creating a script to call as well as script.turn_off.
The best solution seems to be creating a script that can be called and turned off if motion is detected within those final 30 seconds.

@m0wlheld, utilizing the transition attribute could also be a solution, but I would prefer not to have a 30 second fade out. In this scenario, I prefer the stepped dimming.

my new automation:

######                                         ######
##         Kitchen Over Cabinet On Full            ##
######                                         ######
- alias: Kitchen Over Cabinet On Daytime
  trigger:
    - platform: state
      entity_id: binary_sensor.kitchen_motion
      to: 'on'
    - platform: state
      entity_id: binary_sensor.dining_room_motion
      to: 'on'
  condition:
    condition: time
    after: '06:45:00'
    before: '22:30:00'
  action:
    - service: scene.turn_on
      entity_id: scene.kitchen_over_cabinet_full
    - service: script.turn_off
      entity_id: script.kitchen_over_cabinet_off_stepped

######                                         ######
##         Kitchen Over Cabinet On Half            ##
######                                         ######
- alias: Kitchen Over Cabinet On Nighttime
  trigger:
    - platform: state
      entity_id: binary_sensor.kitchen_motion
      to: 'on'
    - platform: state
      entity_id: binary_sensor.dining_room_motion
      to: 'on'
  condition:
    condition: time
    after: '22:30:00'
    before: '06:45:00'
  action:
    - service: scene.turn_on
      entity_id: scene.kitchen_over_cabinet_half
    - service: script.turn_off
      entity_id: script.kitchen_over_cabinet_off_stepped

######                                         ######
##          Kitchen Over Cabinet Off               ##
######                                         ######
- alias: Kitchen Over Cabinet Off
  trigger:
    - platform: state
      entity_id: binary_sensor.kitchen_motion
      to: 'off'
      for:
        minutes: 4
        seconds: 30
    - platform: state
      entity_id: binary_sensor.dining_room_motion
      to: 'off'
      for:
        minutes: 4
        seconds: 30
  action:
    service: script.kitchen_over_cabinet_off_stepped

and my new script:

kitchen_over_cabinet_off_stepped:
  sequence:
    - service: scene.turn_on
      entity_id: scene.kitchen_over_cabinet_half
    - delay: 00:00:30
    - service: scene.turn_on
      entity_id: scene.kitchen_over_cabinet_off

When I attempted

- service: automation.turn_off
  entity_id: kitchen_over_cabinet_off

I would still have to turn the automation back on, otherwise the lights would never turn off, so I wrote:

- service: automation.turn_off
  entity_id: kitchen_over_cabinet_off
- service: scene.turn_on
  entity_id: scene.kitchen_over_cabinet_full
- service: automation.turn_on
  entity_id: kitchen_over_cabinet_off

The problem with this was that the quick off/on of the automation did not seem to reset the automation timer because the lights still turned off after 30 seconds instead of resetting with new motion.