Help with script to implement loop for irrigation cascade

I’m a bit struggling with logic to implement my irrigation.
It was previously done with homeassistant and works perfectly.
I do have a logic in greenhouse to irrigate tomatos and cucumbers and these need different amounts of water in different times (small pump and not enough pressure).
Same time there is a problem that if i start pump for 10 minutes the water will overflow and not end up in soil.
So i made loop to run pump for 30 seconds and then stop for 10 and start over again.
It is done with quite simple loop in OpenHAB - but converting it to HA seems hard.

OH rule is quite simple:

rule "4. Irrigation Cascade"
when
    Item IrrigationRun received command ON
then
	myTimer = createTimer(now.plusSeconds(0), [ |
	    maxLoops = maxLoops - 1
	    if(maxLoops > 0) {
			if(maxLoops % 2 == 0){
				// logInfo("irrigation", "PUMP OFF")
				myTimer.reschedule(now.plusSeconds(8))
			}else{
				// logInfo("irrigation", "PUMP ON")
				myTimer.reschedule(now.plusSeconds(32))
			}
		}
	    if(maxLoops == 0){
			// logInfo("irrigation", "END")
		}
	])
end

And rule to start it is quite simple as well:

rule "3. Irrigation - Cucumbers"
when
	Time cron "0 0 6 ? * MON,WED,SAT *" or
	Item IrrigationKurgid received command ON
then
    if(IrrigationAuto.state == ON || receivedCommand == ON){
		maxLoops = 100
        logInfo("kastmine", "Irrigation cucumber started, turning on")
        sendCommand( cucumber_valve, ON )
		Thread::sleep(1000)
        sendCommand( IrrigationRun, ON )
    }
end

I have managed to send loop_count from automation to script … but not looping in script.

Is there any easy way to implement?

Although there is a repeat script command in Home Assistant, it’s not always best to translate code literally from one language to another.

For example, the following automation does the following without using an explicit repeat command:

  • Starts pump operation at 06:00 and stops it at 07:00. It does this only on Monday, Wednesday, and Saturday.
  • It runs the pump for 30 seconds then turns it off for 10 seconds. It repeatedly cycles the pump in this fashion until it’s automatically turned off at 07:00.
alias: cucumber irrigation
trigger:
  - id: 'on'
    platform: time
    at: '06:00'
  - id: 'off'
    platform: time
    at: '07:00'
  - id: 'off'
    platform: state
    entity_id: switch.pump
    from: 'off'
    to: 'on'
    for:
      seconds: 30
  - id: 'on'
    platform: state
    entity_id: switch.pump
    from: 'on'
    to: 'off'
    for:
      seconds: 10
condition:
  - condition: template
    value_template: "{{ now().isoweekday() in [1,3,6] }}"
action:
  - action: "switch.turn_{{ trigger.id }}"
    target:
      entity_id: switch.pump

It’s entirely event-driven; it uses two Time Triggers and two State Triggers.

Hi Martin,

I would follow up with using @123 's idea and strategy but put the pump operation is a separate script. Then in your automation you would set times and dates to turn on the pump, and turning on the pump (or off) means calling that script. The script will handle the duty cycle of the pump. The automation handle the scheduling. Then easy to have a schedule for separate plant requirements while calling the same script. Also it would prevent trying to run 2 things at once. If the pump script is busy, the other automation cannot have the pump.

When it comes to irrigation, irrigation unlimited is a very comprehensive extension that I advise you should look at. No automation to write, very powerfull.