Sunrise Simulator. Wake up gently to light

That comment made my day!
You are very welcome

Thanks so much for this. Your version works perfectly for me!

Hi, the updated one still causes ALL my lights to turn on rather than the ones called in the automation. I tried the script @zynth posted and that worked. I think it’s a function of the way my light switches work (I have Zooz, he has Shelly, but same issue that caused his not to work seems to apply to mine).
Nevertheless, thanks so much for starting this thread and for trying to get yours to work for me.

Let’s say I wanted to use the sunrise palette BUT THEN move to regular warm white, or maybe move through some of the warmer whites towards daylight white. Would that be doable? Do most bulbs treat “temperatures” of white as a completely different mode from RGB colors?

Yes it is doable, but not with the current script. You should do the CCT transition after the RGB Colour Palette done it’s thing…

Oh, that’s a good idea. I was trying to figure out how to work it into the script, but I suppose I could have the automation run the script, and then run another of my own creation that transitions to warm white and then on to daylight.

Hi there! really great script, I have been using it per months!

Now, I have change the firmware int he desklamp I am using it, unfortunatly, due to this change my lamp doesn’t work for brigthness below 5%, how can stablish a min_bri in the script? I was trying setting a max value between the one is being calculate and this 5 to reach always this min value for brightness_pct but it doesn’t work and I am so lost in here

I tried this:
brightness_pct: {{ [ [maxbri, ((repeat.index * maxbri) / 100)] | min, 5] | max }}

Could you help me?

I don’t think that you should change anything.
Your lamp wouldn’t do anything for the first 5 steps (1-5%) but eventually will turn on after that.
Is it important on your routine that this lamp will turn on directly for the first steps?

Yep, it depends on the timming you manage. I stablish a duration on 60 minutes, so that the lamp doesn’t work during this firsts steps.

I am using this feature to start my daily routine in fornt of the laptop, so that I need ligth from the very beggining. Or at least I would like to :slight_smile:

I was trying to edit it with ChatGPT… never again… it is no as smarter as needed haha

Do the following changes where brightness_pct: is declared and then call the script as normally adding the
minbri: 5 variable in the data: section

1 Like

I didn’t change it in the line 156:

brightness_pct: >
 {{ [((repeat.index * maxbri) / 100) | int , maxbri] | min
 }}

I guess here is not a defintion, more an update.

But it didn’t do the trick. It is still waiting long till it turns on.

Big thanks in advance

EDIT: To be more clear, the code remains as:

alias: sunrise
icon: mdi:weather-sunset-up
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 + minbri | default(0, true))
                      * 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 + minbri | default(0, true)) * 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
brightness_pct: >
 {{ [((repeat.index * maxbri) / 100) | int , maxbri] | min
 }}

Yes this line should also get the minbri:

brightness_pct: >
 {{ [(( (repeat.index + minbri | default(0, true)) * maxbri) / 100) | int , maxbri] | min
 }}

Thank you! working like a charm!

It is a good improvement to get another parameter to stablish the minimum brigthness if needed :slight_smile:

1 Like

Hello,
totally new to HA and I know I have a lot to learn… But I would realy like to try your script and finally get the light alarm clock of my dreams!
Could you describe the necessary actions to take to get it running? What do I have to do with the 2 codes that you have posted at the beginning?
The hue color light bulb and Ikea remote switch that I would like to use for this, are already working together in HA. I am using ZHA if this is of any importance…
Thank you very much for your work!

Hello and welcome to this wonderful world, it needs some time to get used to but then you will enjoy every moment with it…
What you have to do is to create a new script (Settings → Automations & Scenes → Scripts → Create New → Create New Script).
Now click the 3 dots from the top-right corner → Edit as Yaml.
Press ctrl+a on your keyboard to select all the code from this tab and then delete all and paste the code from above (the long) one.
Save it and you have the script available

Everytime you need to call it (to start the light routine) you should do what the example service says from above…

I hope I did help you to go through the necessary steps…

Happy Waking Up!

Thank you very much for your nice reply!
I managed to make the script but I don’t know how to program it, that the script starts at the right time… :see_no_evil:
Once this is done I guess my next question would be how I can have the wake up time available on the dashboard to easily change it or arm/disarm the alarm…or even better… how it could automatically sync with my normal wake up timer on my android phone. As an other blueprint is doing that I have tried…

There are numerous tutorials and documents to search on how to call a script on home assistant…
To synchronize your android clock with ha to have it activated you need the ha companion app and then an automation that listens for the alarm time that contains as action the script call.
That’s the theory, now study… :slight_smile:

Hello everybody, only me using an Yeelight wifi smart bulb RGB not able to have it turned off? Seems like the automation does not send the on command, can’t understand why
Same when changing the yeelight bulb with a led strip.
Any suggestions?

Is your problem related to the script of this thread?
Which automation doesn’t turn the lamps off?

yes this script. I managed to make it turn on although I don’t know why it turns on 2 minutes before the alarm is set but not a big deal, the thing now is that it turns on full power and after 2 secs goes back to 1% power to start sunrising gradually.
And in the end it doesn’t turn off :frowning: