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.
Bill of materials
- 2-channel 5V Relay Module for Arduino ARM PIC AVR DSP Electronic
- WeMos D1 Mini
- 830 hole breadboard, breadboard power module, 65 DuPont Wires and 140 Boxed Wires
- 9V 1A AC to DC Power Supply for Arduino
- 220VAC to 26VAC converter (EG. Hunter/Irritron Power Supply)
- AM2320 Digital Temperature and Humidity Sensor
- 24VAC Solenoid Valve (eg. Hunter/Irritron)
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.
In this case, the address to note would be “COM33”.
Go to ESPHome 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.
Click on Connect.
You should now see a “Device Connected” screen.
Click on “PREPARE FOR FIRST USE”, and click on “INSTALL”
The basic version of ESPHome will now be installled.
Once you see the “Configuration Installed” message, click “close”. A message will appear to connect to WiFi.
Enter your home WiFi details; and click connect.
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.
Go to Add-ons, and click on the ADD-ON STORE at the bottom right of the page.
Find the ESPHome add-on and click on it.
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.
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.
This will create a configuration with an encryption key, click on INSTALL.
This brings up an Install Screen as below.
If the installation is successful, the following screen will appear with some statistics such as Signal strength.
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.
The code used is below:
friendly_name: Irrigation Controller
# Enable logging
# Enable Home Assistant API
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
- id: garden_sprinkler_ctrlr
- valve_switch: "Lawn"
- platform: gpio
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:
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:
- platform: dht
name: "Outside Temperature"
name: "Outside Humidity"
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):
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.
Click on Create Automation, and set it up as follows:
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.
- id: garden_sprinkler_ctrlr
- valve_switch: "Lawn"
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:
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!