Lonsonho Energy Monitoring Smart Plug

Recently bought one of these plugs and managed to get the relay and LED lights to toggle with ESPHome:

Turns out this board has a BL0937 chip for energy monitoring. Unfortunately though I am struggling to get the energy monitoring part reading correctly. It is just showing random numbers. I am assuming I am getting the SEL, CF and CF1 pins wrong.
Does anyone know the best way to troubleshoot these and how to determine which GPIO number is SEL, CF and CF1?


esphome:
  name: socket_monitoring
  platform: ESP8266
  board: esp01_1m

wifi:
  ssid: "ssid"
  password: "pass"

logger:

api:

ota:

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO0
      mode: INPUT_PULLUP
      inverted: true
    name: "Power Button"
    on_press:
      - switch.toggle: relay

  - platform: status
    name: "Status"

switch:
  - platform: gpio
    id: blue_led
    pin:
      number: GPIO13
      inverted: true
      
  - platform: gpio
    id: red_led
    pin:
      number: GPIO16
      inverted: true

  - platform: gpio
    name: "Socket Monitoring Switch"
    pin:
      number: GPIO14
    id: relay

    on_turn_on:
      - switch.turn_on: blue_led
      - switch.turn_off: red_led
    on_turn_off:
      - switch.turn_off: blue_led
      - switch.turn_on: red_led

sensor:      
  - platform: hlw8012
    sel_pin: 
      number: GPIO12
      inverted: True
    cf_pin: GPIO5
    cf1_pin: GPIO4
    current_resistor: 0.0025
    current:
      name: "socket current"
      unit_of_measurement: A
      id: socket_my_amps
    voltage:
      name: "socket voltage"
      unit_of_measurement: V
      id: socket_my_voltage
    power:
      name: "socket power"
      id: socket_my_power
      unit_of_measurement: W
    change_mode_every: 1
    update_interval: 5s
      

image

Managed to get he correct GPIO pins just by trial and error. When I seen a value that was variable when turning on my known load (floodlight in my case) I knew it was power (as voltage and current are on the same GPIO, switched by SEL)

I started trying to calibrate with current_resistor and voltage_divider but was not getting consistent values. So in the end I used a filter in the ESPHome sensor component calibrate_linear

Hooked up a known load and my watt meter to measure the values, inputted them in voila, done deal.

For anyone that stumbles on this post with the same switch, here is my final config. Note your calibration values will be different so you will have to measure them yourself


esphome:
  name: socket_monitoring
  platform: ESP8266
  board: esp01_1m

wifi:
  ssid: "ssid"
  password: "password"

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO0
      mode: INPUT_PULLUP
      inverted: true
    name: "Power Button"
    on_press:
      - switch.toggle: relay

  - platform: status
    name: "Status"

switch:
  - platform: gpio
    id: blue_led
    pin:
      number: GPIO13
      inverted: true
      
  - platform: gpio
    id: red_led
    pin:
      number: GPIO16
      inverted: true

  - platform: gpio
    name: "Socket Monitoring Switch"
    pin:
      number: GPIO14
   #   inverted: true
    id: relay

    on_turn_on:
      - switch.turn_on: blue_led
      - switch.turn_off: red_led
    on_turn_off:
      - switch.turn_off: blue_led
      - switch.turn_on: red_led

sensor:      
  - platform: hlw8012
    sel_pin: 
      number: GPIO12
      inverted: True
    cf_pin: GPIO4
    cf1_pin: GPIO5
    voltage_divider: 763
    current:
      name: "socket current"
      unit_of_measurement: A
      id: socket_my_amps
      accuracy_decimals: 2
      filters:
        - calibrate_linear:
          # Map 0.0 (from sensor) to 0.0 (true value)
          - 0.0 -> 0.0
          - 3.8 -> 2.08
          - 5.4 -> 2.95
    voltage:
      name: "socket voltage"
      unit_of_measurement: V
      id: socket_my_voltage
    power:
      name: "socket power"
      id: socket_my_power
      unit_of_measurement: W
      filters:
        - calibrate_linear:
          # Map 0.0 (from sensor) to 0.0 (true value)
          - 0.0 -> 0.0
          - 1689 -> 506
          - 2370 -> 708
    change_mode_every: 1
    update_interval: 5s