Sunrise Simulator. Wake up gently to light

Tired waking up with a shock from your alarm sound?
Would you like to wake up gently with light?
Do you have smart bulbs with RGB, CCT or just Brightness attributes?
Or even better… Do you have WLED strips with Sunrise effect?

Then I have a script for you…

The script accepts the following parameters:

duration: the duration for the simulation (default: 10minutes)

max_brightness: the maximum brightness that the lamps should turn on (if you don’t need them to turn on to 100%, default: 100)

target:

  • area (i.e. Sleeproom) that contains light entities
  • group that contains light entities
  • or light. entities (default: all light entities)

Entities under the target variable will be automatically sorted depending their supporting color modes (RGB / XY / WLED / CCT / Brightness)
If you need to manually sort the lights to match your preference i.e. only CCT if they support RGB, you can use the following variables:

brightness: force these entities to dim only from 0 to max_britghtness given (see bellow)

cct: force these entities to dim and change the color temperature (no need to use it if the entities are supporting only color temperature)

If any of the parameter is not given, the script uses the default values from above.

Example usage:

    service: script.< script_name >
    data:
      duration: 10
      target: 
        - light.sleeproom
        - group.sleeproom
        - Sleeproom
      max_brightness: 100
  • Replace script.< script_name > with the entity_id from your home assistant

HOW IT WORKS
It catches all the lights that it finds from the data given and separates them to lights with brightness only, color temperature only, lights with rgb and WLED lights, from their attributes.
The lights with brightness only as color_mode make a transition from 1 to brightness given.
For the lights with CT only it makes a transition from warm white to cool white and from 1% brightness to the brightness given.
For the RGB lights it goes through the colors from the Sunrise palette with a brightness transition.
For the WLED entities the script turns them on with Sunrise effect for the given duration as transition and intensity from the brightness given.

alias: Sunrise Simulator Manual
sequence:
  - parallel:
      - if:
          - condition: template
            value_template: "{{ wled | count > 0 }}"
        then:
          - repeat:
              for_each: "{{ wled }}"
              sequence:
                - service: light.turn_on
                  data:
                    effect: Sunrise
                  target:
                    entity_id: |
                      {{ repeat.item }}
                - service: number.set_value
                  data:
                    value: "{{ [(255 * (maxbri/100)) | int, 255] | min }}"
                  target:
                    entity_id: |
                      {{ device_entities(device_id(repeat.item))
                            | select('search', 'number.')
                            | select('search', 'intensity')
                            | list }}
                - service: number.set_value
                  data:
                    value: |
                      {{ [dur, 128] | min }}
                  target:
                    entity_id: |
                      {{ device_entities(device_id(repeat.item))
                            | select('search', 'number.')
                            | select('search', 'speed')
                            | list }}
      - if:
          - condition: template
            value_template: "{{ bright | count > 0 }}"
        then:
          - repeat:
              while:
                - condition: template
                  value_template: |
                    {{ repeat.index <= maxbri
                          and not ( repeat.index > 1 
                                             and states.light
                                                        | selectattr('entity_id', 'in', bright)
                                                        | selectattr('state', 'eq', 'on') | list | count 
                                                      ==  0 ) }}
              sequence:
                - variables:
                    bright: |
                      {% if repeat.index > 1 %}
                        {{ expand(bright)
                              | selectattr('state', 'eq', 'on')
                              | map(attribute = 'entity_id')
                              | list }}
                      {% else %}
                        {{ bright }}
                      {% endif %}
                - service: light.turn_on
                  data:
                    transition: |
                      {{ ( ( dur * 60 ) / (maxbri) ) | int }}
                    brightness_pct: |
                      {{ [maxbri, ((repeat.index * maxbri) / 100)] | min }}
                  target:
                    entity_id: |
                      {{ bright }}
                - delay:
                    milliseconds: |
                      {{ (  ( ( dur  * 60 ) / (maxbri) ) * 1000
                       ) | int }}
      - if:
          - condition: template
            value_template: "{{ temp | count > 0 }}"
        then:
          - repeat:
              while:
                - condition: template
                  value_template: |
                    {{ repeat.index <= 100
                          and not ( repeat.index > 1 
                                             and states.light
                                                        | selectattr('entity_id', 'in', temp)
                                                        | selectattr('state', 'eq', 'on') | list | count 
                                                      ==  0 ) }}
              sequence:
                - variables:
                    step: "{{ repeat.index }}"
                    temp: |
                      {% if repeat.index > 1 %}
                        {{ expand(temp)
                              | selectattr('state', 'eq', 'on')
                              | map(attribute = 'entity_id')
                              | list }}
                      {% else %}
                        {{ temp }}
                      {% endif %}
                - repeat:
                    for_each: "{{ temp }}"
                    sequence:
                      - service: light.turn_on
                        data:
                          transition: |
                            {{ ( ( dur * 60) / 100 ) | int }}
                          brightness_pct: |
                            {{ [ ((step * maxbri) / 100) | int, maxbri] | min }}
                        target:
                          entity_id: |
                            {{ repeat.item }}
                      - service: light.turn_on
                        data:
                          color_temp: >
                            {% set max = state_attr(repeat.item, 'max_mireds')
                            %} {% set min = state_attr(repeat.item,
                            'min_mireds') %} {% set dec = (max-min) / 100 %} {{
                            iif( step < 100, ( max - (dec * (step - 1) ) ) |
                            int, min ) }}
                        target:
                          entity_id: |
                            {{ repeat.item }}
                - delay:
                    milliseconds: |
                      {{ (  ( ( dur  * 60 ) / 100 ) * 1000
                       ) | int }}
      - if:
          - condition: template
            value_template: "{{ color | count > 0 }}"
        then:
          - repeat:
              while:
                - condition: template
                  value_template: |
                    {{ repeat.index <= 100
                          and not ( repeat.index > 1 
                                             and states.light
                                                        | selectattr('entity_id', 'in', color)
                                                        | selectattr('state', 'eq', 'on') | list | count 
                                                      == 0 ) }}
              sequence:
                - variables:
                    color: |
                      {% if repeat.index > 1 %}
                        {{ expand(color)
                              | selectattr('state', 'eq', 'on')
                              | map(attribute = 'entity_id')
                              | list }}
                      {% else %}
                        {{ color }}
                      {% endif %}
                - service: light.turn_on
                  data:
                    transition: |
                      {{ (  ( dur * 60 ) / 100  ) | int }}
                    brightness_pct: >
                      {{ [((repeat.index * maxbri) / 100) | int , maxbri] | min
                      }}
                  target:
                    entity_id: |
                      {{ color }}
                - service: light.turn_on
                  data:
                    rgb_color: |
                      {% if repeat.index <= 20 %}
                        [255,0,0]
                      {% elif repeat.index <= 40  %}
                        [255,77,0]
                      {% elif repeat.index <= 60 %}
                        [255,103,0]
                      {% elif repeat.index <= 80 %}
                        [255,129,0]  
                      {% else %}
                        [255,167,0]
                      {% endif %}
                  target:
                    entity_id: |
                      {{ color }}
                - delay:
                    milliseconds: |
                      {{ (  ( ( dur * 60 ) / 100 ) * 1000 ) | int }}
variables:
  dur: |
    {{ duration | default(10, true) | int(10) }}
  entities: |
    {% if target is defined %}
      {% if target in states.light 
                         | rejectattr('state','in', ['unavailable', 'unknown', None])
                         | selectattr('attributes.supported_color_modes', 'defined')
                         | rejectattr('attributes.supported_color_modes', 'eq', None)
                         | map(attribute='entity_id') 
                         | map('area_name')
                         | list %}
        {% set target = area_entities(target) %}
      {% endif %}
      {{ expand(target)
            | selectattr('domain', 'eq', 'light')
            | selectattr('attributes.supported_color_modes', 'defined')
            | rejectattr('attributes.supported_color_modes', 'eq', None)
            | rejectattr('state', 'in', ['unavailable', 'unknown', None])
            | map(attribute='entity_id')
            | list }}
    {% elif cct is not defined
        and brightness is not defined %}
      {{ states.light 
               | rejectattr('state','in', ['unavailable', 'unknown', None])
               | selectattr('attributes.supported_color_modes', 'defined')
               | rejectattr('attributes.supported_color_modes', 'eq', None)
               | map(attribute='entity_id') 
               | list }}
    {% endif %}
  bright: |
    {{ expand(entities)
          | selectattr('attributes.supported_color_modes', 'eq', [ 'brightness'])
          | map(attribute='entity_id')
          | list + expand( brightness | default([], true) )
                      | rejectattr('state', 'in', [ 'unavailable', 'unknown', None ])
                      | rejectattr('entity_id', 'in', entities)
                      | selectattr('attributes.supported_color_modes', 'defined')
                      | selectattr('attributes.supported_color_modes', 'search', 'brightness')
                      | map(attribute='entity_id')
                      | list  }}
  temp: |
    {{ expand(entities)
          | selectattr('attributes.supported_color_modes', 'eq', [ 'color_temp'])
          | map(attribute='entity_id')
          | list + expand( cct | default([], true) )
                      | rejectattr('state', 'in', [ 'unavailable', 'unknown', None ])
                      | rejectattr('entity_id', 'in', entities)
                      | selectattr('attributes.supported_color_modes', 'defined')
                      | selectattr('attributes.supported_color_modes', 'search', 'color_temp')
                      | map(attribute='entity_id')
                      | list }}
  wled: |
    {{ integration_entities('wled')
          | select('in', expand(entities)
                            | selectattr('attributes.effect_list', 'defined')
                            | selectattr('attributes.effect_list', 'search', 'Sunrise')
                            | map(attribute='entity_id')
                            | list)
          | list }}
  color: |
    {{ entities | reject('in', temp + wled + bright) | list  }}
  maxbri: >
    {% set bri = max_brightness | default(100, true) | int(100) %} {{ bri if bri
    < 100 else 100 }}
mode: single

Last Edit-Update Script: 24/10/2023

  • BREAKING CHANGE:
    max_brightness is the variable to enter for the maximum brightness that the lights should turn on
  • NEW FEATURE:
    new variables:
    brightness:
    entities that should be forced to only dim from 0 to max_brightness given
    cct:
    entities that should be forced to change their color temperature and brightness

Edit: 14/10/2023

  • Ignore entities that turned off during script actions
  • Continue adjusting rgb color / color temperature after max brightness reached

Edit: 13/10/2023

  • Handle color_temp seperate for every entity based in its min/max values
  • Support entities with brightness (dimmable lights only)
  • Support WLED lightstrips with Sunrise effect
8 Likes

Looks good, I’ll give it a try to replace mine, do we need to set up an input_number.sunrise_sim_time

1 Like

Wow good catch! It was a mistake, i corrected it. You only need to pass the variables from above

Not sure if I’m using this properly.

  • Copy/Pasted the above script into a new script.
  • Created a new automation that triggers @ 6 a.m.
    • Action =
service: script.mysunrisescriptname
data:
  duration: 10
  target: 
    - light.nightstand
  brightness: 100

The result is all my lights (and not just the single entity) perform the action.

Any ideas?

Mine is working; however, the transition is very abrupt. Not very smooth transitioning to brightness. Not sure if it is a zigbee coordinator thing or not. I was using phillps hue in the pass and notice the transition up/down when controlling the brightness is better. Also, my light is ikea non rgb.

Hello sorry for the delayed answer. Please double check if you have misspelled the entity

This script is very nice and it works correctly, is there a way to interrupt it while it’s running?
I can’t figure out how to do it. If it’s possible, I would like to create an automation that stops the script if a light bulb in the bedroom is turned on, because it means someone has woken up.

I I’ve been using this blueprint for a few years to do a similar thing, but based in my phone’s alarm. It’s probably my favourite smart home automation:

You can call the script.turn_off service

Thank you for telling me about this command, I didn’t know it.
This way I partially solved the problem, but not completely. I think it doesn’t always work because there is a delay before the system detects that the light has been turned off through Alexa, and if a light intensity variation is made by the script during that time, the light turns back on.

We should modify the script by adding a check at each intensity variation:

  • If the light bulb is still on, the execution should continue normally.
  • If the light bulb is off (it could have been turned off by Alexa, Homeassistant, the proprietary app, etc.), the cycle should be interrupted and nothing else should be done.

I tried to modify it myself, but I wasn’t successful.

I had already tried this blueprint, but it seems that the initial and final colors cannot be adjusted.
I really like this script (Sunrise Simulator) because it transitions from red to yellow, simulating a real sunrise.

I was able to modify the script as I need it, unfortunately I don’t know how to use the name of the light coming from the automation parameters so I entered the name of the light directly inside the script.

These are the pieces of code I added:

repeat:
  count: "1"
  sequence:
    - service: light.turn_on
      data:
        brightness_pct: |
          {{ iif(repeat.index < 100, repeat.index, 100) }}
        color_temp: >
          {{ iif(repeat.index < maxbri, ( 500 - ( (repeat.index-1) * 2.5 *
          (100/maxbri) ) ) | int, 185 ) }}
        transition: |
          {{ ( ( dur * 60) / maxbri ) | int }}
      target:
        entity_id: |
          {{ temp }}
    - delay:
        milliseconds: |
          {{ (  ( ( dur * 60) / maxbri ) * 1000
           ) | int }}

repeat:
  count: "1"
  sequence:
    - service: light.turn_on
      data:
        rgb_color: |
          {% if repeat.index <= maxbri/6 %}
            [255,0,0]
          {% elif repeat.index <= (maxbri/6) * 2  %}
            [255,77,0]
          {% elif repeat.index <= (maxbri/6) * 3 %}
            [255,103,0]
          {% elif repeat.index <= (maxbri/6) * 4 %}
            [255,129,0]  
          {% else %}
            [255,167,0]
          {% endif %}
        brightness_pct: |
          {{ iif(repeat.index < 100, repeat.index, 100) }}
        transition: |
          {{ (  ( dur * 60) / maxbri  ) | int }}
      target:
        entity_id: |
          {{ color }}
    - delay:
        milliseconds: |
          {{ (  ( ( dur * 60) / maxbri ) * 1000 ) | int }}

condition: state
entity_id: light.sleeproom
state: "on"
enabled: true

This is the result in case someone needs it or wants to make it parametric:

alias: Sunrise Simulator Manual with power off support
sequence:
  - parallel:
      - if:
          - condition: template
            value_template: "{{ temp | count > 0 }}"
        then:
          - repeat:
              count: "1"
              sequence:
                - service: light.turn_on
                  data:
                    brightness_pct: |
                      {{ iif(repeat.index < 100, repeat.index, 100) }}
                    color_temp: >
                      {{ iif(repeat.index < maxbri, ( 500 - ( (repeat.index-1) *
                      2.5 * (100/maxbri) ) ) | int, 185 ) }}
                    transition: |
                      {{ ( ( dur * 60) / maxbri ) | int }}
                  target:
                    entity_id: |
                      {{ temp }}
                - delay:
                    milliseconds: |
                      {{ (  ( ( dur * 60) / maxbri ) * 1000
                       ) | int }}
          - repeat:
              while:
                - condition: template
                  value_template: "{{ repeat.index <= maxbri }}"
                - condition: state
                  entity_id: light.sleeproom
                  state: "on"
                  enabled: true
              sequence:
                - service: light.turn_on
                  data:
                    brightness_pct: |
                      {{ iif(repeat.index < 100, repeat.index, 100) }}
                    color_temp: >
                      {{ iif(repeat.index < maxbri, ( 500 - ( (repeat.index-1) *
                      2.5 * (100/maxbri) ) ) | int, 185 ) }}
                    transition: |
                      {{ ( ( dur * 60) / maxbri ) | int }}
                  target:
                    entity_id: |
                      {{ temp }}
                - delay:
                    milliseconds: |
                      {{ (  ( ( dur * 60) / maxbri ) * 1000
                       ) | int }}
      - if:
          - condition: template
            value_template: "{{ color | count > 0 }}"
        then:
          - repeat:
              count: "1"
              sequence:
                - service: light.turn_on
                  data:
                    rgb_color: |
                      {% if repeat.index <= maxbri/6 %}
                        [255,0,0]
                      {% elif repeat.index <= (maxbri/6) * 2  %}
                        [255,77,0]
                      {% elif repeat.index <= (maxbri/6) * 3 %}
                        [255,103,0]
                      {% elif repeat.index <= (maxbri/6) * 4 %}
                        [255,129,0]  
                      {% else %}
                        [255,167,0]
                      {% endif %}
                    brightness_pct: |
                      {{ iif(repeat.index < 100, repeat.index, 100) }}
                    transition: |
                      {{ (  ( dur * 60) / maxbri  ) | int }}
                  target:
                    entity_id: |
                      {{ color }}
                - delay:
                    milliseconds: |
                      {{ (  ( ( dur * 60) / maxbri ) * 1000 ) | int }}
          - repeat:
              while:
                - condition: template
                  value_template: "{{ repeat.index <= maxbri }}"
                - condition: state
                  entity_id: light.sleeproom
                  state: "on"
              sequence:
                - service: light.turn_on
                  data:
                    rgb_color: |
                      {% if repeat.index <= maxbri/6 %}
                        [255,0,0]
                      {% elif repeat.index <= (maxbri/6) * 2  %}
                        [255,77,0]
                      {% elif repeat.index <= (maxbri/6) * 3 %}
                        [255,103,0]
                      {% elif repeat.index <= (maxbri/6) * 4 %}
                        [255,129,0]  
                      {% else %}
                        [255,167,0]
                      {% endif %}
                    brightness_pct: |
                      {{ iif(repeat.index < 100, repeat.index, 100) }}
                    transition: |
                      {{ (  ( dur * 60) / maxbri  ) | int }}
                  target:
                    entity_id: |
                      {{ color }}
                - delay:
                    milliseconds: |
                      {{ (  ( ( dur * 60) / maxbri ) * 1000 ) | int }}
variables:
  dur: |
    {{ duration | default('10', true) | int(10) }}
  entities: |
    {% set res = namespace(r=[]) %}  {% if target is defined %}    
      {% set target = [target] if target is string else target %} 
      {% for t in target %}
        {% if t[0:6] == 'light.' and t in expand(states.light) | map(attribute='entity_id') | list %}
          {% set res.r = res.r + [t] %}
        {% elif t[0:6] == 'group.' and t in states.group | map(attribute='entity_id') | list %}
          {% set res.r = res.r + states.group 
                                  | selectattr('entity_id', 'eq', t) 
                                  | map(attribute='attributes.entity_id')
                                  | list
                                  | default([''], true)
                                  | first
                                  | select('in', states.light 
                                                  | rejectattr('state','in', ['unavailable', 'unknown', None]) 
                                                  | map(attribute='entity_id') 
                                                  | list) 
                                  | list %}
        {% else %}
          {% set res.r = res.r + states.light 
                                   | rejectattr('state','in', ['unavailable', 'unknown', None])
                                   | selectattr('entity_id', 'in', area_entities(t))
                                   | map(attribute='entity_id') 
                                   | list %}
        {% endif %}
      {% endfor %}
    {% else %}
      {% set res.r = states.light 
                       | rejectattr('state','in', ['unavailable', 'unknown', None])
                       | map(attribute='entity_id') 
                       | list %}
    {% endif %} {{ res.r }}
  temp: |
    {% set res = namespace(r=[], l=[]) %}
    {% if entities | default([], true) | count > 0 %}
      {% for sun in entities %}
        {% if states(sun) not in ['unavailable','unknown']
            and states[sun].attributes.supported_color_modes | select('match', 'color_temp') | list | count > 0 
            and states[sun].attributes.supported_color_modes | count == 1 %}
          {% set res.r = res.r + [sun] %}
        {% endif %}
      {% endfor %}
    {% endif %}
    {{ res.r }}
  color: |
    {% set res = namespace(r=[], l=[]) %}
    {% if entities | default([], true) | count > 0 %}
      {% for sun in entities %}
        {% if states(sun) not in ['unavailable','unknown']
            and 'hs' in states[sun].attributes.supported_color_modes
                or 'xy' in states[sun].attributes.supported_color_modes %}
          {% set res.r = res.r + [sun] %}
        {% endif %}
      {% endfor %}
    {% endif %}
    {{ res.r }}
  maxbri: |
    {% set bri = brightness | default('100', true) |int(100) %}
    {{ bri if bri <= 100 else 100 }}
mode: single
icon: mdi:weather-sunset-up

Could you test this script:

alias: Sunrise Simulator Manual
sequence:
  - parallel:
      - if:
          - condition: template
            value_template: "{{ temp | count > 0 }}"
        then:
          - repeat:
              while:
                - condition: template
                  value_template: "{{ repeat.index <= maxbri\n      and not ( repeat.index > 1 \n\t\t\t\t\tand states.light\n\t\t\t\t\t\t\t| selectattr('entity_id', 'in', temp)\n\t    \t\t\t\t\t| selectattr('state', 'eq', 'on')\n\t\t    \t\t\t\t| list | count != temp | count ) }}"
              sequence:
                - service: light.turn_on
                  data:
                    brightness_pct: |
                      {{ iif(repeat.index < 100, repeat.index, 100) }}
                    color_temp: >
                      {{ iif(repeat.index < maxbri, ( 500 - ( (repeat.index-1) *
                      2.5 * (100/maxbri) ) ) | int, 185 ) }}
                    transition: |
                      {{ ( ( dur * 60) / maxbri ) | int }}
                  target:
                    entity_id: |
                      {{ temp }}
                - delay:
                    milliseconds: >
                      {{ (  ( ( states('input_number.sunrise_sim_time') |
                      int(10) * 60) / maxbri ) * 1000
                       ) | int }}
      - if:
          - condition: template
            value_template: "{{ color | count > 0 }}"
        then:
          - repeat:
              while:
                - condition: template
                  value_template: "{{ repeat.index <= maxbri\n      and not ( repeat.index > 1 \n\t\t\t\t\tand states.light\n\t\t\t\t\t\t\t| selectattr('entity_id', 'in', color)\n\t    \t\t\t\t\t| selectattr('state', 'eq', 'on')\n\t\t    \t\t\t\t| list | count != color | count ) }}"
              sequence:
                - service: light.turn_on
                  data:
                    rgb_color: |
                      {% if repeat.index <= maxbri/6 %}
                        [255,0,0]
                      {% elif repeat.index <= (maxbri/6) * 2  %}
                        [255,77,0]
                      {% elif repeat.index <= (maxbri/6) * 3 %}
                        [255,103,0]
                      {% elif repeat.index <= (maxbri/6) * 4 %}
                        [255,129,0]  
                      {% else %}
                        [255,167,0]
                      {% endif %}
                    brightness_pct: |
                      {{ iif(repeat.index < 100, repeat.index, 100) }}
                    transition: |
                      {{ (  ( dur * 60) / maxbri  ) | int }}
                  target:
                    entity_id: |
                      {{ color }}
                - delay:
                    milliseconds: |
                      {{ (  ( ( dur * 60) / maxbri ) * 1000 ) | int }}
variables:
  dur: |
    {{ duration | default('10', true) | int(10) }}
  entities: |
    {% set res = namespace(r=[]) %}  {% if target is defined %}    
      {% set target = [target] if target is string else target %} 
      {% for t in target %}
        {% if t[0:6] == 'light.' and t in expand(states.light) | map(attribute='entity_id') | list %}
          {% set res.r = res.r + [t] %}
        {% elif t[0:6] == 'group.' and t in states.group | map(attribute='entity_id') | list %}
          {% set res.r = res.r + states.group 
                                  | selectattr('entity_id', 'eq', t) 
                                  | map(attribute='attributes.entity_id')
                                  | list
                                  | default([''], true)
                                  | first
                                  | select('in', states.light 
                                                  | rejectattr('state','in', ['unavailable', 'unknown', None]) 
                                                  | map(attribute='entity_id') 
                                                  | list) 
                                  | list %}
        {% else %}
          {% set res.r = res.r + states.light 
                                   | rejectattr('state','in', ['unavailable', 'unknown', None])
                                   | selectattr('entity_id', 'in', area_entities(t))
                                   | map(attribute='entity_id') 
                                   | list %}
        {% endif %}
      {% endfor %}
    {% else %}
      {% set res.r = states.light 
                       | rejectattr('state','in', ['unavailable', 'unknown', None])
                       | map(attribute='entity_id') 
                       | list %}
    {% endif %} {{ res.r }}
  temp: |
    {% set res = namespace(r=[], l=[]) %}
    {% if entities | default([], true) | count > 0 %}
      {% for sun in entities %}
        {% if states(sun) not in ['unavailable','unknown']
            and 'color_temp' in states[sun].attributes.supported_color_modes
            and states[sun].attributes.supported_color_modes | count == 1 %}
          {% set res.r = res.r + [sun] %}
        {% endif %}
      {% endfor %}
    {% endif %}
    {{ res.r }}
  color: |
    {% set res = namespace(r=[], l=[]) %}
    {% if entities | default([], true) | count > 0 %}
      {% for sun in entities %}
        {% if states(sun) not in ['unavailable','unknown']
            and 'hs' in states[sun].attributes.supported_color_modes %}
          {% set res.r = res.r + [sun] %}
        {% endif %}
      {% endfor %}
    {% endif %}
    {{ res.r }}
  maxbri: |
    {% set bri = brightness | default('100', true) |int(100) %}
    {{ bri if bri <= 100 else 100 }}
mode: single

I tried the script but something isn’t working, the light doesn’t turn on.
I downloaded the track if it can be useful.

Trace
{
  "trace": {
    "last_step": "sequence/0/parallel/1/sequence/0/then/0/repeat/while/0",
    "run_id": "71d64fc8753c9e67248d51d1735e6134",
    "state": "stopped",
    "script_execution": "finished",
    "timestamp": {
      "start": "2023-05-15T16:49:49.162151+00:00",
      "finish": "2023-05-15T16:49:49.173542+00:00"
    },
    "domain": "script",
    "item_id": "simulatore_alba_con_supporto_spegnimento",
    "trace": {
      "sequence/0": [
        {
          "path": "sequence/0",
          "timestamp": "2023-05-15T16:49:49.166057+00:00",
          "changed_variables": {
            "this": {
              "entity_id": "script.simulatore_alba_con_supporto_spegnimento",
              "state": "off",
              "attributes": {
                "last_triggered": "2023-05-15T16:49:30.252331+00:00",
                "mode": "single",
                "current": 0,
                "friendly_name": "Sunrise Simulator Manual"
              },
              "last_changed": "2023-05-15T16:49:30.267123+00:00",
              "last_updated": "2023-05-15T16:49:30.267123+00:00",
              "context": {
                "id": "01H0G52AM4WV16MJZ8BKNPD7Z4",
                "parent_id": "01H0G52AM226CYHKSHQTGWP7PW",
                "user_id": null
              }
            },
            "duration": 2,
            "target": [
              "light.balcone_camera"
            ],
            "brightness": 75,
            "dur": 2,
            "entities": [
              "light.balcone_camera"
            ],
            "temp": [],
            "color": [
              "light.balcone_camera"
            ],
            "maxbri": 75,
            "context": {
              "id": "01H0G52X36W56ZJYH58B79QMYH",
              "parent_id": "01H0G52X354VRSFJHEF4AC4SWN",
              "user_id": null
            }
          }
        }
      ],
      "sequence/0/parallel/0/sequence/0": [
        {
          "path": "sequence/0/parallel/0/sequence/0",
          "timestamp": "2023-05-15T16:49:49.167186+00:00"
        }
      ],
      "sequence/0/parallel/0/sequence/0/if": [
        {
          "path": "sequence/0/parallel/0/sequence/0/if",
          "timestamp": "2023-05-15T16:49:49.167334+00:00",
          "result": {
            "result": false
          }
        }
      ],
      "sequence/0/parallel/0/sequence/0/if/condition/0": [
        {
          "path": "sequence/0/parallel/0/sequence/0/if/condition/0",
          "timestamp": "2023-05-15T16:49:49.167383+00:00",
          "result": {
            "result": false,
            "entities": []
          }
        }
      ],
      "sequence/0/parallel/1/sequence/0": [
        {
          "path": "sequence/0/parallel/1/sequence/0",
          "timestamp": "2023-05-15T16:49:49.167828+00:00",
          "result": {
            "choice": "then"
          }
        }
      ],
      "sequence/0/parallel/1/sequence/0/if": [
        {
          "path": "sequence/0/parallel/1/sequence/0/if",
          "timestamp": "2023-05-15T16:49:49.167909+00:00",
          "result": {
            "result": true
          }
        }
      ],
      "sequence/0/parallel/1/sequence/0/if/condition/0": [
        {
          "path": "sequence/0/parallel/1/sequence/0/if/condition/0",
          "timestamp": "2023-05-15T16:49:49.167949+00:00",
          "result": {
            "result": true,
            "entities": []
          }
        }
      ],
      "sequence/0/parallel/1/sequence/0/then/0": [
        {
          "path": "sequence/0/parallel/1/sequence/0/then/0",
          "timestamp": "2023-05-15T16:49:49.168684+00:00"
        }
      ],
      "sequence/0/parallel/1/sequence/0/then/0/repeat": [
        {
          "path": "sequence/0/parallel/1/sequence/0/then/0/repeat",
          "timestamp": "2023-05-15T16:49:49.168888+00:00",
          "changed_variables": {
            "repeat": {
              "first": true,
              "index": 1
            }
          },
          "result": {
            "result": false
          }
        }
      ],
      "sequence/0/parallel/1/sequence/0/then/0/repeat/while/0": [
        {
          "path": "sequence/0/parallel/1/sequence/0/then/0/repeat/while/0",
          "timestamp": "2023-05-15T16:49:49.168931+00:00",
          "result": {
            "result": false,
            "entities": []
          }
        }
      ]
    },
    "config": {
      "alias": "Sunrise Simulator Manual",
      "sequence": [
        {
          "parallel": [
            {
              "if": [
                {
                  "condition": "template",
                  "value_template": "{{ temp | count > 0 }}"
                }
              ],
              "then": [
                {
                  "repeat": {
                    "while": [
                      {
                        "condition": "template",
                        "value_template": "{{ repeat.index <= maxbri\n      and not ( repeat.index > 0 \n\t\t\t\t\tand states.light\n\t\t\t\t\t\t\t| selectattr('entity_id', 'in', temp)\n\t    \t\t\t\t\t| selectattr('state', 'eq', 'on')\n\t\t    \t\t\t\t| list | count != temp | count ) }}"
                      }
                    ],
                    "sequence": [
                      {
                        "service": "light.turn_on",
                        "data": {
                          "brightness_pct": "{{ iif(repeat.index < 100, repeat.index, 100) }}\n",
                          "color_temp": "{{ iif(repeat.index < maxbri, ( 500 - ( (repeat.index-1) * 2.5 * (100/maxbri) ) ) | int, 185 ) }}\n",
                          "transition": "{{ ( ( dur * 60) / maxbri ) | int }}\n"
                        },
                        "target": {
                          "entity_id": "{{ temp }}\n"
                        }
                      },
                      {
                        "delay": {
                          "milliseconds": "{{ (  ( ( states('input_number.sunrise_sim_time') | int(10) * 60) / maxbri ) * 1000\n ) | int }}\n"
                        }
                      }
                    ]
                  }
                }
              ]
            },
            {
              "if": [
                {
                  "condition": "template",
                  "value_template": "{{ color | count > 0 }}"
                }
              ],
              "then": [
                {
                  "repeat": {
                    "while": [
                      {
                        "condition": "template",
                        "value_template": "{{ repeat.index <= maxbri\n      and not ( repeat.index > 0 \n\t\t\t\t\tand states.light\n\t\t\t\t\t\t\t| selectattr('entity_id', 'in', color)\n\t    \t\t\t\t\t| selectattr('state', 'eq', 'on')\n\t\t    \t\t\t\t| list | count != color | count ) }}"
                      }
                    ],
                    "sequence": [
                      {
                        "service": "light.turn_on",
                        "data": {
                          "rgb_color": "{% if repeat.index <= maxbri/6 %}\n  [255,0,0]\n{% elif repeat.index <= (maxbri/6) * 2  %}\n  [255,77,0]\n{% elif repeat.index <= (maxbri/6) * 3 %}\n  [255,103,0]\n{% elif repeat.index <= (maxbri/6) * 4 %}\n  [255,129,0]  \n{% else %}\n  [255,167,0]\n{% endif %}\n",
                          "brightness_pct": "{{ iif(repeat.index < 100, repeat.index, 100) }}\n",
                          "transition": "{{ (  ( dur * 60) / maxbri  ) | int }}\n"
                        },
                        "target": {
                          "entity_id": "{{ color }}\n"
                        }
                      },
                      {
                        "delay": {
                          "milliseconds": "{{ (  ( ( dur * 60) / maxbri ) * 1000 ) | int }}\n"
                        }
                      }
                    ]
                  }
                }
              ]
            }
          ]
        }
      ],
      "variables": {
        "dur": "{{ duration | default('10', true) | int(10) }}\n",
        "entities": "{% set res = namespace(r=[]) %}  {% if target is defined %}    \n  {% set target = [target] if target is string else target %} \n  {% for t in target %}\n    {% if t[0:6] == 'light.' and t in expand(states.light) | map(attribute='entity_id') | list %}\n      {% set res.r = res.r + [t] %}\n    {% elif t[0:6] == 'group.' and t in states.group | map(attribute='entity_id') | list %}\n      {% set res.r = res.r + states.group \n                              | selectattr('entity_id', 'eq', t) \n                              | map(attribute='attributes.entity_id')\n                              | list\n                              | default([''], true)\n                              | first\n                              | select('in', states.light \n                                              | rejectattr('state','in', ['unavailable', 'unknown', None]) \n                                              | map(attribute='entity_id') \n                                              | list) \n                              | list %}\n    {% else %}\n      {% set res.r = res.r + states.light \n                               | rejectattr('state','in', ['unavailable', 'unknown', None])\n                               | selectattr('entity_id', 'in', area_entities(t))\n                               | map(attribute='entity_id') \n                               | list %}\n    {% endif %}\n  {% endfor %}\n{% else %}\n  {% set res.r = states.light \n                   | rejectattr('state','in', ['unavailable', 'unknown', None])\n                   | map(attribute='entity_id') \n                   | list %}\n{% endif %} {{ res.r }}\n",
        "temp": "{% set res = namespace(r=[], l=[]) %}\n{% if entities | default([], true) | count > 0 %}\n  {% for sun in entities %}\n    {% if states(sun) not in ['unavailable','unknown']\n        and 'color_temp' in states[sun].attributes.supported_color_modes\n        and states[sun].attributes.supported_color_modes | count == 1 %}\n      {% set res.r = res.r + [sun] %}\n    {% endif %}\n  {% endfor %}\n{% endif %}\n{{ res.r }}\n",
        "color": "{% set res = namespace(r=[], l=[]) %}\n{% if entities | default([], true) | count > 0 %}\n  {% for sun in entities %}\n    {% if states(sun) not in ['unavailable','unknown']\n        and 'hs' in states[sun].attributes.supported_color_modes %}\n      {% set res.r = res.r + [sun] %}\n    {% endif %}\n  {% endfor %}\n{% endif %}\n{{ res.r }}\n",
        "maxbri": "{% set bri = brightness | default('100', true) |int(100) %}\n{{ bri if bri <= 100 else 100 }}\n"
      },
      "mode": "single"
    },
    "blueprint_inputs": null,
    "context": {
      "id": "01H0G52X36W56ZJYH58B79QMYH",
      "parent_id": "01H0G52X354VRSFJHEF4AC4SWN",
      "user_id": null
    }
  },
  "logbookEntries": [
    {
      "when": 1684169389.16382,
      "state": "on",
      "entity_id": "script.simulatore_alba_con_supporto_spegnimento"
    },
    {
      "when": 1684169389.171553,
      "state": "off",
      "entity_id": "script.simulatore_alba_con_supporto_spegnimento",
      "context_state": "on",
      "context_entity_id": "script.simulatore_alba_con_supporto_spegnimento"
    }
  ]
}

I have edited the code, sorry for the frustration, please try the new one

I have tried the new script, and it works very well. Thank you for your help and willingness!

1 Like

I’m using gledopto bulbs, which have two color modes: xy and color_temp. This automation fails for this, since color temperature operation requires the mode color_temp to be the only one mode present for the light. I wanted to use the color temperature operation here, so I removed the line

and states[sun].attributes.supported_color_modes | count == 1 

in the if statement for the temp variable, then it worked without issues. I’m not sure if using the xy mode instead of hs translates easily, but for me this is satisfactory.

Final script:

alias: Sunrise Simulator Manual
sequence:
  - parallel:
      - if:
          - condition: template
            value_template: "{{ temp | count > 0 }}"
        then:
          - repeat:
              while:
                - condition: template
                  value_template: |
                    {{ repeat.index <= maxbri
                          and not ( repeat.index > 1 
                                             and states.light
                                                        | selectattr('entity_id', 'in', temp)
                                                        | selectattr('state', 'eq', 'on') | list | count 
                                                      != temp | count ) }}
              sequence:
                - service: light.turn_on
                  data:
                    brightness_pct: |
                      {{ iif(repeat.index < 100, repeat.index, 100) }}
                    color_temp: >
                      {{ iif(repeat.index < maxbri, ( 500 - ( (repeat.index-1) *
                      2.5 * (100/maxbri) ) ) | int, 185 ) }}
                    transition: |
                      {{ ( ( dur * 60) / maxbri ) | int }}
                  target:
                    entity_id: |
                      {{ temp }}
                - delay:
                    milliseconds: >
                      {{ (  ( ( states('input_number.sunrise_sim_time') |
                      int(10) * 60) / maxbri ) * 1000
                       ) | int }}
      - if:
          - condition: template
            value_template: "{{ color | count > 0 }}"
        then:
          - repeat:
              while:
                - condition: template
                  value_template: |
                    {{ repeat.index <= maxbri
                          and not ( repeat.index > 1 
                                             and states.light
                                                        | selectattr('entity_id', 'in', color)
                                                        | selectattr('state', 'eq', 'on') | list | count 
                                                      != temp | count ) }}
              sequence:
                - service: light.turn_on
                  data:
                    rgb_color: |
                      {% if repeat.index <= maxbri/6 %}
                        [255,0,0]
                      {% elif repeat.index <= (maxbri/6) * 2  %}
                        [255,77,0]
                      {% elif repeat.index <= (maxbri/6) * 3 %}
                        [255,103,0]
                      {% elif repeat.index <= (maxbri/6) * 4 %}
                        [255,129,0]  
                      {% else %}
                        [255,167,0]
                      {% endif %}
                    brightness_pct: |
                      {{ iif(repeat.index < 100, repeat.index, 100) }}
                    transition: |
                      {{ (  ( dur * 60) / maxbri  ) | int }}
                  target:
                    entity_id: |
                      {{ color }}
                - delay:
                    milliseconds: |
                      {{ (  ( ( dur * 60) / maxbri ) * 1000 ) | int }}
variables:
  dur: |
    {{ duration | default('10', true) | int(10) }}
  entities: |
    {% set res = namespace(r=[]) %}  {% if target is defined %}    
      {% set target = [target] if target is string else target %} 
      {% for t in target %}
        {% if t[0:6] == 'light.' and t in expand(states.light) | map(attribute='entity_id') | list %}
          {% set res.r = res.r + [t] %}
        {% elif t[0:6] == 'group.' and t in states.group | map(attribute='entity_id') | list %}
          {% set res.r = res.r + states.group 
                                  | selectattr('entity_id', 'eq', t) 
                                  | map(attribute='attributes.entity_id')
                                  | list
                                  | default([''], true)
                                  | first
                                  | select('in', states.light 
                                                  | rejectattr('state','in', ['unavailable', 'unknown', None]) 
                                                  | map(attribute='entity_id') 
                                                  | list) 
                                  | list %}
        {% else %}
          {% set res.r = res.r + states.light 
                                   | rejectattr('state','in', ['unavailable', 'unknown', None])
                                   | selectattr('entity_id', 'in', area_entities(t))
                                   | map(attribute='entity_id') 
                                   | list %}
        {% endif %}
      {% endfor %}
    {% else %}
      {% set res.r = states.light 
                       | rejectattr('state','in', ['unavailable', 'unknown', None])
                       | map(attribute='entity_id') 
                       | list %}
    {% endif %} {{ res.r }}
  temp: |
    {% set res = namespace(r=[], l=[]) %}
    {% if entities | default([], true) | count > 0 %}
      {% for sun in entities %}
        {% if states(sun) not in ['unavailable','unknown']
            and 'color_temp' in states[sun].attributes.supported_color_modes %}
          {% set res.r = res.r + [sun] %}
        {% endif %}
      {% endfor %}
    {% endif %}
    {{ res.r }}
  color: |
    {% set res = namespace(r=[], l=[]) %}
    {% if entities | default([], true) | count > 0 %}
      {% for sun in entities %}
        {% if states(sun) not in ['unavailable','unknown']
            and 'hs' in states[sun].attributes.supported_color_modes %}
          {% set res.r = res.r + [sun] %}
        {% endif %}
      {% endfor %}
    {% endif %}
    {{ res.r }}
  maxbri: |
    {% set bri = brightness | default('100', true) |int(100) %}
    {{ bri if bri <= 100 else 100 }}
mode: single

Can you please post the supported_color_modes of your lamps?
Removing this line will possible have a strange impact for lamps with colour since they also have color temperature.

@ChreeceGR, somehow it does not work for me. Could you please help me?
I’ve created the script (copy & paste), created an automation, triggered the automation manually,
but the script does seem to stop hat “Execute in parallel”.
The name of the light is correct. Already tried rerun / restarting HA.

alias: Test
description: ""
trigger: []
condition: []
action:
  - service: script.sunrise_simulator_manual
    data:
      duration: 10
      target:
        - light.hue_surimu
      brightness: 100
mode: single
1 Like

Can you please post the attributes of the light?