Skip to content
GEEKnodes

Electronics

ESPHome with Home Assistant: Your First Sensor Without Coding

Build a temperature and humidity sensor with an ESP32 and ESPHome without writing code: setup, DHT22 wiring, the YAML file and adding it to Home Assistant.

With an ESP32 that costs a few dollars and a sensor, you can measure the temperature of any room and see it in Home Assistant. The best part: you don’t need to know how to code. ESPHome turns a configuration file of just a few lines into complete firmware, with Wi-Fi, over-the-air updates and automatic setup in your smart home system.

In this guide we build a temperature and humidity sensor step by step, from a blank board to the graph on your dashboard.

In short

  • ESPHome generates the ESP32 firmware from a YAML file: you describe what’s connected and it writes the code.
  • The first upload is done over USB; the following ones over Wi-Fi (OTA).
  • Home Assistant discovers the device on its own and creates its entities.
  • To get started you need an ESP32, a DHT22 sensor, three wires and a USB data cable.
  • If you want another smart home system or to integrate the sensor into something else, ESPHome also speaks MQTT.

What ESPHome is and why you don’t need to code

ESPHome is an open-source project that’s part of the Home Assistant ecosystem. Instead of writing a C++ program, you describe the device in a text file: which board you’re using, which Wi-Fi network it joins and which sensors are on each pin.

ESPHome translates that file into code, compiles it and uploads it to the board. If tomorrow you want to add a relay or change how often it measures, you edit two lines and install again.

It works with the classic ESP32 and its variants (S3, C3, C6), with the older ESP8266 and with a few other boards. If you’re still unsure which board to buy, check out the ESP32 vs Arduino comparison: for home automation, the ESP32 is the natural choice.

What you need

ItemWhat it’s forNotes
A working Home AssistantReceiving and showing the dataIf you don’t have it, follow the guide to Home Assistant on Raspberry Pi
ESP32 DevKit boardThe sensor’s “brain”Any board with USB will do
DHT22 moduleTemperature and humidityPreferably the 3-pin module, which already has the pull-up resistor
3 female-to-female jumper wiresConnecting the sensorOr a breadboard
USB data cableThe first uploadMany cables only charge and won’t work

Later on you can swap the DHT22 for another sensor. In DHT22 vs DS18B20 we compare the most common ones.

Step 1: install ESPHome in Home Assistant

ESPHome is managed from a web dashboard called ESPHome Device Builder. If you use Home Assistant OS, it installs like any other app:

  1. Go to Settings > Apps and open the store.
  2. Search for ESPHome Device Builder and install it.
  3. Turn on Show in sidebar and start it.
  4. Open it from the sidebar: you’ll see an empty dashboard with a New device button.

If you run Home Assistant in a container, ESPHome also runs as a separate Docker container. If you already have Docker on the Raspberry Pi, it’s one more line.

Step 2: create the device

  1. Click New device and give it a short name, lowercase and without spaces, for example sensor-salon.
  2. Choose the chip type: ESP32 (or whichever variant you have).
  3. The wizard generates a base configuration with your Wi-Fi and an encryption key for the connection to Home Assistant. Save it: you’ll need it if you’re asked for it when adding the device.
  4. Connect the board to your computer over USB and click Install.
  5. Choose the option to install over USB from this browser (Plug into this computer). You need Chrome or Edge, which allow access to the serial port.

If the browser won’t connect over USB (this happens when Home Assistant isn’t opened over HTTPS), choose Manual download, download the firmware and flash it from the official ESPHome Web site with Chrome or Edge.

The first compile takes a few minutes. The following ones are much faster.

ESPHome workflow in four steps: YAML configuration, compile, first upload over USB and updates over Wi-Fi OTA
From the configuration file to the board: you only need the cable the first time.

Step 3: connect the sensor

Unplug the USB cable before wiring. The DHT22 module has three pins:

Module pinESP32 pinWire
VCC (+)3V3Red
DATA (OUT)GPIO4Yellow
GND (−)GNDBlack

The pin order varies by module manufacturer: go by the silkscreen labels, not the position. If you’re using a “bare” four-pin DHT22, you need to add a 10 kΩ resistor between DATA and VCC. We explain why in the guide to pull-up and pull-down resistors.

GPIO4 is a good choice because it isn’t involved in booting. Avoid pins 0, 2, 12 and 15 on the classic ESP32 for your first builds.

Wiring a DHT22 module to an ESP32: VCC to 3V3, DATA to GPIO4 and GND to GND
Three wires: power, data and ground.

Step 4: describe the sensor in YAML

Go back to the ESPHome dashboard and click Edit on your device. You’ll see the configuration the wizard generated. Add this block at the end:

sensor:
  - platform: dht
    pin: GPIO4
    model: DHT22
    temperature:
      name: "Living Room Temperature"
    humidity:
      name: "Living Room Humidity"
    update_interval: 60s

The part that was already there looks something like this (your values will be different):

esphome:
  name: sensor-salon

esp32:
  board: esp32dev

logger:

api:
  encryption:
    key: "the-key-the-wizard-generated"

ota:
  - platform: esphome

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

Three YAML details that trip people up at first:

  • Indentation matters: use spaces, never tabs.
  • !secret reads the value from the secrets.yaml file, so your password doesn’t show up in the configuration.
  • api: is the connection to Home Assistant and ota: enables updates over Wi-Fi.

Click Save and Install. This time choose Wirelessly: the board is already on your network and you don’t need the cable. When it finishes, you’ll see lines in the log with the temperature every minute.

Step 5: add it to Home Assistant

In most cases Home Assistant discovers it on its own. You’ll get a New devices discovered notification:

  1. Go to Settings > Devices & services.
  2. Under the ESPHome integration, click Configure on your sensor.
  3. If you’re asked for the encryption key, paste the one from the YAML.
  4. Assign it an area (Living Room) and you’re done.

You now have two new entities, temperature and humidity, which you can add to the dashboard, view in the history or use in automations.

The ESP32 node sends temperature and humidity through the native API to the home automation server, which shows them on a dashboard
The native API connects the sensor to Home Assistant with no intermediate servers.

ESPHome or coding with the Arduino IDE

ESPHomeArduino IDE
LanguageYAML configurationC++
Home Assistant integrationAutomaticBy hand, usually over MQTT
Over-the-air updatesIncludedYou have to code them
FlexibilityHigh within its componentsTotal
Ideal forSmart home sensors and actuatorsYour own projects or anything outside home automation

If your goal is home automation, ESPHome saves you hours. If you want to understand what’s going on under the hood or send data to another system, learn how MQTT with ESP32 works. And if you’re going to place the sensor far from an outlet, check out how to power an ESP32 from a battery.

Common problems

  • The computer doesn’t detect the board: it’s almost always the cable (charge-only) or a missing driver for the USB converter (CP2102 or CH340).
  • It gets stuck on “Connecting…” while uploading: hold down the board’s BOOT button as the upload starts.
  • The reading shows as NaN or “unavailable”: check the pin in the YAML, the wiring, and that the model is DHT22 and not DHT11.
  • The temperature reads high: the sensor is too close to the ESP32, which warms up. Move it a few inches away using wires.
  • The device shows as offline: make sure it’s on the same network and that the 2.4 GHz Wi-Fi signal reaches it well.

Frequently asked questions

Do I need to know how to code to use ESPHome? No. All you have to do is copy and adapt YAML configuration blocks. The official documentation has an example for almost every sensor.

Can I use ESPHome without Home Assistant? Yes. You can use its built-in web server or enable the MQTT component to send the data to another system. But the simplest experience is with Home Assistant.

Will any ESP32 work? Practically any. When you create the device you choose the variant and, if needed, the exact board model. To get started, a classic DevKit is the most thoroughly tested.

What happens if the Wi-Fi goes down? The sensor keeps measuring and reconnects on its own when the network comes back. If it goes a long time without a connection, it restarts and tries again.

Where do I go after my first sensor? Add a relay, a button or a display to the same YAML. If you’re just getting started with electronics, our Arduino and ESP32 beginner’s guide shows you the way.

Accessibility preferences

Quick profiles

Text and reading

Text size
Default
Line height
Default
Letter spacing
Default
Word spacing
Default

Colour

Motion and orientation

Cursor