Gradual sunrise / sunset light dimming script - no python required. Native HA

First off, if someone else has already done this as a pure HA script, please ignore.

In the bedrooms in the house we have dimmers. While they are connected to HA, the vendor does not have a sunrise/sunset capability that can be scheduled. The idea is to have people wake up slowly with increased light. This is especially necessary in winter time.

I searched the forums and google to see if anyone had any pre-canned scripts/automations that allowed me to gradually change the dimness of multiple lights over a lengthy time. The only solutions I found were hard coding individual automations per light and/or complex python scrips. While I can code in python, I wanted to challenge my HA skills and see if I could create a script in HA that would take parameters that I could re-use it whenever I wanted and not have to have complex automations or need for python scripts. Here is my creation:

Simply paste it into the YAML of a new script in HA. You can then call it like any other script and you just need to specify the parameters. The parameter names should be self explanatory. My favorite one is include_off_lights as this allows me the ability to limit dimming to only on lights already on (default) or to override and just do all the lights passed to the script.

Enjoy!

alias: Light rise/set
sequence:
  - choose:
      - conditions:
          - condition: template
            value_template: '{{ include_off_lights }}'
        sequence:
          - service: light.turn_on
            data_template:
              entity_id: '{{ single_light_or_light_group }}'
              brightness_pct: '{{ start_brightness }}'
      - conditions:
          - condition: not
            conditions:
              - condition: template
                value_template: '{{ include_off_lights }}'
        sequence:
          - service: light.turn_on
            data_template:
              entity_id: >
                {% set all_lights =
                expand(state_attr(single_light_or_light_group, 'entity_id'))|
                map(attribute='entity_id')|list %} {% set on_lights =
                expand(state_attr(single_light_or_light_group, 'entity_id'))|
                selectattr('state','eq','on')|map(attribute='entity_id')|list %}
                {% if (all_lights | length ) > 0 %} {{on_lights}} {% elif
                is_state(single_light_or_light_group,'on') %} {{
                single_light_or_light_group }} {% endif %}
              brightness_pct: '{{ start_brightness }}'
    default: []
  - repeat:
      count: '{{ (transitions_min*(minutes | int))+1 }}'
      sequence:
        - service: light.turn_on
          data_template:
            entity_id: >
              {% set all_lights = expand(state_attr(single_light_or_light_group,
              'entity_id'))| map(attribute='entity_id')|list %} {% set on_lights
              = expand(state_attr(single_light_or_light_group, 'entity_id'))|
              selectattr('state','eq','on')|map(attribute='entity_id')|list %}
              {% if (all_lights | length ) > 0 %} {{on_lights}} {% elif
              is_state(single_light_or_light_group,'on') %} {{
              single_light_or_light_group }} {% endif %}
            brightness_pct: >-
              {{
              ((((end_brightness|int)-(start_brightness|int))/(transitions_min*(minutes
              |int)) * (repeat.index - 1)) + start_brightness | int) }}
        - delay:
            seconds: '{{ 60/transitions_min | int }}'
mode: parallel
max: 20
icon: mdi:weather-sunset
description: Fades up or down a single light or light group
fields:
  minutes:
    name: Minutes
    description: How long to fade
    default: 10
    required: true
    selector:
      number:
        min: 1
        max: 30
        step: 1
        unit_of_measurement: minutes
        mode: slider
  transitions_min:
    name: Transitions per minute
    description: How many transitions per minute
    default: 2
    required: true
    selector:
      number:
        min: 2
        max: 6
        step: 2
        unit_of_measurement: times
        mode: slider
  start_brightness:
    name: Start Brightness
    description: Where to start
    default: 20
    required: true
    selector:
      number:
        min: 0
        max: 100
        step: 1
        unit_of_measurement: brightness
        mode: slider
  end_brightness:
    name: End Brightness
    description: Where to end
    default: 100
    required: true
    selector:
      number:
        min: 0
        max: 100
        step: 1
        unit_of_measurement: brightness
        mode: slider
  single_light_or_light_group:
    name: Single Light or Light Group
    description: Entity Light
    required: true
    selector:
      entity:
        domain: light
  include_off_lights:
    name: Include off lights
    description: Boolean - include off lights.
    required: true
    default: false
    selector:
      boolean: null

Here is an example automation where I have it turn the lights on in the morning in the bedrooms. Note that light.wake_up is a light group of multiple lights. The script can also take input of a single light entity.

alias: Wake Up Lights
description: ''
trigger:
  - platform: time
    at: '07:35:00'
condition:
  - condition: and
    conditions:
      - condition: time
        weekday:
          - mon
          - tue
          - wed
          - thu
          - fri
action:
  - service: script.your_script_name_here
    data:
      transitions_min: 4
      single_light_or_light_group: light.wake_up
      include_off_lights: true
      start_brightness: 20
      end_brightness: 100
      minutes: 10
mode: single

Why do you use repeat instead of using the transition for the light turn on service?

service: light.turn_on
data:
  entity_id: light.xy
  brightness_pct: 80
  transition: 100

transition breaks with subsequent action sent to devices.

And why is this a problem? What subsequent actions? If I turn off the light manually, I wouldn’t want the sunrise/sunset to continue.

I really like your project. Can you be more specific and specific about the Single Light or Light Group configuration? Error: Error rendering data template: UndefinedError: ‘single_light_or_light_group’ is undefined