Tools

Reading the meter with forty lines of code

A clamp on the meter tail, a small board and a short script give me the house's electricity use every ten seconds. Here is all of it, and what the first week showed.

A workbench with a small circuit board, a clamp-on current sensor, an open notebook with pencil graphs and a mug of tea.
Contents
  1. The parts
  2. The script
  3. What a day looks like

The smart meter’s display gives you a number. I wanted the shape of a day — when the heat pump runs, how long the defrost cycles last, what the house uses at 3 a.m. — and I wanted it in a file I could open.

The parts

A current transformer clamps round the live tail between the meter and the consumer unit, without touching anything live. It gives a small voltage in proportion to the current, which a cheap analogue-to-digital board turns into numbers a small computer can read.

The script

This is the whole thing. It reads the sensor, works out the power, and appends a line to today’s file.

meter.py
import csv, math, time
from datetime import datetime
from sensor import read_samples  # the board's own library

VOLTS = 240          # close enough for a house
RATIO = 100 / 0.05   # 100 A clamp, 50 mA out
BURDEN = 33          # ohms, on the board

def power_watts():
    samples = read_samples(count=2000)            # one mains cycle, many times over
    rms = math.sqrt(sum(s * s for s in samples) / len(samples))
    amps = rms / BURDEN * RATIO
    return round(amps * VOLTS)

while True:
    now = datetime.now()
    with open(f"power-{now:%Y-%m-%d}.csv", "a", newline="") as file:
        csv.writer(file).writerow([now.isoformat(timespec="seconds"), power_watts()])
    time.sleep(10)

It runs as a service so it starts again after a power cut:

/etc/systemd/system/meter.service
[Unit]
Description=Log household power every ten seconds

[Service]
ExecStart=/usr/bin/python3 /home/pi/meter.py
WorkingDirectory=/home/pi/readings
Restart=always

[Install]
WantedBy=multi-user.target

Real power needs the voltage measured too, and the angle between voltage and current; this measures apparent power, which reads a little high for the heat pump’s motor. For seeing the shape of a day it is plenty.

What a day looks like

One cold Tuesday, hour by hour

Average power in each hour. The heat pump does most of its work overnight on the cheap rate.

The numbers
kWWhole house
001.4 kW
021.6 kW
041.5 kW
061.2 kW
080.9 kW
100.6 kW
120.7 kW
140.5 kW
160.8 kW
181.3 kW
200.9 kW
221.1 kW
Source: Ten-second readings from the logger, averaged per hour. 14 January 2025, −1 °C.

The first week showed two things I didn’t know. The heat pump defrosts its outdoor coil for about four minutes every hour when it’s near freezing and damp, which shows as a dip and then a spike. And a dehumidifier in the spare room had been running all day, every day, since the wood-fibre job: 300 watts I’d forgotten about.

Filed under

Markdown version

Sources

  1. The sensor's data sheet

How this was written

AI wrote a first draft from the author's notes and readings; the author rewrote and checked it.

Changes since it was published

  • The script now writes the date into each file's name, so a new file starts each day.
Ines Calloway sitting on the stairs of the house with a notebook on her knee.

About the author

Ines Calloway

Writes Sarking

Ines bought a cold 1930s semi in 2022 and has been writing down every meter reading since. She is a structural draughtsperson by trade and checks her own sums twice.

More by Ines Calloway