DIY Smart Energy Meter With ESP32 + Home Assistant

by Giovanni Aggiustatutto in Circuits > Sensors

29950 Views, 252 Favorites, 0 Comments

DIY Smart Energy Meter With ESP32 + Home Assistant

Questo progetto con ESP32 ti abbassa la bolletta! Misuratore di Consumi SMART fai da te
IMG_8367 crop.jpg
Video ITA 2 00_01_04.73.jpg
Video ITA 2 00_00_19.08.jpg
Video ITA 2 00_00_20.31.jpg

Hi, I am Giovanni Aggiustatutto and welcome to this Instructable! In this guide I will show you how we can save electricity, and thus save money on our electricity bills, with a simple DIY project. This summer I have built a smart energy meter, which allows us to measure the electricity consumption of our whole house or apartment and see it from an app on our smartphone. This in my opinion is essential if we want to start saving electricity, because it allows us to know how much energy we are consuming at a precise moment and how much we have consumed in the various days of the week, during the last month or in the previous 24 hours, so that we can understand what appliances consumes the most and maybe even find the small energy draws that we usually do not notice.

The device I have built uses a simple current clamp and transformer to accurately measure the energy consumed in watt-hours, as well as voltage, current, frequency and power factor. Through WiFi, the data is sent to the Home Assistant app, from which we can also see interactive charts showing trends in the energy consumption over time. To make the project look more professional, I also added a small display to visualize the values being measured and the status of the WiFi connection. The project is quite simple and, most importantly, does not require us to make major changes to the electrical wiring of our apartment. But now, let's get started!


As always, I’ve also made a video about this project, that you can find on my YouTube channel (it has English subtitles).

Supplies

Video ITA 2 00_09_14.02.jpg
Video ITA 2 00_00_58.36.jpg
Video ITA 2 00_07_20.78.jpg
Video ITA 2 00_03_22.30.jpg

To make this project I used:

  1. JSY-MK-194G energy meter module (link here)
  2. ESP32 Wemos D1 mini board (link here)
  3. Meanwell APV-8-5 5V 8W power supply (link here)
  4. SSD1306 i2c 0.96” Oled display (link here)
  5. A piece of perfboard (link here)
  6. 26 AWG wire with silicone sheating
  7. Solid copper wire
  8. PCB male and female jumper 2.54 mm connectors (link here)
  9. 4-pin JST XH 2.54 mm PCB connector with female connector (link here)
  10. 2-pin 5.08 mm screw terminal block
  11. Heat shrink tubes
  12. Power cable with plug (can be recycled from old appliances)
  13. 12 M3 brass threaded inserts (link here)
  14. 12 M3 10 mm-long bolts (link here)

Tools:

  1. Soldering iron
  2. Hot glue
  3. Screwdrivers, pliers and other basic tools

For the enclosure for the electronics:

  1. Electrical junction box of appropriate size

Or

  1. 3D printer
  2. Two colors of PLA of your choice (I used green and grey)

Measuring AC Power

Video ITA 2 00_03_22.30.jpg
Video ITA 2 00_03_53.09.jpg
Video ITA 2 00_03_17.20.jpg
03_33.94.jpg

When I had the idea for this project, it all seemed quite easy: take a current clamp, connect it to an ESP32, and you have the energy consumption data directly into Home Assistant. The reality is… that it doesn’t work that way. In fact, after a bit of research I realized that measuring energy in AC is much more complicated than measuring it in DC. Long story short, in DC to get the energy consumption in watts you simply multiply the current by the voltage, while in alternating current you have to take into account the power factor, which varies according to the type of load. Here things get quite complicated, and I must admit that at this stage I was about to abandon the project. After a few days and a bit of research, I found the solution: using an energy meter module. For my project I went with the JSY-MK-194G module from the Chinese company Jian Si Yan Meters.

In the box we have the module and its current clamp, which allows us to measure the AC current flowing on a wire. The CT clamp has quite a long wire which can be connected to the module with its connector. On the board we also have a small isolation transformer that makes it possible to safely measure mains voltage, and another CT clamp to measure current on a second circuit, although we will not use it for this project. On the other side we have some integrated circuits that, starting from the values measured by the current clamps and the transformer, accurately calculate the power drawn along with voltage, current, frequency, and power factor. This way, we don't need to take care of the calculations, and the data is ready to be read by an Arduino or ESP32 using the TX and RX pins to communicate with the board through the Modbus protocol.

There is also a version of this module with two detached current clamps, that can be useful to measure the energy consumption on two different channels (for example, two circuits from an electrical panel). Both versions have the same electronics, so you can choose which one to use without modifying anything else of this project.

Electronics Connections

IMG_7911 CUT.jpg
Video ITA 2 00_03_53.09.jpg
Schematic_Energy-meter_2024-10-07.png
Video ITA 2 00_03_25.17.jpg

Other than the energy meter module, for this project I used an ESP32 Wemos D1 mini board and an Oled display. The ESP32 is a key part for this project, as it will be used to read the data from the energy meter module and send it to Home Assistant using the integrated WiFi connection. The Oled display will be used to visualize the values being measured by the energy meter and the status of the WiFi connection. Lastly, we will need a 5V power supply to power the ESP32 and the energy meter module.

The connections between the different components are really simple: we need to connect the ESP32 3v3 and GND pins to, respectively, VCC and GND screw terminals of the energy meter module. For the Modbus communication we need to connect the TX terminal to pin 16 and the RX terminal to pin 17. The Oled display needs power (3v3 and GND connect to VCC and GND of the display) and i2c communication, for which pin 22 goes to SCL and pin 21 goes to SDA. Lastly, + and - of the 5V power supply go to, respectively, +5V VCC and GND of the ESP32 board.

As for the mains voltage connections, line and neutral coming from the power cable will connect to the two input wires of the power supply and to the L and N screw terminals on the energy meter module.

In the picture above you can see the schematics for the connections.

Downloads

Circuit Board

Video ITA 2 00_07_16.32.jpg
Schema.jpg
Video ITA 2 00_07_14.50.jpg
Video ITA 2 00_01_12.82.jpg

In order to make permanent connections between the different components I decided to mount them onto a circuit board. First, I cut a 54x74 piece of perfboard. Then I soldered some male jumper connectors to the ESP32 board. With female jumper connectors, I put the board on the perfboard. Next to the ESP32 I soldered on the perfboard a 4 pin female jumper connector, that I used to install the Oled display. Lastly I soldered on the board a block of two screw terminals to connect the power supply, and a 4-pin male JST XH connector that will be used to connect the energy meter module to the board.

Once all the parts were soldered on the board, I made the connections under the board following the wiring diagram. For the connections I used some stiff copper wire to make some traces and wires with silicone sheathing for the intersecting traces.

In the picture above, you can see the layout of the connections under the board (the pins of each component are labeled); if you need to zoom into the picture, I've attached the full resolution file below.

If you prefer, instead of making the circuit board by hand you can design a simple PCB for this project and have it manufactured.

Downloads

3D Printed Enclosure

Video ITA 2 00_07_36.29.jpg
Video ITA 2 00_07_37.92.jpg
Video ITA 2 00_07_57.77.jpg

Now that the circuit board was ready I designed a box to fit all the electronics for this project in Fusion 360. As always, I spent some time on the design of the box to take care of the aesthetics of the project. Then I 3D printed the box and its lid (the 3D files are below). For 3D printing I chose some green PLA for the box and gray PLA for the lid. After about a day of printing, the parts came out very nice.

The box has two holes on one side: one is for the CT clamp cable going to the energy meter module, while the other one can be used to let the mains voltage power cable pass through. For this last hole, there are a few options: if, like in my case, the power cable you use has a grommet, you can print the file with the slot to insert it; if your cable, like in most cases, doesn't have one, you can print the file that has a hole to directly insert the .

The lid has the space to install the Oled display, and some openings that follow a wave pattern, that while are mainly made for aesthetics, are also useful for aeration.

If you prefer, you can skip the 3D printing part and use, instead, an electrical junction box to install the electronics.

Installing the Electronics

Video ITA 2 00_08_23.62.jpg
Video ITA 2 00_08_04.76.jpg
Video ITA 2 00_08_05.64.jpg
Video ITA 2 00_08_15.94.jpg

Now that we have the 3D printed box and its lid we can assemble the whole project. The first thing I did was to place some M3 brass threaded inserts in all the holes in the enclosure using a soldering iron set to around 230°C. The threaded inserts in the base will be used to mount the electronics, while the ones on the four corners will be used for securing the lid.

Then I placed the circuit board with the ESP32 in the box, and marked the position of the two threaded inserts below the board in order to drill two holes in it, being careful not to hit the cables. This way, I could secure the board to the threaded inserts with two M3x10 mm screws. With four more screws I mounted the energy meter PCB to the enclosure, leaving the low-voltage terminal block oriented towards the ESP32 board. Lastly, I mounted the 5V power supply with two screws, keeping the two 5V output cables towards the ESP32 board.

Final Connections

Video ITA 2 00_06_54.81.jpg
Video ITA 2 00_08_35.72.jpg
Video ITA 2 00_09_02.43.jpg
Video ITA 2 00_09_06.54.jpg

Moving on to the electrical connections, I connected the positive and negative of the power supply to the 5V terminals on the circuit board with the ESP32. Then, using a female four-pin JST connector with wires attached on it, I connected GND, VCC, TX and RX of the energy meter to the ESP32 board (TX goes to pin 16, RX goes to pin 17, VCC to 3v3 and GND to GND).

Now we are left with the mains voltage connections, and for these I want to remind you that mains voltage can be very dangerous, so always be very careful and do everything at your own risk. To connect the energy meter to an electrical outlet, I salvaged this power cord with a 2-pin plug, which has a grommet on one end that fits perfectly into the 3D printed part. If you are using any other power cable, after 3D printing the appropriate file for the enclosure (look at Step 5) you can install a cable gland to secure the cable to the box. Then I joined the line and neutral of the power cable with the two input wires of the power supply, and connected them to the L and N terminals of the energy meter.

The connections are finished, and seeing these wires so neat always gives me such a great satisfaction.

Closing the Lid

00_08.65.jpg
Video ITA 2 00_09_22.40.jpg
Video ITA 2 00_09_29.06.jpg
Video ITA 2 00_09_39.02.jpg

Now we need to install the lid onto the enclosure. The lid has an opening onto which I secured the Oled screen with some hot glue. To connect it to the jumper connector on the circuit board, I created a small extension cable with two 4-pin jumper male and female connectors and four wires. To protect the soldered joints, I used some heat shrink tubing.

Finally I put the lid on top of the enclosure, and secured it with four M3 screws.

Connecting the ESP32 to Home Assistant

Video ITA 2 00_05_06.78.jpg
Video ITA 2 00_00_20.31.jpg

To see the data measured by the energy meter from our mobile phone, we need to connect the ESP32 to Home Assistant using the ESPHome add-on. This way, from the Home Assistant app we will be able to see the real-time data from the energy meter and the charts showing the energy consumption in various periods.

Home Assistant is a very powerful open-source platform with which we can manage all of our smart devices from a single interface. Home Assistant works in our local network, so we need a device to run it: we can use a Raspberry Pi or, like I do, an old Windows PC with Home Assistant running on a virtual machine. To access the interface you can log into the webpage from a computer or download the Home Assistant app on your smartphone. To connect to my Home Assistant from outside the local network I'm using the Nabu Casa Cloud, which is the simplest and safest solution but it's not free. There are other solutions but they are not totally safe, so I would use them only if you know really well what you are doing.

To connect the ESP32 to Home Assistant we will use ESPHome. ESPHome is an really well-made add-on that allows us to connect ESP32 and ESP8266 boards to Home Assistant via WiFi. To connect the ESP8266 to ESPHome you can follow these steps:

  1. Install the ESPHome plugin in Home Assistant
  2. On ESPHome's dashboard, click on + NEW DEVICE
  3. Give your device a name (in my case it was "Energy meter")
  4. Once you are prompted to install the firmware onto your device, select SKIP THIS STEP: we will install the firmware later
  5. Select ESP32 and click NEXT
  6. Copy the encryption key that is given, we will need it later
  7. Once again, skip the installation of the firmware on the ESP32
  8. Once the device appears in the ESPHome dashboard, click on EDIT to see the device's code
  9. Under wifi, insert your wifi ssid and password:
wifi:
‎ ssid: wifi_ssid
‎ password: wifi_password


  1. To make the connection more stable, I strongly recommend setting a static IP address for the ESP32, with this code (to copy the code, use this link: https://pastebin.com/GHuJEAJk):
wifi:
‏‏‎ ‎ ssid: wifi_ssid
‏‏‎ ‎ password: wifi_password
manual_ip:
‏‏‎ ‎ # Set this to the IP of the ESP
‏‏‎ ‎ static_ip: 192.168.1.97
‏‏‎ ‎ # Set this to the IP address of the router.
‏‏‎ ‎ gateway: 192.168.1.1
‏‏‎ ‎ # The subnet of the network.
‏‏‎ ‎ subnet: 255.255.255.0


  1. At the end of the code given by ESPHome, paste the one you can find at this link: https://pastebin.com/dcS1SQv8
  2. In the end, your code should look like the one you can find at this link (but don't copy this code directly into the ESPHome configuration).

The code reads the values from the energy meter module using the Modbus protocol. Each value has its own Modbus address. Here is an example of how the code reads one value:

sensor:
​ # First channel voltage
​ - platform: modbus_controller
​ ‏‏‎ ‎ modbus_controller_id: jsymk
​ ‏‏‎ ‎ id: voltage1
​ ‏‏‎ ‎ name: "Voltage"
​‏‏‎ ‎ icon: mdi:alpha-v-box
‏‏‎ ‎ ​ device_class: energy
​‏‏‎ ‎ address: 0x0048
​‏‏‎ ‎ unit_of_measurement: "V"
​‏‏‎ ‎ register_type: holding
​‏‏‎ ‎ value_type: U_DWORD
​ ‏‏‎ ‎ accuracy_decimals: 1
​‏‏‎ ‎ filters:
​ ‏‏‎ ‎ - multiply: 0.0001
​ ‏‏‎ ‎ register_count: 1
​ ‏‏‎ ‎ response_size: 4


  1. The module can read a lot of different values, which you probably don't need. The code is written to be able to read every value, but if you don't need or don't want one to show un in Home Assistant, just comment its name, like this:
...
name: "Voltage"
...
The value "Voltage" is sent to Home Assistant.


...
#name: "Voltage"
...
The value "Voltage" is not sent to Home Assistant; as you can see I've added the # symbol before the name line.


Note: don't paste the code written above in the black rectangles directly into the ESPHome configuration, as it doesn't have the right indentation; please follow the links to open the properly formatted and ready to be copied code.

Installing the Energy Meter

Video ITA 2 00_10_25.23.jpg
Video ITA 2 00_00_19.08.jpg
Video ITA 2 00_00_45.22.jpg
Video ITA 2 00_10_43.46.jpg

The last thing to do is to installing our energy meter in the electrical wiring of our house or apartment. Of course, this part may vary a lot depending on your country and electrical standards. As I wanted to measure the power consumption of my whole apartment, I first had to locate the wires that go from the utility meter to the electrical panel that I have in the apartment. In my case they pass though a junction box on the wall that is right below the panel; for me, this was the perfect place to install the current clamp of the energy meter. Once I located the main line cable I installed the current clamp on it; since the current clamp can be opened, installing it does not require disconnecting the cable, which is a great thing.

To do a test I plugged in the energy meter and I immediately started viewing on the screen the power consumption in watts, the voltage and the current flowing on the cable. For a more permanent setup I pulled the current clamp cable through a conduit for about three meters, in order to get it from the junction box into the cabinet where I also have my LAN network switch. In this cabinet I mounted the energy meter on the wall using a wall plug; in order to screw the energy meter to the wall, I drilled a hole in the back of the 3D printed box. Lastly I connected the current clamp cable to the energy meter module, closed the lid and finally plugged in the energy meter.

Home Assistant Dashboard

Screenshot 2024-10-11 alle 21.04.16.png
Video ITA 2 00_01_04.73.jpg
Video ITA 2 00_00_35.50.jpg

To view the data measured by the energy meter, I created a dashboard for Home Assistant in which I can see, in addition to the instant power consumption, a few different charts, that include:

  1. Energy consumption day by day for the last seven days (with energy consumed during the last seven days and energy consumed during the day you're in, starting from 00:00)
  2. Energy consumption day by day over an entire year (with energy consumed during the last month and energy consumed during the last 365 days)
  3. Energy consumption hour by hour over the last 24 hours (with also the minimum and maximum values of power consumption for that day)

For the nerds there are also voltage, current, frequency and power factor values.

Looking at the data on power consumption can be very interesting, for example I found that in my apartment the average energy consumption in a day is around 6 kWh, while the minimum consumption that is reached, when everything is turned off, is about 60W, due to all the electronic devices that are connected 24 hours a day, such as the internet router and the TV.

If you want to take inspiration from my dashboard to build your own, the first thing you need to do is creating the helpers that calculate the energy consumed during the day you're in, energy consumed during the last seven days, energy consumed during the last month and energy consumed during the last 365 days. For calculating the energy consumed during the day you're in, you can use the visual configuration:

  1. Go to the Helpers page
  2. Click + CREATE HELPER
  3. Select Utility Meter
  4. Give your helper a name; in my case this was Energy consumption 24H
  5. Choose the energy entity (the one measured in kWh) of the energy meter as the input sensor
  6. Choose Daily as the reset cycle of the utility meter
  7. Enable the Periodically resetting and Sensor always available toggles
  8. Save the helper

This helper should give you the energy consumed in a period of time (day), and reset this value at a fixed time. So, the value you see is not the energy in the last 24 hours, but the energy consumed starting at 00:00.

For calculating the energy consumed during the last seven days, energy consumed during the last month and energy consumed during the last 365 days I used the statistics platform, that unfortunately is not supported by the visual editor. Using the statistics platform I also calculated the minimum and maximum value of the power drawn in the last 24 hours. To configure this part:

  1. Open the configuration.yaml file with the file editor of your choice
  2. Under the sensor: entry (or in the sensors file, if you have created one), add this code (to copy the formatted code use this link: https://pastebin.com/vDvu66ph):
sensor:
- platform: statistics
‏‏‎ ‎ name: "Energy consumption Week"
‏‏‎ ‎ entity_id: sensor.energy_meter_energy_1
‏‏‎ ‎ state_characteristic: sum_differences_nonnegative
‏‏‎ ‎ max_age:
‏‏‎ ‎ ‏‏‎ ‎ days: 7
- platform: statistics
‏‏‎ ‎ name: "Energy consumption Month"
‏‏‎ ‎ entity_id: sensor.energy_meter_energy_1
‏‏‎ ‎ state_characteristic: sum_differences_nonnegative
‏‏‎ ‎ max_age:
‏‏‎ ‎ ‏‏‎ ‎ days: 28
- platform: statistics
‏‏‎ ‎ name: "Energy consumption Year"
‏‏‎ ‎ entity_id: sensor.energy_meter_energy_1
‏‏‎ ‎ state_characteristic: sum_differences_nonnegative
‏‏‎ ‎ max_age:
‏‏‎ ‎ ‏‏‎ ‎ days: 365
#calculate min and max values of power drawn
- platform: statistics
‏‏‎ ‎ name: "Energy consumption Max 24H"
‏‏‎ ‎ entity_id: sensor.energy_meter_power_1
‏‏‎ ‎ state_characteristic: value_max
‏‏‎ ‎ max_age:
‏‏‎ ‎ ‏‏‎ ‎ hours: 24
- platform: statistics
‏‏‎ ‎ name: "Energy consumption Min 24H"
‏‏‎ ‎ entity_id: sensor.energy_meter_power_1
‏‏‎ ‎ state_characteristic: value_min
‏‏‎ ‎ max_age:
‏‏‎ ‎ ‏‏‎ ‎ hours: 24
  1. The entity IDs need to be changed based on how you named the entities of the energy meter; if you didn't change the names, you shouldn't need to do anything
  2. You should also add the energy meter entities (in my case, sensor.energy_meter_energy_1 and sensor.energy_meter_power_1) to the Home Assistant recorder, if you disabled it by default; this is needed because the statistics platform uses the data from the recorder to calculate the output values

The first three sensors in the code give the value of the energy consumed in a certain period of time that ends with the present time; for example, if the max_age is set to 7 days, you are going to get the value of the energy consumed in the last seven days. The last two entries calculate the minimum and maximum value of the power drawn in the last 24 hours.

Lastly, you need to create the dashboard. To make my own, I used the Mushroom cards, Stack In Card, card-mod and apexcharts-card HACS integrations. If you want to take inspiration from my dashboard, although it is not perfect and still a work in progress, you can find the full YAML code at this link: https://pastebin.com/NXtP9Lkq.

It Works!

Video ITA 2 00_00_20.31.jpg
Video ITA 2 00_00_16.86.jpg
Video ITA 2 00_00_19.80.jpg
Video ITA 2 00_09_14.02.jpg

Overall, I am very happy with how this project turned out, and I'm sure that, by knowing my energy consumptions, I will be able to lower them, which is great both for the environment and for the wallet. I hope you found this guide interesting and maybe useful. To see more details about this project, watch the video on my YouTube channel. Bye!