Schedule automation for a water pump

Hello.

I want to turn on a water pump every hour for 5 minutes.
Its for a flood and drain hydroponic system.

What is the easest way of doing this. It is a D1 mini i am using.

use an automation with a time pattern trigger that triggers every hour (/1).

then in the actions turn on the pump, delay 5 minutes, turn off pump.

Achieving that is easy. What’s more challenging is to ensure the pump is set to the correct state even when Home Assistant is restarted, or automations are reloaded.

  • If the restart/reload occurs precisely when the pump should be turned on, the pump will remain off.
  • If it occurs when the pump should be turned off, the pump will be left on.

The following automation ensures the pump is turned on every hour, turned off 5 minutes later, and set to the correct state after a restart/reload.

alias: Pump Control
trigger:
  - platform: time_pattern
    hours: '/1'
  - platform: time_pattern
    minutes: 5
  - platform: homeassistant
    event: start
  - platform: event
    event_type: automation_reloaded
condition: []
  - service: switch.turn_{{ 'on' if now().minute < 5 else 'off' }}
    target:
      entity_id: switch.pump
1 Like

That’s what the OP said they wanted…easy…

if they said “it is important that the pump turns on and off every time” then the answer would have been more involved and less easy…like yours. :wink: :laughing:

I get your point but I think the difference between “easy” and my “more involved” suggestion isn’t very much.

Here’s what you suggested. It is vulnerable to restart/reloads during the entire 5-minute period when it should be on (due to the use of a delay).

alias: Pump Control
trigger:
  - platform: time_pattern
    hours: '/1'
condition: []
  - service: switch.turn_on
    target:
      entity_id: switch.pump
  - delay: '00:05:00'
  - service: switch.turn_off
    target:
      entity_id: switch.pump

The following automation doesn’t employ a delay so it’s immune to restart/reloads during the 5-minute period. However, it’s vulnerable to restart/reloads that occur exactly at the time when the automation should be triggered.

alias: Pump Control
trigger:
  - id: 'on'
    platform: time_pattern
    hours: '/1'
  - id: 'off'
    platform: time_pattern
    minutes: 5
condition: []
  - service: switch.turn_{{ trigger.id }}
    target:
      entity_id: switch.pump

This one is immune to restart/reloads at all times.

alias: Pump Control
trigger:
  - platform: time_pattern
    hours: '/1'
  - platform: time_pattern
    minutes: 5
  - platform: homeassistant
    event: start
  - platform: event
    event_type: automation_reloaded
condition: []
  - service: switch.turn_{{ 'on' if now().minute < 5 else 'off' }}
    target:
      entity_id: switch.pump

If you compare the amount of code/complexity in all three, the last one isn’t all that more complicated than the others. Yes, it has 2 or 3 more triggers than the other examples but just one (templated) action.

Thanks for the help. I will try it.

I know.

I was just joking. I figured you’d pick up on that.

How were you planning on using the D1? Is it to directly control a small pump or is it to control a valve? Will it fail safe by not flooding the place or risk not drying out the plants? How are you planning to power the D1 by battery or mains ? Mixing water and mains power does pose safety risks. I suppose you may be doing it in a manual way at present but not every hour.

Its a small system, with a pump that runs from a 5v mobile phone charger. The d1 is connected to a relay. The plants are above the water so it can never be a leek.

The idea is that it will stand by the window.

I am getting a error. The code is further down. Do you have any idea what it is?

ERROR Error while reading config: Invalid YAML syntax:

while parsing a block mapping
  in "/config/esphome/esphome-web-015908.yaml", line 1, column 1:
    esphome:
    ^
expected <block end>, but found '<block sequence start>'
  in "/config/esphome/esphome-web-015908.yaml", line 45, column 3:
      - service: switch.turn_{{ 'on' i ... 
      ^

This is how the code looks. I am a beginner so it is probable me.

esphome:
  name: esphome-web-015908

esp8266:
  board: esp01_1m

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:


wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Esphome-Web-015908"
    password: "kMaHH4byBUwP"

captive_portal:

switch:
  - platform: gpio
    pin: 4
    name: "hydro01"
    
    
    
    alias: Pump Control
trigger:
  - platform: time_pattern
    hours: '/1'
  - platform: time_pattern
    minutes: 5
  - platform: homeassistant
    event: start
  - platform: event
    event_type: automation_reloaded
condition: []
  - service: switch.turn_{{ 'on' if now().minute < 5 else 'off' }}
    target:
      entity_id: switch.hydro01

well, it seems none of us had a clue that you were looking for the code to put on the D1 Mini itself.

The code offered was for an automation running in HA not on the device.

Sounds good setup. I replaced my relay with a transistor as a switch in my setup. It can be a hit and miss affair with how long the pumps will last. It probably is better to keep the automation on the HA so you can adjust them quickly without having to refresh the D1 each time anyway.

Yes, of course. My bad. Told you i was new at this. :slight_smile:
I will do an automation to put it in.

1 Like

Hi, Would appreciate it if you could help me fix the below code.

Condition : time between 01:00:00 AM TO 03:00:00 AM

entity_id: binary_sensor.input_2 if the status is Off
THEN
entity_id: switch.e16s_output2 should ON

vice versa

my current code