Help with a script to gradually change colour

Hello everyone,

as the title says, i need some help to adjust a script that i found online. The following script is turns a light on and within 30 min it gradually increases the brightness but also is changing the temperature from warm light to natural light. This works as expected, however 1% of brightness with my bulb is too bright to fulfill it’s intention (sunrise wake up).

The idea is instead of using the temperature, to use rgb values which the bulb has significantly lower brightness output.

Here is the script:

alias: Sunrise wake-up
sequence:
  - variables:
      start_temp: 370
      end_temp: 273
      duration_mins: 30
  - service: light.turn_on
    data:
      color_temp: '{{ start_temp }}'
      brightness: 1
    entity_id: light.mybedlight
  - delay: '1'
  - repeat:
      count: '{{ duration_mins }}'
      sequence:
        - service: light.turn_on
          data:
            brightness: '{{ (repeat.index / duration_mins * 255) | int }}'
            color_temp: >-
              {{ (start_temp + repeat.index / duration_mins * (end_temp -
              start_temp)) | int }}
          entity_id: light.mybedlight
        - delay: '60'
mode: single

Could anyone convert the above to use rgb values?
Unfortunately my lights do not support transition.

Thank you very much
Kind Regards
M

1 Like

Have a look hs value instead.
It’s far simpler to use when you want to change color gradually from one point to another.

Thanks, i didn’t think of that… Updated to the following but it doesn’t work:

alias: Sunrise color
sequence:
  - variables:
      start_hs:
        - 360
        - 100
      end_hs:
        - 30
        - 100
      duration_mins: 10
  - service: light.turn_on
    data:
      hs_color: '{{ start_hs }}'
      brightness: 1
    entity_id: light.mybedlight
  - delay: '1'
  - repeat:
      count: '{{ duration_mins }}'
      sequence:
        - service: light.turn_on
          data:
            brightness: '{{ (repeat.index / duration_mins * 255) | int }}'
            hs_color:
              - {{ (start_hs + repeat.index / duration_mins * (end_hs - start_hs)) | int }}
              - 100
          entity_id: light.mybedlight
        - delay: '60'
mode: single

I assume that the Hue value is the problem. I don’t really understand the logic that was applied even with the temperature so i cannot figure it out…

Although the script runs correctly and there are no errors, the light turs Red (which is correct) but in the lovelace it appears as off and of course it doesn’t change color or brightness… I think i am out of the 360 degrees value for the hue…

start_hs is an array/list.
I’m not good at yaml. But I believe you need to use start_hs[0] and the same with end_hs

Didn’t work as well… Thanks for your help the suggestion to use the hs_color was very helpful.

Now for the syntax, if anyone else knows I would be grateful!

Thanks

I believe there is something wrong with your formula.
When I write it out, I get this:
(perhaps I misunderstood something)

I doubt the HS wheel is that sensitive to changes.
Most likely removing 0.0003 is done and then rounded back to 360.

In my HS color automation in Node red I use steps of 3

Hey Hellis,

thanks for still checking out on this. As i mentioned, i got the script online specifically from this site:
Sunrise Alarm with Home Assistant - peddicord.xyz and i thought i could convert it to RGB at first.

The script is setting the variables in Mireds and it works as intended but now that we changed the variables we may need to change the hs_color calculation as well. It seemed logical to me to use HS (after your suggestion) because in reality hue is just one number (unlike RGB) and i thought that it can work. But i didn’t think it through correctly.

It seems fairly simple as the logic is: get the starting number 0 or 360 (this is the red colour) and turn it to 30 (orange colour) depending on the time in minutes. To simplify it we can set time to 30 min to do the transition and since we speak only for 30 value changes (from 0 to 30) it should change 1 degree per minute.

Unfortunately, i don’t have the programming knowledge to achieve that.

How do you calculate it in Node Red?

Thanks
Kind regards

This is how I did it in Node red.

I explain it in details since it seems you do not use Node red(?).

From the left the code comes when I click twice on a remote.
It checks the current state of the light and this node returns all the data you see in developer tools of this entity and passes that with the message.

The function node is the one with the programming.
In my case I also loop the saturation between 100 and 40%
If I would not have done that the “middle” section of the code could be removed.

var sat =100;

// below is to control the saturation
if (msg.dir == true){
    if (msg.data.attributes.hs_color[1] > 70){
        sat = msg.data.attributes.hs_color[1]+1;
    }else{
        sat = msg.data.attributes.hs_color[1]+2;
    }
    if (sat > 100) sat = 100;
    msg.dir = true;
    if (sat == 100) msg.dir = false;
}else{
    if(msg.data.attributes.hs_color[1] > 70){
        sat = msg.data.attributes.hs_color[1]-1;
    }else{
        sat = msg.data.attributes.hs_color[1]-2;
    }
    if (sat < 40) sat = 40;
    msg.dir = false;
    if (sat <= 40) msg.dir = true;
}
// to here...

msg.payload = { data: {
    'entity_id': 'light.matildas_lampa',
    'hs_color': [(msg.data.attributes.hs_color[0]+3)%360, sat] // here I say set the hue to current value + 3.
 } };

return msg;

At the end it passes the new values in the message to the next node which does the service call.
A small delay and then check a variable if the loop should continue (if you change mode of the bulb or turn it off this variable is changed).
And then the code is back where it started.

1 Like

Sorry for bringing up an old topic, but I wanted to do the same thing and got it working based off this script, so thought others might find it useful.
I’m very new to home assistant and scripting with yaml, so I’m sure there’s plenty of room for improvement, but it works for me.
I took a slightly different approach in that I wanted a start point and an end point and I wanted it to go start to end in a timeframe - pretty similar though.
I also used x/y colours for no particular reason.
Here’s the code - hope it helps.

sunrise:
  alias: Sunrise
  sequence:
    - variables:
        start_brightness: 3
        end_brightness: 155
        start_x: 0.7
        end_x: 0.484
        start_y: 0.3
        end_y: 0.447
        duration_minutes: 20
        increment_brightness:
          "{{ (end_brightness - start_brightness) / duration_minutes
          }}"
        increment_x: "{{ (end_x - start_x) / duration_minutes }}"
        increment_y: "{{ (end_y - start_y) / duration_minutes }}"
    - service: light.turn_on
      data:
        xy_color: "{{ start_x }},{{ start_y }}"
        brightness: "{{ start_brightness }}"
      entity_id: light.jason_bedside_light
    - delay: 60
    - repeat:
        count: "{{ duration_minutes }}"
        sequence:
          - service: light.turn_on
            data:
              xy_color: "{{ state_attr('light.jason_bedside_light', 'xy_color')[0] | float + increment_x }},{{ state_attr('light.jason_bedside_light', 'xy_color')[1] | float + increment_y }}"
              brightness: "{{ state_attr('light.jason_bedside_light', 'brightness') | int + increment_brightness }}"
            entity_id: light.jason_bedside_light
          - delay: 60
  mode: single
  icon: mdi:sun-clock
1 Like

Hi,

Thanks for reviving this topic, i have tested your script and it works flawlesly!

Thank you very much
Kind Regards
M

1 Like

Thanks @casramsey.

Can someone please help on how to pass a defined variable into a loop? (Or what I’m doing wrong!)

The correct lamp turns on (variable works there), but the script fails when it hits the loop the variable light is unknown so has no data. (nb times reduced for testing)

- service: script.turn_on
  target:
    entity_id: script.sunrise_light
  data:
    variables:
      light: light.jason_lamp
  sunrise_light:
    alias: Sunrise light
    mode: parallel
    icon: mdi:sun-clock
    sequence:
      - variables:
          start_brightness: 1
          end_brightness: 155
          start_x: 0.7
          end_x: 0.484
          start_y: 0.383
          end_y: 0.496
          duration_minutes: 15
          increment_brightness: "{{ (end_brightness - start_brightness) / duration_minutes }}"
          increment_x: "{{ (end_x - start_x) / duration_minutes }}"
          increment_y: "{{ (end_y - start_y) / duration_minutes }}"
      - service: light.turn_on
        target:
          entity_id: "{{ light }}"
        data:
          xy_color: "{{ start_x }},{{ start_y }}"
          brightness: "{{ start_brightness }}"
      - delay: 5
      - repeat:
          count: "{{ duration_minutes }}"
          sequence:
            - if:
                - "{{ states('light') in ['off','unavailable'] }}"
              then:
                - stop: "Light turned off"
            - service: light.turn_on
              target:
                entity_id: "{{ light }}"
              data:
                xy_color: "{{ state_attr('light', 'xy_color')[0] | float + increment_x }},{{ state_attr('light', 'xy_color')[1] | float + increment_y }}"
                brightness: "{{ state_attr('light', 'brightness') | int + increment_brightness }}"
                transition: 5
            - delay: 5
Logger: homeassistant.components.script.sunrise_light
Source: helpers/script.py:410
Integration: Scripts (documentation, issues)
First occurred: 10:15:12 (2 occurrences)
Last logged: 10:15:12

Sunrise light: Repeat at step 4: Error executing script. Error for call_service at pos 1: Error rendering data template: UndefinedError: None has no element 0
Sunrise light: Error executing script. Error for repeat at pos 4: Error rendering data template: UndefinedError: None has no element 0