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 or the light has been turned off mid execution.   

    ```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: or
          conditions:
            - 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 %}'
            - condition: template
              value_template: '{{ (repeat.first is false) and (is_state(light_entity,"off"))}}'
      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

  • 2024-06-06: added condition if light is turned off mid execution
  • 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

4 Likes

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.

I keep having lights timing out on one of the brightness step increases. Maybe use continue_on_error on the service calls, perhaps configurable on/off in the blueprint?

I’m wondering if the script has some rounding errors. Is your target brightness 0% or 100%? Try setting it to 2% / 98% and see if that helps. Please report back eitehr way.

Could you add an extra condition to the loop: If the light is turned off mid-operation then stop the script.

Something like this:

    - if:
      - condition: state
        entity_id: !input light_entity
        state: "off"
      then:
        - stop: "Light has been manually turned off"

(If you do this you have to take care that the initial brightness isn’t 0, since in some cases that’s synonymous with off)

thanks! :stuck_out_tongue:

Hi Gaff, have you had the chance to test what i have sent you in the DM?

Would probably be real neat except for some reason I keep getting an error:
float division by zero
Have had a look to see what it means but as Im a layman ive not been successful - this, you see, is why I use blueprints - the hard work is usually already done.
Anyway - if any suggestions please sing out

Can you share your initial values? And maybe a screenshot of the trace?

Awesome, this works great to create a wake up light

1 Like