Gradual Light Brightness % over Time

Trying to create a script to adjust brightness of selected Light(s) over a certain time. I’ve re-created this script multiple times and now it’s to the point I want to throw out Home Assistant and go back to Smartthings.

I’m trying to create a wake up routine that will brighten my lights and increase my Bose Soundtouch volume over a certain amount of time. Currently, I am writing the script for the lights. It will also be used by other automations so I’m trying to make it not as rigid.

For those about to say, “Use the Light:Turn On Service”, it stopped working. Transition doesn’t work anymore. If I set it to Transition of 10 seconds, with a brightness of 100%, and activate it. It goes straight to 100% in under a second, Every Time. And no, I didn’t go in and mess with the coding for the service. It stopped working a while back for some unknown reason thus, I keep waking up to my lights turning on to 100% the moment my alarm goes off. Thus, why I’m ready to chuck the whole thing out the window.

Example of the code is to Call Service
Select Lights, set initial brightness, set final brightness, set duration (transition).

alias: Gradual Light Brightness Increase
fields:
  target_lights:
    name: Target lights
    description: Target lights to have brightness adjusted
    required: true
    example: light.ceiling_fan
    selector:
      entity:
        domain: light
        multiple: true
  initial_brightness:
    name: Initial Brightness
    description: Initial brightness percentage
    required: true
    default: 10
    example: "10"
    selector:
      number:
        max: 100
        min: 0
        step: 1
        mode: slider
  final_brightness:
    name: Target Brightness
    description: Target brightness
    required: true
    default: 100
    example: "100"
    selector:
      number:
        max: 100
        min: 0
        step: 1
        mode: slider
  duration:
    name: Fade duration
    description: Length of time in seconds the fade should take.
    required: true
    default: 5
    example: "5"
    selector:
      number:
        mode: box
        min: 0
        max: 100000
        unit_of_measurement: s
  delay:
    name: Delay
    description: Delay between adjustments.
    required: true
    default: 2
    example: "2"
    selector:
      number:
        mode: box
        min: 0
        max: 10
variables:
  initial_time: "{{ now() }}" #Time script starts
  final_time: "{{ initial_time + timedelta(seconds=duration) }}" #Time script should stop by
  
  brightness_range: "{{ final_brightness - initial_brightness }}"
  brightness_increment: "{{ brightness_range / duration }}"
  
  duration: "{{ duration | int }}"
  delay: "{{ delay | int }}"
  delay_time: 00:00:{{ delay }} #formatting
  duration_left: "{{ duration }}" #Duration left set = duration initially

sequence:
  - service: light.turn_on #Set lights to initial brightness
    data_template:
      entity_id: "{{ target_lights }}"
      brightness_pct: "{{ initial_brightness }}"
  
  - repeat:
      until: "{{ ( duration_left <= 0 ) or ( now() >= final_time ) }}"
      sequence:
        - delay: "{{ delay_time }}"
        
        - variables: #Calculate brightness setpoint for this iteration
            current_duration: "{{ loop.index0 * delay }}"
            current_brightness: "{{ initial_brightness + brightness_increment * current_duration | round(0) }}"
        
        - service: light.turn_on
          data_template:
            entity_id: "{{ target_lights }}"
            brightness_pct: "{{ current_brightness }}"
        
        - variables: #calculate time left
            duration_left: "{{ duration_left - delay }}"

I got it to set the Initial light brightness before, but after that it would give me an issue about the loop being undefined even though it was and there was no syntax issues I could find. I’ve tinkered and messed with this too much that I’m at my wits end.

This was the latest error code I saw when trying to get this thing to work.

1 Like

Seems there is a Blueprint specially made for this, which seems to work great for many people, hope you can find it.

The bulb has to support the transition effect natively. In other words, Home Assistant’s transition option uses the bulb’s native ability to perform the effect. If the bulb’s firmware doesn’t support the effect (or the its integration doesn’t implement it), the light simply turns on/off instantly.

FWIW, there are existing scripts, Blueprints, and a python script that implement the transition effect in software (i.e. like what you’re trying to achieve).

https://community.home-assistant.io/search?q=Light%20fader%20