Home Assistant: 10$ WiFi energy meter with ESPhome

Ever since we’ve moved to our new house we have been having trouble keeping our energy usage in check. To combat this problem I’ve bought a bunch of 10$ energy meters which can be flashed without any soldering to the awesome ESPhome firmware!

This guide will show you how to do this and how to get everything setup in Home Assistant and Grafana!

Update 2020-01-25

  • I have been receiving reports that the Teckin plugs sold in the US now contain an incompatible Realtek chipset. Best will be to get a different Tuya based brand!

Update 2019-06 “Preventative measures”

  • Tuya, the manufacturer of these plugs has taken some preventative measures that make the below mentioned OTA flashing method impossible. That means you can’t use the OTA method anymore and you have to resort to soldering serial leads and flashing that way.
    This takes a little bit more work but once converted, you can do with the plug whatever you want. This still makes everything discussed in this article viable and a great and cheap hardware solution, it’s just a little bit harder to achieve
  • Since releasing this video and article a new method of calibrating these plugs has become available within ESPhome, this is easier to use and delivers a better (more accurate) result in the end! I have no updated this article for this (this method still works) but Frenck, a colleague has made an excellent article how to use this new method, check it out on his website!

Post linked with Video

Although this is a written guide it’s meant to be used in conjunction with the video I made. So make sure to watch that too, in that video I will refer to text blocks which can be found here in this article.

 

 

 

 

 Shopping links

If you want to buy any of these energy meters I would appreciate it very much if you used any of these (affiliated) shopping links! 🙂

Energy meters:
EU Style: https://geni.us/eSgZi
UK Style: https://geni.us/1DTCc
US Style: https://geni.us/yUqtLDb

–2020-04 I have getting reports the Teckin versions might sometimes have different hardware now (Realtek chipset), please use links to go to Amazon and select a different Tuya ESP based model!

–2020-12 Some version are still flashable, some are not, it’s becoming harder and harder to identify.

On Aliexpress, this seller is selling versions pre-flashed to Tasmota, so those can easily be used that way or flashed over to ESPhome (as I show in my guides).

Raspberry Pi 3 B+: https://geni.us/WAq4p

Cheap Compatible WiFi dongle: https://geni.us/e2oGA
(Works for PC or Raspberry Pi without WiFi)

PR10-C Power Meter: http://geni.us/h8kokVq

Text Blocks

As mentioned above, the following text blocks are for if you want to follow along with the video!

Contents of “temp_flash”

esphomeyaml:
  name: temp_flash
  platform: ESP8266
  board: esp8285

wifi:
  ssid: 'MYWIFI'
  password: 'MYPASSWORD'

# Enable logging
logger:

# Enable Web server
web_server:
  port: 80

# Enable Home Assistant API
api:
  password: 'MYPASSWORD'

ota:
  password: 'MYPASSWORD'

Make sure to fill in your own WiFi SSID, password and passwords for API and OTA! We are going to need these later on!

#Installing Git
sudo apt install git

# Using Git to pull down software
git clone https://github.com/ct-Open-Source/tuya-convert

# Changing directory
cd tuya-convert

# Running software install script
./install_prereq.sh

# Installing aptitude package manager (optional)
sudo apt install aptitude

# Fixing broken NPM install (optional)
sudo aptitude install npm


Now that we have all the software we need, we need to find the names the operation system has given our network cards. This differs per Linux version, in my case I’m using Ubuntu 18.10 which renames adapters.

# Command to find current Wireless Adapter name
dmesg | grep wlan

# Command to find current Wireless Adapter name
dmesg | grep eth

# Enter into config.txt file
nano config.txt

# After pasting in values
CTRL + X, yes to save

Pay attention! In my case the adapters where renamed to different names so I had to change them in the config.txt file. If your adapters are called “wlan0” and “eth0” you don’t need to change the config.txt file since the default values are correct.

My .bin file is not on my flashing machine

If your file is not on the machine you are running the flash software on (for instance you are using a Raspberry Pi with a clean raspbian install) you need to transfer it over. The easiest way to do this is by downloading the file on your desktop as instructed in the video and then running a program called “WinSCP” or “FileZilla Client”. With it you can transfer files over an SSH connection and transfer the file over the Linux PC.

Once you have the file in place in the “files” directory we we can start the flash script.

# Starting flash program
./start_flash.sh

Once that is running and you’ve entered “yes”, open a second terminal window or SSH session and run the following to see the “wifi” log file.

# Changing directories
cd tuya-convert
cd scripts

# Tail log file
tail -f smarthack-wifi.log

Problem with Port 53 being used already, stopping that program

# Stop systemd-resolved
sudo systemctl stop systemd-resolved

After this startup the software again and follow the instructions. If more things go wrong or nothing seems to happen make sure to take a close look at the other log files also located in the “scripts” directory, they can give you valuable information what might be wrong.

 

Second video

In this video we are going to upload the actual config we are going to use in ESPhome, calibrate the energy meter and then make it available in Home Assistant and build a simple graph, if you want to follow along, take a look at the video and find all text fields you might need below! 🙂

 

 

 

 

Full ESPhome config

This will be part of the second video where we will flash the full config on the now ESPhome running smart-plug and link it to Home Assistant.

Use CTRL-F to find and replace “energy_temp” with unique values for this specific meter.

–update There is updated code in which you don’t have to replace all the values yourself, check down below for “updated code”

esphomeyaml:
  name: energy_temp
  platform: ESP8266
  board: esp8285

wifi:
  ssid: 'SSIDSSIDSSID'
  password: 'WIFIPASSWORD'

# Enable logging
logger:

# Enable Web server
web_server:
  port: 80

# Enable Home Assistant API
api:
  password: 'PASSWORD'

ota:
  password: 'PASSWORD'

time:
  - platform: homeassistant
    id: homeassistant_time
    
binary_sensor:
  - platform: gpio
    pin:
      number: GPIO3
      inverted: True
    name: "energy_temp_button"
    
    
switch:
- platform: gpio
  name: "energy_temp_Relay"
  pin: GPIO14
  restore_mode: ALWAYS_ON

- platform: gpio
  name: "energy_temp_LED_Blue"
  pin: GPIO1
  inverted: True
  restore_mode: ALWAYS_OFF

- platform: gpio
  name: "energy_temp_LED_Red"
  pin: GPIO13
  inverted: True
  restore_mode: ALWAYS_OFF

sensor:
  - platform: hlw8012
    sel_pin:
      number: GPIO12
      inverted: True
    cf_pin: GPIO04
    cf1_pin: GPIO05
    # Higher value gives lower watt readout
    current_resistor: 0.00221
    # Lower value gives lower voltage readout
    voltage_divider: 871
    current:
      name: "energy_temp_Amperage"
      unit_of_measurement: A
    voltage:
      name: "energy_temp_Voltage"
      unit_of_measurement: V
    power:
      name: "energy_temp_Wattage"
      unit_of_measurement: W
      id: "energy_temp_Wattage"
    change_mode_every: 8
    update_interval: 10s
  - platform: total_daily_energy
    name: "energy_temp_Total Daily Energy"
    power_id: "energy_temp_Wattage"
    filters:
        # Multiplication factor from W to kW is 0.001
        - multiply: 0.001
    unit_of_measurement: kWh
      
# Extra sensor to keep track of plug uptime
  - platform: uptime
    name: energy_temp_Uptime Sensor

!There is updated code lower on this page, go and copy that instead of what is above this text!

Calibrating the meter

Once that is uploaded and active I advise you to do some calibration with an external meter. I recommend using the following meters for that, these are quite a bit more expensive but come with an easy to read and responsive screen and it has a factory good factory installed auto-adjusting calibration!

The calibration can be changed in the ESPhome code by varying the “current_resistor” and “voltage_divider” values. To get a higher wattage use a lower number for the “current_resistor”.  Voltage works in reverse, to get a higher voltage, use a higher number in “voltage_divider”!

Changes should be made with small numbers, so try raising or lowering the numbers by 5 to start with.

LoveLace Graph card

The Home Assistant lovelace card used is called “mini-graph-card” and can be found by clicking on the name!

animate: true
entities:
  - entity: sensor.energy_1_wattage
    show_state: true
  - entity: sensor.energy_2_wattage
    show_state: true
  - entity: sensor.energy_3_wattage
    show_state: true
hour24: true
hours_to_show: 12
labels: true
legend: true
name: LED Lighting - 12 Hours
points_per_hour: 4
type: 'custom:mini-graph-card'

Updated ESPhome code

Someone commented on my videos that since a recent version of ESPhome it’s now possible to use “variables” or as they are named in ESPhome: substitutions

This makes replacing the number in all the separate metering plugs a lot easier since you can now just change the one substitution and it will change all the parts of the code for you! I’ve also done the same for the “current_resistor” and “voltage_divider” values so you can always edit them at the top!

substitutions:
  plug_name: energy_17
  # Higher value gives lower watt readout
  current_res: "0.00221"
  # Lower value gives lower voltage readout
  voltage_div: "955"

esphome:
  name: ${plug_name}
  platform: ESP8266
  board: esp8285

wifi:
  ssid: 'SSIDSSIDSSID'
  password: 'WIFIPASSWORD'

# Enable logging
logger:

# Enable Web server
web_server:
  port: 80

# Enable Home Assistant API
api:
  password: 'PASSWORD'

ota:
  password: 'PASSWORD'

time:
  - platform: homeassistant
    id: homeassistant_time
    
binary_sensor:
  - platform: gpio
    pin:
      number: GPIO3
      inverted: True
    name: "${plug_name}_button"
    
switch:
- platform: gpio
  name: "${plug_name}_Relay"
  pin: GPIO14
  restore_mode: ALWAYS_ON
- platform: gpio
  name: "${plug_name}_LED_Blue"
  pin: GPIO1
  inverted: True
  restore_mode: ALWAYS_OFF
- platform: gpio
  name: "${plug_name}_LED_Red"
  pin: GPIO13
  inverted: True
  restore_mode: ALWAYS_OFF
sensor:
  - platform: hlw8012
    sel_pin:
      number: GPIO12
      inverted: True
    cf_pin: GPIO04
    cf1_pin: GPIO05
    current_resistor: ${current_res}
    voltage_divider: ${voltage_div}
    current:
      name: "${plug_name}_Amperage"
      unit_of_measurement: A
    voltage:
      name: "${plug_name}_Voltage"
      unit_of_measurement: V
    power:
      name: "${plug_name}_Wattage"
      unit_of_measurement: W
      id: "${plug_name}_Wattage"
    change_mode_every: 8
    update_interval: 10s
  - platform: total_daily_energy
    name: "${plug_name}_Total Daily Energy"
    power_id: "${plug_name}_Wattage"
    filters:
        # Multiplication factor from W to kW is 0.001
        - multiply: 0.001
    unit_of_measurement: kWh
      
# Extra sensor to keep track of plug uptime
  - platform: uptime
    name: ${plug_name}_Uptime Sensor

Third video

The third video is about using Grafana to make some nice graphs about all the data we are now collecting in Home-Assistant.

This isn’t a full tutorial on how to setup Home-Assistant and have it send its data to InfluxDB and then then let Grafana use that data. Lots of how-to’s have been written for that, so check one of those if you don’t have this setup yet. Or watch the video below and then decide if you want to set this up.

 

 

 

 

Fourth video

The fourth and last video in this series (for now) is going to be how we were able to lower our power usage because of the insight these plugs provided and some automations using Node-Red that can be configured to again, use less power and hopefully, in the end, make these plugs pay for themselves!

38 thoughts on “Home Assistant: 10$ WiFi energy meter with ESPhome”

  1. For people purchasing the Teckin SP22 from Amazon.de, I found that the pinout is different from the one in the configuration above here:

    Button: GPIO1
    Relay: GPIO4
    LED Blue: GPIO13
    LED RED: GPIO3

  2. Has anyone got the Teckin SP23 working? According to Tasmota, “The Teckin SP23 appears to be a clone of the BlitzWolf SHP2”, but I don’t see that listed as an option on ESPHome. Using GPIO15 lets the relay work, but the sensors are all wrong, and the button doesn’t work

  3. First of all. Thanks for the great video’s. I bought 2 of these plugs after watching them : )

    However, the energy_XX_Total Daily Energy in the plug web interface seems to be different from the total usage in Grafana, using the integral and cumulative_sum, like you explained in your video. I can’t figure out why this is. Any thoughts?

    1. Interesting, I haven’t noticed a difference. I’ll take a look to see if that’s the case for me too. I mostly look at the Grafana graphs and those seem correct with what I would expect. 🙂

    2. Do you perhaps have periods where your device uses 0 Watt? There seems to be an issue with calculating the integral on those periods, as InfluxDB has no data at those moments.

      Currently trying to figure out why that is. If it is ESPHome not sending data to HA when it is 0, or if HA does not transfer it to InfluxDB.

      I get strange upwards lines in my graph at periods of no usage. fill(0) in the grouping should help, but doesn’t for me

  4. Well, to answer my own question, here’s what I’ve done so far. Evereything works (including the button press) except for the red LED, maybe someone could figure that out?

    substitutions:
    plug_name: teckin_plug
    # Higher value gives lower watt readout
    current_res: “0.00221”
    # Lower value gives lower voltage readout
    voltage_div: “940”
    esphome:
    name: ${plug_name}
    platform: ESP8266
    board: esp8285
    wifi:
    ssid: ‘SSID’
    password: ‘password’
    # Enable logging
    logger:
    # Enable Web server
    web_server:
    port: 80
    # Enable Home Assistant API
    api:
    password: ‘PASSWORD’
    ota:
    password: ‘PASSWORD’
    time:
    – platform: homeassistant
    id: homeassistant_time

    binary_sensor:
    – platform: gpio
    pin:
    number: GPIO13
    inverted: True
    name: “${plug_name}_button”
    on_press:
    then:
    – switch.toggle: “${plug_name}_Relay”
    – switch.toggle: “${plug_name}_LED_Blue”
    switch:
    – platform: gpio
    name: “${plug_name}_Relay”
    id: “${plug_name}_Relay”
    pin: GPIO15
    restore_mode: ALWAYS_ON
    – platform: gpio
    name: “${plug_name}_LED_Blue”
    id: “${plug_name}_LED_Blue”
    pin: GPIO2
    inverted: True
    restore_mode: ALWAYS_ON
    – platform: gpio
    name: “${plug_name}_LED_Red”
    pin: GPIO4
    inverted: True
    restore_mode: ALWAYS_OFF
    sensor:
    – platform: hlw8012
    sel_pin:
    number: GPIO12
    inverted: True
    cf_pin: GPIO05
    cf1_pin: GPIO14
    current_resistor: ${current_res}
    voltage_divider: ${voltage_div}
    current:
    name: “${plug_name}_Amperage”
    unit_of_measurement: A
    voltage:
    name: “${plug_name}_Voltage”
    unit_of_measurement: V
    power:
    name: “${plug_name}_Wattage”
    unit_of_measurement: W
    id: “${plug_name}_Wattage”
    change_mode_every: 8
    update_interval: 10s
    – platform: total_daily_energy
    name: “${plug_name}_Total Daily Energy”
    power_id: “${plug_name}_Wattage”
    filters:
    # Multiplication factor from W to kW is 0.001
    – multiply: 0.001
    unit_of_measurement: kWh

    # Extra sensor to keep track of plug uptime
    – platform: uptime
    name: ${plug_name}_Uptime Sensor

  5. Can you explain if the complete git clone needs to be transferred to the root map of the raspberry pi? the map tuya-convert is now on the raspberry only the command ”/install_prereq.sh” can’t exclude. there is something that i do wrong on that part.

  6. Awesome vid! I immediately bought several Blitzwolf BW-SHP2 via the AliExpress the affiliate link as a token of appriciation and for adding them to my home automation setup of course.

    However, these now seem to be shipped with updated firmware that is not yet supported by tuya-convert.
    Installation on a RPI3 B. Logs show no errors, but the communication is never started.

    I noticed an issue was already opened on the Tuya-Convert project page. New buyers might want to back off until a fix is found and released.

    1. I find it hard pressed that you got one with updated firmware, it’s going to take weeks if not months before those are out of the factory. Did you by chance try the plug in the Tuya App? Because then yes, they try and prevent this method!

  7. Any ideas appreciated!
    I bought 4 TP23 (via provided link) and successfully flashed them. When I look at the outputs though…
    2.0 Amps 248.1 Volts 914.5 Watts Meter 2.10 Amps
    1.1 Amps 245.7 Volts 516 Watts Meter: 2.11Amps
    1.2 Amps 245.1 Volts 529.5 Watts Meter: 2.11 Amps
    1.2 Amps 252.8 Volts 542.4 Watts Meter 2.11 Amps
    This is a pure resistive (500Watt-ish) load and the Meter is an in-line ammeter
    The first I “adjusted” the resistor value to get the right current – power now wrong. The rest are pre-adjustment – current wrong.
    Config is as suggested by Pat – I’ve taken one apart and checked the signals and it seems correct.

  8. Vanilla bw-shp6 from factory not working with tuya convert. Didn’t use the app, solder method worked. Even replaced the antenna with a external one for better coverage.

  9. Hi i got the SP1-C_V2.4 Blitzwolf BW-SHP2
    Flashed it with your instructions. Just the button is not working.
    By any chance do you know the new GPIO for this board? Thanks

    1. I’m not fully sure what module you have, I show the EU Gosund version. Check the tuya-flash github page, they have a wiki with the pinouts for lots of different models!

    1. That I don’t know, if you can flash something to the plugs that support Homekit, then it can work, other then that, I don’t kow.

  10. Does your “Update 2019-06 “Preventative measures” ” mean I can’t flash any plug with tuya convert anymore? Or just the ones sold now.

    I’ve got one that I purchased december 2018 and never connected to the app. Will it work?

    1. It’s only for new ones that are made in the factory right now or you connect to the app. Older stock (so also if you’ve already had it for a while) will work just fine. 🙂

    1. No updates right now, but the 2.0 method should work on plugs that have the “blocked” firmware! When I have time I’ll try and and make an updated video about it!

  11. Thanks for this post!
    I recently ordered a Blitzwolff BW-SHP6 and was able to follow your instructions including OTA with tuya-convert. There were some slight variations (especially the menu while flashing) but in general it worked very well.

    For those who also have a BW-SHP6 (instead of the BW-SHP2!) you could look at these configurations:
    https://community.home-assistant.io/t/esphome-blitzwolf-bw-shp6-configuration/113938/2

    Also for those who can give a static IP but face issues with DNS (like it stays temp_flash.local), you can change the name:
    https://esphome.io/components/esphome.html#esphome-changing-node-name

  12. Hi Guys,
    I got the following working with one of the latest Blitzwolf BW-SPH2 ( SPC1 – V2.4 on the printboard )
    The button on the top works. And the led turns red as soon as the relay is switched on

    time:
    – platform: homeassistant
    id: homeassistant_time

    binary_sensor:
    – platform: gpio
    name: ${plug_name}_button
    pin:
    number: GPIO3
    mode: INPUT_PULLUP
    inverted: True
    on_press:
    – switch.toggle: relay
    – platform: status
    name: ${plug_name}_Status

    switch:
    – platform: gpio
    id: relay
    name: “${plug_name}_Relay”
    pin: GPIO14
    restore_mode: ALWAYS_ON
    on_turn_on:
    – switch.turn_on: red_led
    on_turn_off:
    – switch.turn_off: red_led

    – platform: gpio
    id: blue_led
    name: “${plug_name}_LED_Blue”
    pin: GPIO1
    inverted: True
    restore_mode: ALWAYS_OFF
    – platform: gpio
    id: red_led
    name: “${plug_name}_LED_Red”
    pin: GPIO13
    inverted: True
    restore_mode: ALWAYS_ON
    sensor:
    – platform: hlw8012
    sel_pin:
    number: GPIO12
    inverted: True
    cf_pin: GPIO04
    cf1_pin: GPIO05
    current_resistor: 0.00221
    voltage_divider: 871
    current:
    name: “${plug_name}_Amperage”
    unit_of_measurement: A
    voltage:
    name: “${plug_name}_Voltage”
    unit_of_measurement: V
    power:
    name: “${plug_name}_Wattage”
    unit_of_measurement: W
    id: “energie_wasmachine_Wattage”
    change_mode_every: 8
    update_interval: 10s
    – platform: total_daily_energy
    name: “${plug_name}_Total Daily Energy”
    power_id: “energie_wasmachine_Wattage”
    filters:
    # Multiplication factor from W to kW is 0.001
    – multiply: 0.001
    unit_of_measurement: kWh

    # Extra sensor to keep track of plug uptime
    – platform: uptime
    name: ${plug_name}_Uptime Sensor

  13. First of all, thx for the tutorial, helping me to manage the calibrating process. Changing the values, i could set up the voltage, and the wattage, but the current shows almost the half of the measured one. How can i set it up to the correct level? Thank you in advance for your help!!
    All ideas from anybody is more than welcome!!

  14. For anyone checking – got a pack of 4 Gosund Sp1 from amazon.de – worked very easy with tuya-convert or rpi3. esphome flashed and working good. Thanks OP for a detailed write up.

  15. Can confirm this is still working. Got 16 Gosund SP-1’s from Amazon.de and they all running ESPHome atm.

    Thanks Quindor for the extensive video and blogpost!

  16. Just to let you know that I got 4 Gosund SP-1 from Amazon.es and this tutorial is still valid.

    They are already running ESPHome.

    Thanks for the amazing job

Leave a Reply

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