ESPHome with Conditional on_press

In ESPHhome I have config to start pumping water into a barrel when empty.
When vlotter1 on, it starts pumping 3 minutes, waits 3, etc.
with this code:

binary_sensor:
  - platform: gpio
    id: vlotter1
    # bottom Vlotter
    # On is vlotter down (empty)
    name: '${esp_name} - Vlotter 1'
    pin:
      number: GPIO25
      mode:
        input: true
        pullup: true
    on_press:
      - delay: 1s
      - switch.turn_on: relay
      - delay: 3min
      - switch.turn_off: relay
      - delay: 3min
      - switch.turn_on: relay
      - delay: 3min
      - switch.turn_off: relay

  - platform: gpio
    id: vlotter2
    # Top Vlotter
    # On is vlotter down (empty)
    name: '${esp_name} - Vlotter 2'
    pin:
      number: GPIO27
      mode:
        input: true
        pullup: true
    on_release:
      - delay: 1s
      - switch.turn_off: relay

switch:
  - platform: gpio
    pin: GPIO22
    id: relay
    name: '${esp_name} - Switch'
    icon: mdi:pump
    on_turn_on:
      - delay: 3min
      - switch.turn_off: relay
      - delay: 3min
      - switch.turn_on: relay
      - delay: 3min
      - switch.turn_off: relay

This works most of the time but sometimes it keeps pumping when the barrel is full.

I would like to make the switch.turn_on conditional. But I am wrestling with how to do that.

I came up with the following:

	then:
      - if:
          condition:
            switch.is_off: vlotter2
          then:
            - switch.turn_on: 
              id: relay

But whatever I try I cannot get it correctly inserted in the switch and binary sensor code.

Can you please help out to make the switching conditional?

Why don’t you just buy a 10$ float switch? Your making it more complicated than it needs to be with qll these delays and in/off triggers. A simple float switch will turn on a relay and pump when the float drops to a certain level and when it rises back up with the water level, it will shut off the relay switch.

A “vlotter” is a floatswitch.

Please help me with the right syntax of the code :slight_smile:

There are multiple issues with your code that you have to solve:

  • You call a switch.turn_on from within the on_press handler of that same switch, effectively creating a loop that may trigger itself over and over again.
  • You have implemented 3 minute on/off schedules in two places, acting on the same switch. These schedules can be triggered at the same time and their on and off commands will overlap and interfere in a completely unpredictable way.

I strongly recommend that you move your 3 minute cycle logic to a script component that you can call from wherever it is needed.

The beauty of scripts is that the script.mode parameter allows you to control what will happen if the script is called again while it is still running.

You also have a script.stop method that you can call to break off the execution of the script if needed.

1 Like

Will do that, but how do I get a “conditional” on_press?

This must be it:

binary_sensor:
  - platform: gpio
    id: vlotter1
    name: '${esp_name} - Vlotter 1'
    pin:
      number: GPIO25
      mode:
        input: true
        pullup: true
    on_press:
      then:
        - if:
            condition:
               binary_sensor.is_on: vlotter2
            then:
              - script.execute: pomp3min

  - platform: gpio
    id: vlotter2
    name: '${esp_name} - Vlotter 2'
    pin:
      number: GPIO27
      mode:
        input: true
        pullup: true
    on_release:
      - delay: 1s
      - switch.turn_off: relay
      - script.stop: pomp3min

switch:
  - platform: gpio
    pin: GPIO22
    id: relay
    name: '${esp_name} - Switch'
    icon: mdi:pump
    on_turn_on:
      then:
        - if:
            condition:
               binary_sensor.is_off: vlotter2
            then:
              - switch.turn_off: relay
              - script.execute: pomp3min
  
script:
  - id: pomp3min
    then:
      - switch.turn_on: relay
      - delay: 3min
      - switch.turn_off: relay
      - delay: 3min
      - switch.turn_on: relay
      - delay: 3min
      - switch.turn_off: relay
      - delay: 3min
      - switch.turn_on: relay
      - delay: 3min
      - switch.turn_off: relay
1 Like

Now 1 thing left… when the barrel is full (vlotter2 is off) I want the switch (relay) to turn of when turned on. I thought to do this:

</s> <s>switch:</s> <s> - platform: gpio</s> <s> pin: GPIO22</s> <s> id: relay</s> <s> name: '${esp_name} - Switch'</s> <s> icon: mdi:pump</s> <s> on_turn_on:</s> <s> then:</s> <s> - if:</s> <s> condition:</s> <s> binary_sensor.is_off: vlotter2</s> <s> then:</s> <s> - switch.turn_off: relay</s> <s> - script.execute: pomp3min</s> <s>

But that does not work as expected… whats the best way to approach this?

Works, had a logical typo.

Perfect. Congratulations! :clap: