I am trying to setup an automation that will cycle certain lights through colors. Ultimately I would like to automatically pick colors based on the current holiday but for now I would like to at least make a list of colors and then have lights cycle through them.
For example, for Christmas, I would like light1, light2, and light 3 to change between red, green, and gold every 5 minutes, in order. Any guidance on how I get started on this?
Thank you.
Here’s the basic principle:
alias: example color change
trigger:
- platform: state
entity_id: input_boolean.color_changer
to: 'on'
action:
- repeat:
while: "{{ is_state('input_boolean.color_changer', 'on') }}"
sequence:
- service: light.turn_on
target:
entity_id:
- light.first
- light.second
- light.third
data:
color_name: "{{ states('input_select.light_colors') }}"
- delay:
minutes: 5
- service: input_select.select_next
target:
entity_id: input_select.light_colors
mode: single
When the input_boolean is turned on it starts a repeat
that will loop continuously until the input_boolean is turned off. Each iteration of the repeat
turns on three lights and sets their color to the value in the input_select. It waits 5 minutes then sets the input_select to the next color in its list of colors.
This helps a lot. Would I just need to create a helper called “light_colors” as the colors I want? If I want each light to be a different color would I add individual light.turn_on services?
Here’s a way to do it without an Input Select, set each light to a different color, and cycle each one through the sequence of colors (red, green, gold).
alias: example color change
trigger:
- platform: state
entity_id: input_boolean.color_changer
to: 'on'
action:
- variables:
colors:
- red
- green
- gold
- repeat:
while: "{{ is_state('input_boolean.color_changer', 'on') }}"
sequence:
- service: light.turn_on
target:
entity_id: light.first
data:
color_name: "{{ colors[(repeat.index - 1) % 3] }}"
- service: light.turn_on
target:
entity_id: light.second
data:
color_name: "{{ colors[repeat.index % 3] }}"
- service: light.turn_on
target:
entity_id: light.third
data:
color_name: "{{ colors[(repeat.index + 1) % 3] }}"
- delay:
minutes: 5
mode: single
Thanks a lot. This helps a lot.
I think I will make this one big automation and then use the different sensors available for holidays and set those as conditions for the actions.
You’re welcome!
Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title signaling to other users that this topic has been resolved. This helps users find answers to similar questions.
For more information about the Solution tag, refer to guideline 21 in the FAQ.
Any progress to report?
I have not had the chance to fully implement this yet but it looks like it will work as I need so I will mark this as the solution.
If I want to have different number of entries in my colors variable, how can I easily change that quantity that is specified in the data: color_name information?
Four lights, each set to one of four colors:
alias: example color change
trigger:
- platform: state
entity_id: input_boolean.color_changer
to: 'on'
action:
- variables:
colors:
- red
- green
- gold
- blue
- repeat:
while: "{{ is_state('input_boolean.color_changer', 'on') }}"
sequence:
- service: light.turn_on
target:
entity_id: light.first
data:
color_name: "{{ colors[(repeat.index - 1) % 4] }}"
- service: light.turn_on
target:
entity_id: light.second
data:
color_name: "{{ colors[repeat.index % 4] }}"
- service: light.turn_on
target:
entity_id: light.third
data:
color_name: "{{ colors[(repeat.index + 1) % 4] }}"
- service: light.turn_on
target:
entity_id: light.fourth
data:
color_name: "{{ colors[(repeat.index + 2) % 4] }}"
- delay:
minutes: 5
mode: single
EDIT
Correction. Failed to correctly increment the index value of the fourth light.
Any way of doing it dynamically? For example, if I have just two colors is there a way of changing this
color_name: “{{ colors[(repeat.index - 1) % 4] }}” to something like color_name: “{{ colors[(repeat.index - 1) % number of colors in variable] }}” that way I don’t have to manually update the “4” when I decide to change how many colors I have in my variable?
Create another variable that computes the number of items in the colors
list.
action:
- variables:
colors:
- red
- green
- gold
- blue
qty: "{{ colors | count }}"
Replace the number 4
in each one of the color_name
templates with the new variable.
{{ colors[(repeat.index + 1) % qty] }}
Awesome. Thanks for the quick reply.
In your four lights exmaple, will the two lights get the same color since you add “1” to the index? If I wanted to loop through the variable in case I have more lights than number of colors, how could I do that?
Good catch! You’re right, I just blindly copy-pasted it and forgot to properly increment the fourth light’s index value. I corrected the example posted above.