Quick Answer

ESPHome lets you program ESP32/ESP8266 microcontrollers using simple YAML config files. Connect a DHT22 sensor to an ESP32, write a 20-line YAML config, flash it via your browser, and it appears in Home Assistant automatically. No C++ required, no cloud, no subscription — just local Wi-Fi communication and real sensor data.

Table of Contents

  1. What ESPHome Is
  2. Installing the ESPHome Add-on
  3. First YAML Config: DHT22 Sensor
  4. Flashing Your Device
  5. Adding to Home Assistant
  6. Adding a Relay Output
  7. Troubleshooting
  8. FAQ

What You Need

What ESPHome Is

ESPHome is a framework that compiles YAML configuration files into C++ firmware for ESP microcontrollers. Instead of writing code, you describe what hardware is connected and what it should do — ESPHome handles the rest.

Once flashed, an ESPHome device connects to your Wi-Fi, exposes its sensors and controls via the ESPHome API (or optionally MQTT), and auto-discovers itself in Home Assistant. Updates are wireless — you never need to plug the USB cable in again after the first flash.

ESPHome is ideal for:

Installing the ESPHome Add-on

  1. Go to Settings → Add-ons → Add-on Store
  2. Search for ESPHome and click Install
  3. Enable Start on boot and Show in sidebar
  4. Click Start, then Open Web UI

The ESPHome dashboard shows all your configured devices. Click New Device to create your first one.

First YAML Config: DHT22 Temperature Sensor

Connect your DHT22 sensor to the ESP32: VCC → 3.3V, GND → GND, DATA → GPIO4 (with a 10kΩ pull-up resistor to 3.3V, or use GPIO's internal pull-up).

Here's the complete, working YAML config for an ESP32 with a DHT22 sensor:

ESPHome YAML — ESP32 with DHT22
esphome:
  name: bedroom-sensor
  friendly_name: Bedroom Sensor

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: !secret esphome_api_key

ota:
  - platform: esphome
    password: !secret ota_password

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Fallback hotspot if WiFi fails
  ap:
    ssid: "Bedroom-Sensor Fallback"
    password: "fallback123"

captive_portal:

sensor:
  - platform: dht
    pin: GPIO4
    model: DHT22
    temperature:
      name: "Bedroom Temperature"
      unit_of_measurement: "°C"
      accuracy_decimals: 1
    humidity:
      name: "Bedroom Humidity"
      unit_of_measurement: "%"
      accuracy_decimals: 1
    update_interval: 60s

The !secret references load from a secrets.yaml file in your ESPHome config directory. Create it with:

ESPHome secrets.yaml
wifi_ssid: "YourNetworkName"
wifi_password: "YourWiFiPassword"
esphome_api_key: "your_32_char_base64_key_here"
ota_password: "your_ota_password"
Generate an API key: In the ESPHome dashboard, click New Device → it generates a random encryption key automatically. Copy it to your secrets.yaml.

Flashing Your Device

The first flash must be done via USB. After that, all updates are wireless (OTA).

  1. Click your device in the ESPHome dashboard
  2. Click InstallPlug into this computer
  3. Your browser opens the Web Serial flasher — select your ESP32's COM/ttyUSB port
  4. Click Connect and wait for the firmware to compile and flash (~2-3 minutes)
  5. Watch the logs — you should see "Connected to WiFi" and "API connected"
Browser requirement: The Web Serial flasher requires Chrome or Edge. Firefox doesn't support Web Serial. If you can't use Chrome, use the ESPHome CLI: esphome run bedroom-sensor.yaml

Adding to Home Assistant

Once your device connects to Wi-Fi, Home Assistant discovers it automatically (within ~30 seconds):

  1. A notification appears: "ESPHome node discovered"
  2. Go to Settings → Integrations → ESPHome
  3. Click Configure on your new device
  4. Enter the API encryption key from your secrets.yaml
  5. Click Submit — the device and its sensors appear in HA

Your temperature and humidity sensors now appear as HA entities and update every 60 seconds.

Adding a Relay Output

To control a relay (for switching lights, fans, or any AC device), add this to your YAML config:

ESPHome YAML — Add relay switch
switch:
  - platform: gpio
    pin: GPIO26
    name: "Bedroom Fan"
    id: bedroom_fan
    restore_mode: RESTORE_DEFAULT_OFF
    icon: "mdi:fan"

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO0
      mode:
        input: true
        pullup: true
      inverted: true
    name: "Bedroom Fan Button"
    on_press:
      then:
        - switch.toggle: bedroom_fan

This creates a switch entity in HA for the relay, plus a physical button on GPIO0 that toggles it locally — even without internet. Connect a 5V relay module's IN pin to GPIO26, COM to your load's live wire, and NO (normally open) to complete the circuit.

Safety warning: Working with AC mains voltage is dangerous. If you're not experienced with electrical work, use a relay module in a properly insulated enclosure and consider hiring an electrician for the AC wiring.

Troubleshooting

FAQ

Does ESPHome need an internet connection to work?

No — once flashed, ESPHome devices communicate directly with Home Assistant over your local Wi-Fi. Internet is only needed during firmware compilation (which happens on your HA instance, not the device).

Can I use ESPHome with ESP8266 instead of ESP32?

Yes — ESP8266 boards (like the D1 Mini or NodeMCU) are fully supported. Change the board type in the config. ESP32 is preferred for new projects due to dual-core, more GPIO pins, and better Wi-Fi stability.

How do I update ESPHome firmware wirelessly?

After the first USB flash, click Install in the ESPHome dashboard and choose Wirelessly. The device downloads and applies the new firmware via OTA and reboots — no USB cable needed.

Can ESPHome use MQTT instead of the native API?

Yes — add an mqtt: component to your config and ESPHome will publish sensor readings and accept commands via MQTT instead. Useful if you want to use the device with a non-HA MQTT system.

SmartWired uses affiliate links. If you buy through our links, we may earn a commission at no extra cost to you. See our Affiliate Disclosure.