Having lights turn to 50% a few seconds before actually turning off and other issues

Hello,
I have 2 separate timers, one for the actuall lights to turn off, and one that is 10 seconds shorter, which triggers lowering the brightness to 50%. The problem is, when there is movement again during those 10 seconds, the illuminance is checked again (which is of course higher now, because the lights are on). So the lights stay on, but at 50% until the timer is at 0 again.
Also, doesnt it work to have something like “time between sunset and 11pm”? Because it seems like it does not for me.

This is what I got so far:

alias: light_flur
description: ""
trigger:
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.light_timer_flur
    id: Timer Finished
  - platform: event
    event_type: timer.finished
    id: Timer Finished (off warning)
    event_data:
      entity_id: timer.light_timer_flur_off_warning
  - type: no_motion
    platform: device
    device_id: ffad842268cbc4cd42514bd0efc4c439
    entity_id: binary_sensor.0x54ef441000799684_occupancy
    domain: binary_sensor
    id: Motion Stopped
  - type: motion
    platform: device
    device_id: ffad842268cbc4cd42514bd0efc4c439
    entity_id: binary_sensor.0x54ef441000799684_occupancy
    domain: binary_sensor
    id: Motion Detected
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: Motion Stopped
        sequence:
          - service: timer.start
            data:
              duration: "60"
            target:
              entity_id: timer.light_timer_flur
          - service: timer.start
            data:
              duration: "50"
            target:
              entity_id: timer.light_timer_flur_off_warning
            enabled: true
      - conditions:
          - condition: trigger
            id: Motion Detected
          - condition: numeric_state
            entity_id: sensor.0x54ef441000799684_illuminance_lux
            below: 50
            enabled: true
          - condition: time
            after: "07:00:00"
            weekday:
              - mon
              - tue
              - sun
              - wed
              - thu
              - fri
              - sat
            before: "00:00:00"
          - condition: sun
            before: sunset
        sequence:
          - service: timer.cancel
            data: {}
            target:
              entity_id: timer.light_timer_flur
          - service: light.turn_on
            data:
              color_temp: 500
              brightness_pct: 20
              transition: 1
            target:
              device_id: 4a7b2a419a91f9ea580582e0a28e1806
      - conditions:
          - condition: trigger
            id: Timer Finished
        sequence:
          - service: light.turn_off
            data:
              transition: 10
            target:
              device_id: 4a7b2a419a91f9ea580582e0a28e1806
      - conditions:
          - condition: trigger
            id: Timer Finished (off warning)
            enabled: true
        sequence:
          - service: light.turn_on
            data:
              transition: 1
              brightness_step_pct: -50
            target:
              entity_id: light.0x0017880108585140
            enabled: true
mode: single




I’m not sure why you’d need to use 2 timers, I set my lights to come on based the motion sensor detection, which cycles them through some colours, before settling on a ‘final’ colour that I can set in a helper, I then use a wait _for_trigger to detect no motion, then turn the lights first to red, then to off.

I also trigger on illuminance changing, as I want the lights to come on if I’m already in there and the motion sensors have already detected me.

This sounds similar to what you are looking to do.

Critically, I use mode:restart to constantly refresh the motion sensor, but if the automation is triggered again because motion is detected at any time, it has an if condition to see if the lights are already on and therefore abort. This stops the initial colour cycling from repeating if the lights are already on, but constantly refreshes the wait on trigger timeout if someone is present in the room.

My automation is below, might give you some hints

alias: Kitchen - Motion Lights v2
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.kitchen_motion_sensor_group
  - platform: state
    entity_id:
      - sensor.lumi_lumi_motion_ac02_illuminance
condition: []
action:
  - if:
      - condition: numeric_state
        entity_id: sensor.lumi_lumi_motion_ac02_illuminance
        below: input_number.kitchen_illuminance_threshold
      - condition: state
        entity_id: light.ewelight_leds_kitchen_arch_forceonlight
        state: "off"
      - condition: state
        entity_id: binary_sensor.kitchen_motion_sensor_group
        state: "on"
    then:
      - service: script.colour_loop_lights
        data:
          counter: 5
          targetid: light.kitchen_light_group
          transition: 1
      - service: light.turn_on
        data:
          brightness_pct: 100
          rgb_color:
            - "{{states('input_text.kitchen_leds_final_colour').split(',')[0]}}"
            - "{{states('input_text.kitchen_leds_final_colour').split(',')[1]}}"
            - "{{states('input_text.kitchen_leds_final_colour').split(',')[2]}}"
          transition: 1
        target:
          entity_id: light.kitchen_light_group
  - wait_for_trigger:
      - platform: state
        entity_id:
          - binary_sensor.kitchen_motion_sensor_group
        from: "on"
        for:
          hours: 0
          minutes: 0
          seconds: 15
    timeout:
      hours: 0
      minutes: 0
      seconds: 16
      milliseconds: 0
    continue_on_timeout: true
  - if:
      - condition: not
        conditions:
          - condition: state
            entity_id: binary_sensor.occupancy_kitchen
            state: "on"
    then:
      - service: light.turn_on
        data:
          brightness_pct: 100
          rgb_color:
            - 255
            - 0
            - 0
          transition: 1
        target:
          entity_id: light.kitchen_light_group
      - service: light.turn_off
        data: {}
        target:
          entity_id: light.kitchen_light_group
mode: restart

Thank you for your answer!

Just out of curiosity, why are your lights cycling colors?
I am still pretty new to all of this, especially in text form. Maybe I should have been clearer,
I want the lights to be on when there is motion. But if I happen to not move for a bit longer, it should only dim down before going off, so I know that I need to move to keep the lights on (like for example in the Philips Hue app).

I don’t see how that it in your blueprint, or maybe I just don’t understand it yet.
The code I posted before was shortened a bit, I have several options included depending on what time of day it is.

I only colour cycle the lights for a bit of theatre as I enter the room.
Now I understand your use case a little more, it should be pretty straightfoward

Start your automation to be triggered by your motion sensor.
Turn on your lights
Use a wait_for_trigger to wait for motion to stop being detected. You should set a minimum duration that no motion should be detected, as I have in my automation:

- wait_for_trigger:
      - platform: state
        entity_id:
          - binary_sensor.kitchen_motion_sensor_group
        from: "on"
        for:
          hours: 0
          minutes: 0
          seconds: 15

The script will now sit here indefinitely until 15 seconds of no motion is detected. You can add a timeout for this, but I dont think you need it in your use case.

Set lights to 50%
create another simple wait for (say) 10 seconds.
Turn off the lights
Set the script mode to restart (the default is single). This means every time motion is not detected then detected again, you’ll keep triggering the script again, which will sit in the first 15 second wait period.

Thanks again for your answer.
I have tried this, with no luck so far (the light turns on but stays on):

description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.0x54ef441000799684_occupancy
condition: []
action:
  - if:
      - condition: state
        entity_id: binary_sensor.0x54ef441000799684_occupancy
        state: "on"
    then:
      - service: light.turn_on
        data:
          transition: 1
          brightness_pct: 100
        target:
          entity_id: light.0x0017880108585140
  - wait_for_trigger:
      - platform: state
        entity_id:
          - binary_sensor.0x54ef441000799684_occupancy
        from: "on"
        for:
          hours: 0
          minutes: 0
          seconds: 10
        to: "off"
    continue_on_timeout: true
  - service: light.turn_on
    data:
      transition: 1
      brightness_step_pct: -50
  - delay:
      hours: 0
      minutes: 0
      seconds: 10
      milliseconds: 0
  - service: light.turn_off
    data:
      transition: 1
    target:
      entity_id: light.0x0017880108585140
mode: restart

Alright I found a solution, but I don’t know why it works.

This has to be included during the “wait for trigger”:

 continue_on_timeout: true
    timeout:
      hours: 0
      minutes: 0
      seconds: 11
      milliseconds: 0

Can you tell me why? What does “continue on timeout” even mean?

Continue on timeout means that if the wait for trigger DOESN’T get fired within the time out period, then the script will just continue anyway.

For example, say you set a wait on trigger for no motion to be detected for 30 seconds.
You might create a continue on timeout of 1 minute.

That means regardless of whether or not the wait for trigger is fired, the script will move on after a minute anyway.

Ah, alright, thank you :slight_smile:

I see you also have an illuminance check in there. When I use that with the script I posted, there is a problem. It ALWAYS checks illuminance, so when the light is about to turn off, I can’t move sometimes to turn it back on, because it’s now too bright for the automation to trigger again. What could be done about that?