All articles

    ESPHome with Home Assistant: a Smart Home with an ESP32 and No C++ Code

    How to turn an ESP32 into a smart home device in Home Assistant using ESPHome: where to run Home Assistant, your first device step by step, a YAML file for a temperature sensor, a relay and a button, wireless updates (OTA), automations on the device and in Home Assistant, working without internet, security, and the comparison with ordinary code and MQTT.

    5 min read

    In the MQTT article we wrote C++ code so the ESP32 could send its readings. There's a much easier way if your goal is a smart home: ESPHome. You write a description of the device in a short YAML file (the sensor on this pin, the relay on that pin), ESPHome builds the code and uploads it, and the device appears in Home Assistant by itself.

    The ESP32 sends readings to Home Assistant and receives commands from it, and you control it from your phone
    The ESP32 sends readings to Home Assistant and receives commands from it, and you control it from your phone

    Who does what?

    • Home Assistant: the home's "brain". Free software that gathers every device in one place, and provides a dashboard, automations and a phone app.
    • ESPHome: a tool that turns a YAML file into firmware for the ESP32 or ESP8266, and the device talks to Home Assistant directly.

    Where does Home Assistant run?

    WhereNotes
    Raspberry Pi 4 or 5The most common, with low consumption. Use an SSD instead of a memory card
    An old mini PCMore powerful and more reliable, and you can find one second-hand at a good price
    A Home Assistant Green or YellowReady-made from the project itself
    A virtual machine on an always-on computerIf you already have a server

    What matters is that it's running 24 hours a day and connected to the router by cable.

    Your first device, step by step

    1. In Home Assistant, install ESPHome Builder from the add-ons.
    2. Connect the ESP32 to the computer with a USB cable, open ESPHome, and choose New Device. It creates an initial configuration and uploads it from the browser.
    3. The device connects to Wi-Fi, and Home Assistant tells you "new device discovered". Accept it.
    4. From here on, any change to the YAML is uploaded wirelessly (OTA).

    If this is your first ESP32, see first steps with the ESP32.

    The YAML file: a temperature sensor, a relay and a button

    esphome:
      name: living-room
    
    esp32:
      board: esp32dev
    
    wifi:
      ssid: !secret wifi_ssid
      password: !secret wifi_password
    
    api:
      encryption:
        key: !secret api_key
    
    ota:
      - platform: esphome
    
    logger:
    
    sensor:
      - platform: dht
        pin: GPIO4
        model: DHT22
        temperature:
          name: "Living room temperature"
        humidity:
          name: "Living room humidity"
        update_interval: 30s
    
    switch:
      - platform: gpio
        pin: GPIO26
        id: relay_1
        name: "Living room light"
        inverted: true          # active-low relay module
    
    binary_sensor:
      - platform: gpio
        pin:
          number: GPIO27
          mode: INPUT_PULLUP
          inverted: true
        name: "Wall button"
        on_press:
          - switch.toggle: relay_1   # the button toggles the light even if Home Assistant is down
    • !secret: passwords are written in a separate secrets.yaml file, not in the device file.
    • inverted: true on the relay because most modules switch on with LOW. See the relay module.
    • The button is linked to the relay inside the device itself, so if the Wi-Fi or Home Assistant goes down, the light still works from the wall. This is the most important tip in smart homes.

    Wiring the DHT22 and its pull-up resistor is in the DHT article.

    Automations: where do you write them?

    On the device (in the YAML): for things that must always work even without Home Assistant, such as the button and relay above, or "switch off the heater if the temperature passes 60".

    In Home Assistant (from the interface, with no code): for things that link more than one device or depend on time and your phone, such as:

    • "If the temperature is above 30 and it's between 2 and 6 pm, turn on the air conditioner."
    • "If the motion sensor detects something after midnight, send a notification to my phone."
    • "When the last person leaves home, switch off all the lights."

    Why ESPHome instead of ordinary code?

    Arduino code + MQTTESPHome
    WritingC++, reconnection and error handlingA description in YAML only
    Appearing in Home AssistantNeeds setup or discoveryAutomatic
    UpdatingBy cable, or OTA you write yourselfReady-made OTA
    FlexibilityCompleteHigh, with the option of small C++ snippets inside
    Suited toA dedicated product or complex logicHome devices and sensors

    ESPHome supports hundreds of sensors and modules out of the box: DHT, DS18B20 and BME280, WS2812 strips, displays, IR, PIR, and current and power measurement. And if you're making a product to sell, or the logic is complex, ordinary code with MQTT gives you more control.

    Does it work without internet?

    Yes. The ESP32 talks to Home Assistant inside the local network. If the internet goes down, everything in the home keeps working normally: the automations, and the dashboard from your phone while you're on the same Wi-Fi. The only thing that stops is control from outside the home.

    For control from outside: use Home Assistant Cloud (paid) or a VPN into your home. Don't expose Home Assistant to the internet directly.

    Security

    • Keep the encryption key (api: encryption) on every device.
    • Put a password on OTA, and a strong password on Home Assistant itself.
    • If the device controls 220 V (a heater or pump), electrical protection matters more than programming. Read mains safety, and it's best to use ready-made isolated modules in a closed enclosure.

    Well-known faults

    SymptomUsual cause
    The device keeps showing offlineWeak Wi-Fi at the location, or a weak supply causing resets
    The sensor reads NaNWrong wiring or pin, or a missing pull-up resistor
    The relay works backwardsNeeds inverted: true
    The OTA upload failsThe device isn't on the same network, or there isn't enough memory for a large file

    If you're struggling with resets, see powering the Arduino and ESP32.

    FAQ

    Can I use an ESP8266 instead of an ESP32? Yes, ESPHome supports it, and it's cheaper. But it has less memory and fewer pins, so for larger devices or ones with Bluetooth, the ESP32 is better.

    Can I use ready-made Sonoff or Tuya switches? Many of them contain an ESP8266 or ESP32 and can have ESPHome loaded onto them, but that requires opening the device and connecting a programmer, and newer models sometimes use other chips. And if you want someone to design and install a complete smart home system, see the smart home service.

    Circuits Nerd
    © 2026 Circuits Nerd. All rights reserved.