Reduce Your Energy Bills — Part 3 Tracking the Electricity Usage

Page against the machine
12 min readMar 17, 2022

--

Based on the charts created for the first post in this series, I spend between £3 and £7 ($4–9) a day on grid electricity. I wanted to understand what that is being used for to see if I can reduce those costs the way I reduced my gas costs.

I realized that to understand and reduce my electricity usage, I needed to look at three factors: how much electricity I was using from the grid, how much I was generating with the solar panels on my roof (a requirement for newer houses in Scotland), and how much I was consuming and where. I already had the grid electricity usage from the previous blog, so I added that chart to a new dashboard.

I have three kilowatts of solar generation capability on my roof with an inverter from a company called Solis that brings it into the domestic electrical system. Excess power generation is returned to the grid but we do not receive any credit for that. When we bought the house, there was no way to know how much energy was being produced by the panels. There was a small display on the inverter but that is in a pretty inaccessible position in the attic space. Researching a few years ago, I discovered that the manufacturer made a WiFi-enabled data logger for the system which uploads to a cloud IOT site, so I’ve been logging this data to the cloud for some time, although seldom looking at it.

The problem with pulling this data into my calculation is that the vendor does not have an API. You can see details of your usage on their website but nothing is available programmatically. Fortunately for me, their webpage uses JavaScript to pull all the data required from a single URL as a JSON document, and both that internal API call and the login process are very easy to emulate. I therefore wrote a small Python script using requests to log into the site and call the internal API. That script looks like this.

I could have written this in JavaScript and called it in a Realm trigger but I was already running scripts on the Raspberry Pi that captures the meter images, so that seemed a reasonable place to host this code.

After I get the data, I look for the latest electricity reading document and add the field “solarwatts” to it with the number of watts my solar cells are generating. I call this Python script at the end of the script that reads the electricity meter. This is a nice example of how the dynamic schema in MongoDB allows me to just add new fields as I require them.

From my previous blog about electric and gas meter monitoring, I can see that I use a steady 800–1000w normally. When I was testing this, the weather was very bad and the solar system was generating almost nothing. But on the days the weather was improved, I was pleasantly surprised to see a dramatic drop in grid electricity usage. My next step was to update the chart showing the view of the grid usage and change the item displayed to show solar generation.

I placed them one above the other with the timescales aligned.

This shows how increased solar generation reduces the grid usage and makes it very clear that the solar system is working — at least, on the rare days the weather is good in Scotland. On the other hand, I needed to understand how much I was actually consuming compared to returning to the grid, and what that was being used for.

Measuring what devices are using electricity

I first made a short list of my electricity-consuming devices by usage. I’m ignoring the 7KW water heater and 10.5KW shower that are a backup should there be a problem with the gas boiler, as those are only used infrequently.

  • Ovens (2 x 3 KW)
  • Washing machine (3 KW)
  • Dryer (3KW)
  • Fridge
  • Deep freezer (x2)
  • Dishwasher
  • Toaster (2KW)
  • Kettle (3KW)
  • Fish tanks
  • TV
  • Lights
  • Computers

The first step to understanding usage will be to log what equipment is running at any given time and ideally, how much power it’s drawing.

I’m using three techniques to gather information. Some of my white goods are part of the Samsung Smart-things range and include an API to get status and control information. Most of the light bulbs are Philips Hue and I can get information about them using an API too. Finally, I bought some TP-Link T110 smart plugs which let me measure power and have a Python API.

I decided to ignore the very infrequent things like the kettle and toaster as they run for only a few minutes a day. (We make an industrial-sized pot of filter coffee each morning, for those wondering why the kettle isn’t on constantly.)

Reading data from Philips Hue bulbs to Atlas

There is an official API for Philips Hue and a number of different libraries to access it. As I am already running scripts on the Raspberry Pi reading the meter, and as the APIs require direct access to the Hue hub, I chose to write a simple Python script to gather this data too. This gets all the Hue bulbs that are flagged as “on” and adds them to an array of devices in the latest electricity record using the function find_one_and_update(). The code looks like this.

Authentication with Philips Hue works by running the script and then pressing a physical button on the hub to pair. Credentials are then stored in a hidden file.

I know Hue bulbs are typically 9 watts at full power, so I also added an additional value, “watts” being 9 multiplied by the relative brightness of the bulb. I have 34 Hue bulbs in my house, due to an abundance of GU10 ceiling spots, so this could account for up to 300 watts of energy.

Measuring my Samsung SmartThings™ and recording in Atlas

My ovens, dishwasher, fridge, and washing machine are all part of Samsung’s SmartThings™ range, which allows us to control them from a smartphone — for example, to look inside the fridge when at the supermarket and then warm up the oven to be ready for whatever we bring home to cook.

SmartThings also has a published API that makes it easy to get a JSON description of the capabilities and state of a device. Unfortunately, whilst calling this is very easy and there are a number of simple wrapper APIs and libraries, including pysmartthings, interpreting it took much more time. For example, most of the devices continually report themselves as on — except the dishwasher, which turns itself off. For some, you need to look at the “state” to understand what it’s doing, and then look at context. For example, is an oven up to temperature or warming up? The latter takes a lot more energy.

In the end, I had to look at the available information for each device and write a heuristic to estimate the usage — except for the washing machine, which was kind enough to provide a working energy usage API. The dishwasher has this but never populates it. I suspect we needed the next model up for that.

The refrigerator would not return any information at all, always returning a 403 error code for API calls. Fortunately, it does have an on-screen display (yes, the refrigerator has a screen, and you can play Doom on it too) of recent usage, though, which let me estimate it uses an average of 83 watts. I added constant 83 watts to the usage figures to account for this.

My rather messy smartthings.py code looks like this. The estimate_wattage() function is the one that examines the JSON to work out the consumption. I will probably improve this heuristic over time.

Using Tapo T110 plugs to record energy usage into Atlas

Getting usage data was simplified by investing in some Tapo T110 smart plugs. These allow you to remotely turn on and off devices plugged into them but also to request the current energy usage in watts. Whilst there is no official API for Tapo plugs, I selected them because there were some easy-to-use community APIs, like PyP110. The following script reads the current wattage from each plug and adds it to the latest electrical usage document in Atlas.

I used these for my dryer, two fish tanks, my television, deep freeze, and my computer desk, as I have a laptop and two monitors on all day long.

Graphing the estimated energy usage

Once I had all this information, I set out to first compute the total energy usage and then graph how it breaks down by device. To calculate the total energy usage, all I needed to do was add a computed field in charts with a value of {$sum : “$devices.watts”}. However, that graph was very spiky, so I chose to apply the time series function $expMovingAverage to smooth it off.

This gave me this graph. You can see my peak usage is above 5 KW but it doesn’t tell me what is using it.

It does, however, provide the final part of the puzzle to see if we are using more energy than we are generating, how much, and when. I display this with an area chart as it’s easy to see when we are spending and when we are wasting solar energy. I’ve selected a screenshot for this chart, which shows when we have net excess solar power.

Visualizing individual device power usage

Next, I wanted a breakdown of the individual devices. For that, I use a stacked column chart. I $unwind the devices array and take the watts field to plot and the names to label the individual parts.

Integrating the power curve to calculate total usage

It became clear from the charts that there is a baseline of electricity that cannot easily be lowered: two fish tanks, a fridge, and a large freezer that are on 24 hours a day. I also have monitors on my desk or a television turned on a lot of the time.

These are not the major users of electricity though. From the charts, it appeared as if more electricity was being used for laundry then anything else. I wanted to quantify how much, so I set out to compute the total usage of each device using Window Functions — specifically, d $integral. I haven’t used this operator before, so it was an interesting new challenge.

I used a Donut (sic) chart with the query below to compute the usage. By using $integral rather than simply summing the readings I had, I could take any missing readings into account correctly.

This is showing a week of data. It does not surprise me that the washing machine is the largest electricity user. The fridge is a surprisingly large amount of usage and I may need to look at whether I am measuring that correctly. It’s also alarming to see how significant a laptop and two monitors are.

Controlling lights from triggers using IFTTT

Now that I have this information, the question is how to use it — how to reduce the net electricity usage. When I was growing up, I was forever being told by my parents to turn off lights and stop wasting electricity. We no longer live in the days of 60 and 100W tungsten bulbs but still, I have a lot of lights, and leaving them on when not required is wasting power.

As that is a low-hanging fruit, I decided to tackle it first by creating a timed trigger that would turn off all lights if the solar generation was above 800 watts — i.e., it was daylight outside without too much cloud.

I have a free tier account at IFTTT.com (If This, Then That) , a site which provides a no-code solution to react to events on connected systems and services by causing changes in other systems and services. Got a new email? Flash the house lights. House too warm? Then turn on a fan.

I walked through the screens below to connect my Hue lightbulbs to a webhook and then to MongoDB scheduled triggers.

Log in to the IFTTT site. You can use Google, Facebook, and other identity providers.

I want to hook up my Hue lights to a Realm functions, in IFTTT, I created what they call an Applet linking a webhook to Philips Hue. First click ”Create.”

Click the ”Add” button on “If This.”

Type “webhook” and then click the button “Receive a web request” and “connect.” I will call it All LightsOff. Finally, click “CreateTrigger.”

Then click “Add” next to “Then That.

Type “Hue,” search and add “Philips Hue” and choose “Turn Off Lights.”

I am then taken through a wizard to connect my Hue account before I can enable the Applet.

After my Applet is created, I need to get an API key and URL to call it. After all, I don’t want just anyone turning my lights on and off. Finding that information is a little less obvious. Go to the information on webhooks in general at (https://ifttt.com/maker_webhooks) and then click “Documentation.” The documentation examples include your own unique key.

I can see I need to use:

curl “https://maker.ifttt.com/trigger/AllLightsOff/with/key/XXXXX-XX-XXXXX-XXXXX"

After a quick test at the command line (that did turn out the lights), I added my API key as a Secret in MongoDB Realm, then created a new scheduled trigger with the following code, telling it to turn off the lights if the solar panels were generating 800 watts or more on the last check.

And that’s all — no more leaving lights on in this house.

Making the most of free electricity

Unlike the gas I was using to overheat rooms, I’m not sure there is a lot on the electrical side that can easily be cut down. What is obvious, though, is that there are times when our solar generation is larger than our consumption and we are returning energy to the grid to no benefit. (Energy tariffs that pay you for returning energy stopped in the UK in 2019.) I decided to make one final dashboard that would show our usage and when and how much free electricity we were missing out on. And then, in keeping with the spirit of using things from the junk drawer, I would display this on an old Kindle Fire tablet for the whole family to see. We could then focus on perhaps running the dishwasher or doing laundry during the day. As my wife pointed out, this seems counterintuitive as we grew up with electricity units costing less overnight, and therefore, people being used to starting energy-expensive tasks at bedtime.

This time, I just wanted some easy-to-read and understandable numbers. I created a simple dashboard to show the latest solar wattage, the current consumption, and consumption minus solar. I highlight the square with conditional formatting if there is surplus solar power we could be using.

In addition, I filtered to add a sum of all records where solar minus consumption was more than zero, and integrated this curve to show how much solar power we had failed to use. I also addressed an issue where the servo motor wasn’t working and added a final health check to verify the last two readings had different types. (When the servo doesn’t work, we get continuous gas or electricity readings, but not both.)

The final chart looked like this when displayed on the Kindle.

Conclusion

This has been an interesting project. Overall changes to gas usage have saved about 30% of my monthly bill, which makes it very worthwhile. It’s harder to cut electrical usage. I’m not about to turn off my tropical fish tanks, and circumstances mean we do have a lot of washing to do — but at least now we understand it’s good to do the washing during the day rather than in the evening.

I will refine this system over time and you can see how we are getting on with this electricity usage dashboard.

You can also see the meter readings and gas usage from the previous posts.

--

--

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.