Flash Teckin Smart Plug for Home Assistant

・4 min read

I have been using Teckin smart plugs around the house for quite some time now. They’re really handy as they integrate with Google Home via the Smart Life app ecosystem. This means that each night we can tell Google to “turn everything off” and our lamps all switch off at the wall socket.

What I have wanted to do for some time, after seeing my friend do it with great success, was to flash the Teckin firmware to esphome so that I can control the sockets via Home Assistant and also get realtime values for the socket’s watt, amps and voltage loads. This would allow me to monitor the number of watts on things like the washing machine and then send myself a notification when the watts reduce after a specific period of time. So, when the washing machine has finished, shortly afterwards I’ll get a notification that it’s done. Simple.

The first step is to get the tuya-convert files.

# git clone https://github.com/ct-Open-Source/tuya-convert
# cd tuya-convert
# ./install_prereq.sh

Run ./start_flash.sh. It’s important to run this command and get the wifi network called vtrust-flash working on a phone or second computer before powering on the smartplug - as the plug will look for this SSID on boot. If you notice the plug is broadcasing an SSID of vtrust-recovery then this means the smartplug did not detect our flash attempt.

When the smartplug successfully connects to the flash SSID it will then transfer the flashing firmware and prompt for which version you want to load. The options are tasmota-wifiman.bin and espurna-base.bin. I have only used espurna-base.bin and it has worked perfectly.

Once the flash process is completed you can then end the flashing script by choosing N to flash another device.

Now we can connect to the smartplug on the new espurna-base.bin firmware by connecting to the SSID ESPURNA-XXXX with the password fibonacci. Once connected we need to go to the web interface which is at 192.168.4.1 and set a password. This password can be anything you want because it will be reset again shortly.

Now that the smart plug is running espruna and we know we can connect to the web interface, it’s time to create our own esphome firmware to upload.

We need esphome which is available via pip. I had to use sudo pip3 to get mine working.

Once esphome is installed (running esphome from your command prompt should display the available options, if it’s installed correctly) we need to create a yaml file with the required options.

Here’s the one I used, with thanks to Sam for giving me this file which worked perfectly first time.

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

wifi:
  ssid: 'YOUR HOME SSID HERE'
  password: 'YOUR HOME WIFI PASSWORD HERE'

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


# Enable logging
logger:

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

ota:
  password: 'RANDOM PASSWORD HERE'

time:
  - platform: homeassistant
    id: homeassistant_time

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO13
      inverted: True
    name: "${plug_name}_button"
    on_press:
      - switch.toggle: relay  # this interects with switch.relay on the device not via hass

switch:
- platform: gpio
  name: "${plug_name}_Relay"
  pin: GPIO15
  restore_mode: ALWAYS_ON
  id: relay #this line gives the entity an id so the teckin plug can do some onboard stuff - see button 
- platform: gpio
  name: "${plug_name}_LED_Blue"
  pin: GPIO2
  inverted: True
  restore_mode: ALWAYS_OFF


sensor:
  - platform: hlw8012
    sel_pin:
      number: GPIO12
      inverted: True
    cf_pin: GPIO05
    cf1_pin: GPIO014
    # # Higher value gives lower watt readout
    # current_resistor: ${current_res}
    # # Lower value gives lower voltage readout
    # voltage_divider: ${voltage_div}
    current:
      name: "${plug_name}_Amperage"
      unit_of_measurement: A
      accuracy_decimals: 3
      filters:
        # Map from sensor -> measured value
        - calibrate_linear:
            - 0.0 -> 0.008
            - 10.33324 -> 8.212


        - lambda: if (x < (0.01 - 0.008)) return 0; else return (x - 0.013);


    voltage:
      name: "${plug_name}_Voltage"
      unit_of_measurement: V
      filters:
        # Map from sensor -> measured value
        - calibrate_linear:
            - 0.0 -> 0.0
            - 640.98718 -> 241.0


    power:
      name: "${plug_name}_Wattage"
      unit_of_measurement: W
      id: "${plug_name}_Wattage"
      filters:
        # Map from sensor -> measured value
        - calibrate_linear:
            - 0.0 -> 0.6
            - 11640.70117 -> 1957


        # Make everything below 2W appear as just 0W.
        # Furthermore it corrects 1.14W for the power usage of the plug.
        - lambda: if (x < (2 + 0.6)) return 0; else return (x - 1.14);



    change_mode_every: 3
    update_interval: 5s

  - 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

Save the file as something sensible - I used teckin01.yaml and then run:

sudo esphome teckin01.yaml compile

This will create the firmware and store it in a directory that is not even remotely obvious…

/home/username/projects/tuya-convert/teckin01/.pioenvs/teckin04/firmware.bin

Now we just go to the web interface of the plug (192.168.4.1), go to Admin and then at the bottom of the page we upload this firmware image and we’re done.

The smart plug will reboot and will now be running esphome. Provided you used a unique name, the SSID and password are correct and Home Assistant is running - the new smart plug will pop up as a new device in home assistant.

Magic.

Note: I managed to get into a state of no response from one of the Teckin plugs when I was flashing a number of them at once. To rectify this I ran the following command:

sudo esphome teckin01.yaml upload

This command works because the smart plug is broadcasting a DNS name on the LAN of teckin01.local which the esphome program recognises - even if it doesn’t have a usable IP address. Worth remembering.