Calibrating an ESPHome flashed power plug

C
12 gosund ESPHome flashed wall plugs, plugged into each other making a big stack.
I’ve calibrated twelve wall plugs!

So you got a power plug or any other device that can measure power, current or the voltage flowing through it. Nice! So did I, and I flashed them with ESPHome since well, that is my firmware of choice for ESP based devices. The problem, however, is that measurements using these DIY methods are rarely accurate.

Fortunately, there’s a solution: You can correct these measurements, right in ESPHome itself, by calibrating your device. In this article, I’ll take you through this calibration process. It is not that hard! I promise.

Nevertheless, it is time-consuming. I’ve spent almost six hours flashing & programming twelve wall plugs. For these kinds of things, it is essential to work accurately, to stay calm and check everything twice.

Please note, this article mainly focusses on calibrating, it assumes you are already able to flash your device with ESPHome.

My journey started with some cheap devices by a manufacturer called “gosund”. However, in reality, Tuya makes those devices and are based around an ESP chip. Tuya is a large manufacturer of IoT devices, which are mainly sold white-labeled and are known to be flash-able with custom firmware like, for example, ESPHome or Tasmota.

Picture showing 4 gosund wall plugs

EU version

I’ve got some of these gosund SP-1 devices.
You can get 4 of them for around 35 euro.

UK version

Sold under several names: gosund, Teckin, and koogeek. All created by Tuya.

US version

They can measure current (A), power (W) and voltage (V), and based on an ESP chip.

The price of these devices is truly unbeatable, especially if you take the power measuring capabilities into account. The power measurement abilities open up tons of automation possibilities in Home Assistant, besides just monitoring energy usage. Let me list you a couple of things you could do with them.

  • Fridge/Freezer: Alert when no power used in X time: Save the food!
  • Dishwasher/Washer/Dryer: Notifications when power drops: Program finished!
  • Multimedia/TV set: Monitor hours watched, limit TV use and a standby-killer.
  • Waterboiler: Optimize the power use, don’t heat it when not at home.
  • Central ventilation box: Turn it off part of the day, when not home.

Besides this all, it is also just a remote switch. So, yeah, basically you can remotely switch and automate anything with a power cord.

Initially flashed my devices using a method called “tuya-convert“.

Stuff you’ll need

Let me kick off real quick here; you’ll need ESPHome. Home Assistant is, of course, lovely to have but not directly needed for what you are doing here. However, you do need:

  • ESPHome
  • A power meter device
  • ESPHome flash-able wall plugs (EU version, UK version, US version)
  • A webcam (not required, but helpful. I’ll explain later)
  • Multiple devices/appliances that consume power

Power meter

You unquestionably need a power meter device; you have to have a reference point. In general, if you are a home automation enthusiast and don’t own one: Get one! It is a useful tool to have around. I use a Zhurui PR10-C Power Recorder.

Image of the Zhurui PR10 Power Recorder.

Zhurui PR10 Power Recorder

I use this power meter to calibrate my ESPHome based devices. It is a remarkably delightful device to have in your toolbelt in general. Besides checking current usage, it can also log, record, and even measure the costs of devices plugged in.

It is pretty darn accurate, has a decent build quality, easy to control (didn’t even need a peek at the manual) and a freaking darn sexy display. 😎

Get the Zuhrui PR10-C (EU/DE version)
Get the Zuhrui PR10-F (UK version)
Get the Zuhrui PR10-E (US version)
Get the Zuhrui PR10-D (EU/FR version)
Get the Zuhrui PR10-A (AU version)

Power consuming devices/appliances

To calibrate, you are going to measure the power used by a device using the power meter and your ESPHome device and trying to get them as close to each other as possible. Therefore, you’ll need a couple of devices/appliances that consume power. For a correct calibration, I recommend at least 3 different devices, but more = better.

However, it is crucial that you use devices that consume energy in a low, mid, and high range since this improves the calibration. I recommend using devices that have simple electronics.

Some recommendations:

  • A hairdryer, since they usually have multiple fan and heating levels, you can use.
  • A vacuum cleaner, bonus if it has a high and low vacuum speeds.
  • Old fashioned light bulbs (e.g., 60W).
  • (Soldering) Iron (although annoying when it reaches its set temperature).
  • A LED flood light (those usually consume around 10-20W).

Even though I recommend a device consuming high power, please be sure you are not overloading the device you are calibrating. Always stay well within the specifications.

More complex electronics are usually not consistent in their power consumption and not recommended. For example, a laptop charger usually has a switching adapter and are just harder to use in this case.

I’ve used a simple LED flood light (around ~10W), a heated iron board (with 2 settings, giving me ~250W & ~265W), a steam iron (steam takes long to heat up, ~2000W) and a vacuum cleaner in low and high fan speed (respectively ~650W & ~1200W).

Flashing the initial firmware

Here you are, ready to go and start of this journey. Start by flashing in some ESPHome firmware on which you base your readings. Chief at this point: ESPHome should NOT do any corrections. You want to be using the actual data from the power sensor in our device.

I’ve created a little starter ESPHome template you could use to flash into your device. It contains the basic needs: enables the power sensor, makes the button on the switch work, toggles to blue LED to indicate the state of the relay and uses the red LED to show the status of ESPHome.

The code in this article uses the hlw8012 sensor, used by the plugs I’ve listed in this article. If your device uses a different sensor (e.g., in a Sonoff POW2), you’ll need to adjust the code a bit. However, the methods in this article still apply.

Setting up for measuring

If you’ve flashed in the above, you should be ready to start creating reference measurements. What you are going to do is pretty simple: You are going to make measurements of appliances hooked up to the wall plug and are going to register what the power meter displays and what your wall socket registers.

Let me walk you through the preparation for creating measurement:

  1. Plug in your power meter into a power socket.
  2. Plug in your ESPHome flashed wall plug into the power meter.
  3. Point your webcam at the power meter display.
  4. Open up ESPHome and view the log of the power plug (alternatively, visit the plug’s web interface by browsing to its IP address).
  5. Open up a program on your computer that allows you to view your webcam. If you don’t have any, I would recommend using VLC Media Player.

Please be sure to put the wall plug into the power meter and not the other way around. The other way around causes you to measure the energy of the power meter itself as well, which well, isn’t there forever. Let me show you how it looked when I did it:

Image of the measurement setup. Showing the power meter, wall plug and webcam.

Oh, the webcam! That is a simple and neat trick to get more accurate registrations. So you’ll need to look at the power meter display and ESPHome log output at the same time, and that is hard to do. The webcam, allows you to view both at the same time, and even more convenient, no need to write them down: A screenshot does the job just perfect! Moreover, almost without delay.

Creating reference measurements

Image showing the relation between the ESPHome debug log and the values measured by the Zhurui PR10 Power Recorder

As you can see in the above screenshot, it shows the measurements from ESPHome and the measurement of the power meter. I just waited for the log output to show up and immediately hit the screenshot button. It gave me an instant and the most accurate registration of them both at the same time.

Ready? Go!

  1. Start without any appliance plugged into your wall plug. You’ll notice the power meter showing about 1 Watt. That is the power used by your wall plug.
  2. Create a screenshot, just like above of that. You need those starting values later.
  3. Plug in an appliance, and turn it on.
  4. Give both the power meter and wall plug a moment to stabilize the measurement.
  5. Take screenshots of the values as soon as the log in ESPHome pops up (the faster, the better). Please be sure to get screenshots of power, current & the voltage.
  6. Go to step 3 and start with the next appliance, repeat until you have registered the power usage of all devices.

Right now, you should have ended up with a bunch of screenshots, which should have perfectly registered the value shown by the ESPHome sensor versus the power monitor. That is awesome! Let’s put all this data into ESPHome.

Applying corrections to the firmware

Time to apply all this collected information to calibrate the wall plug. In the previous step we’ve registered: ESPHome sensor vs the power monitor. The great thing is: Exactly what ESPHome needs to calibrate the sensor. Now, you don’t have to calculate anything, just put the collected data into the ESPHome YAML file.

ESPHome has a sensor filter called calibrate_linear. It allows for calibrating sensor values by using the values you measured. By mapping the sensor values to the measured values, ESPHome fits a linear equation to the values using least squares. Don’t worry if that sounded a bit difficult, as I promised: It is easy!

The first registration without an appliance plugged in, is imperative. It gives you the base power (Watt) and current (Amps) that the power plug itself uses. Those values you can use to correct the measured values by subtracting before sending out those values. Unless, of course, you want to register both the plug and the appliance.

So here is an example of what the power sensor in the YAML files looked like after I’ve put in the registered values:

As you can see above, I’ve put in a list of ESPHome values mapped to the power meter values. ESPHome uses this mapping to correct the data registered by the wall plug sensor to the correct and real-world value. I’ve also added a small lambda filter, that finally subtracts the power usage of the wall plug itself.

Next, do the same for the current sensor:

The voltage sensor is a bit different. You need to start with a 0 -> 0 reference since you have to assume, zero is zero. There is no way you can measure a zero value. Also, there is no additional filter for subtracting a base value, since well, the power consumption of the wall plug does not affect the voltage in this case.

And now, you’re thinking: “Nice Frenck, but how does this look like when I’ve put it all together?” Merging the initial firmware and these calibrations, results in: calibrated_firmware.yaml

You did it! Nice! Upload your new firmware into your wall plug: Time to put it to the test.

Check the results

Checking the results is probably the most logical thing to do next. Keep the wall plug plugged in into the power meter, since well, you want to verify it, right? Plug an appliance into the wall plug and compare the output of ESPHome with the power meter. Alternatively, you can also add it to Home Assistant and watch the sensors from there.

Image showing the result of the wall plug calibration. Both the power recorder and Home Assistant show almost the same value.

In my case: Perfect! Amazing, isn’t it? Expect some small deviations, but you should be pretty close. However, if the deviation is too significant for your taste, you’ll have to start over again. Be sure you are using stable appliances for the reference measurements and take a good look at your screenshots if you can find the measurement that caused the deviation.

The final product

If all went according to plan, you should have ended up with a pretty decent calibrated wall plug! I’m sure you’re with me on this one: Time to start monitoring your energy usage and automating things 😬.

During writing this blog, I received an question on social media:

Frenck! Why calibrate them all? Once you have one calibrated, you can use the same profile on all the plugs, right?”

True! You could save yourself a lot of time since, in theory, you can reuse the mapping with all devices of the same brand and type. However, during my testing, I noticed there were small deviations visible. You guessed it… I decided to calibrate each plug individually.

One more thing, you could consider extending the firmware, you’ve just created, with more features. Consider adding:

  • API & OTA passwords for more security
  • Time synchronization with Home Assistant
  • Sensors for:
    • Current running ESPHome version
    • The IP address of your devices
    • The WiFi BSSID & SSID it is connected with
    • WiFi signal strength
    • Uptime sensor
    • Daily used energy
  • A switch to restart your device with

If you want to find out what my final version looks like, be sure to take a look at: final_firmware.yaml

../Frenck

This blog was inspired by the “10$ WiFi Energy plug meters with ESPhome” YouTube series by Intermit.Tech.

About the author

Franck Nijhof

A Human. Being. Him/He. Dad. Open Sourcerer. Home Assistant developer. Passionate. Dedicated. Life motto: Make a positive difference every single day❤️ Be the change.

35 comments

Leave a Reply

  • Excellent post. One of the things that puts me off ESPHome is that the yaml to use is so impenetrable and examples seem to be few and far between. I’d love a proper community to ask the questions on.

  • Hello,
    I am intending to move the Blitzwolf BW-SPH6 to esphome and I am wondering where, or how, I can find follow info, used in your case:
    board: esp8285
    platform: hlw8012
    Thanks

  • Which you can open fairly easy with a solid rubber mallet. I have done that to 4 peaces without leaving a mark because tuya_convert doesn’t work on the newer devices.

  • I think it’s worth considering to solder out the relay if it’s not needed since it’s consumes 0.5W when it’s energized meaning if you have let’s say 4 x switches on just to measure consumption you will end up paying for 17,52kW which where i come from is 6€ for a year unless you can gain that back and use the relay to stop appliances to spend even more that you gain by removing the relay. I wish there was a small device just to measure power since personally i don’t need the relay part. You could even go further by enabling deep sleep so the ESP is only awake every 10sec or so to send data since the ESP alone consumes 0.5w without the relay.

  • Hi there, I’d like, first of all, to thank you so much for all the effort you put in HomeAssistant and for all the awesome Hass.io plugin you’ve developed so far!

    I’ve decided to share my experience after feeling so inspired by both you and Adries (Quindorian of Intermittent Technology).

    I’m italian but I’ve been living in Ireland for 1.5 years so I got the UK plug for my home. After successfully flashing a Tasmota firmware with the tuya-convert method, I tried to flash ESPHome. Unfortunately the schematic is different from the EU version: in fact the ones that I’ve got are Teckin SP23 which are clones of BlitzWolf SHP2 and thanks to https://github.com/arendst/Sonoff-Tasmota/blob/193b986a01aa18bed730fb6a4e013066d2727cd4/REFERENCE.md I was able to change the yaml.

    Btw https://blakadder.github.io/templates/index.html is also a good start if you want to look for some accessory which compatible with Tasmota and basically with ESPHome also.

    Thanks!

  • Franck, thank you for this calibration. My BSD29 It is working accurately. Can I check how can I disable the logs that come in ESPHome? I have kept the logger in DEBUG mode in yaml file. All the indicators for Power, Current and Voltage are still showing up in the logs. Can I somehow disable the logs but get the sensor values only in the lovelace frontend ?

  • Great tutorial Frank! I am going to give a try today for the tuya based plugs that I have.

    Question : How did you figure out these are the GPIOs that are attached to sel pin, CF & CF_1
    – platform: hlw8012
    sel_pin:
    number: GPIO12
    inverted: true
    cf_pin: GPIO04
    cf1_pin: GPIO05

    Please clarify so that I can identify the GPIOs for my plugs & proceed as per your tutorial
    Thanks in advance.

  • Thanks a lot Frenk for sharing this! I’m using four Gosund SP112 with Tasmota, tuya-convert worked great. Now i tried to flash your “final_firmware.yaml” which results in constant and very fast On/Off-switching of the plug…only solution is to turn it off.. Did you recognize a similar behaviour? Thanks in advance!

  • For everyone looking for well working devices without opening them, well you could as the still have screws 😉
    Living in Germany and purchased via the big Ama…
    The following devices worked for me:
    Gosund SP1 for 16A (which is normally useless as per DIN the normal cables 1,5mm are only allowed to power 10A)

    Avatar AWP07L 10A (different GPIO config, google TASMOTA AVATAR for that)

    Gosund SP111 10A with buitiful Design, (different GPIO config, google TASMOTA GOSUND SP111 for that)

    The teckin stuff (SP22 / SP23) did not work and have an RTL Chip installed, so reflashing is not possible by TUYA convert.

  • Correct me, if I’m wrong, but the lambda function of the current sensor in the example above seems odd. With the substaction as in your example, a slightly negative current is returned if there is no load on the plug.

    I think instead of “(0.01 – 0.013)” it should be “(0.01 + 0.013)”, similar to the lambda func for the power sensor:

    lambda: if (x < (0.01 + 0.013)) return 0; else return (x – 0.013);

  • Realy great, just started with home assistant (came from Domoticz wich i ran more than 6 years)
    and used your methode to calibrate my new plugs and it worked flawless.

    thaNks to Quindor of https://blog.quindorian.org/ i adjusted the YAML slightly and added

    substitutions:
    plug_name: Whatever name you want

    and named the sensors:

    current:
    name: “${plug_name}_current”

    so they can be found moer easy bij name and it made more easy to configure multiple plugs.

  • Hi Franck,

    I want to point out that the EU version has changed. the TECKIN SP22’s have a different chipset now. theyre based on a quallcomm or something like that? they went off ESP8266. I bought my switches a month ago and unfortunately I can’t flash them anymore.

  • +1 for this, I see it exactly as Andreas describs. There will never be negative output from the sensor itself.

    By the way excellent post. This is by far the best approach of calibrating smart plugs. I’ve never given much to this, but since stating with automations by power behavior it has become more intersting and important to have more accurae values.

  • Hi Frenck,

    I would like to make a small contribution to your post, I found the equivalent product in Australia to do this, they are the Brilliant Smart Plugs with Energy Monitoring, available at Officeworks. Link: https://www.officeworks.com.au/shop/officeworks/p/brilliant-lighting-smart-wifi-plug-with-energy-monitoring-bl20925
    I was able to flash esphome with Tuya Convert and everything works flawlessly. Please feel free to add this information to your post.

    GPIO pins are as follows:

    #GPIO3 – Button
    #GPIO4 – Energy Monitoring
    #GPIO5 – Energy Monitoring
    #GPIO13 – Blue LED
    #GPIO14 – Relay + Red LED
    #GPIO12 – Energy Monitoring

    Also as a cool idea, I flashed some plugs so that restore mode is DEFAULT_ON and some are DEFAULT_OFF. In order to easily identify the behaviour in Home Assistant without having to reboot them, I’ve added the following sensor as part of their config:

    substitutions:
    plug_name: pem_bsm_08
    plug_restore_mode: “ALWAYS_ON”

    text_sensor:
    – platform: template
    name: ${plug_name}_restore_mode
    lambda: |-
    return {“${plug_restore_mode}”};

    That’s my 2c.

    Cheers!

  • Great content; the Gosund European plugs currently (June 2020) sold on Amazon.de still work great. And now they’re calibrated too!

    Quick side note on your “this is what you can do with them” list: never turn of your ventilation; it’s essential for a healthy living environment. And check the power usage of your boiler; mine is cheaper to leave on and maintain heat, than it is to switch off and reheat from scratch periodically 🙂

  • I’ve got myself some AVATAR AWP07L which I flashed with Tasmota via tuya_convert. After I saw this blog post I wanted to switch to ESPHome since I find it’s more stable.

    But I can’t for the life of me properly calibrate these plugs, the values are fluctuating heavily.. On low powered devices like an 8W LED lamp and a 40W incandescent bulb the values are off by around 2-3W, my 1900W water cooker on the other hand shows more or less proper values.

    But since this post is almost a year old, isn’t there anything out there to make this calibration easier and less time consuming? I’ve got 16 of these plugs and I’ve sat 2 hours on a single plug which in the end, doesn’t even show the proper values.
    Especially voltage is all over the place, my power meter shows 230.1V where as my plug shows values between 594 and 603 for this voltage at random, how can I be sure which values are correct?

    With Tasmota I could simply use a UserScript someone wrote for autocalibration and the results were pretty accurate, sometimes they were off around 0.5W but mostly the calibration was accurate, but with ESPHome I can’t seem to get the values straight.
    I don’t really wanna go back to Tasmota..

  • Thanks for this tutorial.
    I have one question. This sockets with stock firmware are calibrated? Can I buy 2 this sockets, flash one and calibrate it with other an then repeat it for another?

  • Hi Frenck,
    I came along your post, and ordered some gosund plugs to play around. I am now wondering why the power consumption needs to be measured and calibrated if the powerconsumption is voltage multiplied by current? wouldn’t it be easier to just measuere the voltage and current accuratly and let the power be calculated based on that? Is this even possible with esphome or the plug itself? In my case I have a multimeter setup and I do not get the wattage anyhow, so I would do the calculation to but the data in, but esphome should be capable of doing this anyhow.

    Thanks for all your work and would be interesting whats your idea on this.

  • Franck/Frenck,
    A suggestion, in order to make these device to work/integrate with the Energy Dashboard in HA , you may need to add the

    state_class: measurement

    Have fun, Yann

Franck Nijhof

A Human. Being. Him/He. Dad. Open Sourcerer. Home Assistant developer. Passionate. Dedicated. Life motto: Make a positive difference every single day❤️ Be the change.

Follow Me