Smart home

Energy meter pulses ESP32 ESPHome

Track power and energy in Home Assistant with S0 pulses, ESP32-C6 and ESPHome: wiring, YAML and safety notes. Parts list, GPIO wiring, ESPHome YAML and Hom

Updated · Emmanuel SAUVAGE

  • LevelAdvanced
  • Time2–4 h
  • Budgetabout $15–45 depending on the seller (meter not included)

Rough estimate, check with your seller: prices vary by shop and period.

Project goal

You will follow your home’s electricity use in Home Assistant, the software that gathers your connected devices, without ever touching the 230 V mains. The Linky, the communicating meter installed in French homes, blinks for every fraction of a kilowatt-hour consumed; an optical sensor placed in front of this small pulse LED sends these flashes to an ESP32, a small electronic board with Wi-Fi. The board works out the instantaneous power in watts (W) and the accumulated energy in kilowatt-hours (kWh), ready to use in the Energy dashboard.

Why build it

This tracking helps you spot appliances that draw power on standby, follow consumption hour by hour and trigger an alert when you go over a limit. The LED method is the safest because it needs no electrical contact. The Linky’s TIC output (the I1 and I2 terminals), the meter’s data link, also gives the readings per tariff and the apparent power, but it needs a specific interface, which is beyond the scope of this page.

Useful terms for this project: ESP32 (small Wi-Fi board) · ESPHome (prepares the board’s program) · GPIO (the board’s pins) · OTA (update without a USB cable)

Prerequisites

  • A meter with an S0 output (DIN-rail meters often have one) or a pulse LED; read the pulses per kWh (imp/kWh) on its front.
  • Home Assistant with ESPHome and 2.4 GHz Wi-Fi up to the meter location.
  • A qualified electrician if the meter is not installed yet or if you must open the consumer unit.

Diagrams

Wiring diagram : Energy meter (pulse output)
Wiring diagram : Energy meter (pulse output)GPIO10 → S0 meter output S0 +; GND → S0 meter output S0 −ESP32-C6 DevKitC-1S0 meter outputGPIO10S0 +GNDS0 −Data or signalGround (GND)
Principle diagram : Energy meter (pulse output)
Principle diagram : Energy meter (pulse output)Energy meter (S0) → ESP32-C6 + ESPHome → Home AssistantEnergy meter (S0)Isolated pulse output (S0)Pulses per kWh printed on the meterS0 + on GPIO10, S0 − on GNDESP32-C6 + ESPHomeCounts pulses (pulse_meter)Computes power and energy2.4 GHz Wi-Fi + encrypted APIHome AssistantPower (W) and Energy (kWh) entitiesWorks with the Energy dashboard

Parts list

This ESP32 board is the base for every project in the series. ESPHome also works with other ESP32 boards: the board-choice guide helps you compare them. Which ESP32 board to choose?

ESP32-C6-DevKitC-1

Suggested board

ESP32-C6-DevKitC-1

check with the seller
  • Wi-Fi 6, Bluetooth 5, Zigbee and Thread (at hardware level)
  • Supported by ESPHome (ESP-IDF framework)
  • USB-C for flashing and power

Watch-outs

  • 2.4 GHz Wi-Fi only
  • Use a USB cable that carries data

Main components

ItemQuantityRole and precautions
ESP32-C6-DevKitC-11Control board (see the box above).
kWh meter with S0 pulse output (if you do not have one)0–1Price varies widely and installation belongs to an electrician. Note its imp/kWh value.
2-core low-voltage cable (0.25 mm²)1 to 5 mFrom the S0 terminals to the ESP32, routed away from mains cables.

Small parts

ItemQuantityRole and precautions
USB-C data cable1For flashing and power. Charge-only cables do not carry data.
USB 5 V power supply (CE/UKCA-marked charger)1At least 1 A for the board alone; more if peripherals draw power from it.
Dupont jumper wires (male-female and female-female)1One mixed pack covers every prototype.
Breadboard1To test without soldering; replace with a soldered build if you need it permanent.
Vented plastic or 3D-printed enclosure1Protects the board; leave a cable gland and vents if the sensor measures air.

Amazon links are affiliate links: the site may earn a commission at no extra cost to you.

Step-by-step build

Step 1. Prepare ESPHome and create the device

  1. In Home Assistant, open the ESPHome Device Builder add-on (see the tutorial “Install ESPHome in Home Assistant (Device Builder)”). ESPHome is the software that turns a simple settings file into the program for your ESP32 board, without you having to write any code.
  2. Open “Secrets” (top right): this file keeps your passwords apart, so you can share your configurations without revealing them. Check that wifi_ssid (the name of your Wi-Fi network), wifi_password, api_encryption_key (the key that encrypts the link with Home Assistant), ota_password (for updates without a cable) and ap_password (for the board’s fallback Wi-Fi network) exist, and add any missing line. A valid API key is a long 32-byte string encoded in base64, which you can generate with “openssl rand -base64 32”.
  3. Click “New device”, name it “compteur-energie”, pick ESP32-C6, skip the flash for now, then replace the file content with the YAML from this page (“Edit”). YAML is a configuration file written in plain text: it describes, line by line, what the board must do.

What you should see: The file opens in the editor with syntax highlighting and no red underline.

Check: The “Validate” action (⋮ menu) answers “Configuration is valid”.

Step 2. Connect the S0 output to the ESP32

  1. With three Dupont wires, flexible wires whose ends plug in without soldering, connect the optical sensor to the board with the USB power unplugged: VCC (the power) to the 3V3 pin, GND (the ground) to a GND pin and DO (the sensor’s digital output) to GPIO10, one of the metal pins lined up along the edge of the board, called GPIO.
  2. Connect S0 + to GPIO10 and S0 − to GND with a two-core cable.
  3. The ESP32’s internal pull-up (INPUT_PULLUP in the YAML) is enough for most S0 outputs; check your meter’s datasheet for its minimum voltage.

What you should see: With the load on, the pulse indicator of the meter blinks and the S0 contact closes briefly each time.

Check: Use a multimeter in continuity mode on S0 +/−: it beeps for each pulse.

Step 3. Flash the board and add it to Home Assistant

  1. Connect the board to your computer with the USB-C data cable: “flashing” means writing the program into the board’s memory, and this first write must be done over a cable. The board can already be wired or sitting on the breadboard, but any external peripheral with its own power supply must stay switched off.
  2. In ESPHome click “Install”, then “Plug into this computer” (Chrome or Edge over HTTPS) or “Manual download” and upload the file with ESPHome Web (web.esphome.io). If the port is missing, follow the flashing tutorial.
  3. After the reboot open Settings → Devices & services: the ESPHome device is discovered; click “Configure” and paste the API encryption key.

What you should see: The “Logs” show the Wi-Fi connection and then “Successfully connected to Home Assistant”.

Check: The device appears under Devices & services → ESPHome with its entities; later updates need no cable and go over Wi-Fi (this is OTA, “Over The Air”).

Step 4. Set the conversion factors

  1. First read on the front of the Linky the number of pulses per kWh, often 1000 (“1000 imp/kWh”), because the power and energy calculations depend on it.
  2. In the YAML adjust “multiply: 60” (power in W) and “multiply: 0.001” (energy in kWh) using the formulas in the comments.
  3. Install over Wi-Fi, switch on a known load (a 2 kW kettle, for example) and compare with the meter’s display.

What you should see: Power rises a few seconds after switching on and returns to 0 after 5 minutes without pulses.

Check: The difference with the meter’s instantaneous reading stays within a few percent.

ESPHome code (YAML)

YAML is a configuration file written in plain text: it tells ESPHome what the board must do. Paste this one into ESPHome after creating the device. It targets an ESP32-C6 board; values to personalise are marked with “CHANGE” comments.

esphome:
  name: compteur-energie
  friendly_name: Energy meter

esp32:
  board: esp32-c6-devkitc-1   # XIAO ESP32C6: use board: seeed_xiao_esp32c6
  framework:
    type: esp-idf             # required for ESP32-C6

logger:                       # serial logs, also visible over Wi-Fi

api:
  encryption:
    key: !secret api_encryption_key

ota:
  - platform: esphome
    password: !secret ota_password

wifi:
  ssid: !secret wifi_ssid       # 2.4 GHz network only
  password: !secret wifi_password
  ap:                           # fallback hotspot if the Wi-Fi is unreachable
    ssid: "compteur-energie fallback"
    password: !secret ap_password

captive_portal:

sensor:
  - platform: pulse_meter
    name: "Power"
    pin:
      number: GPIO10               # pulse input (S0 output or optical sensor)
      mode: INPUT_PULLUP
    internal_filter: 10ms          # ignore bounces shorter than the real pulse (check your meter's datasheet)
    timeout: 5min                  # report 0 W after 5 minutes without a pulse
    unit_of_measurement: "W"
    device_class: power
    state_class: measurement
    accuracy_decimals: 0
    filters:
      # CHANGE with the imp/kWh printed on your meter.
      # pulses per minute x 60000 / imp-per-kWh = watts  ->  1000 imp/kWh: x60 | 500 imp/kWh: x120 | 2000 imp/kWh: x30
      - multiply: 60
    total:
      name: "Energy"
      unit_of_measurement: "kWh"
      device_class: energy
      state_class: total_increasing
      accuracy_decimals: 3
      filters:
        # CHANGE: kWh per pulse = 1 / imp-per-kWh  ->  1000 imp/kWh: 0.001 | 500: 0.002 | 2000: 0.0005
        - multiply: 0.001

  - platform: wifi_signal
    name: "WiFi signal"
    update_interval: 120s
    entity_category: diagnostic

YAML comments are in English, as in the ESPHome documentation. This code has not been run on your hardware: use “Validate” before flashing and adjust pins and names.

Home Assistant integration

  • The “Power” (W) and “Energy” (kWh, total increasing) entities appear under the ESPHome device.
  • In Settings → Dashboards → Energy add “Energy” as “Grid consumption”: history and cost appear after the first full hour.
  • This measurement is indicative and does not replace your bill: the utility meter remains the reference.

Common problems and troubleshooting

SymptomFix
Power stays at 0 WNo pulses reach GPIO10. Check the S0 polarity, test continuity with a multimeter and confirm the meter really has an active S0 output.
Values far too highThe conversion factor does not match imp/kWh. Recompute the multipliers; also raise internal_filter if contact bounce is counted.
Energy does not show in the Energy dashboardThe entity must have the energy device class and the total_increasing state class (already set in the YAML), with unit kWh.
The counter resets after a rebootThe “total” of pulse_meter is stored in memory: Home Assistant keeps the history, and the total_increasing class handles resets without losing statistics.

FAQ

What if my meter only has a pulse LED?

Use an optical sensor (phototransistor with a digital output) in front of the LED, shielded from ambient light, and keep the same code.

Can I measure a water or gas meter the same way?

Yes, if it provides pulses (dry contact or optical): change the unit and the factor, and add the entity to the Energy dashboard if relevant.

How accurate are the values?

Energy is as reliable as the pulse counting (no missed pulse); power is the average over the interval between two pulses.

Related tutorials

These step-by-step guides explain the gestures common to every project in the series.

Other ESP32 projects

Sources and documentation

Cette page vous a-t-elle été utile ?

Have a question about this guide or need advice? Ask us here

Torn between two models or stuck on a build? Drop us a line and we will get back to you by email.