Beginner Question: Cycling a light through pre-defined colors

I’m starting to get into the more advanced features of Home Assistant, but still a novice so excuse the simple question.

I have a pair of RGB light bulbs in the front of the house. I’d like to specify a starting color and then cycle them between three pre-defined colors every 30 seconds. Is there a post or can someone provide some sample code to help me do this?

I know I’ll need a script or combination of scripts but I’m not exactly sure what I need. I found this block of code that picks a random color from a set of pre-defined colors, but it doesn’t cycle through them continuously (it only picks one random color from the pre-defined colors when I run script.loop_lights and then leaves that color on):

light_cycle:
  sequence:
    - service: homeassistant.turn_on 
      entity_id: light.wifi_light_bulb_3
      data_template:
        brightness: 250
        color_name: > 
          {% set colors = ['orange', 'purple' , 'green'] %}
          {{ colors|random }}
    - service: script.loop_lights

loop_lights:
  sequence:
    - delay: '00:00:02'
    - service: script.light_cycle

Really appreciate the help in advance!

The quickest way I can think of right now is to put the color names you want in an input select, and have an automation that triggers every 30 seconds, sets the light to the color currently in the input_select and then advances the input_select to the next entry ready for the next time the automation triggers, something like:

input_select:
  light_color:
    options:
      - red
      - blue
      - green

automation:
  alias: Cycle light color every 30s
  trigger:
    platform: time_pattern
    seconds: '/30'
  action:
    - service: light.turn_on
      data:
        entity_id: light.YOUR_RGB_LIGHT_GROUP
        color_name: "{{ states('input_select.light_color') }}" 
    - service: input_select.select_next 
      entity_id: input_select.light_color
9 Likes

This worked! Thank you! I’m still getting the hang of how to “think” like Home Assistant… I figured an automation was needed but I wasn’t exactly sure how to set it up. This is super helpful!

2 Likes