EFUN SH331W Smart Plug ESPHome Configuration file


I am new to Home assistant, wanted to convert my smart plug to ESPHome plug but I didn’t find any “premade” configuration for it.

Here the amazon link if you want to buy them :

It’s based on a ESP8266-S3 ( which is the one made for slotted PCB I believe ).

After reading this page :

I have tweaked it to add the ESP blue led and the proper red led integration.


#Configuration for EFUN SH3331W Smart Plug

  plug_name: esun_plug
  # Higher value gives lower watt readout
  current_res: "0.00221"
  # Lower value gives lower voltage readout
  voltage_div: "940"

  name: ${plug_name}
  platform: ESP8266
  board: esp01_1m

  ssid: "SSID"
  password: "PASS"

  # Enable fallback hotspot (captive portal) in case wifi connection fails
    ssid: "Esun Plug Fallback Hotspot"
    password: "1J01nevV4P5p"


# Enable logging

# Enable Home Assistant API
  password: "PASS"

  password: "PASS"

  - platform: homeassistant
    id: homeassistant_time
  - platform: gpio
      number: GPIO13
      inverted: True
    name: "${plug_name}_button"
        - switch.toggle: "${plug_name}_Relay"
        - switch.toggle: "${plug_name}_LED_Blue"

  - 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}_ESP_LED"
    pin: GPIO4
    inverted: True
    restore_mode: ALWAYS_ON
  - platform: gpio
    name: "${plug_name}_LED_Red"
    pin: GPIO0
    inverted: True
    restore_mode: ALWAYS_OFF

  - platform: hlw8012
      number: GPIO12
      inverted: True
    cf_pin: GPIO05
    cf1_pin: GPIO14
    current_resistor: ${current_res}
    voltage_divider: ${voltage_div}
      name: "${plug_name}_Amperage"
      unit_of_measurement: A
      name: "${plug_name}_Voltage"
      unit_of_measurement: V
      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"
        # 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

Thanks for the config! Replying here in Dec 2020 to say Tuya convert still works on some recently bought SH331-s from Amazon.

Any suggestions how best to calibrate a device when given known current and power are measured at multiple points?

I only see the config for a gain (multiplier ) in the current setup.
I don’t know where this value comes from. Do you have data on the current curve ?
( At this moment the plug is used to monitor the christmas tree, so the current consumption isn’t that important :stuck_out_tongue: )

For reference if you need to hack the plug and remove the whole back :

I’m trying to calibrate with a 30W soldering iron that has a rock-solid 30W resistive load that produces a power factor of 1.00.

Yet the numbers coming out the SH331 don’t seem to make sense!

  • amperage: 0.145 A
  • voltage: 133 V
  • wattage: 33.9 W

But 133 V * 0.145 A = 19.3 VA, which is way off the wattage for a resistive load. Any idea what could be going on?

By adjusting current_resistor and voltage_divider I could calibrate two of the three quantities, but not all three. The power and voltage (or current) are computed separately: https://github.com/esphome/esphome/blob/ac15ce576b1c22e59a7186014de0202e79de959b/esphome/components/hlw8012/hlw8012.cpp#L55

Could there be anything else different in these chips that might need tweaking?

Note: The HLW8012 library that Esphome links to has apparently separate multipliers for the three quantities: https://github.com/xoseperez/hlw8012/blob/2dace562c339e77a2889446d461e8e56048fe634/src/HLW8012.cpp#L153