Is there a more elegant way to do this?

I’ve managed to do what I wanted but is there a more elegant way of doing this?
This goes through the 5 different light scenes, showing each scene for 10 seconds. Then at 11pm, it shuts off the light.

Also, I want to replace the hard coded 11pm with a time input parameter that the user can set. Also, I want to replace the every 10 seconds to be a calculated value based on this:
(time input parameter - (sunset - 1hour)) / # of color scenes I have
(11pm - (7pm - 1hr)) / 5
5 hours / 5 = show 1 scene every 60 minutes

configuration.yaml

light:
  - platform: flux_led
    devices:
      192.168.1.177:
        name: Kitchen_Ceiling_LED
        mode: "rgb"
counter:
  sunset_color_num:
    initial: 0
    step: 1

automations.yaml

- alias: LEDs on
  trigger:
    platform: time
    seconds: '/10'
  condition: 
    - condition: time
      before: '23:00:00' 
  action:
    - service: script.color_scenes
- alias: LEDs off
  trigger:
    platform: time
    at: '23:00:00'
  action:
    - service: light.turn_off
      entity_id:
        - light.Kitchen_Ceiling_LED
    - service: counter.reset
      entity_id: counter.sunset_color_num

scenes.yaml

- name: color_1
  entities:
     light.Kitchen_Ceiling_LED:
         state: 'on'
         color_name: 'red'
         brightness: 200
- name: color_2
  entities:
     light.Kitchen_Ceiling_LED:
         state: 'on'
         color_name: 'yellow'
         brightness: 200
- name: color_3
  entities:
     light.Kitchen_Ceiling_LED:
         state: 'on'
         color_name: 'blue'
         brightness: 200
- name: color_4
  entities:
     light.Kitchen_Ceiling_LED:
         state: 'on'
         color_name: 'green'
         brightness: 200
- name: LED_off
  entities:
     group.LED_group:
         state: 'off'

scripts.yaml

color_scenes:
  sequence:
    - condition: state
      entity_id: sun.sun
      state: 'below_horizon'
    - service: counter.increment
      entity_id: counter.sunset_color_num
    - service: scene.turn_on
      data_template:
        entity_id: >
          scene.color_{{ states.counter.sunset_color_num.state }}

This isn’t a bad implementation, there are different ways to perform it. One thing i noticed, your colors only go to 4 so this will only work for 40 seconds… at sunset. At 50 seconds it will error because you don’t have a color after that, unless i’m not seeing the reset when the counter gets to 5.

Anyways, here is a solution that is only 1 automation and no color scenes. This will also give you the ability to adjust the time at which it ends. You just need to create an input_datetime or 2 input_numbers (one for hour, one for minute).

- alias: LEDs on
  trigger:
    platform: time
    seconds: '/10'
  condition:
    - condition: template
      # Add now().Minute check if you want.
      # Replace 23 with the state of the input_datetime state (using .Hour) or input_number.state.
      value_template:"{{ is_state('sun.sun', 'below_horizon') and now().Hour < 23 }}"
  action:
    - service: counter.increment
      entity_id: counter.sunset_color_num
    - service: light.turn_on
      entity_id: light.Kitchen_Ceiling_LED
      data_template:
        brightness: 200
        color_name: >
          {% set colors = ['red', 'yellow', 'blue', 'green'] %}
          {{ colors[states.counter.sunset_color_num.state % 4] }}

A few things to note:

states.counter.sunset_color_num.state % 4

The percent sign here is called modding. This basically yields the remainder when dividing. So 0%4 returns 0, 1%4 returns 1… 5%4 returns 1, 6% 4 returns 2. Essentially this takes any number and returns the remainder. So what its doing here is allowing any number to always be in a range of 0-3:

So you’ll never run into an issue of the counter not getting the correct number. You also don’t have to worry about reseting the counter while you are ‘on’.

^ that is a beautiful solution :+1:

Thanks Petro. Way less config!