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, and every few months 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. Here’s what this project achieves:

credit is due to the helpful folk at OpenEnergyMonitor
  • Measures electricity in any unit – Watts, kW or money
  • Can display that 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 for 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.

substitutions:
   devicename: meter

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

# If you have a camera add the esp32_camera block 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

 # THIS IS THE CODE FOR THE GAUGE  
entity: sensor.power_meter
 min: 0
 name: electricity use
 severity:
   green: 1000
   red: 3000
   yellow: 2000
 theme: default
 type: gauge
 max: 5000 
 # THIS IS THE CODE FOR THE SMOOTH LINE GRAPH 
type: sensor
 entity: sensor.power_meter
 graph: line
 hours_to_show: 24
 detail: 2
# THIS IS THE CODE FOR THE DETAILED HISTORY GRAPH 
type: history-graph
 entities:
 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 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 a subscription and or dependance on a business concern. I have seen enough of these firms disappear to feel wary.

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.

Leave a Reply

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