ESPHome and using YAML to program automation on the ESP-32

Hello all,
I’m trying to use an ESP-32 to remotely control my pool pump via a series of relays. I’ve managed to use ESPHome to upload yaml files into the ESP32 so that i can view 4 switches in my home assistant GUI. Now i want to create schedules to control the pump throughout the day but want to off load that work to the ESP32 rather than use HA.
I can muddle my way through VB and C (Arduino IDE) but i just can’t figure out how to do this in YAML.

I would like to set relays in a certain on/off position and then based on a time passed or clock to change the relay positions to their next position sequence.

Can someone point me in the right direction?
I’m looking to cycle through my pump timers via relay inputs based on time of day. Also being able to activate a certain relay sequence as needed via HA automation. (turn on water jets then kick pump to full power and vice versa)
Untitled

My current code:

esphome:
  name: pool
  platform: ESP8266
  board: nodemcuv2

wifi:
  ssid: "xxxxxxx"
  password: "xxxxxx"

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "xxxxxx"
    password: "xxxxx"

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:
  password: "xxxxx"

ota:
  password: "xxxxxx"
  
switch:
  - platform: gpio
    name: "Relay 1"
    pin: GPIO4
  - platform: gpio
    name: "Relay 2"
    pin: GPIO5
  - platform: gpio
    name: "Relay 3"
    pin: GPIO12
  - platform: gpio
    name: "Relay 4"
    pin: GPIO14
1 Like

Take a look at the on_time config in the ESPHome docs.

That will do what you need for recurring events at specific times of day.

You can then do something like this:

     - minutes: 00
        hours: 10
        days_of_week: SUN-SAT
        months: APR-OCT
        then:
          - switch.turn_on: waterfall_pump
      - minutes: 00
        hours: 22
        days_of_week: SUN-SAT
        months: APR-OCT
        then:
          - switch.turn_off: waterfall_pump
      - minutes: 00
        hours: 10
        days_of_week: SUN-SAT
        months: NOV-MAR
        then:
          - switch.turn_on: waterfall_pump
      - minutes: 10
        hours: 10
        days_of_week: SUN-SAT
        months: NOV-MAR
        then:
          - switch.turn_off: waterfall_pump
      - minutes: 00
        hours: 00
        then:
          - sensor.integration.reset: pond_water_usage
2 Likes

This part sounds like a script that is activated by a user defined service on the esp unit that can be called from home assistant. See Native API Component — ESPHome . But it can be implemented in many ways depending on what you find most useful for your use case.

1 Like

@ashscott That is great. Go figure the add-on has great documentation. Here i just went straight to the web to learn yaml.

@MatthewFlamm I’m thinking just an automation in HA for the deckjets relay and calling a script for the high flow sequence on the pump. thanks for the idea!

I suggest you put your automations on the device if possible and leave HA automations to situations where you need the state of multiple devices. What happens if your HA instance is down because of an upgrade snafu?

I took your advice and created a switch in HA to turn on deck jets that opens an automated valve (via relay) and changes the pump speed. then does the opposite when turned off. Just need to figure out how to stop the system from running on_time triggers when deck jet switch is on and also when turned off how to return the pump to the correct timer cycle… I’ll think on that a bit.

switch:
  - platform: gpio
    name: "Relay 1"
    pin: GPIO4
    inverted: yes
    id: relay1
  - platform: gpio
    name: "Relay 2"
    pin: GPIO5
    inverted: yes
    id: relay2
  - platform: gpio
    name: "Relay 3"
    pin: GPIO12
    inverted: yes
    id: relay3
  - platform: gpio
    name: "Relay 4"
    pin: GPIO14
    inverted: yes
    id: relay4
  - platform: gpio
    name: "Deck Jets"
    pin: GPIO13
    inverted: yes
    id: deck_jets
    on_turn_on:
    - switch.turn_on: deck_jets
    - delay: 10s
    - script.execute: timer_1
    on_turn_off:
    - switch.turn_off: deck_jets
    - delay: 10s
    - script.execute: timer_3
time:
  - platform: homeassistant
    id: homeassistant_time #Point to HA to provide a time update to ESP32
    on_time:
      - seconds: 0
        minutes: 0
        hours: 3
        then: #turn pump off
        - script.execute: pump_off
      - seconds: 0
        minutes: 0
        hours: 7
        then: #turn on timer 4
        - script.execute: timer_4
      - seconds: 0
        minutes: 30
        hours: 8
        then: #turn on timer 5
        - script.execute: timer_5
      - seconds: 0
        minutes: 0
        hours: 12
        then: #turn on timer 1
        - script.execute: timer_1
      - seconds: 0
        minutes: 30
        hours: 12
        then: #turn on timer 6
        - script.execute: timer_6
      - seconds: 0
        minutes: 0
        hours: 16
        then: #turn on timer 2
        - script.execute: timer_2
      - seconds: 0
        minutes: 45
        hours: 16
        then: #turn on timer 3
        - script.execute: timer_3
      - seconds: 0
        minutes: 0
        hours: 19
        then: #turn pump off
        - script.execute: pump_off
      - seconds: 0
        minutes: 0
        hours: 23
        then: #turn on timer 7
        - script.execute: timer_7
script:
  - id: timer_1
    then:
        - switch.turn_off: relay1
        - switch.turn_off: relay2
        - switch.turn_off: relay3
        - switch.turn_off: relay4
  - id: timer_2
    then:
        - switch.turn_on: relay1
        - switch.turn_off: relay2
        - switch.turn_off: relay3
        - switch.turn_off: relay4
  - id: timer_3
    then:
        - switch.turn_off: relay1
        - switch.turn_on: relay2
        - switch.turn_off: relay3
        - switch.turn_off: relay4
  - id: timer_4
    then:
        - switch.turn_on: relay1
        - switch.turn_on: relay2
        - switch.turn_off: relay3
        - switch.turn_off: relay4
  - id: timer_5
    then:
        - switch.turn_off: relay1
        - switch.turn_off: relay2
        - switch.turn_on: relay3
        - switch.turn_off: relay4
  - id: timer_6
    then:
        - switch.turn_on: relay1
        - switch.turn_off: relay2
        - switch.turn_on: relay3
        - switch.turn_off: relay4
  - id: timer_7
    then:
        - switch.turn_off: relay1
        - switch.turn_on: relay2
        - switch.turn_on: relay3
        - switch.turn_off: relay4
  - id: timer_8
    then:
        - switch.turn_on: relay1
        - switch.turn_on: relay2
        - switch.turn_on: relay3
        - switch.turn_off: relay4
  - id: pump_off
    then:
        - switch.turn_off: relay1
        - switch.turn_off: relay2
        - switch.turn_off: relay3
        - switch.turn_on: relay4
    

Do all your automations using scripts. There is a condition to check if a script is already running and you can handle all the possibilities that way. For example, it script is running, turn off script, turn off all relays, execute new script. This way there are go clashes between automations.

Scripts are very useful but are buried in the documentation. See here.

You are already using scripts I see sorry. Just use the is_running condition to make sure only one script is running at a time if needed.

im not familiar with using the time from HA to set time automations. Will this fail to work when the connection happens to drop momentarily at the wrong time on your device? My esp devices do drop out of connection briefly several times a day. If you can get the timer on the esp device itself, it would be preferred, but again, I don’t have any direct experience there.

Hi All,
I managed to figure out a method to have the deck jets on and turn the pump up to full power then on switch.off the cycle resumes at the correct time spot. I’ve used variables to get the timer_# being used then when exiting the deck_jet mode it should fire the proper sequence. See code below. I’m having issues with getting the variable to work on exiting the deck_jet. Any thoughts on what i’m doing wrong?
Note: I did not worry about stopping a script as they are run fairly instantaneous. they execute then the ESP keeps looping until the next trigger occurs.

  - platform: gpio
    name: "Deck Jets"
    pin: GPIO13
    inverted: yes
    id: deck_jets
    on_turn_on:
    - switch.turn_on: deck_jets
    - delay: 10s
    - script.execute: timer_1 #turn on relay sequence with high flow rate for pump
    on_turn_off:
    - switch.turn_off: deck_jets
    - delay: 10s
    - script.execute: id(cycle_on) # This is the problematic line. resume schedule in the correct pump rate based on variable being written at each sequence change
 
time:
  - platform: homeassistant
    id: homeassistant_time #rPoint to HA to provide a time update to ESP32
    on_time:
      - seconds: 0
        minutes: 0
        hours: 3
        then: #turn pump off
          - if:
             condition:
              switch.is_off: deck_jets
             then:
              - globals.set:
                 id: cycle_on
                 value: 'pump_off'
              - script.execute: pump_off
             else:
              - globals.set:
                 id: cycle_on
                 value: 'pump_off'
      - seconds: 0
        minutes: 0
        hours: 7
        then: #turn on timer 4
          - if:
             condition:
              switch.is_off: deck_jets
             then:
              - globals.set:
                 id: cycle_on
                 value: 'timer_4'
              - script.execute: timer_4
              else:
              - globals.set:
                  id: cycle_on
                  value: 'timer_4'