Light turn on brightness based on sun's horizon position?

Add the template to the brightness_pct option like this:

action:
  - service: light.turn_on
    data:
      transition: 1
      brightness_pct: >
        {% set sr = today_at((state_attr('sun.sun', 'next_rising') | as_datetime | as_local).strftime('%H:%M')) %}
        {% set pre_sunrise =  sr - timedelta(minutes=30) %}
        {% set post_sunrise = sr + timedelta(minutes=30) %}
        {% set ss = today_at((state_attr('sun.sun', 'next_setting') | as_datetime | as_local).strftime('%H:%M')) %}
        {% set pre_sunset = ss - timedelta(minutes=30) %}
        {% set post_sunset = ss + timedelta(minutes=30) %}
        {% set t = now() %}
        {{ { pre_sunrise <= t < post_sunrise: 50 + ((t - pre_sunrise).seconds * 50/3600)|int(0),
             post_sunrise <= t < pre_sunset: 100,
             pre_sunset <= t < post_sunset: 100 - ((t - pre_sunset).seconds * 50/3600)|int(0)
           }.get(true, 50) }}

EDIT

Correction. The template produces a value from 50 to 100 so it should be used with the brightness_pct option.

1 Like