โก Quick Answer
Install the ESPHome add-on in Home Assistant, create a new device config with your Wi-Fi credentials and DHT22 sensor settings, flash it to your ESP32 via USB using the ESPHome web flasher at web.esphome.io, then adopt the device in HA. The whole process takes about 20 minutes and requires zero soldering for a basic sensor build.
๐ What You Need
- ESP32 development board โ the microcontroller you'll flash ESPHome onto
- DHT22 temperature/humidity sensor โ module with breakout pins is easiest
- USB-A to micro-USB cable โ for initial flashing (data cable, not charge-only)
- Home Assistant instance with the ESPHome add-on installed
In This Guide
1. What is ESPHome and Why Use It
ESPHome is a system that turns ESP8266 and ESP32 microcontrollers into smart home sensors and switches โ configured entirely through YAML, with no programming knowledge required. You write a config file describing what your device does (read a temperature sensor, toggle a relay, blink an LED), and ESPHome compiles and flashes the firmware automatically.
The killer feature is native Home Assistant integration. Once your ESPHome device is on your Wi-Fi network, HA detects it automatically and imports all its sensors and switches as entities โ no MQTT setup, no manual configuration. Everything stays local, so there's no cloud dependency or subscription fee.
Compared to flashing Tasmota or writing Arduino code, ESPHome is dramatically simpler. You describe what you want, not how to do it. It handles all the firmware details for you.
2. Installing the ESPHome Add-on in Home Assistant
If you're running Home Assistant OS or Supervised, installing ESPHome takes about two minutes:
- In HA, go to Settings โ Add-ons โ Add-on Store
- Search for ESPHome
- Click it, then click Install
- Once installed, enable Show in sidebar and click Start
- Open the ESPHome dashboard from the sidebar
docker run -d --name esphome -v /config/esphome:/config -p 6052:6052 ghcr.io/esphome/esphome
3. Creating Your First Device Config
In the ESPHome dashboard, click + New Device. Give it a name like my-sensor and choose ESP32 as the device type. ESPHome will generate a base config โ but you'll replace it with the full config below.
Click Edit on your new device and paste this complete configuration:
esphome:
name: my-sensor
friendly_name: My Sensor
esp32:
board: esp32dev
framework:
type: arduino
# Enable logging
logger:
# Enable Home Assistant API
api:
encryption:
key: "your-encryption-key-here"
ota:
- platform: esphome
password: "your-ota-password-here"
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot in case Wi-Fi fails
ap:
ssid: "My-Sensor Fallback"
password: "fallback-password"
sensor:
- platform: dht
pin: GPIO4
temperature:
name: "Room Temperature"
humidity:
name: "Room Humidity"
model: DHT22
update_interval: 60s
Replace your-encryption-key-here with a random 32-character base64 string (ESPHome can generate one for you โ click the key icon). Replace your-ota-password-here with any password you choose.
Setting up secrets
Notice that wifi_ssid and wifi_password use !secret. This keeps your credentials out of the device config. In ESPHome, click the Secrets button (or edit secrets.yaml in your ESPHome config folder) and add:
wifi_ssid: "YourWiFiName" wifi_password: "YourWiFiPassword"
Wiring the DHT22
If you have a 3-pin DHT22 module (with the resistor already on the PCB): connect VCC โ 3.3V, GND โ GND, and DATA โ GPIO4. That's the pin referenced in the YAML above. If you have a raw 4-pin DHT22 IC, you'll also need a 10kฮฉ pull-up resistor between DATA and VCC.
4. Flashing via Web Browser (ESPHome Web Flasher)
For the very first flash, you need a physical USB connection โ over-the-air (OTA) updates only work after the device is already running ESPHome.
- Connect your ESP32 to your computer via USB
- In the ESPHome dashboard, click the three-dot menu on your device โ Install
- Choose Plug into this computer
- ESPHome will compile the firmware (takes 1โ3 minutes)
- A browser dialog will ask you to select a serial port โ choose the one that appears when your ESP32 is plugged in (usually
COM3on Windows or/dev/ttyUSB0on Linux) - Click Connect and then Install
After flashing completes, your ESP32 will reboot and connect to your Wi-Fi. Watch the ESPHome logs โ you should see Connected to WiFi within 30 seconds.
5. Adding to Home Assistant
Once your ESPHome device is online, Home Assistant auto-discovers it. You'll see a notification in HA under Settings โ Devices & Services โ Integrations showing a new ESPHome device. Click Configure and enter the encryption key you set in the YAML. That's it.
After adoption, navigate to the device page and you'll see two entities: Room Temperature (ยฐC or ยฐF) and Room Humidity (%). These update every 60 seconds as specified in your config. Add them to a dashboard card to start monitoring.
6. Expanding: Adding a Relay Output
Once your sensor is working, adding a relay output is just a few more lines of YAML. Many ESP32 dev boards have a built-in LED on GPIO2 you can test with, or wire a relay module to any GPIO pin.
Add this block to your existing config (below the sensor: section):
switch:
- platform: gpio
pin: GPIO26
name: "Relay Output"
id: relay_1
restore_mode: RESTORE_DEFAULT_OFF
output:
- platform: gpio
pin: GPIO2
id: onboard_led
light:
- platform: binary
name: "Onboard LED"
output: onboard_led
After saving and clicking Install (this time it'll flash OTA wirelessly โ no USB needed), a new Relay Output switch entity appears in HA. You can toggle it from the dashboard or use it in automations.
Wire your relay module: connect the relay's IN pin to GPIO26, VCC to the 5V pin on the ESP32, and GND to GND. The relay's NO/COM/NC terminals connect to whatever you want to control (a lamp, a water pump, etc.).
restore_mode: RESTORE_DEFAULT_OFF means the relay starts OFF after a power cut, which is the safe default for most applications. Change to RESTORE_DEFAULT_ON if your use case requires it.
7. Troubleshooting
Device not appearing for adoption in HA
Check that your ESP32 and HA are on the same subnet. mDNS discovery (used by ESPHome) doesn't cross router boundaries. If you're on a VLAN setup, either move the ESP32 to the same VLAN as HA or enable mDNS repeating on your router.
DHT22 readings show "unavailable" or NaN
This usually means a wiring issue. Double-check that DATA is connected to GPIO4 (not GPIO5 or another pin). If you're using a raw DHT22 chip (not a module), ensure you have the 10kฮฉ pull-up resistor. Also try increasing update_interval to 30s minimum โ DHT22 can't be polled faster than every 2 seconds, and ESPHome's default is 60s, but some faulty sensors need longer gaps.
OTA updates fail
If OTA flashing times out, the device may have lost Wi-Fi. Use the fallback hotspot: look for the "My-Sensor Fallback" Wi-Fi network on your phone, connect to it, and navigate to 192.168.4.1 to see the device's status and re-configure Wi-Fi.
Compilation errors
YAML is whitespace-sensitive. Use the ESPHome validator (the checkmark button) before flashing. Most errors are caused by incorrect indentation โ use spaces, never tabs.
8. FAQ
Can I use an ESP8266 instead of an ESP32?
Yes. Replace the esp32: block with esp8266: board: nodemcuv2 (or d1_mini, etc.) and remove the framework: type: arduino line. The DHT22 sensor config is identical.
Do I need to keep ESPHome running for the device to work?
No. Once flashed, the device runs independently. ESPHome (the add-on) is only needed to make config changes and reflash. The Home Assistant API connection is direct between the ESP device and HA core.
Is it safe to expose ESPHome devices on my network?
Yes, especially with API encryption enabled (as in the config above). All communication between ESPHome devices and Home Assistant is encrypted and local. Nothing leaves your home network.
How many devices can I run?
Dozens. ESPHome is very lightweight. Most Home Assistant setups run 10โ50 ESPHome devices without any performance issues on the HA side.
The sensor pin doesn't match GPIO4 on my board โ what pin number do I use?
Use the GPIO number (the internal ESP32 GPIO numbering), not the physical pin number printed on the board's silk screen. Search for your specific board's pinout diagram to find which physical pin corresponds to which GPIO.
SmartWired participates in the Amazon Associates Programme. We may earn a commission from qualifying purchases at no extra cost to you.