Water pump automation for multiple cycles in light on period

Hi

So my tent light switches on at 4 am and off at 10pm.

I would like the water pump to run for 2 minutes every 2 hours, starting from 4:30 am till 8:30pm, so that’s a total of 9 cycles of 2 minutes each.

Right now,I have the below automation, for switching the pump on at specified time

alias: 4x4 Watering
description: ""
trigger:
  - platform: time
    at: input_datetime.4x4_watering_time_1
  - platform: time
    at: input_datetime.4x4_watering_time_2
  - platform: time
    at: input_datetime.4x4_watering_time_3
  - platform: time
    at: input_datetime.4x4_watering_time_4
  - platform: time
    at: input_datetime.4x4_watering_time_5
  - platform: time
    at: input_datetime.4x4_watering_time_6
  - platform: time
    at: input_datetime.4x4_watering_time_7
  - platform: time
    at: input_datetime.4x4_watering_time_8
  - platform: time
    at: input_datetime.4x4_watering_time_9
condition: []
action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.sonoff_
  - service: input_datetime.set_datetime
    target:
      entity_id: input_datetime.watering_off_time
    data:
      datetime: >-
        {{ now() +
        timedelta(minutes=states('input_number.4x4_watering_time')|int(1)) }}
mode: single

and to switch the pump off, I use the below automation

alias: 4x4_Watering_Minutes_Slider
description: ""
trigger:
  - platform: time
    at: input_datetime.watering_off_time
condition: []
action:
  - service: switch.turn_off
    target:
      entity_id: switch.sonoff_
    data: {}
mode: single

This is turning out to be clunky as I need to edit the timers and the automation each time I want to make a change.

Can I use this instead?

alias: 4x4 Automation
description: ""
trigger:
  - platform: time
    at: "04:30:00"
condition: []
action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.sonoff_1000eca7dd
  - delay:
      hours: 0
      minutes: 2
      seconds: 0
      milliseconds: 0
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.sonoff_1000eca7dd
  - delay:
      hours: 2
      minutes: 0
      seconds: 0
      milliseconds: 0
mode: queued
max: 9

I guess the HA will lose the count in case of a power failure in the middle of the day?

Any suggestions to make this less clunky?

I would personally not use any delay and or wait for more than a matter of a few minutes in any automation and or script, as any restarting of HA and or any editing of automations in the front end will cause all automations to be reloaded and cease currently running actions.

I would maybe use a time pattern trigger to trigger on every half the hour but with the time range required as a condition, something like this maybe:

alias: 4x4 Automation
description: ''
trigger:
  - platform: time_pattern
    minutes: '30'
condition:
  - condition: time
    after: '04:29:00'
    before: '20:31:00'
action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.sonoff_
  - delay:
      hours: 0
      minutes: 2
      seconds: 0
      milliseconds: 0
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.sonoff_1000eca7dd
mode: single

This will switch on the pump every half hour right?

I need the pump to be on from 4:30 to 4:32,6:30 to 6:32…8:30 to 8:32

This will trigger every 30 mins past the hour, but the actions will only occur if the time is between 4:30 & 20:30 as per the condition.

EDIT: Sorry just noticed you wanted it every 2 hours and not every hour

Time pattern trigger is still the way to do it though.

trigger:
  - platform: time_pattern
    hours: "/2"
    minutes: "30"
    seconds: "0"
condition:
  - condition: time
    after: '04:29:00'
    before: '20:31:00'
action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.sonoff_1000eca7dd
  - delay:
      hours: 0
      minutes: 2
      seconds: 0
      milliseconds: 0
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.sonoff_1000eca7dd
mode: single
1 Like

Will this automation be affected in case the Pi restarts due to a power failure or internet outage?

Only if the system was to go down during the 2 mins delay from switching on to switching off and then in theory the switch would remain on, if this is a concern then there is nothing stopping you from making an additional automation that is triggered by the switch having been on for more then 2.5 mins for example.

I would assume your sonoff devices were local so internet not required!

Or just use two automations. No switch.turn_off in the first one. The second one turns off the pump after 2 minutes, any time it’s on. Sometimes two simple automations are better than one complex one. This also avoids using delay.

That no longer survives a restart either. It used to be my goto way of dealing with this issue.

That complicates things. Maybe turn it off if the time is not between XX:30 and XX:35, and it’s on?

You could add a timer in the mix and start the timer for the 2 minutes and turn the pump off when it finishes rather than have that delay in the script. You’d have a trigger for the event for hte timer finishing to turn it back off. Then after a reboot it would finish and turn it off as well.

alias: pumpon
description: ""
trigger:
  - platform: time_pattern
    hours: "/2"
    minutes: "30"
    seconds: "0"
condition:
  - condition: time
    after: '04:29:00'
    before: '20:31:00'
action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.sonoff_1000eca7dd
  - service: timer.start
    data:
      duration: 120
    target:
      entity_id: timer.pumptimer
mode: single
alias: pumpoff
description: ""
trigger:
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.pumptimer
condition: []
action:
  - service: switch.turn_off
    target:
      entity_id: switch.sonoff_1000eca7dd
    data: {}
mode: single

Here’s a single, self-contained automation that triggers at the scheduled times and survives Home Assistant restarts and automation reloads. It turns on the switch if the current time falls within one of the two allowed 2-minute time periods (hour or half-hour plus two minutes) otherwise it turns off the switch.

alias: 4x4 Watering
description: ""
trigger:
  - id: 'on'
    platform: time
    at:
      - '04:30:00'
      - '05:00:00'
      - '05:30:00'
      - '06:00:00'
      - '06:30:00'
      - '07:00:00'
      - '07:30:00'
      - '08:00:00'
      - '08:30:00'
  - id: 'off'
    platform: time
    at:
      - '04:32:00'
      - '05:02:00'
      - '05:32:00'
      - '06:02:00'
      - '06:32:00'
      - '07:02:00'
      - '07:32:00'
      - '08:02:00'
      - '08:32:00'
  - platform: homeassistant
    event: start
  - platform: event
    event_type: automation_reloaded
condition:
  - condition: template
    value_template: "{{ 4 <= now().hour <= 8 }}"
action:
  - service: "switch.turn_{{ iif((0 <= now().minute < 2) or (30 <= now().minute < 32), 'on', 'off') }}"
    target:
      entity_id: switch.sonoff_1000eca7dd
mode: single

For example, if Home Assistant is restarted at 06:59:45 and comes back at 07:01:10 it will have missed its scheduled Time Trigger at 07:00:00. However, the automation is triggered by a restart therefore its condition confirms the current hour (7) is between 4 and 8 and the service call’s template determines that the current minute (1) is between 0 and 2 so it produces switch.turn_on.

Similarly if the restart occurs at 07:01:50 and Home Assistant comes back at 07:02:15 it will miss its scheduled Time Trigger at 07:02:00. However, the automation will go through the same process I just described but this time the template will produce switch.turn_off.


EDIT

A shorter version of the service call’s template:

  - service: "switch.turn_{{ iif(now().minute in [0, 1, 30, 31], 'on', 'off') }}"

FWIW, if you wish, the two Time Triggers can be consolidated into a single Time Trigger. They’re shown as two separate triggers only for legibility (to distinguish which times are for turning on the switch versus turning it off). The service call’s template doesn’t need two separate Time Triggers to set the switch’s state correctly.


EDIT

Correction. Replaced outer single-quotes with double-quotes.

1 Like

Taras : I tried to use your template t control the hot water circulation pump in the house, to avoid it running 24/7. The template in your post #12 is accepted as an automation only if I remove the “service” line. HA doesn’t want to save the yaml file if I use either of your proposed if action statements :
action:

  • service: ‘switch.turn_{{ iif((0 <= now().minute < 2) or (30 <= now().minute < 32), ‘on’, ‘off’) }}’
  • service: ‘switch.turn_{{ iif(now().minute in [0, 1, 30, 31], ‘on’, ‘off’) }}’

Where am I going wrong?

Replace the outer single-quotes with double-quotes. I have corrected the original example posted above.

1 Like

Tried it as per the edit you posted, when I copy paste the line into the automation and save it. HA removes the " and the pump never gets switched on. Almost there :slight_smile: If i change the “iif” to “if” (typo?), it complains "Message malformed: string value is None for dictionary value @ data[‘action’][0]]‘service’]

HA removes the ", so that leaves me with :
service: switch.turn_{{ if(now().minute in [0, 1, 30, 31], ‘on’, ‘off’) }}

Thanks for help

It’s not a typo, it’s an Immediate If.

You mean HA’s Automation Editor removes the double-quotes. It follows its own formatting guidelines.

The outer double-quotes aren’t entirely necessary in this particular case because, even though there’s a template, the value is understood to be a string.

As to why it doesn’t turn on the switch, that will depend at what time this service call is executed (because it will only turn on when the current minute is one of only four values).

2 Likes

My .yaml now reads this and it works :

service: switch.turn_{{ iif(now().minute in [0, 1, 30, 31], ‘on’, ‘off’) }}

As to why it didn’t trigger my outlet switches, I used entity_id as per the original example, nothing even appeared in the logs. Switching to device_id made it work instantly. No clue why though.

Hope it helps someone in the future :slight_smile:

You probably entered the wrong entity_id. You can use go to Developer Tools > States to check the switch entity’s spelling.