Light Loop - simpler way to write with a time range?

Hello. I have this current script that somehow works as it should be. It’s a script that loops my light sequence when triggered. But I’m just wondering if this can be written in a much simpler way?

themesloop_s:
  alias: Themes Loop
  sequence:
  - delay: '00:00:00'
  - service_template: >
      {% if states.sensor.time.state > '16:00' %}
        {{ entity_id }}
      {% elif states.sensor.time.state < '04:00' %}
        {{ entity_id }}
      {% elif is_state('script.light_themes_off_s', 'off') %}
        script.light_themes_off_s
      {% endif %}

So currently, it works as I want it to. But when I check the history, It starts, play the entire sequence, and then stops at the end of it. Then repeats it again until it meets the time specified.

Is there a simpler way to write something that will loop the light from 4PM to 4AM?

Thanks.

If it works then why try to rewrite it?

What’s so complicated about what you have now?

I wrote this as an exercise, but I think your script is more concise.

automation:
  - alias: loop_the_script
    trigger:
      - platform: time
        at: '16:00:00'
      - platform: state
        entity: 'script.the_script_to_loop'
        to: 'off'
    condition:
      - condition: time
        after: '16:00:00'
        before: '04:00:00'
    action:
      - service: script.the_script_to_loop

  - alias: light_themes_off_s
    trigger:
      - platform: time
        at: '04:00:00'
    condition: []
    action:
      - service: script.light_themes_off_s

I think the second elif should be an else.
Other than that, I think it’s fine

Or maybe : -
{{‘entity_id’ if not ‘04:00’ <= states(sensor.time) <= ‘16:00’ else ‘script.light_themes_off_s’}}

Not quite sure how you are using this.

Edit: Sorry, I had the comparators the wrong way around

Not sure this can be considered ‘simpler’ but it’s a bit shorter.

themesloop_s:
  alias: Themes Loop
  sequence:
  - delay: '00:00:00'
  - service_template: >
      {% if states('sensor.time') > '16:00' or states('sensor.time') < '04:00' %}
        {{ entity_id }}
      {% elif is_state('script.light_themes_off_s', 'off') %}
        script.light_themes_off_s
      {% endif %}

Question for you:
What if the current time is not > 16:00 or < 04:00 and script.light_themes_off_s is on? Your service_template doesn’t produce a result for that possibility (and will cause an execution error).