ESPHome SoilMoisture Sensore with water pump

Hi!

I have a school project, where I would like to do an automated Irrigation system.
I have an esp8266 mini D1 pro, a capacitive moisture sensor v1.2, and a basic 5V water pump with a MOSFET.
My previous project was this but in Arduino now I have to implement it in ESPhome.

My Arduino code:

void loop()
{
  soil_moisture_value = analogRead(sensor);
  soil_moisture_percent = map(soil_moisture_value, 720, 410, 0, 100);

  Serial.print("Sensor_value: ");
  Serial.println(soil_moisture_value);
  Serial.print("Percentage: ");
  Serial.print(soil_moisture_percent);
  Serial.println("%");

 if(soil_moisture_percent >5 && soil_moisture_percent < 100)
{
 //Irrigation
 if(soil_moisture_percent < 30){
 Serial.println("Start irrigation...");
 digitalWrite(D7, HIGH);
 Serial.print("Irrigating for ");
 Serial.print(irrigation_time);
 Serial.println(" seconds");
 delay(irrigation_time*1000);
 digitalWrite(D7, LOW);
 Serial.println("Stop irrigation...");
 }
}

I connected the pump with the MOSFET so if the MOSFET Gate gets a high voltage the pump starts. (D7 pin).

I would like to do the same in HomeAssistant.

I have already made the code for the sensor and a switch, but I don’t know how to put voltage on the D7 pin if the required percentages meet. Plus I would like to start the pump manually.

ESPHome code:

sensor:
  - platform: adc
    pin: A0
    name: "Soil Moisture"
    update_interval: 10s
    unit_of_measurement: "%"
    filters:
    - median:
        window_size: 7
        send_every: 4
        send_first_at: 1
    - calibrate_linear:
        - 0.396 -> 100.00
        - 0.729 -> 0.00
    - lambda: if (x < 1) return 0;  else if (x > 100) return 100; else return (x);
    accuracy_decimals: 0

switch:
 - platform: gpio
   name: "Water_Pump"
   id: relay_1
   pin: GPIO13

I’m not really good at coding. Can someone help me with how to do it? I’m working on the project too if I make any progress I will update the thread.

Sorry for my bad English.

I think you need an on_value_range automation on your soil moisture sensor.

@petizzz , did you manage the sensor coding i.a.w. with the pump? I would be interested in your final project due to I’ve the same setting as you.

I made something similar, still in the testing phase but so far it works well.
Maybe you can use it for your project as well!

esphome:
  name: PlantAssistant
  platform: ESP8266
  board: esp01_1m

# WiFi connection, correct these
# with values for your WiFi.
wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  ap: {}

# Enable logging.
logger:

# Enable Home Assistant API.
api:

# Enable over-the-air updates.
ota:

# Enable Web server.
web_server:
  port: 80

# Sync time with Home Assistant.
time:
  - platform: homeassistant
    id: homeassistant_time

# Text sensors with general information.
text_sensor:
  # Expose ESPHome version as sensor.
  - platform: version
    name: PlantAssistant ESPHome Version
  # Expose WiFi information as sensors.
  - platform: wifi_info
    ip_address:
      name: PlantAssistant IP
    ssid:
      name: PlantAssistant SSID
    bssid:
      name: PlantAssistant BSSID

# Sensors with general information.
sensor:
  # Uptime sensor.
  - platform: uptime
    name: PlantAssistant Uptime

  # WiFi Signal sensor.
  - platform: wifi_signal
    name: PlantAssistant WiFi Signal
    update_interval: 60s
    
  # moisture sensor
  - platform: adc
    id: moisture_sensor_voltage_internal
    pin: A0
    update_interval: 1s
    icon: "mdi:sprout-outline"
    unit_of_measurement: "v"
    filters:
      - multiply: 3.3
  - platform: template
    id: moisture_sensor_percentage_internal
    update_interval: 1s
    lambda: return id(moisture_sensor_voltage_internal).state;
    filters:
      -  calibrate_linear:
          - 0.93 -> 100.00
          - 1.63 -> 0.00
      - clamp:
          min_value: 0
          max_value: 100
    accuracy_decimals: 0
    on_value_range:
      # Only turn on the pump when watering is enabled.
      - below: 10
        then:
          - if:
              condition:
               - switch.is_on: watering_enabled
              then:
               - switch.turn_on: pump
      - above: 90
        then:
         - switch.turn_off: pump
  - platform: template
    name: "Soil moisture percentage"
    id: moisture_sensor_percentage
    icon: "mdi:sprout-outline"
    unit_of_measurement: "%"
    update_interval: 10min
    lambda: return id(moisture_sensor_percentage_internal).state;
  - platform: template
    name: "Soil Moisture voltage"
    id: moisture_sensor_voltage
    icon: "mdi:sprout-outline"    
    unit_of_measurement: "v"
    update_interval: 10min
    lambda: return id(moisture_sensor_voltage_internal).state;
      
# Global to store the on/off state of the pump
globals:
  - id: pump_state
    type: bool
    restore_value: true
    initial_value: 'true'

# Exposed switches.
switch:
  # Switch to restart.
  - platform: restart
    name: PlantAssistant Restart

  # Switch to turn on/off the automatic watering.
  - platform: gpio
    id: pump
    pin: GPIO14

  # Switch to turn on/off watering automation
  #
  # It creates a "virtual" switch based
  # on a global variable.
  - platform: template
    name: PlantAssistant watering enabled
    id: watering_enabled
    restore_mode: RESTORE_DEFAULT_ON
    turn_on_action:
      - globals.set:
          id: pump_state
          value: 'true'
    turn_off_action:
      - globals.set:
          id: pump_state
          value: 'false'
    lambda: |-
      return id(pump_state);
1 Like