Skip to content

EFUN SH331W Smart Plug

Charles Powell edited this page Aug 30, 2020 · 9 revisions

Product Details

10A, 1200W rating - but don't use it for anywhere near this much, as the plug is NOT UL listed.

Configuration

Note this configuration uses substitution, for some variables and for the ESPHome !secrets (requires a secrets.yaml file in the ESPHome directory). Or you can just replace the secret callouts with your values.

Calibration

The current_res and voltage_div values were calibrated using a Kill-a-Watt. The KaW was placed between the plug and the wall, with the load attached to the plug. A 90W bulb was used as a steady resistive load. Individual plugs will have variation on these values, and they can be adjusted as needed. Alternatively the calibrate_linear ESPHome filter can be used to further improve the calibration using multiple data points.

Configuration File

Configuration file, reproduced below. Note below may be out of date to the repo configuration file, but is provided for reference:

#Configuration for EFUN SH3331W Smart Plug

substitutions:
  plug_name: efun_plug1
  # Plug state to set upon powerup (or after power loss)
  # See options here: https://esphome.io/components/switch/gpio.html
  restore_mode: ALWAYS_ON
  
  # Base calibration to 90W lightbulb, Kill-a-Watt between plug and wall
  # Detail calibration can be done with calibrate_linear sensor filters below
  current_res: "0.00236"
  voltage_div: "878"
  # Increasing current_res reduces reported wattage
  # Increasing voltage_div increases reported voltage

esphome:
  name: ${plug_name}
  platform: ESP8266
  board: esp01_1m
  includes:
    - espsense.h
  # Uses the ESPAsyncUDP library
  libraries:
    - "ESPAsyncUDP"
    - "ArduinoJson-esphomelib@5.13.3"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_pass
  fast_connect: on

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "${plug_name} Fallback Hotspot"
    password: !secret ap_pass

ota:
  safe_mode: True
  password: !secret ota_pass

captive_portal:

# Enable logging
logger:
  # level: DEBUG
  baud_rate: 0 # Disable UART logging, we have no physical connections!

# Home Assistant API
# Comment out if not using API, but you'll also need to remove the total_daily_energy and
# time sensors below
api:

time:
  - platform: homeassistant
    id: homeassistant_time
    
binary_sensor:
  - platform: gpio
    pin:
      number: GPIO13
      inverted: True
    name: "${plug_name}_button"
    on_press:
      then:
        - switch.toggle: "relay"

switch:
  # Main plug control relay
  - platform: gpio
    name: "${plug_name} Relay"
    id: "relay"
    pin: GPIO15
    restore_mode: ${restore_mode}
    # Use blue LED to indicate relay state
    on_turn_on:
      - switch.turn_on: "led_blue"
    on_turn_off:
      - switch.turn_off: "led_blue"
    
  - platform: gpio
    name: "${plug_name} Blue LED"
    id: "led_blue"
    pin: GPIO2
    inverted: True
    restore_mode: ${restore_mode} # Matches relay
  
  # - platform: gpio
  #   name: "${plug_name}_LED_Red"
  #   pin: GPIO0
  #   inverted: True
  #   restore_mode: ALWAYS_OFF

status_led:
  # Use Red LED as ESPHome's built-in status indicator
  pin:
    number: GPIO0
    inverted: True

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
      filters:
        # - calibrate_linear:
        #   # Map X (from sensor) to Y (true value)
        #   # At least 2 data points required
        #   - 0.0 -> 0.0
        #   - 1.0 -> 1.0 #load was on
    voltage:
      name: "${plug_name} Voltage"
      unit_of_measurement: V
      filters:
        # - calibrate_linear:
        #   # Map X (from sensor) to Y (true value)
        #   # At least 2 data points required
        #   - 0.0 -> 0.0
        #   - 1.0 -> 1.0 #load was on
    power:
      id: "wattage"
      name: "${plug_name} Wattage"
      unit_of_measurement: W
      filters:
        # - calibrate_linear:
        #   # Map X (from sensor) to Y (true value)
        #   # At least 2 data points required
        #   - 0.0 -> 0.0
        #   - 1.0 -> 1.0 #load was on
    change_mode_every: 8
    update_interval: 3s
    
  - platform: total_daily_energy
    name: "${plug_name} Total Daily Energy"
    power_id: "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
    
custom_component:
- lambda: |-
    auto sensor_power = new ESPSense(id(wattage), 120);
    return {sensor_power};

Attribution

Base configuration provided by Eriobis on the Home Assistant forums