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