Set a random color on a light

This service call will set a random color. Examples based on rgb_color will often make the light pale or even white. The example below uses hs_color (hue/saturation) to always select an actual color. The color in this example will have a random 80%-100% saturation and full brightness.

    - service: light.turn_on
      entity_id: light.office
      data_template:
        hs_color:
          - "{{ range(360)|random }}"
          - "{{ range(80,101)|random }}"
        brightness_pct: 100

The next example cycles a light through all saturated colors. It will move around the color wheel by 30 degrees each time it is called, for example when pressing a button (that makes it 12 steps though all colors).

    - service: light.turn_on
      entity_id: light.office
      data_template:
        hs_color:
          - "{{ (30 + (state_attr('light.office', 'hs_color')[0] or 0)) % 360 }}"
          - 100
        brightness_pct: 100
        transition: 1
34 Likes

This is great!! thx

if anyone is struggling with this like i did, here is an example:

- id: disco
  alias: Disco
  trigger:
  - entity_id: sensor.xkontakt1
    platform: state
  condition:
  - condition: state
    entity_id: sensor.xkontakt1
    state: double
  action:
    service: light.turn_on
    entity_id: light.mqtt_json_light_4
    data_template:
      hs_color:
        - "{{ (30 + (state_attr('light.mqtt_json_light_4', 'hs_color')[0] or 0)) % 360 }}"
        - 100
      brightness_pct: 100
      transition: 1
5 Likes

SO GOOD
you saved my day <3

1 Like

May anyone explain the numerical part?! I’m wondering how this is working :slight_smile:

I tried to do the same with hs_color to the brightness but I’m not sure how exactly the code works… I mean for example why do I need the
-100

That is because hs_color has two components, hue and saturation:

        hs_color:
          - hue here
          - saturation here
        brightness_pct: brightness here

The value 100 means full saturation (i.e. no white).

1 Like

oh shit i see… sorry this was really too easy :smiley:

and how can i imagine the calculation line?!
I mean you are “reading” the first element of the hs_color this is [0] and add 30, right?! Or where do i find a good explanation for this kind of super squeezed one line calculation :slight_smile: :slight_smile: :slight_smile:

thanks a lot

{{ (30 +

Add 30 …

(state_attr('light.office', 'hs_color')[0]

… to the current hue …

or 0))

… or to zero if no hue exists (because the light is off or set to white) …

% 360 }}

… and clamp to values 0-359 (because hue is on a color wheel with 360 degrees).

1 Like

nice, a complete color loop would also be helpful

The standard light integration has built-in colorloop and random colors. See the effect attribute.
effect: colorloop will make the light change color continuously.
effect: random will set a static random color.

Don’t work with trådfri and the Ikea gateway. Or at least I can’t get it to work.

That’s right, the effects depend on device specific implementation. I noticed that also deconz unfortunately is missing the random effect.
I guess it’s time for me to learn Python and get to work! :smile:

Hi all,

If you guys are looking for a smooth change of color and wanted to change it with a controller, I recommend you to take a look to ControllerX, which is an AppDaemon app that allows binding remotes with lights and media players.

In the case of color lights, ControllerX detects automatically the type of light and it goes through a loop of colors when pressing the controller. Here you have an example of this. You can check the documentation in here to know the supported controllers.

Let me know if you have any questions :slight_smile:

1 Like

sounds elegant, but doesn’t work for me with yeelight

Hello there, I’m trying ot reproduce this on Node-red

{“hs_color”:[“{{ range(360)|random }}”,“{{ range(80,100)|random }}”]}

But this is giving me this error:
“Call-service API error. Error Message: expected float for dictionary value @ data[‘hs_color’]”

what is my mistake here?

Thanks in advance!

Hi there,

Has anyone tried to set a random color by using the profiles in light_profiles.csv as described in Light?

Thanks in advance!

Hi guys,

Have been using this template for my RGBCCT bulbs for a long time and it has been working so well.
But after the 2021.8.0 update, I have seen that my color wheel also shows the White and Warm Slider, which was not present before. I did also update all bulbs to the latest esphome after the updates, so I’m not 100% sure whether it could be that which is giving me issues.

What I experience is, when pushing the button now, it just changes from one white to another. I never get any colors.

What do I need to add to this in order to set warm and cold to 0?

- service: light.turn_on
      entity_id: light.laura_alle_spots
      data_template:
        hs_color:
          - "{{ range(360)|random }}"
          - "{{ range(80,101)|random }}"
        brightness_pct: 30

Thanks :slight_smile:

Searching through forum trying to figure out how to set the color on my Slyvania outside lights.
I check the entity and it has an attribute called ‘h’ and ‘hue’ but when I call it I get an error in my logs that says:

Error while executing automation automation.98_turn_on_outside_lights: extra keys not allowed @ data['hue']
Error while executing automation automation.98_turn_on_outside_lights: extra keys not allowed @ data['h']

Which is funny because it does indeed know I’m trying to call it because it gives me those 2 errors ‘hue’ and ‘h’… which are both listed in the attributes.

Here’s the automation I’m using that gives me the error:

- alias: 98 - Turn on Outside lights
  description: ""
  trigger:
    - platform: sun
      event: sunset
      offset: "00:30:00"
  action:
    - service: light.turn_on
      entity_id: light.outside_front_lights
      data_template:
        hue:
          - "{{ (30 + (state_attr('light.outside_front_lights', 'hue')[0] or 0)) % 360 }}"
          - 100
        brightness: 80
        mode: single

    - service: notify.mobile_app_sm_f926u1
      data:
        message: "TTS"
        title: "Turning on Outside Lights."
        data:
          channel: alarm_stream_max
          ttl: 0
          priority: high

Is there a particular reason why I cannot set the attribute on this?

Thank you!

Nobody else in this thread uses hue:
I assume that is the reason.

Ok so I set it to hs_color then I get this:

Error while executing automation automation.98_turn_on_outside_lights: extra keys not allowed @ data['hue']
Error while executing automation automation.98_turn_on_outside_lights: extra keys not allowed @ data['h']
Error while executing automation automation.98_turn_on_outside_lights: extra keys not allowed @ data['hs_color:']

So that tells me it knows I’m trying to set ‘hue’ color but it says ‘No’

This is my listed attributes:

outside

Not sure but leaning on my old php days wouldn’t that be ‘Color[0].hue’?

Don’t use lovelace/dashboard when you are developing. Use the developer tools (hence the name).

But why are you still getting errors on hue?