project: smart electricity meter with historic energy use

fit a sensor to the electricity meter for a minute by minute readout

The UK electricity meter has a display showing electricity use in kWatt hours, and sometimes someone pops by to read the meter. The meter has a flashing red LED, every flash of red corresponds to 1 Watt, after a thousand flashes you’ve used 1kW or about £0.20 of disposable income. As a light sensor can respond to those flashes, we can measure how much electricity we are using.

credit is due to the helpful folk at OpenEnergyMonitor

Here’s what this project achieves:

  • Measures electricity in any unit – Watts, kW or money
  • Can display readings as a gauge or history graph on Home Assistant.
  • Can send an email; a phone notification or flash a smart bulb when energy use is unusual
  • Uses a photodiode with built-in amplifier TSL257-LF (~ £3.50 ebay)
  • Uses an ESP32 wifi chip (~ £5).
  • Might monitor a gas meter as well – so get two sensors to double the value!
A hurriedly printed case the ESP-32.

How to connect up a light sensor and ESP32

The TSL257 photodiode has three pins, GND, 5v and signal. Connect a 10K ohm load resistor between signal and ground. (Luckily my RPi kit had a pack of 10K ohm resistors). CARE – this sensor is PLASTIC: I soldered the connections, using a large metal clip as a heatsink. Finally the sensor wires were glue-gunned, covered in heat-shrink and mounted in a 3D printed cover (below).

This is the schematic – actually the resistor was connected further down the wire, all covered in glue and heat shrink.

The diameter of this fits inside an indentation on my meter.
Note my usual glue gun mess, however the sensor needed to be both protected and easily removed to keep the electricity company happy.
The blob of ‘blu-tak’ makes it look less permanent. The white sensor holder does fit neatly in the round recess.

Thus far you’ve three wires from the sensor joining an ESP32. You now need a lead from a USB charger to bring 5v power and GND to the ESP32. I used an ESP32-CAM to provide me with an almost free extra camera. Here’s the box that I hope soon to make more attractive:

Program the ESP32

For this step you need an already working ESPHome installation in Home Assistant as shown in HERE. It’s a project in four steps starting with installing Home Assistant on a Raspberry Pi – see HERE.

In Home Assistant go to the Esphome plugin and add a new ‘node’. A wizard walks you through. Eventually you see an editor where you can add the code below but change the items in bold to suit your board, your wifi, your network and the GPIO pin you connected to.

   devicename: meter

   name: ${devicename}
   platform: ESP32
   board: esp32dev
# Enable Home Assistant API
   password: "YOUR HA password"
   password: "Optional password"
 ssid: "YOUR_SSID"
   # Set this to the IP you want on the ESP
   # Set this to the IP address of the router. Often ends with .1
   # The subnet of the network. works for most home networks
# The LED output and light sections are optional
   platform: gpio
   pin: GPIO4
   id: gpio_4 
  platform: binary
  output: gpio_4
  name: ${devicename} light 
# THIS IS THE IMPORTANT PART - remember you connected the sensor to pin 12
  platform: pulse_counter 
  pin: GPIO12 
  unit_of_measurement: 'Watts' 
  update_interval: 20s 
  accuracy_decimals: 0 
  name: 'power meter' 
    - multiply: 60 

# If you have a camera add the esp32_camera block below this (see here)
  • Note the last bit of code above. The filter will convert pulses per minute to Watts. If you prefer a reading in kWatts, change ’60’ to ‘0.06’ and change the unit to’ kW’
  • While you’re in the ESPhome section of Home Assistant look for options to VALIDATE your code and then to COMPILE it. The compile operation takes a few minutes as ESPhome assembles code libraries and creates a firmware ‘.bin’ file.
  • Download this bin file. Flash the bin file using the ESPHome-flasher tool (see here).
  • When the flashing is done, power-up the ESP32 and go to Home Assistant > Integrations > + > ESPHome > add node. You might have to enter the IP address you chose in the code. You’ll then see the ‘meter’ entity that has been added to Home Assistant.
  • Finally, go to the Home Assistant overview – the frontend view that displays your stuff – and choose Configure UI (top corner). Use a history graph and a gauge to display your electricity use as follows.

Display your meter in Home Assistant

entity: sensor.power_meter
 min: 0
 name: electricity use
   green: 1000
   red: 3000
   yellow: 2000
 theme: default
 type: gauge
 max: 5000 
type: sensor
 entity: sensor.power_meter
 graph: line
 hours_to_show: 24
 detail: 2
type: history-graph
 entity: sensor.power_meter
 hours_to_show: 24
 refresh_interval: 0 

When you’ve got it working set up a notification

Now comes a point about having a time graph of electricity use: a few years ago the time graph on my electricity monitor helped me discover unusually high usage – I tracked it down to an outbuilding where an immersion heater had been left switched on for two years. So the next part of this project is to set a routine (or ‘automation’) to alert of unusual electricity use. I’m still thinking about it, so meanwhile, find out how to create automations that do this. Here’s one automation project.

There are other ways to monitor electricity

The lovely onzo meter (ebay £8.50) uses a clamp on the mains cable. It provides a live reading of electricity use. Given its design and usefulness I suggest you own it if, like me, you don’t have a UK smart meter. The smart meters appear to be informative but I really want to see my energy use history.

Commercial electricity monitoring kits may require you to subscribe to a service or depend on it. That’s not a good thing to do in the long term. For a couple of years (2014) I used the very reliable OWL Intuition. I sold the kit when OWL asked for a subscription.

The kit at OpenEnergyMonitor is something I will consider if I decide to buy solar panels.

The ONZO energy meter tells how much energy you’re using in any instant. At £10 it’s an excellent substitute for a smart meter.

6 Responses

  1. Ron says:

    Hi, great stuff! Is there a way to read a gauge directly? I have this kind of gauge:
    How can I track the red part with my espcam? Also another question, while using the espcam for it’s cam usage, can I use other pins on there for other sensors such as temp sensor and door sensor?

    • roger says:

      Hello and welcome to the world of home monitoring. Yes you can do all that Ron. I am working on a direct display of data using a TTGO st7789v which, with a sensor, is all you’d need for monitoring and a direct display. The TTGO module is an ESP32 + LED display in one package + several spare pins to add other sensors for temperature or presence detection.
      In your case you attach my smart meter light sensor to the TTGO instead of the ESP32 that I used. There are Arduino library examples for this display that provide a very nice graphic meter – I’ll make progress with this future project when I learn how to incorporate that meter display example in ESPhome.
      I’ve added a link to a project that uses that display. The cost of the TTGO is trivial. You don’t need a separate ESP32 module to measure. I needed two modules here because the display needed to be away from the oven.

  2. Shahid says:

    Hi, just came across your website. this provides very valuable information to the novice smart home enthusiastic so thank you for sharing your knowledge. secondly, I like to know are you still using this step to monitor electricity usage. i was thinking to build something similar along with an oil level monitor. any watch out or lesson learned you like to share? thanks

  3. Mr D Wilkinson says:

    Thanks for taking the time to write all this stuff down. It has really helped me capture my home electricity use by monitoring the flashing light on the “smart” meter. Keep up the great work. it’s very much appreciated.

    • roger says:

      Good to hear so – you’ve no doubt overcome a few challenges. I don’t recall mine ever failing over three years use.

Leave a Reply

Your email address will not be published.