Store Random Color From List

Hi!

So the method below take a random color from a list and turns the light on that color.

Is there a way I can create a sensor (or something like it) and set a random color from the list below to the sensor via automation and use the sensor as the color value? This was I can use the color for other things as well.

special_lights:
  sequence:
    - service: light.turn_on
      entity_id:
        - group.all_special_lights
      data_template:
        brightness: 255
        color_name: >
          {% set colors = ['red', 'yellow' , 'blue', 'green' , 'purple'] %}
          {{ colors|random }}
1 Like

just what you said. Try this

sensor:
  - platform: template
    sensors:
      random_color:
        value_template: "{{ ['red', 'yellow' , 'blue', 'green' , 'purple'] | random }}"

to use it in your automations:

special_lights:
  sequence:
    - service: light.turn_on
      entity_id:
        - group.all_special_lights
      data_template:
        brightness: 255
        color_name: "{{ states('sensor.random_color') }}"
1 Like

Thanks! How can I set an automation to change the color in sensor.random_color daily?

Color changes every time you evaluate the template. Regarding the automation above, it will change every time your light turns on… You want the color to change once a day and stay in that color the whole day?

I do. I was thinking of doing this unless you there is a better way:

- alias: Color of The Day
  trigger:
    - platform: time
      at: '15:30'
  action:
    - service: homeassistant.update_entity
      entity_id: sensor.random_color

I believe that with your last example, light color will change every time you turn a light on… try it out just in case. Perhaps there’s a better way but… I believe this will work. You have to create the input_text before:

In configuration.yaml

input_text:
  daily_color:

Change color daily. Choose your preferred way of setting value, don’t leave the two.

- alias: Color of The Day
  trigger:
    - platform: time
      at: '15:30'
  action:
    - service: input_text.set_value
      data_template:
        entity_id: input_text.daily_color
        value: "{{ ['red', 'yellow' , 'blue', 'green' , 'purple'] | random }}" or "{{ states('sensor.random_color') }}"

then in your automations

special_lights:
  sequence:
    - service: light.turn_on
      entity_id:
        - group.all_special_lights
      data_template:
        brightness: 255
        color_name: "{{ states('input_text.daily_color') }}"
1 Like