Marcio Cunha

How to Monitor Power Consumption with ESPHome and ESP32

Learn how to integrate ESP32-based power meters into Home Assistant using ESPHome in a practical, secure, and fully local way.

Marcio Cunha•3 min
Also available in:PortuguêsEspañol
Summary
  • Smart energy monitors provide complete visibility into residential electricity use without relying on external clouds.
  • The ESPHome ecosystem simplifies creating custom firmware for microcontrollers using simple configuration files.
  • Local communication via native API with Home Assistant eliminates delays and ensures operation even without internet.
  • Calibrating analog sensors with a reliable multimeter is the critical step to ensure accurate consumption data.
  • Automations based on real-time power data help prevent overloads and significantly reduce electricity bills.

Why monitor your home's power consumption?

Knowing exactly how much electricity each home appliance consumes is no longer just a curiosity; it has become a powerful tool for savings and safety. When we understand electrical grid behavior, we can identify old equipment silently wasting energy or prevent dangerous overloads in home wiring. Instead of guessing the culprit on your monthly electricity bill, home automation allows you to view real-time graphs and act with surgical precision.

For those who prefer keeping absolute control of their data at home, relying on closed commercial smart plugs that send information to servers abroad is rarely a good idea. Many of these commercial solutions expose your privacy to security flaws and stop working if the service provider shuts down. This is precisely where the open ecosystem shines, uniting versatile microcontrollers with local automation platforms.

If you have ever been curious about improving the resilience of your smart infrastructure, it is worth exploring advanced local connectivity concepts and how to configure the MQTT protocol on ESP32 for home automation to understand how devices talk to each other without cloud intermediaries. The freedom to build your own solution eliminates hidden fees and ensures your house operates smoothly even if your internet service provider drops.

Choosing the ideal hardware with ESP32 and dedicated meters

The core of our monitoring project is an ESP32 family microcontroller, a small, inexpensive chip equipped with native Wi-Fi and Bluetooth. However, measuring electrical current safely requires specialized circuits that isolate the high voltage of the 110V or 220V grid from sensitive logic components operating at just 3.3 volts. The most famous component for this task is the BL0937 or HLW8012 integrated chip, commonly found in cheap smart plugs ready for modification.

Another excellent alternative for workbench prototyping is non-invasive current transformers, commonly known as clamp sensors or Rogowski coils. They work by inducing a small current proportional to the magnetic flux generated by the power cable, allowing you to monitor general power consumption at the breaker panel without stripping any wires. To ensure physical connections and solder joints are solid before powering everything up, it is always wise to review practices on how to fix a broken circuit board trace with wire to prevent dangerous short circuits.

Working with electricity requires absolute respect for safety standards, as an assembly mistake can fry components or cause fires. Always disconnect the main circuit breaker before handling exposed wires and use multimeters to verify there is no residual voltage. The satisfaction of building your own measurement hardware rewards every minute spent on the workbench, provided caution remains your primary guideline.

Writing firmware with ESPHome and YAML

ESPHome is a revolutionary tool that turns the chore of programming microcontrollers into filling out configuration files based on the YAML format. Instead of writing hundreds of lines of C++ code in the Arduino IDE, you simply declare which sensors are connected to which pins of the ESP32. The system compiles the code automatically and pushes it to the chip, either via USB cable during initial setup or wirelessly through over-the-air updates.

Below is a functional example of an ESPHome configuration snippet for an energy meter based on the BL0937 chip, mapping voltage and current pulse pins:

esphome:  name: office-plug  friendly_name: Office Plug  esp32:    board: esp32devlogger:sensor:  - platform: hlw8012    sel_pin:      number: GPIO12      inverted: true    cf_pin: GPIO4    cf1_pin: GPIO5    current:      name: