Set a random color on a light

This is what the developers tool tells me:

action:
    - service: light.turn_on
      entity_id: light.outside_front_lights
      data_template:
        hs_color: >
          - 30
          - 100
        brightness: 80
        mode: single

Here’s a new error

Error while executing automation automation.98_turn_on_outside_lights: Error rendering data template: UndefinedError: None has no element 0

Not sure what code you mean… the automation above has the code in it.

Not the end of the world apparently my scope of knowledge isn’t where it needs to be because I’m not understanding some things here… No big deal. Work calls so I must go :wink:

Just like that… from my office desk I finally figured it out and it works :slight_smile: Thanks @Hellis81 for your input!

action:
    - service: light.turn_on
      entity_id: light.outside_front_lights
      data_template:
        xy_color: [0.41, "0.{{ range(1, 100000)|random }}"]
        brightness: 80

i am trying to make random light buttons…like a scene, but random colors… i cant figure out how to do it… … I am sure it is simple enough, but what are you supposed to do with this?.. make an automation?.. a script?..a scene?.. paste it somewhere?..arrrrgggg! …lol…

There is a full example in the first response.

If that is not enough to make it work for you, show us what you’ve got and someone will help.

i dont know where to start?.. i see the examples… i just dont know what to do with them.

if i try to paste it into developer tools/services i get a “The UI does not support templates, you can still use the YAML editor.”…odd because i am in a yaml editor… but i digress…

if i paste it into automations i get “Message malformed: required key not provided @ data[‘trigger’]”…

if i try to make it a script i get " Message malformed: expected a dictionary"

okie dokies… a little further… :)… this finally worked in Developer Tools/Services… i couldnt get the first example to work… im sure i am just missing the thing right in front of my face…lol…here be meh code :slight_smile:

service: light.turn_on
target:
entity_id: light.study_1_2
data:
color_name: “{{ [‘lawngreen’, ‘blueviolet’, ‘magenta’, ‘yellow’, ‘red’, ‘blue’, ‘lime’, ‘magneta’, ‘purple’] | random }}”

…what do i do with it now?.. i get errors when i try to save it as a script(Message malformed: extra keys not allowed @ data[‘service’]) and as an automation I get the same error… and i guess for a whole room of different random color lights I would need to repeat it with the entities changed…?.. something like that?

thank you:)

Use the GUI to create a light turn on, then switch to yaml mode and copy paste lines from your code here to the yaml editor.
Since you didn’t use the prefromatted text button you will need to replace all the quotes with proper quotes.

ok… now i got some more gravy :)… and a preformatted text thinga-ma-bob :)… it works…yay :)… it will change both of those lights to the same color…

still dont know what to do with it…:slight_smile: still kinda new with home assistant… im still learning the nuts and bolts :)…when you say “create a light turn on”…do you mean the stuff tn Developer Tools/Services… or create a button?..

what can i do to make it take that random list of colors and apply it to a room where each bulbs gets a random color?.. …In my setup, light.study has 7 lights…i want them all to have random colors with a click…i know once i figure out how to do it it will be easy enough and trigger-able a million ways… I have a good deal of automations ( seems to be growing everyday) and a decent amount of integrations in my set up… use scripts for a handful of things… this random-color-lights-for-a-room thing has been eluding me…lol…

service: light.turn_on
data:
  color_name: "{{ ['lawngreen', 'blueviolet', 'magenta', 'yellow', 'red', 'blue', 'lime', 'magneta', 'purple'] | random }}"
target:
  device_id:
    - b9cb7d06ee98e3477ea1006fd75afcdd
    - a66d6669d3548bee990a1d1f0e687510

Open a new script and add call service with light turn on:

In upper right corner, three dots, yaml mode:

alias: New Script
sequence:
  - service: light.turn_on
    data: {}
mode: single

Now lets take the parts you have and add them to the automation:

alias: New Script
sequence:
  - service: light.turn_on
    target:
      entity_id: light.study_1_2
    data:
      color_name: "{{ ['lawngreen', 'blueviolet', 'magenta', 'yellow', 'red', 'blue', 'lime', 'magneta', 'purple'] | random }}"

This should set study_1_2 to a random color.
If you want all entities to be the same color then list them:

    target:
      entity_id:
        - light.study_1_2
        - light.study_1_3
        - light.study_1_4

If you want each light to be a different color then you need to duplicate the call service part:

alias: New Script
sequence:
  - service: light.turn_on
    target:
      entity_id: light.study_1_2
    data:
      color_name: "{{ ['lawngreen', 'blueviolet', 'magenta', 'yellow', 'red', 'blue', 'lime', 'magneta', 'purple'] | random }}"

  - service: light.turn_on
    target:
      entity_id: light.study_1_3
    data:
      color_name: "{{ ['lawngreen', 'blueviolet', 'magenta', 'yellow', 'red', 'blue', 'lime', 'magneta', 'purple'] | random }}"

  - service: light.turn_on
    target:
      entity_id: light.study_1_4
    data:
      color_name: "{{ ['lawngreen', 'blueviolet', 'magenta', 'yellow', 'red', 'blue', 'lime', 'magneta', 'purple'] | random }}"

Running this script should give you either all same color or different color on each light.

Lastly. I’m not sure the method of color name is the best if you want random colors.
I would prefer RGB or hue and saturation.

In my opinion Hue and saturation is easier to randomize.
So instead of data with color name, replace it with:

data:
  hs_color:
    - '{{ range(0,360) | random }}'
    - '{{ range(0,100) | random }}'

You need to try it yourself but on my Shelly bulbs a saturation of less than 60 (of 100) is more or less white. So having it randomize from 0-100 then chances are that you will have a lot of white lights.
You could make that range

data:
  hs_color:
    - '{{ range(0,360) | random }}'
    - '{{ range(60,100) | random }}'

Or if you only want full saturated colors then fix it to 100.

data:
  hs_color:
    - '{{ range(0,360) | random }}'
    - 100

Here’s just an example…this is what I use to turn on random colors with my front outside lights…

- service: light.turn_on
              target:
                entity_id: light.outside_front_lights
              data:
                hs_color: > 
                  [ {{ range(10,300) | random }},  100 ]
      default:
        - service: light.turn_off
          entity_id: light.outside_front_lights

Hope it helps!

duuuuude,… i knew it…thank you!..

it was totally the simplest thing :)… it was like the first step where you “called service” as an option…that is what i was missing… the rest was downhill! :)… boom!.. i owe you a piece of delicious cake, good sir! :slight_smile:

just wanted to say thank you again, friends! :slight_smile: … and wanted to show you what you helped me build :slight_smile: this is one area…the final part was that random button :)…lol… i have already created all the other ones too

ahhhh…Home Assistant… makes the complicated stuff easy…and the easy stuff complicated…lol… love it… boom

1 Like

Thanks for sharing, so awesome!!

1 Like

I don’t know what i’m doing wrong. I want to have random colors every 3mins for when me and my kids watch movies. Any help is appreciated

Something new to try…
Seemed to be an appropriate place to put this

Open your Home Assistant instance and open a repository inside the Home Assistant Community Store.

Thanks everyone. Was able to create some simple yet effective color automations thanks to this thread.

alias: Roll Bedroom
variables:
  control_entity_state: "{{ is_state('input_boolean.onecolorrule', 'on') }}"
  apply_brightness: "{{ is_state('input_boolean.apply_custom_brightness', 'on') }}"
  custom_brightness: "{{ states('input_number.custom_brightness_level') | int }}"
  bulb_ids:
    - light.wiz_rgbw_tunable_d33748
    - light.wiz_rgbw_tunable_d30740
    - light.wiz_rgbw_tunable_3e921e
    - light.wiz_rgbw_tunable_d30090
    - light.wiz_rgbw_tunable_d2f110
  color_list:
    - Aqua
    - Aquamarine
    - Brown
    - CadetBlue
    - Chartreuse
    - Chocolate
    - Coral
    - Crimson
    - Cyan
    - DeepPink
    - DeepSkyBlue
    - DodgerBlue
    - FireBrick
    - ForestGreen
    - Fuchsia
    - Gold
    - GoldenRod
    - Green
    - GreenYellow
    - HotPink
    - IndianRed
    - Indigo
    - Khaki
    - LawnGreen
    - LemonChiffon
    - LightCoral
    - LightSalmon
    - Lime
    - LimeGreen
    - Magenta
    - Maroon
    - MediumBlue
    - MidnightBlue
    - Navy
    - OldLace
    - Olive
    - OliveDrab
    - Orange
    - OrangeRed
    - Orchid
    - PaleGoldenRod
    - PaleGreen
    - PaleTurquoise
    - PaleVioletRed
    - PapayaWhip
    - PeachPuff
    - Peru
    - Pink
    - Plum
    - Purple
    - Red
    - RosyBrown
    - RoyalBlue
    - SaddleBrown
    - Salmon
    - SandyBrown
    - SeaGreen
    - Sienna
    - SkyBlue
    - Tan
    - Teal
    - Tomato
    - Turquoise
    - Violet
    - Wheat
    - Yellow
    - YellowGreen
sequence:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ control_entity_state }}"
        sequence:
          - variables:
              chosen_color: "{{ color_list | random }}"
          - service: input_text.set_value
            target:
              entity_id: input_text.last_requested_color
            data:
              value: "{{ chosen_color }}"
          - repeat:
              count: "{{ bulb_ids | length }}"
              sequence:
                - choose:
                    - conditions:
                        - condition: template
                          value_template: "{{ apply_brightness }}"
                      sequence:
                        - service: light.turn_on
                          target:
                            entity_id: "{{ bulb_ids[repeat.index - 1] }}"
                          data:
                            color_name: "{{ chosen_color }}"
                            brightness: "{{ custom_brightness }}"
                  default:
                    - service: light.turn_on
                      target:
                        entity_id: "{{ bulb_ids[repeat.index - 1] }}"
                      data:
                        color_name: "{{ chosen_color }}"
    default:
      - repeat:
          count: "{{ bulb_ids | length }}"
          sequence:
            - choose:
                - conditions:
                    - condition: template
                      value_template: "{{ apply_brightness }}"
                  sequence:
                    - service: light.turn_on
                      target:
                        entity_id: "{{ bulb_ids[repeat.index - 1] }}"
                      data:
                        color_name: "{{ color_list | random }}"
                        brightness: "{{ custom_brightness }}"
              default:
                - service: light.turn_on
                  target:
                    entity_id: "{{ bulb_ids[repeat.index - 1] }}"
                  data:
                    color_name: "{{ color_list | random }}"
mode: restart
icon: mdi:dice-multiple

New script lists the last color rolled (when one color rule is on), one color rule changes all bulbs to a single random color vs. each bulb to a distinct random color. If apply brightness is toggled on, the brightness level of all bulbs are set when the colors are applied.

1 Like

What if you just want the color to change to the next one in the “array”?

color_name: "{{ ['lawngreen', 'blueviolet', 'magenta', 'yellow', 'red', 'blue', 'lime', 'magneta', 'purple'] | random }}"

Can the “random” be changed to anything else?

I have an RGBW led strip, and I cannot figure out why this automation just turns the strip on white.

** Edit - If I have use brightness value (including 100), it turns the strip white. When I remove “brightness_pct”, it works. What would be the data name for COLOR brightness?