Yeelight brightness adjust to sunrise/sunset during day and night

I have an automation to adjust brightness to time, but after a year of using, it works well, but it seems like the sun does not care about the current time :smiley:

Motion turns on the yeelight, and another automation set the brightness, here is the current solution:

- alias: Dining Room brightness
  trigger:
  - platform: state
    entity_id: light.etkezo_rgbw
    from: 'off'
    to: 'on'
  action:
  - service: light.turn_on
    data_template:
      brightness_pct: >
        {% set  hour = now().hour | int  %}
        {% if   hour >=  7 and hour < 8  %} 15
        {% elif hour >=  8 and hour < 9  %} 40
        {% elif hour >=  9 and hour < 18 %} 100
        {% elif hour >= 18 and hour < 19 %} 50
        {% elif hour >= 19 and hour < 20 %} 30
        {% elif hour >= 20 and hour < 21 %} 10
        {% else                          %} 1
        {% endif %}
      kelvin: >
        {% set  hour = now().hour | int -%}
        {% if   hour >=  6 and hour < 8  %} 3000
        {% elif hour >=  8 and hour < 18 %} 3500
        {% elif hour >= 18 and hour < 20 %} 2500
        {% else                          %} 1700
        {% endif %}
    entity_id: light.etkezo_rgbw

How can I transform this to sunrise+1 hour, sunrise+2hours, sunset+1hour, sunset+2hours, or something like this, or if you have better idea it would be nice too, I’m thinking about it since autumn where we first set the 1 hour to winter time.

Paste this into your Template Editor and experiment with it.

{{ as_timestamp(state_attr('sun.sun', 'next_rising')) | timestamp_local }}

{{ (as_timestamp(state_attr('sun.sun', 'next_rising')) + 3600) | timestamp_local }}

{{ as_timestamp(state_attr('sun.sun', 'next_setting')) | timestamp_local }}

{{ (as_timestamp(state_attr('sun.sun', 'next_setting')) + 3600) | timestamp_local }}

The examples show how to determine sunrise+1 hour, sunset+1 hour, etc. For more information about the sun component:

It shows the next rising, I need to use the currrent one, and without date.

Like sunset is now at 20:08 and I want to lower the lamp to 70% at 19:08 and 50% at 20:08 and 10% at 21:08 and 1% after 22:08.

Yes, it’s the next rising, which means there’s a time difference of what? A minute or two? How can that tiny difference be relevant for setting a light’s brightness? Your current automation works in units of whole hours.

If you don’t need the date, use timestamp_custom to use only the time.

Have you considered something like this? It does what you want very nicely. You can use it to change brightness, colour temp or both.

The lights I would use this with have switches too for maximum brightness and minimum brightness, this custom component can’t work with it, tried it a few months ago, it got bugged very fast.

Hoped it is easy to use the sunset/sunrise time. :frowning: