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!

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

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!

Please follow and like us:

19 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!

Leave a Reply

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