Random rgb color does not work

I have tried to make an automation that turns on some RGB light with a random color each time.
It works very well when using color names but as soon as i replace it with rgb colors I get erros.
Someone can see what is wrong?

ERROR (MainThread) [homeassistant.core] Invalid service data for light.turn_on: None for dictionary value @ data[‘rgb_color’]. Got ‘[000,255,255]’

action:

  • service: light.turn_on
    entity_id: light.ur
    data_template:
    rgb_color: >
    {{ [
    “[255,000,000]”,
    “[000,255,000]”,
    “[000,000,255]”,
    “[255,165,000]”,
    “[000,255,255]”,
    “[255,000,255]”,
    “[070,000,255]”
    ] |random }}
    brightness: 20

action:

  • service: light.turn_on
    entity_id: light.ur
    data_template:
    color_name: >
    {{ [
    “blue”,
    “red”,
    “green”,
    “orange”,
    “cyan”,
    “magenta”,
    “darkviolet”,
    “purple”
    ] |random }}
    brightness: 20

Yes, the problem is, rgb_color wants a list of three numbers. Unfortunately, a single template cannot return a list; it can only return a string.

I believe the usual way around this (although I haven’t really tried it myself), is to use three templates, something like this:

rgb_color: ["{{range(256)|random}}","{{range(256)|random}}","{{range(256)|random}}"]

I believe this works even though the individual numbers are really strings because they will get “coerced” into numbers. At least, I think so. At least it’s worth a try. (BTW, range(256)|random will return a number between 0 and 255, inclusive.)

Thanks for your answer :slight_smile:
Unfortuneately I don´t want a random color (a lot of colors are not pretty) but just random from a predefined list.

Hmm, then not sure how to do that. I suppose you could call a script with the random string version of the rgb_color, and the script could have a template that turns the light on that uses templates, kind of like I did, to pull the three individual pieces out. Or, I don’t know if this is possible, but can you define your own color names and use them? Or maybe use scenes?

I do not think it is possible to create own colornames unfortuneately.

I’m not sure this will work (again, can’t test because I don’t have a color bulb), but maybe something like this is worth a try:

automation:
  - alias: Random color
    trigger:
      # Some appropriate trigger
    action:
      service: script.random_color
      data_template:
        entity_id: light.ur
        rgb_color: >
          {{ [[255,000,000],
              [000,255,000],
              [000,000,255],
              [255,165,000],
              [000,255,255],
              [255,000,255],
              [070,000,255]
             ]|random }}
        brightness: 20
script:
  random_color:
    sequence:
      service: light.turn_on
      data_template:
        entity_id: "{{ entity_id }}"
        rgb_color:
          - "{{ rgb_color.replace('[','').replace(']','').split(', ')[0] }}"
          - "{{ rgb_color.replace('[','').replace(']','').split(', ')[1] }}"
          - "{{ rgb_color.replace('[','').replace(']','').split(', ')[2] }}"
        brightness: "{{ brightness }}"

I might have the syntax off a bit (although hopefully it’s right :slight_smile:), but the idea is, the autotmation will select one of the rgb_color’s, and turn it into a corresponding string (e.g., [255,000,000] will turn into “[255, 0, 0]”) and that is passed to the script. The script then extracts each number into separate rgb_color list members.

I am not at home a couple of days but will try it out when I return :slight_smile:

Hey guys, a little late to the party but did you have any luck with @pnbruckner’s script ? I’m trying to pull random mixes of red to orange and would like to check the states and use the numbers when selecting from the color wheel.

I can´t really remember all the things I tried out with this issue, but I am still using color names in my script so I guess it didn´t work out for me with RGB values.
But would really like to know if someone has more luck as I would rather use RGB values.