How do you loop your automations in scripts?

Good day,
I’m having trouble converting the timing on my relays to have different timing for night and day.
Originally i have been using this code which is crude i know but it worked for what was needed.

- platform: gpio
    pin: D7
    name: "Greenhouse Pump"
    id: "greenhousepump"
    inverted: yes
    icon: "mdi:water-pump"

- platform: gpio
    pin: D8
    name: "Dutch Bucket Solenoid Left"
    id: "dbsolenoidl"
    restore_mode: ALWAYS_ON
    inverted: yes
    interlock: ["towersolenoid", "dbsolenoidr"]
    icon: "mdi:valve"
    on_turn_on:
      - delay: 10min
      - switch.turn_on: "towersolenoid"
      - delay: 30min
      - switch.turn_on: "dbsolenoidr"
      - delay: 10min
      - switch.turn_on: "towersolenoid"
      - delay: 30min
      - switch.turn_on: "dbsolenoidl"      
      
  - platform: gpio
    pin: D1
    name: "Dutch Bucket Solenoid Right"
    id: "dbsolenoidr"
    inverted: yes
    interlock: ["towersolenoid", "dbsolenoidl"]
    icon: "mdi:valve"

  - platform: gpio
    pin: D5
    name: "Tower Solenoid"
    id: "towersolenoid"
    inverted: yes
    interlock: ["dbsolenoidl", "dbsolenoidr"]
    icon: "mdi:valve"

This would keep the greenhouse pump running 24/7 and would alternate the open relays for the solenoids.

When changing the code to use sunset/sunrise like below. I can get the script to fire when sunset/sunrise is triggered, but it only runs through the script once, where i would like the relays to keep looping until state in the sun has changed.

- platform: gpio
    pin: D7
    name: "Greenhouse Pump"
    id: "greenhousepump"
    inverted: yes
    icon: "mdi:water-pump"

- platform: gpio
    pin: D8
    name: "Dutch Bucket Solenoid Left"
    id: "dbsolenoidl"
    inverted: yes
    interlock: ["towersolenoid", "dbsolenoidr"]
    icon: "mdi:valve"   
      
  - platform: gpio
    pin: D1
    name: "Dutch Bucket Solenoid Right"
    id: "dbsolenoidr"
    inverted: yes
    interlock: ["towersolenoid", "dbsolenoidl"]
    icon: "mdi:valve"

  - platform: gpio
    pin: D5
    name: "Tower Solenoid"
    id: "towersolenoid"
    inverted: yes
    interlock: ["dbsolenoidl", "dbsolenoidr"]
    icon: "mdi:valve"

script:
  - id: day_time
    then:
      - switch.turn_on: "greenhousepump"
      - switch.turn_on: "towersolenoid"
      - delay: 30min
      - switch.turn_on: "dbsolenoidr"
      - delay: 10min
      - switch.turn_on: "towersolenoid"
      - delay: 30min
      - switch.turn_on: "dbsolenoidl"

  - id: night_time
    then:
      - switch.turn_off: "greenhousepump"
      - delay: 60min
      - switch.turn_on: "towersolenoid"
      - switch.turn_on: "greenhousepump"
      - delay: 10min

sun:
  latitude: xx.xx
  longitude: xx.xx
   on_sunrise:
    - then:
        - script.stop: night_time
        - script.execute: day_time
        
  on_sunset:
    - then:
        - script.stop: day_time
        - script.execute: night_time

My Coding expertise are not great at all and would be really helpful if someone could explain to me how to keep them looping. I presume i would need to make it a while condition for sun status above or below the horizon. but i cant get it right and breaks the code. any help or feedback would be a huge help.

Well gettiing no where further… not idea how to trigger the scripts on_boot

esphome:
  name: test
  platform: ESP8266
  board: nodemcuv2
  on_boot:
  - if:
     condition:
      - sun.is_above_horizon: 
     then:
      - script.execute: "day"
     else:
      - script.execute: "night" 

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:
  password: "9cd7eda8287ff23074abc3e61d77439e"

wifi:
  ssid: "xxx"
  password: "xxx"

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Test Fallback Hotspot"
    password: "IMyww9rsjCRW"

captive_portal:

switch:
  - platform: gpio
    pin: D6
    name: "Switch1"
    id: "switch1"
    

  - platform: gpio
    pin: D7
    name: "Switch2"
    id: "switch2"

    
  - platform: gpio
    pin: D8
    name: "Switch3"
    id: "switch3"

sun:
  latitude: -34.002081209143334
  longitude: 25.529509985532012
  
# At least one time source is required
time:
  - platform: homeassistant
  
script:
  - id: "day"
    while:
      condition:
        - sun.is_above_horizon
    then:
      - switch.turn_on: switch3
      - delay: 1min
      - switch.turn_off: switch3
      - switch.turn_on: switch2
      - delay: 1min
      - switch.turn_off: switch2
        
      
  - id: "night"
    while:
      condition:
        - sun.is_below_horizon: true
      then:
        - switch.turn_on: switch3
        - delay: 1min
        - switch.turn_off: switch3
        - switch.turn_on: switch2
        - delay: 1min
        - switch.turn_off: switch2

ESPhome documentation is soo all over the place i have no idea what structure goes where and every thing is just so confusing and frustrating when it gets to automations, templates, lambas scripts… Can someone please help me with some examples that has extra code and not just 2 lines

Im trying to on boot up, start a script, based on whether the sun is above or below horizon to run a script either day or night that triggers different timings of switches.

So i have got a bit further. The script seems to run from boot, depending on night or day, but once sunset or sunrise status changes, the log stops output for script that was running and the other script doesnt start.

switch:
  - platform: gpio
    pin: D6
    name: "Sw1"
    id: "sw1"
    

  - platform: gpio
    pin: D7
    name: "Sw2"
    id: "sw2"

    
  - platform: gpio
    pin: D8
    name: "Sw3"
    id: "sw3"
    
  - platform: gpio
    pin: D8
    name: "Sw4"
    id: "sw4"
    


sun:
  latitude: -34.002081209143334
  longitude: 25.529509985532012
  
  
# At least one time source is required
time:
  - platform: homeassistant
    on_time_sync:
      - if:
          condition:
            - sun.is_above_horizon: 
          then:
            - script.execute: day
          else:
            - script.execute: night
  
script:
  - id: day
    then:
      - while:
          condition:
            - sun.is_above_horizon:
          then:
            - switch.turn_on: sw3
            - delay: 1min
            - switch.turn_off: sw3
            - switch.turn_on: sw4
            - delay: 1min
            - switch.turn_off: sw4
          
  - id: night
    then:
      - while:
          condition:
            - sun.is_below_horizon:
          then:
            - switch.turn_on: sw1
            - delay: 1min
            - switch.turn_off: sw1
            - switch.turn_on: sw2
            - delay: 1min
            - switch.turn_off: sw2
            
          

The only trigger you have is when the node boots, you can use the sun component to trigger at sunrise/sunset