ESPHome faking Start and Stop sprinkler buttons

Hi Guys,
my sprinkler has two buttons to Start and Stop irrigation and I want to automate it by adding two relays in order to fake my finger pressing.
So on my ESPHome yaml I added two switch to control relays, and a third template switch to turn on and off “irrigation time”. Everything works but the 3rd template switch turns off by itself after 1 sec.

Can anybody suggest how to fix?

esphome:
  name: d1-mini-claber

esp8266:
  board: esp01_1m
  restore_from_flash: true
  
substitutions:
  hostname: "D1 Mini Claber:"    

logger:
api:

ota:
  password: "xxxxx"

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

captive_portal:

switch:
  - platform: gpio
    pin: GPIO14 
    name: "Claber Start Relay" 
    id: D5
    inverted: yes

  - platform: gpio 
    pin: GPIO12  
    name: "Claber Stop Relay"
    id: D6
    inverted: yes

  - platform: template
    name: "Claber Aquauno Irrigation"
    id: irrigation
    
    turn_on_action:
    - switch.turn_on: D5
    - delay: 500ms
    - switch.turn_off: D5
    turn_off_action:
    - switch.turn_on: D6
    - delay: 500ms
    - switch.turn_off: D6

Guess that’s because of this code:

What logic is it exactly that you need? Could you explain it in a few sentences?

Yeah.
I want to simulate my finger pressing on the physical start button for 0.5 sec (and stop button of course)

acting one relay per button

I see, maybe you need to be more optimistic and add:

  optimistic: true

to your template switch :raised_hands:

LOL, I am, and it works properly!
Thank you.
It was intuition or did you get any working example?

1 Like

Well, I was at this point long time ago :joy: And the optimistic option on the template switch is something one doesn’t forget :wink:

In case this thread ends up as monolog the waybackmachine got you covered :wave:

It is basically because there is no feedback to the esp that the command has worked. ‘Optimistic’ means ‘assume it works’.

I wish to share with you all the final YAML file that works perfectly.

esphome:
  name: d1-mini-claber

esp8266:
  board: esp01_1m
  
substitutions:
  hostname: "D1 Mini Claber:"    

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:
  password: "XXXXXXXXXXXXXXXXXXXXXXXXXX"

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

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "D1-Mini-Claber Fallback Hotspot"
    password: "XXXXXXXXX"

captive_portal:

switch:
  - platform: gpio
    pin: GPIO14  #D5
    name: "Claber Start Relay" 
    id: D5
    inverted: yes
    on_turn_on:
    - delay: 600ms
    - switch.turn_off: D5

  - platform: gpio 
    pin: GPIO12  #D6
    name: "Claber Stop Relay"
    id: D6
    inverted: yes
    on_turn_on:
    - delay: 600ms
    - switch.turn_off: D6

  - platform: template
    name: "Irrigazione Claber Aquauno"
    id: irrigazione
    optimistic: true
    
    turn_on_action:
    #reset della programmazione
    - switch.turn_on: D5
    - switch.turn_on: D6
    - delay: 500ms
    - switch.turn_off: D5
    - switch.turn_off: D6
    - delay: 1sec
    #pressione pulsante Start
    - switch.turn_on: D5
    - delay: 500ms
    - switch.turn_off: D5
    
    turn_off_action:
    #pressione pulsante Stop
    - switch.turn_on: D6
    - delay: 500ms
    - switch.turn_off: D6
    - delay: 1sec
    #reset della programmazione
    - switch.turn_on: D5
    - switch.turn_on: D6
    - delay: 500ms
    - switch.turn_off: D5
    - switch.turn_off: D6
    
light:
  - platform: status_led
    name: $hostname Status LED
    pin:
      number: GPIO2
      inverted: false
    restore_mode: ALWAYS_ON
    id: status_LED
    
text_sensor:
  - platform: wifi_info
    ip_address:
      name: $hostname IP Address
      
sensor:
  - platform: wifi_signal
    name: $hostname WiFi Signal
    id: rssi_value
    icon: mdi:wifi
    update_interval: 60s