Creating Sunrise routine in Homeassistant for my Stair light with WLED
It's now more indoor time, so I want to adjust my lights for the stairs. I switch them on after sunset and turn them off at a specific time. So the turnoff will be done when I/we go into bed. But I wanted a smoother turn-on light. So, my idea was to identify the sunset and turn the light slowly on like a sunrise.
As Light, I use WLED, which is also integrated into my homeassistant. I can control the complete stuff from WLED via Homeassistant now.
Why the transition cannot be used
Sure, you can say that I can use a transition with a specific runtime, but that is limited to a maximum of 20 seconds. To be clear, in Germany, a sunrise or sunset is not done in 20 seconds. So, another solution must be created to support longer transitions so that I can simulate a sunrise.
What was required?
In my Homeassistant instance, I created a counter helper that stores the light's brightness value. You can find this in Settings-> Devices-> Helpers.
I called my helper "StairsBrightness" with the following settings
Now, we must create a new automation that will do the following:
- React when there is a sunset happening
- Increate the counter
- Wait a few seconds and repeat
For that I created an automation the trigger is mandatory, it is only a reaction on the sunset event. The most important part is the loop within the automation.
This will check if the counter value is below the maximum 255 because the WLED Device will throw an error. Next, I will check if the time is before 8:10 PM because, at this time, another automation will turn off the lights.
The loop will increase the helper with 1 and wait for 3 seconds. I figured out that 3 seconds would be smooth enough. You can adjust this to your needs. So, I am here with my YAML configuration.
alias: Sonnenuntergang
description: ""
trigger:
- platform: time_pattern
minutes: "*"
enabled: false
- platform: sun
event: sunset
condition:
- condition: sun
after: sunset
action:
- repeat:
count: 255
sequence:
- if:
- condition: numeric_state
entity_id: counter.treppe_helligkeit
below: 255
- condition: time
before: "20:10:00"
then:
- action: counter.increment
target:
entity_id: counter.treppe_helligkeit
data: {}
- delay:
hours: 0
minutes: 0
seconds: 3
milliseconds: 0
mode: single
Controlling WLED
We increased the counter, but I didn't send the value directly to the WLED Device. This is only why I use the WLED Device for other automation definitions. I use a trigger that will listen to change events from the counter helper. My automation will look like this.
alias: Setze WLED Helligkeit basierend auf Counter
description: >-
Setzt die Helligkeit der WLED-Instanz auf den Wert des Counters bis maximal
250.
trigger:
- platform: state
entity_id:
- counter.treppe_helligkeit
condition:
- condition: time
before: "20:10:00"
- condition: sun
after: sunset
action:
- target:
entity_id: d790004af9e666f1b0b3072a2b2d145a
data:
brightness: "{{ states('counter.treppe_helligkeit') | int }}"
action: light.turn_on
- action: light.turn_on
metadata: {}
data: {}
target:
entity_id: light.wled_treppe
mode: single
So I set the brightness. Now it's time to test
Testing
I adjusted the wait time to one second for demonstration purposes. Then, I started to simulate a sunset, and the magic would happen.
My thoughts
So my idea was to get a little sunrise at my stairs. This works now like a charm. But that's not all, you are free to add more routines like night light at a specific time, so that you must not work down in the dark and not brighten up the light.
What are your thoughts about that?