Increase or decrease light brightness over time

This script increases or decreases the brightness of a light over time.

if your light supports transitions, you may not need this, use: light.turn_on with transition and brightness_step

This script increases or decreases the brightness of a light over time.

Given the transition time, initial brightness, target brightness and brightness step, the script will calculate the number of steps required to reach the target brightness and the delay between each step.

The script will then turn on the light with the initial brightness, then start increasing or decreasing the brightness by the brightness step every delay between steps until the target brightness is reached.

blueprint:
  name: Increase or decrease light brightness over time
  description: |
    # Increase or decrease light brightness over time

    This script increases or decreases the brightness of a light over time. 
    Given the **transition time**, **initial brightness**, **target brightness** and **brightness step**, the script will calculate the number of steps required to reach the target brightness and the delay between each step. 
    The script will then turn on the light with the initial brightness, then start increasing or decreasing the brightness by the brightness step every delay between steps until the target brightness is reached.   
    
    ```text
    Example: (increase)
                          100% β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                               β”‚                                                                                            β”‚
                               β”‚                                                                                            β”‚
                               β”‚                                                                                          β”Œ ◄─ target brightness
                               β”‚                                                                                          β”‚ β”‚
                               β”‚                                                                                          β”‚ β”‚
                               β”‚                                                                         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ 
                               β”‚                                                                         β”‚                  β”‚
                               β”‚                                                                         β”‚                  β”‚
                               β”‚                                                       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                  β”‚
                               β”‚                     calculated delay                  β”‚                                    β”‚
                               β”‚                            β”‚                          β”‚                                    β”‚
                               β”‚                            β”‚        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                                    β”‚
                               β”‚                            β”‚        β”‚                                                      β”‚
                               β”‚                            β–Ό        β”‚                                                      β”‚
                               β”‚                   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                                                      β”‚
                               β”‚                   β”‚ ◄──────────── brightness step                                          β”‚
                               β”‚                   β”‚                                                                        β”‚
           initial brightness β”€β”€β–Ίβ”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                                                                        β”‚
                               β”‚ β”‚                                                                                          β”‚
                               β”‚ β”‚                                                                                          β”‚
                            0% β””β”€β”΄β—„β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Ίβ”€β”˜
                               0s                                     transition time   
    

    ```
    ### Notes / Calculations
    - _avoid short and low brightness steps to reduces turn_on light service calls_
    - _steps = (target brightness - initial brightness) / brightness step_
    - _delay = transition_time / steps_
  
  domain: script
  author: op00
  source_url: https://community.home-assistant.io/t/increase-light-brightness-over-time/600349
  input: 
    light_entity: 
      name: Light Entity
      description: "Select your light"
      selector:
        entity:
          filter:
            domain: light
          multiple: false
    transition_time: 
      name: Transition Time
      description: "From 1 minute up to 1 hour"
      default: 120
      selector:
        number:
          min: 60
          max: 3600
          step: 1
          unit_of_measurement: seconds
          mode: slider
    target_brightness: 
      name: Target Brightness
      description: "if you want to dimm the lights, this needs to be lower than initial brightness"
      default: 100
      selector:
        number:
          min: 0
          max: 100
          step: 1
          unit_of_measurement: '%'
          mode: slider

    initial_brightness: 
      name: Initial Brightness
      description: "if you want to dimm the lights, this needs to be higher than target brightness"
      default: 0
      selector:
        number:
          min: 0
          max: 100
          step: 1
          unit_of_measurement: '%'
          mode: slider

    brightness_step:
      name: Brightness Step
      description: "default is 2%, if target brightness is lower than initial brightness, it's negative"
      default: 2
      selector:
        number:
          min: 1
          max: 100
          step: 1
          unit_of_measurement: '%'
          mode: slider
    
mode: single
icon: mdi:lightbulb-on-60
variables:
  transition_time: !input transition_time
  brightness_step: !input brightness_step
  initial_brightness_pct: !input initial_brightness
  target_brightness_pct: !input target_brightness
  light_entity: !input light_entity

sequence:
  - variables:
      target_brightness: '{{(target_brightness_pct * 255 / 100)|round}}'
      initial_brightness: '{{(initial_brightness_pct * 255 / 100)|round}}'
      required_steps: '{{(((target_brightness_pct - initial_brightness_pct) / brightness_step))|round|int|abs}}'
      delay_between_steps: '{{ (transition_time / required_steps)|round|int|abs }}'
      brightness_step: '{% if (initial_brightness > target_brightness) -%}
                               {{ brightness_step * (-1) }}
                             {%- else -%}
                               {{ brightness_step }}
                             {%- endif %}'

  - service: light.turn_on
    data:
      brightness: '{{ initial_brightness }}'
      transition: 0
    entity_id: !input light_entity

  - repeat:
      until:
        - condition: template
          value_template: '{% if (brightness_step > 0) -%}
                             {{ state_attr(light_entity, "brightness") | int(0) >= target_brightness }}
                           {%- else -%}
                             {{ state_attr(light_entity, "brightness") | int(0) <= target_brightness }}
                           {%- endif %}'
      sequence:
        - service: light.turn_on
          data:
            transition: 0
            brightness_step_pct: '{{ brightness_step }}'
          entity_id: !input light_entity
        - delay:
            hours: 0
            minutes: 0
            seconds: '{{ delay_between_steps }}'
            milliseconds: 0

:tipping_hand_man: Usage

  1. Import Blueprint
    Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.
  2. Create and save script based on the imported blueprint
  3. Run or use the script in your automations

:hammer_and_wrench: Changelog

  • 2023-08-12: added dimming option
  • 2023-08-12: removed initial delay
  • 2023-08-12: fixed 0% initial brightness bug
  • 2023-08-12: a few text changes
  • 2023-08-07: added user customizable initial delay
  • 2023-08-07: Initial release

:construction: ToDo

  • :black_square_button: Spellcheck
  • :black_square_button: Multi entity handling

πŸ§‘πŸΌβ€πŸ€β€πŸ§‘πŸ» Contributions

1 Like

You could also make that delay user-configurable like this:

blueprint:
  name: Increase light brightness over time
  description: |
    # Increase light brightness over time

    This script increases the brightness of a light over time. 
    Given the **transition time**, **initial brightness**, **target brightness** and **brightness step**, the script will calculate the number of steps required to reach the target brightness and the delay between each step. 
    The script will then turn on the light with the initial brightness, wait 5 seconds and then start increasing the brightness by the brightness step every delay between steps until the target brightness is reached.   
    
    ```text

                     100% β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                          β”‚                                                                                                   β”‚
                          β”‚                                                                                                   β”‚
                          β”‚                                                                                         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β—„ target brightness
                          β”‚                                                                                         β”‚         β”‚
                          β”‚                                                                                         β”‚         β”‚
                          β”‚                                                                       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β”‚
                          β”‚                                                                       β”‚                           β”‚
                          β”‚                                                                       β”‚                           β”‚
                          β”‚                                                     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                           β”‚
                          β”‚                   calculated delay                  β”‚                                             β”‚
                          β”‚                          β”‚                          β”‚                                             β”‚
                          β”‚                          β”‚        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                                             β”‚
                          β”‚                          β”‚        β”‚                                                               β”‚
                          β”‚                          β–Ό        β”‚                                                               β”‚
                          β”‚                 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                                                               β”‚
                          β”‚                 β”‚ ◄──────────── brightness step                                                   β”‚
                          β”‚                 β”‚                                                                                 β”‚
    initial brightness    β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                                                                                 β”‚
                          β”‚                                                                                                   β”‚
                       0% β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                          0s                                                                                           transition time   
    ```
    
    ### Calculations
    _steps = (target brightness - initial brightness) / brightness step)_
    _delay = transition_time / steps_
    
  domain: script
  author: op00
  source_url: https://community.home-assistant.io/t/increase-light-brightness-over-time/600349
  input: 
    light_entity: 
      name: Light Entity
      description: "Select the target Light\n\n
                    _single entity_"
      selector:
        entity:
          filter:
            domain: light
          multiple: false
    transition_time: 
      name: Transition Time
      description: "Select the transition time\n\n
                    _min is 100 seconds, max is 3600 seconds = 1 hour_"
      default: 100
      selector:
        number:
          min: 100
          max: 3600
          step: 1
          unit_of_measurement: seconds
          mode: slider
    target_brightness: 
      name: Target Brightness
      description: "Select the target brightness\n\n
                    _default is 100%_"
      default: 100
      selector:
        number:
          min: 0
          max: 100
          step: 1
          unit_of_measurement: '%'
          mode: slider

    initial_brightness: 
      name: Initial Brightness
      description: "Select the initial brightness\n\n
                    _default is 10%_"
      default: 10
      selector:
        number:
          min: 0
          max: 100
          step: 1
          unit_of_measurement: '%'
          mode: slider

    brightness_step:
      name: Brightness Step
      description: "Select the brightness step\n\n
                    _default is 2%_"
      default: 2
      selector:
        number:
          min: 2
          max: 100
          step: 1
          unit_of_measurement: '%'
          mode: slider
    initial_delay:
      name: Initial delay
      description: "Select the delay before starting the transition in milliseconds\n\n
                    _default is 5000ms (5 seconds)_"
      default: 5000
      selector:
        number:
          min: 0
          max: 10000
          step: 100
          unit_of_measurement: '%'
          mode: slider

mode: single
icon: mdi:lightbulb-on-60
variables:
  transition_time: !input transition_time
  brightness_step: !input brightness_step
  initial_brightness_pct: !input initial_brightness
  target_brightness_pct: !input target_brightness
  light_entity: !input light_entity
  initial_delay: !input initial_delay

sequence:
  - variables:
      target_brightness: '{{(target_brightness_pct * 255 / 100)|round}}'
      initial_brightness: '{{(initial_brightness_pct * 255 / 100)|round}}'
      required_steps: '{{(((target_brightness_pct - initial_brightness_pct) / brightness_step))|round|int}}'
      delay_between_steps: '{{ (transition_time / required_steps)|round|int }}'

  - service: light.turn_on
    data:
      brightness: '{{ initial_brightness }}'
      transition: 0
    entity_id: !input light_entity
  - delay:
      hours: 0
      minutes: 0
      seconds: 0
      milliseconds: {{initial_delay}}
  - repeat:
      while:
        - condition: template
          value_template: '{{ state_attr(light_entity, "brightness") | int < target_brightness }}'
      sequence:
        - service: light.turn_on
          data:
            transition: 0
            brightness_step_pct: '{{ brightness_step }}'
          entity_id: !input light_entity
        - delay:
            hours: 0
            minutes: 0
            seconds: '{{ delay_between_steps }}'
            milliseconds: 0

Sorry if formatting is screwed up, did it on my phone…

1 Like

It works perfectly thank you

Thanks @aaroneisele55, :white_check_mark:added

You don’t need this. The service light.turn on has a parameter transition_time which does it all for you behind the scenes.

@Rudd-O, I couldn’t get this to work properly for my esphome lights.
Let me know if there is a way, I’d rather use built-in functionalities.

But you are right a service call:

service: light.turn_on
data:
  transition: 30
  brightness_step_pct: 100
target:
  entity_id: light.somelight

would achieve more or less the same and it’s smoother. However my esphome light

light:
  - platform: esp32_rmt_led_strip
    chipset: WS2812
    rmt_channel: 0
    pin: GPIO16
    num_leds: 71
    rgb_order: GRB
    id: somelight
    name: "somelight"

waits 30 seconds and turns on the light from 0 to 100%

Only if the light you have supports transitions. Many don’t.

3 Likes

Would it be possible to add light dimming?
I would like to use it to light the aquarium and simulate the day.
I tried setting default brightness to 80% and target brightness to 10% but it doesn’t work.

:tada::tada:new version, that supports dimming released​:tada::tada:
thanks @pepe59 for testing, fixing and contributing.

1 Like

I’ll just add. Tested 12 HUE lights in a group.
It works as expected, both brightening and dimming perfectly.

1 Like

I do not know, what I’m doing wrong, but my script runs indefinitely, I have to stop it manually every time.