Random Light Automation

I have been trying to get an automation working that randomly turns on & then off a light.

I have two triggers:

One is when the current time is equal to a randomly generated time.

The other is an input_boolean that i want to control the functioning of the automation in a loop such that it restarts itself repeatedly until a predetermined thing occurs (as yet to be determined).

I have gotten the time based trigger to work. I haven’t been successful with the input_boolean control.

here is the code:

- alias: Random Livingroom Lights
  trigger:
    - platform: template
      value_template: "{{ is_state('sensor.time', '%02d'|format(states('input_slider.random_hour_lr')|int) ~ ':' ~ '%02d'|format(states('input_slider.random_minute_lr')|int)) }}"
    - platform: state
      entity_id: input_boolean.bool_4
      from: 'off'
      to: 'on'
  condition:  
    condition: and
    conditions:
      - condition: state
        entity_id: 'sun.sun'
        state: 'below_horizon'
  action:
    - delay: '00:00:10'
    - service: homeassistant.turn_off
      entity_id: input_boolean.bool_4
    - delay: '00:{{ (range(1, 5)|random|int) }}:00'
    - service: homeassistant.turn_on
      entity_id: 
        - light.zw_light_1_level
    - delay: '00:{{ (range(1, 10)|random|int) }}:00' 
    - service: homeassistant.turn_off
      entity_id: 
        - light.zw_light_1_level
    - service: homeassistant.turn_on
      entity_id: input_boolean.bool_4

The first time the automation is run based on the random time, everything works as desired. the lights turn on at a random time in the future, the lights turn off at another random time in the future, the input_boolean turns on.

Then nothing.

I’ve looked at the logbook and it says that the automation is triggered when the input_boolean is turned on but the sequence never progresses after that. the input_boolean should turn off after 10 seconds but it doesn’t and the light never cycles.

If I try to trigger the automation by manually turning off then on the boolean it doesn’t trigger.

Anyone see any problem with the code?