Reduce Your Energy Bills using MongoDB — Part 1 Monitoring Power Usage

Page against the machine
5 min readFeb 24, 2022

--

Domestic fuel prices are soaring in Europe, and with the UK government cap on home gas and electric tariffs being raised in April to prevent energy companies going bust, the whole country will see a 54% jump in these monthly bills.

I don’t like that idea much, so I’ve dug into my spare parts drawer to see what I have that will help me understand and reduce my energy usage. This is the first in a series of blogs where I use relatively off-the-shelf hardware and software along with the MongoDB Data Platform to help me understand and optimize my usage.

The first step is understanding how much energy my household uses, in what forms, and when.

You can read about reducing heating gas use and reducing electric bills in Parts 2 and 3.

Capturing meter readings with a camera and OCR

A couple of years back, my energy supplier fitted smart meters. These are supposed to report my usage back to the supplier and save me sending them meter readings manually. Unfortunately, as a first generation meter, it stopped working when we changed suppliers six months later. It does, however, have an in-home display to show current usage, and this can also show me the meter readings. This was stuffed in a drawer but I dug it out for this project and it still works perfectly.

To get the readings in electronic form, I built the following device with Lego bricks, a Raspberry Pi Zero, a Pi camera, and a servo motor to press the button to switch between electric and gas readings. The box has capacitive buttons, so there is a circle of aluminum foil on the end of the servo arm to make it work.

I use a BASH script to capture the readings. This is run every 10 minutes using the crontab.

First, I photograph the display. It looks like this.

Then, I process it with ImageMagick, cropping, rotating, and thresholding into this.

Then I OCR it using TesseractOCR and a custom language set designed for 7 segment digits, and I upload the results to MongoDB Atlas using cUrl and the Atlas Data API.

I could load my data into a dedicated Time Series collection but with such a tiny quantity of data, the compression and speed benefits won’t be noticeable. I will still make use of time series analytic functions, which can work on normal or Time Series collections.

Visualizing the data using MongoDB Charts

The first thing I needed was a dashboard to show the usage and verify everything was working. For this, MongoDB Charts is an easy drag-and-drop solution. First up was a very basic line chart showing the absolute usage on both meters. This showed me I needed to add a $match clause to the data source to filter out cases where the OCR has gone wrong and missed a digit. I knew my readings were both between 1000 and 40000, so I added that as a filter for any misreads.

It was surprising to me that we have used three times as much electricity as gas since we moved in but gas units are far cheaper. Perhaps I need a gas-powered electrical generator!

What I want to see, though, is the delta usage — how much we use per hour. For that, I used $setWindowFields to compute, for each reading, what the reading was an hour previously and then the change in the values. This let me plot a continuous graph of usage for the previous 60 minutes.

To do this, I added a second Data Source based on the meter collection called ‘energy.meter.delta’ with the following aggregation pipeline using Time Series’ $setWindowFields to add the lowest value of ‘reading’ from the preceding 60 minutes.

This then adds a value ‘meterdelta’ which is the difference between the reading now and what it was one hour previously. I plotted this with a continuous line chart. I decided on separate charts for gas and electricity.

Visualizing daily usage

I also decided to summarize each calendar day in a bar chart. To do this, rather than create another data source, I simply defined an aggregation as part of the chart itself that $grouped the data by type and the date truncated with $dateTrunc to just the calendar day. You can read the aggregation code below the image.

This, along with a simple table of the maximum values, gives me an overall meter usage dashboard that looks like this.

You can view the live version of this here.

The next step is to work out what all that energy is being used for. My next post will show how I start to unravel that mystery using some educational computing devices and more of the MongoDB Application Data Platform.

--

--

Page against the machine

John Page is a Document database veteran, who after 18 years building full-stack document database technologies for the Intelligence community joined MongoDB.