Sunrise Simulator. Wake up gently to light

How did you call the script?
Can you please post trace from the script?

Yes that’s a strange behaviour. It shouldn’t dim any lights if you passed the target variable with incompatible lamps

I cant even save it bacause of this error. It shows up after pasting the code and trying to save the script

Can’t replicate, just added another instance without any problem. Please create a new script, edit in yaml, delete all contents and then paste the script code

=Multiple complete updates of this comment=
I have 2 issues with this script.

Issue #1

When starting the script, with the light turned off:
Nothing happens. My light entity does not become included in neither the color nor temp array. And therefore none of the 2 if-loops is started.

When starting the script, with the light turned on:
It works fine. The light entity becomes part of the color array and the color-if-statement-loop works fine.

My dimmer attributes when it is off:

supported_color_modes:
  - brightness
friendly_name: light_kitchen_bar
supported_features: 32

My dimmer attributes when it is on:

supported_color_modes:
  - brightness
friendly_name: light_kitchen_bar
supported_features: 32
color_mode: brightness
brightness: 255

This line is the issue. This brightness-attribute only exists for my dimmer when the light is on.

                | selectattr('attributes.brightness', 'defined')

@ChreeceGR is there a good reason for this check to exist here? Removing it means the script works for me.
As soon as I know, I will create an updated version of this script that also supports non-color lights in a nice way. Then this wakeuplight script is the script that rules them all :wink:

Issue #2
My lidl ledstrip supports both xy and color_temp.
For a wakeuplight, xy is less desired than color_temp because in ledstrips that last one is usually way brighter.
Unfortunately, when supporting both, the script chooses for xy instead of color_temp.

I would therefore propose to rewrite this piece of code to the following:

      temp: |
        {{ expand(entities)
              | selectattr('attributes.supported_color_modes', 'contains', 'color_temp')
              | map(attribute='entity_id')
              | list }}

A subsequent issue is that not the entire kelvin-range is used by the script. My ledstrip supports 2000-6666K, but the script ends on 5405. That piece of code looks weird to me.
What exactly does this code do and why? It does not seem to make to me, sense setting the color_temp this way.

                    - service: light.turn_on
                      data:
                        color_temp: >
                          {{ iif(repeat.index < maxbri, ( 500 - ( (repeat.index-1) *
                          2.5 * (100/maxbri) ) ) | int, 185 ) }}

Test specs: newest standard HA, Shelly Dimmer 2.

Thank you for your post.
My thoughts creating this script was simulate something like sunrise (therefore the name of it Sunrise Simulator).
Using RGB lights would be the best available simulation for it so if the lamp support it, that would be the preferable method.
If still, there is an option for color temperature would be also ok since there could be a transition from warm light to cool white (not exactly sunrise but close to the warm morning light).
I can understand that dimming lights from 0 to 100 could also wake you up, but it wasn’t the scope of this script…
As for prefering color_temp over xy I understand your preference but is also something that someone else wouldn’t prefer since you are mentioning also a specific hardware…

The line with the brightness is necessary for the script because the brightness of the lights should increase in the script.

Please read the conditions for the light entities that work with the script, since dimmers are not supported.

You are right about the color_temp calculation, it isn’t right and needs a rewrite, will try to change it and update the script

Thanks!
I have made some modifications, including an update of the color_temp calculation.
This way it works with more hardware as generic wakeuplight.
With all my zigbee/wifi smart lights and dimmers, this script now works.

    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
                                                          ==  0 ) }}
                  sequence:
                    - service: light.turn_on
                      data:
                        ######################
                        # Updated. Instead of using color_temp it now uses Kelvin.
                        # User can add kelvin_min and kelvin_max as parameter to the script call.
                        # Otherwise the script will figure out what the min and max is (see variables)
                        ######################
                        kelvin: >
                          {{ iif(repeat.index < maxbri, ( kelv_min + ( (repeat.index-1) *
                          ((kelv_max-kelv_min)/maxbri) ) ) | int, kelv_max ) }}
                        transition: |
                          {{ ( ( dur * 60) / maxbri ) | int }}
                      target:
                        entity_id: |
                          {{ temp }}
                    - service: light.turn_on
                      data:
                        brightness_pct: |
                          {{ iif(repeat.index < 100, repeat.index, 100) }}
                      target:
                        entity_id: |
                          {{ temp }}
                    - delay:
                        milliseconds: |
                          {{ (  ( ( dur  * 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
                                                          == 0 ) }}
                  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 %}
                        transition: |
                          {{ (  ( dur * 60 ) / maxbri  ) | int }}
                      target:
                        entity_id: |
                          {{ color }}
                    - service: light.turn_on
                      data:
                        brightness_pct: |
                          {{ iif(repeat.index < 100, repeat.index, 100) }}
                      target:
                        entity_id: |
                          {{ color }}
                    - delay:
                        milliseconds: |
                          {{ (  ( ( dur * 60 ) / maxbri ) * 1000 ) | int }}
    variables:
      dur: |
        {{ duration | default(10, true) | int(10) }}
      
      ################
      # In the expand(target) code below I removed the line "*| selectattr('attributes.brightness', 'defined')*"
      # Some dimmers don't have this attribute when the light is off, only when on.
      ################
      entities: |
        {% if target is defined %}
          {{ 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 }}
        {% else %}
          {{ 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 %}
      ################
      # Updated the code below so that it prefers "color_temp" over "xy" and others, when available.
      ################
      temp: |
        {{ expand(entities)
              | selectattr('attributes.supported_color_modes', 'contains', 'color_temp')
              | map(attribute='entity_id')
              | list }}
      color: |
        {{ entities | reject('in', temp) | list }}
      maxbri: |
        {% set bri = brightness | default(100, true) | int(100) %}
        {{ bri if bri < 100 else 100 }}
      ################
      # Added support for defining your own kelvin range.
      # When not specified, it will take the min/max of the first entity that you gave.
      ################
      kelv_min: |
        {% set tmp = kelvin_min | default(state_attr(entities[0], 'min_color_temp_kelvin'), true) | int(2000) %}
        {{ tmp }}
      kelv_max: |
        {% set tmp = kelvin_max | default(state_attr(entities[0], 'max_color_temp_kelvin'), true) | int(6000) %}
        {{ tmp }}
    mode: single

Nice! Thank you!
Please share it to a seperate post where you can also maintain it :wink:

I will consider that.
It would be awesome to have one wakeuplight-script to rule them all :smiley:
It’s such a generic feature that it makes sense to have that.

For now, I won’t share it separately because I don’t really know if it exists anywhere else.
I can’t imagine that it doesn’t, but haven’t found it yet.
If there isn’t, then I will consider it.

In the meantime, you can consider using the kelvin method I proposed in the code.
For your usecase, that would be a better way to handle the “color_temp” situation.

The kelvin method you proposed unfortunately would fail if user gives values outside the lamp’s ranges, or if there are lamps with variable ranges (not all the entities have the same values).
I am rebuilding the script to get the max/min values from the entities itself.
I’m testing it and will update it in the next minutes.

So I have updated the script, should also work just as wakeup light if there are entities with brightness color_mode.
Also it will change the color temperature based on each entity seperately.
Needs testing
@zynth, @urnlahzer can you please test it?

NEW UPDATE

WLED entities that include the Sunrise effect are also supported

When I call the script from dev tools all is working fine.
Using it through an automation gives high brightness / weird transitions at the beginning.

Wondering if someone else also has this issue? I’m using a Lidl led strip over Zigbee.
Thinking it maybe has to do with Light transition not working - Simple light transition - Configuration - Home Assistant Community (home-assistant.io)

Seems to be a general issue that isn’t related to the script. Sorry I don’t think I can help with that :frowning:

Is it possible to force CCT mode with lights that has RGB as well?

Not with the current code. Will implement that asap

I have updated the script to support that. You can use the cct: variable to achieve that.
Also please notice the breaking change. The variable brightness: now is used for the entities to be forced only to dim and the new variable for the maximum brightness is max_brightness:
Please check it and reply if it works as intended, unfortunately I’m unable to test it

Works perfectly! Thank you!

1 Like

I updated the script some minutes ago, please double check if you have the latest one

Just wanted to say thanks! My Kasa bulbs don’t support transitions and this somehow works well! Really appreciate the effort here.

1 Like