Garden watering pump, prevent running dry

I have a microdrip/ sprinkler setup to pump water from my rainwater bin trough a set of hoses in my garden.
System is powered by a Wemos D1 mini on a breakout pcb board:

Pump is powered by a relais.
Water level in tank is monitored with an ultrasonic sensor( JSN-SR04T ).
Sprinkler is set in motion by either a homeassistant automation, homeassistant button, or a physical button on a gpio on the ESP.

What i would like to do, is setup esphome, so that the relais will not turn on, when the tank level is below a certain point(say 10%), to prevent my pump from running dry.
I could do this in homeassistant, but as the wifi connection is not great in my shed, i think it would be better to do this in esphome.

What is working, is that the pump will shut off when the water level drops below 10%, but what if someone presses the button when the water level is already below 10%, then my pump will burn itself out after running dry.

My code is:

  - platform: ultrasonic
    trigger_pin: 4
    echo_pin: 3
    unit_of_measurement: "%"
    icon: "mdi:water-percent"
    accuracy_decimals: 0
    update_interval: 60s
    name: "Regenton percentage"
    id: regenton_percent
    filters:
      - lambda: return (1-((x-.05)/1.00))*100;
      - filter_out: nan
    on_value_range:
        - below: 10.0
          then:
            - switch.turn_off: relais1
      
  - platform: ultrasonic
    trigger_pin: 4
    echo_pin: 3
    update_interval: 60s
    name: "Regenton liters"
    unit_of_measurement: "l"
    accuracy_decimals: 0
    filters:
      - lambda: return (1-((x-.05)/1.00))*240;
      - filter_out: nan

switch:
  - platform: gpio
    pin: 0
    name: "Tuin Water Relais"
    id: relais1
    icon: mdi:watering-can
    inverted: True
    restore_mode: ALWAYS_OFF
    internal: true
  - platform: template
    name: Tuin sproeiers
    icon: "mdi:watering-can"
    turn_on_action:
      then:
        - switch.turn_on: relais1
        - delay: 1200s
        - switch.turn_off: relais1
    lambda: 'return id(relais1).state;'

binary_sensor:
  - platform: gpio
    pin:
      number: 12
      mode:
        input: true
        output: false
      inverted: true
    name: waterknop
    device_class: power
    filters:
    - delayed_off: 500ms
    disabled_by_default: false

Hoping someone could help me. Thanks in advance.
Greetings
Mark.

Sounds like you just need an if: action under your existing turn_on_action.

The example is quite close to what I think you need.

I think if you invert it and base it on >10 rather that <10 then this will be more reliable as it won’t turn on if the sensor is unavailable (I think).

(edited mistake)

Thanks @Mahko_Mahko , i have looked at the example before, don’t know why i discarded it at the time.
I have it working now:

sensor:
  - platform: ultrasonic
    trigger_pin: 4
    echo_pin: 3
    unit_of_measurement: "%"
    icon: "mdi:water-percent"
    accuracy_decimals: 0
    update_interval: 60s
    name: "Regenton percentage"
    id: regenton_percent
    filters:
      - lambda: return (1-((x-.05)/1.00))*100;
      - filter_out: nan
    on_value_range:
        - below: 10.0
          then:
            - switch.turn_off: relais1
      
  - platform: ultrasonic
    trigger_pin: 4
    echo_pin: 3
    update_interval: 60s
    name: "Regenton liters"
    unit_of_measurement: "l"
    accuracy_decimals: 0
    filters:
      - lambda: return (1-((x-.05)/1.00))*240;
      - filter_out: nan

switch:
  - platform: gpio
    pin: 0
    name: "Tuin Water Relais"
    id: relais1
    icon: mdi:watering-can
    inverted: True
    restore_mode: ALWAYS_OFF
    internal: true
  - platform: template
    name: Tuin sproeiers
    icon: "mdi:watering-can"
    turn_on_action:
      then:
        - if:
            condition:
              lambda: 'return id(regenton_percent).state > 10;'
            then:
              - switch.turn_on: relais1
              - logger.log: "Tuin Sproeien gestart."
              - delay: 1200s
              - switch.turn_off: relais1
              - logger.log: "Tuin Sproeien gestopt."
            else:
              - logger.log: "Error, waterlevel low!"
              - homeassistant.service:
                  service: notify.test
                  data:
                    message: Error, waterlevel low!
                    title: "Tuin Sproeier"
    turn_off_action:
      then:
        - switch.turn_off: relais1
    lambda: 'return id(relais1).state;'

binary_sensor:
  - platform: gpio
    pin:
      number: 12
      mode:
        input: true
        output: false
      inverted: true
    name: waterknop
    device_class: power
    filters:
    - delayed_off: 500ms
    disabled_by_default: false
2 Likes

‘Would appreciate if help me in fixing the below code’

Condition - time 01:00:00 TO 3:00:00 AM
entity_id: - binary_sensor.input_2 - if the status is OFF
THEN
entity_id: switch.e16s_output2 - status change from OFF to ON
ViceVersa

my current code

You just wrote out exactly what you need to do. If you Rent good at yaml, why don’t you just use the UI? The automation your wanting to make is very basic and a beginner level automation that can quickly be done in the UI.