HOW TO USE ESPHOME IRRIGATION CONTROLLER IN HOME ASSISTANT (Part 1)

PAUL VERBURG

This is a multi-part blog about using ESPHome integrated into Home Assistant for home irrigation and monitoring.

Part 1 of the blog goes through the steps of setting up ESPHome on an ESP device, adding it to Home Assistant, adding temperature and humidity measurements and setting up a schedule for irrigation.

What is ESPHome?

ESPHome is a firmware developed for ESP devices (ESP8266 and ESP32), and recently also included the Raspberry Pi Pico microcontroller. This firmware allows you to do simple yet powerful configurations for home automation systems by using existing “components” in the firmware. If you don’t want to sit through hours writing code, but you still like the DIY process, then ESPHome is the ideal platform.

ESPHome was bought by die creators of Home Assistant, which led to a greatly simplified implementation of ESPHome into your Home Assistant ecosystem.
ESPHome Website

Bill of materials

Connecting Wemos D1 Mini to Home Assistant

To get started, connect your Wemos D1 Mini (or other ESP device) to your PC or Laptop using a USB cable (USB micro for Wemos D1 min). Once connected, open your device manager if you’re using Windows, go to “Ports (COM & LPT)”, and find the ESP com port, most likely the device with “CH340G” in the name, as below.
Device Manager

In this case, the address to note would be “COM33”.

Go to ESPHome Web

ESP Home Web

Click on CONNECT, this will prompt a popup where you need to select the COM port, this is the port that we identified in device manager. In my case it is COM33.

ESPHome Web Com Port

Click on Connect.

You should now see a “Device Connected” screen.

ESPHome Web Device Connected

Click on “PREPARE FOR FIRST USE”, and click on “INSTALL”

ESPHome Web Prepare For First Use

The basic version of ESPHome will now be installled.

ESPHome Web Installatin

ESPHome Web Succesful installation

Once you see the “Configuration Installed” message, click “close”. A message will appear to connect to WiFi.

ESPHome WEB Enter WiFi Credentials

Enter your home WiFi details; and click connect.

ESPHome Web WiFi Connected

ESPHome Home Assistant Add On 

With your ESP device flashed with the core ESPHome firmware, we need to add the device to your Home Assistant Server.

Note: Setting up your Home Assistant server is not covered here, look at our Earlier Blog for instructions.

Go to the settings tab in Home Assistant.

Home Assistant Settings

Go to Add-ons, and click on the ADD-ON STORE at the bottom right of the page.

Home Assistant Add Ons

Home Assistant Add-On Store

Find the ESPHome add-on and click on it.

HA ESPHome Add ON

You should see a screen with the ESPHome add-on information. Click on Install.

After installation is complete, put the “Start on Boot, Watchdog, Show in sidebar and Watchdog” sliders active.

Now you can click on “Run” to start the ESPHome add-on for Home assistant.

Adopt ESP Device into ESPHome dashboard

The ESPHome dashboard will now be visible in the sidebar on the left. Proceed to that dashboard by clicking on ESPHome in the sidebar, or by clicking on “OPEN WEB UI” on the current add-on page.

If your ESPHome flash was successful, and the device is still powered on and connected to your WiFi, the device will be available for adoption in the ESPHome dashboard.

ESPHome HA Device Discovered

NOTE: FOR THE ADOPTION PHASE OF THE DEVICE, THE ESP DEVICE NEEDS TO BE PHYSICALLY CONNECTED TO THE HOME ASSISTANT SERVER, NOT TO THE PC/LAPTOP USED FOR VIEWING

Click on ADOPT, you are prompted with a popup to enter a name for the device, enter a sensible name, for this project it is “Irrigation Controller”. Then click “ADOPT” on the pop-up.

ESPHome Add Device Give Name

This will create a configuration with an encryption key, click on INSTALL.

ESPHome HA Device Added

This brings up an Install Screen as below.

ESPHome HA Install Configuration

If the installation is successful, the following screen will appear with some statistics such as Signal strength.

ESPHome HA Installation Succesful

TIP: You can set your WiFi details in your “Secrets” file. This allows you to not manually enter it in your yaml file, it also allows you to post your code on forums without people getting access to your WiFi SSD and Password. 

Your ESP device is now ready to be configured and used in Home Assistant!

Configure ESP Device for Sprinkler Control

For part 1 of the blog, we are only adding one valve to control one sprinkler area.

The ESPHome component that is used for this project, is the Sprinkler Controller component. All code used in this project is available from the component link.

On your ESPHome dashboard, click on “edit” on your Irrigation Controller device. This will open the .yaml file which contains the configuration of your controller.

Irrigation Controller yaml

The code used is below:

esphome:

  name: irrigation-controller

  friendly_name: Irrigation Controller

 

esp8266:

  board: d1_mini

 

# Enable logging

logger:

 

# Enable Home Assistant API

api:

  encryption:

    key: "YreLyy/Nl9yxrXIdmXf5zqxYGYOis4JIQId+np8rqoQ="

 

ota:

  password: "629afccaba1be3d4a7f71e4b2b8a142c"

 

wifi:

  ssid: !secret wifi_ssid

  password: !secret wifi_password

 

  # Enable fallback hotspot (captive portal) in case wifi connection fails

  ap:

    ssid: "Irrigation-Controller"

    password: "dhodvuqh64pq"

 

captive_portal:

 

sprinkler:

  - id: garden_sprinkler_ctrlr

    valves:

      - valve_switch: "Lawn"

        run_duration: 300s

        valve_switch_id: garden_sprinkler_valve

 

switch:

  - platform: gpio

    id: garden_sprinkler_valve

    pin: D5

 

This code can be used exactly as is above, but there are a few things to consider.

  • If you’re not using a Wemos D1 Mini, ensure that your “board” is correct.
  • You can change the “run duration” of your valve to your preferred duration.
  • The “pin” must correlate to the physical pin that you will connect your relay for the solenoid.

Save the configuration, and the install the new configuration to your ESP device. This change in code can be done wirelessly and the ESP does not need to be connected with a cable to your Home Assistant server anymore. In fact, you can update your code from anywhere on your network while the controller is installed and active!

After the update, new entities are added to Home Assistant for you to use anywhere. Add a Card to your dashboard, and add the Irrigation Controller Lawn entity to the card.

All the setup necessary for you to control your irrigation system from Home Assistant is now complete! But we need to connect all the hardware first.

Connecting All Hardware

First step is to add power to our breadboard and put the ESP device on the breadboard and provide power.

We will use 5V for all components in this project, ensure that your breadboard power supply is set to 5V, not 3.3V.

For all information regarding the breadboard and power supply, follow this link.

Add the Wemos D1 Mini to the breadboard, and provide power to the ESP on the VBUS and GND pins from the breadboard power rails.

NOTE: Do not put 5V on the 3.3V pin

The ESP device is now powered. Confirm that everything is still working by going to your ESPHome dashboard in Home Assistant, and verify that the Irrigation Controller device is still Online.

Next connect the 2 relay board to the correct pin on the Wemos D1 mini as well as provide it power.

The relay board has 6 pins, 2 of them are connected to each other with a jumper, these are COM and GND. Leave them as is.

The other 4 pins are as follows:

  • VCC
  • GND
  • IN1
  • IN2

Connect VCC to the 5V rail of your breadboard. Connect the GND pin to the GND rail of your breadboard.

Your relay board will now indicate power by a red LED.

Connect IN1 to the pin you specified in the yaml file to control your valve. In the code above it is D5.

Test if everything is working by going to Home Assistant, and clicking on the Irrigation Controller card created earlier. You should see a light go on on the relay, and hear a small click noise as the relay switches.

Add DHT22 to ESPHome and Breadboard 

Now that the relay control for the solenoid is tested, we can add a DHT22 sensor for temperature and humidity monitoring, before installing the project at the irrigation valve.

Open the yaml file of the irrigation controller, and add the following code:

sensor:

  - platform: dht

    pin: D6

    temperature:

      name: "Outside Temperature"

    humidity:

      name: "Outside Humidity"

    update_interval: 5s

Save the configuration, and install wirelessly.

After successful installation, a new temperature and humidity entities will be available in your Home Assistant Dashboard.

Add cards for temperature and humidity.

The dashboard should look similar to the below (without temperature and humidity data available):

HA Irrigation Dashboard

In the code we assigned pin D6 for the DHT22 sensor.

Connect VCC of the DHT22 to the 5V rail of the breadboard. Connect GND to GND on the breadboard, and connect the Signal pin of the DHT22 to pin 6 on the Wemos D1 mini.

On your Home Assistant Dashboard, temperature and humidity will now be visible on the cards you created.

NOTE: If you are getting strange data readings from the DHT22 after connecting, reinstall the code on your ESP device.

Connect solenoid valve to ESP Device

Add a 24VAC solenoid valve to your irrigation pipe, as shown below: 

Your solenoid valve will look similar to below:

On the part labeled “Solenoid”, there are 2 wires connected. Connect one of the wires to the common (middle pin) of your relay board.

Connect the other wire to on of the two wires from your 24VAC power supply.

The remaining wire from your 24VAC power supply is now connected to the normally open side of the relay. This is the pin on the same side as the label “1” or “2” on the relay board.

 

With this setup, you can control your irrigation system from Home Assistant!

Go to your Irrigation controller card on your Home Assistant Dashboard. Click on the card, and your sprinkler should start.

Setting up schedule for Irrigation

It’s nice to be able to control your sprinkler from Home Assistant, but it’s critical to be able to schedule it as well.

Go to settings in Home Assistant, and proceed to Automations.

Home Assistant Automations

Click on Create Automation, and set it up as follows:

Automation Configuration_2

Automation Configuration_3

The settings above triggers an action based on a fixed time with a condition. The fixed time is the start time of our sprinkler, the condition is to ensure that is not already running, and the action is to toggle the sprinkler on.

From our code, we have a duration variable.

sprinkler:

  - id: garden_sprinkler_ctrlr

    valves:

      - valve_switch: "Lawn"

        run_duration: 300s

        valve_switch_id: garden_sprinkler_valve

There we have set 300 seconds. This means we only need to set start times for the sprinkler, as it will stop after the set 300 seconds.

The irrigation system is now active with a schedule, and temperature and humidity monitoring.

To confirm that everything is still working when you’re not at home, click on “logbook” on the Home Assistant sidebar, and you’ll see the actions performed:

HA Log Book

Conclusion

Part 1 allowed us to build a basic irrigation control system using ESPHome and Home Assistant, and to add a sensor.


In Part 2 and onward, we will look at installing everything in a waterproof box, adding soil moisture sensors, automating according to sensor data, using NoderRED for more complicated automations and much more!

1 comment

Jacques

Jacques

Thanks for the helpful article – managed to successfully set up the solenoid valve control via ESPHome & Home Assistant using the provided steps :)

Leave a comment