Set a random color on a light

I use Studio Code for that. :slight_smile:
Not sure why Iā€™m getting errors itā€™s just odd.

Noā€¦ you are using the dashboard. Itā€™s very apparent from the image you posted.

Either because itā€™s still there or because itā€™s lying to you.
I canā€™t see your code, and I doubt itā€™s lying. :man_shrugging:

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