Graphing energy usage with Python and a Raspberry Pi

For the last few years I’ve been tracking my household energy usage: entering meter readings every few weeks into a spreadsheet and (naturally) graphing them.

When my electricity and gas meter were replaced by smart meters, it opened up new possibilities of tracking my household energy usage. I bought a USB to P1 cable, used it to hook up a Raspberry Pi to my smart meter, and wrote some code.

Most smart electricity meters deployed in The Netherlands are equipped with a “P1 port”, identifiable as a RJ11 input port. It allows you to poll usage data over a serial interface.

“Dumb” electricity meters are being rapidly replaced by smart ones throughout the NL, and gas meters are replaced by smart units that communicate (some wirelessly) with the main electricity meter. The main meter is equipped with a GPRS module. This allows your energy provider to remotely monitor your power usage. The P1 port allows you to connect retail monitoring devices like TOON to very granularly monitor your energy usage.

I love what the people at TOON have created and I might buy their product in the future, but for now I figured it would be a nice hobby project to build some software to gain insight into my energy usage.

In the case of my smart meter, new datapoints are being broadcasted every ten seconds. That looks like this:

$ minicom -D /dev/ttyUSB0


The values we’re looking for are:

0-1:24.2.1  : Total gas usage in m3
1-0:1.7.0   : Current electricity usage in kW
1-0:1.8.2   : Total high rate electricity usage in kWH
1-0:1.8.1   : Total low rate electricity usage in kWH

The first thing I did was write a data collector. Initially, as a proof of concept, I simply wrote down the values in JSON format into a text file. Later on, I added SQlite support in order to be able to query the data.

The next step was adding a simple web interface and graphing the data. I used the Flask framework to write a simple web service and the excellent library Chart.js to create graphs.

Current status:

Future plans:

  • Intuitive touch interface instead of interval*datapoints input fields
  • Add possibility of graphing specific periods
  • Aggregate power datapoints
  • Add a dashboard with multiple graphs (e.g. weekly, daily and different averages)
  • Graph energy production (from PV panels)
  • Pattern detection

Like Linus says: “Talk is cheap. show me the code.” Check out the GitHub repository if you’re interested.



One thought on “Graphing energy usage with Python and a Raspberry Pi”

Leave a Reply

Your email address will not be published. Required fields are marked *