5 min read

A house full of sensors with ESPHome

A house full of sensors with ESPHome

These days you can waste a lot of money on smart appliances. Or make such smart devices yourself.


As I said in my previous article about our staircase with 600 LEDs, Espressif ESP boards are ideal for when you want to make things that connect to the Internet:

Luckily other development boards are very good at that. Those boards contain the ESP8266 microchip. That chip can be programmed in the same way as an Arduino but has built-in WiFi and the necessary libraries to enable IoT applications. His big brother, the ESP32, is also equipped with Bluetooth (and is twice as fast).

Go big or go home

This is what I made with my very first ESP8266 board:

Many wires, a motion sensor, a light sensor, a microphone, a temperature and humidity sensor, a screen, and a laser beam. This thing is still hanging up in our meeting room at VRT and, of course, is total overkill to see whether the lights are on.

I put way too much time into it at the time, but it taught me how to connect an ESP to the network and also the principles of MQTT and node-red.

Everything these sensors capture is forwarded every 10 seconds to an MQTT server (a kind of telephone exchange for IoT applications), which communicates it to a small application that I wrote in node-red.

Someone pulled the plug during this pandemic.

Little did I know there’s already a project that does everything I did and more:  ESPHome.


ESPHome is a system to control your ESP8266/ESP32 by simple yet powerful configuration files and control them remotely through Home Automation systems.

In short, ESPHome is a firmware for ESP that allows you to create things like the above without writing any code and integrates with Home Assistant and others.

After installation (pip3 install esphome), you can quickly start a new project via the wizard (esphome serverkast.yaml wizard), and after answering some questions, the wizard writes out a YAML file containing the minimum configuration:

  name: serverkast
  platform: ESP8266
  board: nodemcuv2

  ssid: ""
  password: ""

  # Enable fallback hotspot (captive portal) in case wifi connection fails
    ssid: "Sample Fallback Hotspot"
    password: ""


# Enable logging

# Enable Home Assistant API


When you flash this on your ESP (esphome run serverkast.yaml), your ESP will reboot, connect to your WiFi network and appear in Home Assistant.

Of course, this does absolutely nothing. So it’s time to connect some sensors. Inside my server cabinet, there is an ESP8266 with a DHT11 temperature and humidity sensor connected to PIN D2. In the configuration file, this becomes (beneath ota:):

  - platform: dht
    pin: D2
    model: DHT11
      name: "Serverkast Temperature"
        - calibrate_linear:
            - 0.0 -> 0.0
            - 24.0 -> 23.5
      name: "Serverkast Humidity"
    update_interval: 60s

(Because DHT11 temperature sensors are cheap but not precisely accurate, a small calibration occurs: 24 degrees is adjusted to 23.5.)

After reflashing (via USB or over WiFi), these sensors appear in Home Assistant:

In Home Assistant, the data from these sensors can now be used to perform automations or to create beautiful graphs using, for example, InfluxDB:

Our radiator broke down while we were away …

Sensors, buttons & screens

The great thing about ESPHome is that everything you can think of works out of the box. Hang an e-paper display on it? A camera? Or create your own IR remote control? It’s all possible, and it all works through the same configuration file. You can find an overview of all supported components here.

Automations in ESPHome

An automation I have planned for the ESP8266 in my server cabinet is to automatically start a fan when the temperature is higher than 30°. You can do this in the Home Assistant interface but also in the configuration file itself:

  - platform: dht
    pin: D2
    model: DHT11
      name: "Serverkast Temperature"
        - calibrate_linear:
            - 0.0 -> 0.0
            - 24.0 -> 23.5
        - above: 30.0
            - fan.turn_on: fan
        - below: 30.0
            - fan.turn_off: fan

The latter has the advantage that your automation works even when Home Assistant is not accessible and is especially recommended for simple automation that does not require other devices in your network.

Automations in Home Assistant

During the Christmas period, I connected an ESP with some LEDs (I didn’t know WLED at that time) and a button. When you hit that button, our Google Nest Hub started playing Christmas music, and the lights turned on.

To do this, the ESPHome sends an event to Home Assistant:

  - platform: gpio
      number: D0
      mode: INPUT_PULLDOWN_16
    name: "Button"
        - homeassistant.event:
            event: esphome.button_pressed
              title: Button was pressed

Home Assistant sets the Nest’s volume to 75%, selects a random Christmas song, lights on, and turns on the Philips Hue’s Cozy scene.

- id: '1608044655971'
  alias: Play xmas
  description: ''
  - platform: event
    event_type: esphome.button_pressed
  - condition: device
    type: is_off
    device_id: 3a18233816ee484b9780e3448c416fec
    entity_id: light.xmas_leds
    domain: light
  - service: media_player.volume_set
    entity_id: media_player.woonkamer
      volume_level: '0.75'
  - service: media_player.play_media
      media_content_type: music
    entity_id: media_player.woonkamer
      media_content_id: '{{  [
] | random }}'
  - type: turn_on
    device_id: 3a18233816ee484b9780e3448c416fec
    entity_id: light.xmas_leds
    domain: light
    brightness_pct: 50
  - service: hue.hue_activate_scene
      group_name: Living
      scene_name: Gezellig

Be careful if you make something like this, and you have kids. You’ll get tired of this very quickly.

Update ESPHome in Home Assistant

After the first flash via USB, your ESPHome can update OTA (over-the-air – over WiFi). This is useful because it allows you to keep your sensors where they are and still make configuration changes.

In Home Assistant, you also have an ESPHome community add-on. It contains the latest version of ESPHome and allows you to perform updates from the Home Assistant interface.

I always copy my configuration files here after flashing via USB. This allows updating wherever possible and gives easy access to my various devices’ logs and status.

The result

I want to do some crazier things with ESPHome in the future, but I keep the temperature and humidity for now.