Migration power plug from Tasmota to ESPhome : BL0937 pinout

Hello,

I have two Tuya 20A Power Monitoring Plug (AJW-02_8285) on Tasmota that I would like to migrate to ESPHome. Theses plugs use a BL0937 chip for power monitoring and my problem is that I don’t know wich pins to set for CF1 and SEL since in Tasmota, I only have to set the CF pin. The hlw8012 esphome component seem required at least the 3 pins but Tasmota seem known how to works with only one. Their is a way to configure ESPhome hlw8012 component with only the CF pin ?

Thanks !

I know nothing about the plug you have … however I have 5 of “Arlec PC191HA smart plugs with Energy Monitoring” which I have converted some to ESPhome. They also use the BL0937 chip for power monitoring.

I don’t know if it will be relevant to you … I guess it depends on whether this describes the BL0937 chip itself or the hardware/firmware of my PC191HA devices. Anyway, the information I gleaned in research for my devices indicates:

  • CF1 pin monitors the POWER used
  • CF pin monitors CURRENT OR VOLTAGE
  • the SEL pin is used to indicate whether VOLTAGE or CURRENT should be reported by CF pin
  • ESPhome is able to toggle the SEL pin periodically to give you both values (though not at the same time)
  • CF and CF1 of BL0937 are opposite from HLW8012, so should be inverted
  • I believe the electrical formula is “Power (W) = Volts (V) * Current (A)” - however I was unable to find divider values which would return realistic values for all three values. I’m not too worried about accuracy - just want to know if the washing machine in the laundry downstairs has finished its cycle :wink: My work-around is to keep SEL set to VOLTAGE, and calculate Current from Power and Volts.

This is the part of my ESPhome yaml file relating to power monitoring. Hopefully it might also be relevant to you.

#
# PC191HA sensors - power monitoring
#
sensor:
    # PC191HA includes a BL0937 chip for measuring power consumption
    #     and BL0937 is a variation of hlw8012, but using inverted SEL pin functionality
  - platform: hlw8012
    model: BL0937     # note that the model must be specified to use special calculation parameters
    sel_pin:          # I believe that cf_pin reports either Voltage or Current depending on this select pin
      inverted: true  # determine whether true reports Voltage
      number: P24
    cf_pin:           # current or voltage (ele_pin: 7)
      inverted: true  # the logic of BL0937 is opposite from HLW8012
      number: P7
    cf1_pin:          #  Power (vi_pin: 8)
      inverted: true  # the logic of BL0937 is opposite from HLW8012
      number: P8
    update_interval: "30s"         # How often to measure and report values

    # PC191HA measures and returns Voltage OR Current according to the value of sel_pin, 
    #   but it can change the value of sel_pin periodically 
    initial_mode: "VOLTAGE"             # reports VOLTAGE or CURRENT
    change_mode_every: "4294967295"            # do NOT swap between reporting Volts or Amps (well, swap after 4000 years)
        #   reporting Voltage or Current. Note that the first value reported should be ignored as inaccurate

    # Adjust according to the actual resistor values on board to calibrate the specific unit
    voltage_divider:  "775"               # LOWER VALUE GIVES LOWER VOLTAGE
    current_resistor: "0.0009"          # HIGHER VALUE GIVES LOWER WATTAGE

# how the power monitoring values are returned to ESPHome
    voltage:
      name: $friendly_name Voltage
      id:   ${name}_voltage
      unit_of_measurement: V
      accuracy_decimals: 1
      filters:
        - skip_initial: 2
    power:
      name: $friendly_name Power
      id:   ${name}_power
      unit_of_measurement: W
      accuracy_decimals: 2
      filters:
        - skip_initial: 2

    # power is simply current x voltage -- except that the pc191ha doesn't follow that formula.
        # Setting current_resistor to give an accurate Amperage does NOT also give the correct Wattage
        # my work-around is to calculate current from power / voltage
  - platform: template  
    name: $friendly_name Current
    id:   ${name}_current
    unit_of_measurement: A
    accuracy_decimals: 2
    update_interval: "30s"
    lambda: |-
      return (id(${name}_power).state / id(${name}_voltage).state);
    filters:  
      - skip_initial: 5     # give time for data to settle to avoid NaN

Hey @donburch would you mind sharing your full ESPHome config for the PC191HA plug? I have cloud cut mine but struggling to figure out the correct pins etc…

I am most glad to, but may be more relevant in the Energy consumption and Arlec Grid Connect (Tuya) smart plug - #71 by donburch888 thread.

1 Like

With some last hints about the correct pins found in the example from @donburch888 I was able to get my similar smartplug to work and report the energy (need to calibrate but getting values now :slight_smile: ).

For others that are searching for the correct config. Next yaml is working for my SmartPlug (https://nl.aliexpress.com/item/4000060646437.html visual similar to the one of @brenard ). The main chip is a bk7231t qfn32 with a BL0937 energy monitor. The back of the main board also has the label LSP16DL.

substitutions:
  devicename: smartplug
  friendly_name: My plug
  device_description: My plug description
  current_res: "0.00221" # Power monitoring calibration
  voltage_div: "955" # Power monitoring calibration


esphome:
  name: ${devicename}
  friendly_name: ${friendly_name}
  comment: ${device_description}

bk72xx:
  board: generic-bk7231t-qfn32-tuya

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: !secret api_encryption

ota:
  password: !secret ota_password

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot in case wifi connection fails
  ap:
    ssid: ${friendly_name} Fallback Hotspot
    password: !secret wifi_ap_password
    

# Enable time component for use by daily power sensor
time:
  - platform: homeassistant
    id: homeassistant_time

sensor:
# Reports how long the device has been powered (in minutes)
  - platform: uptime
    name: Uptime
    filters:
      - lambda: return x / 60.0;
    unit_of_measurement: minutes

# Reports the WiFi signal strength
  - platform: wifi_signal
    name: Wifi Signal
    update_interval: 60s

# Reports the Current, Voltage, and Power used by the plugged-in device
  - platform: hlw8012
    model: BL0937
    sel_pin:
      number: GPIO24
      inverted: true
    cf_pin:
      number: GPIO7
      inverted: true
    cf1_pin:
      number: GPIO8
      inverted: true
    
    change_mode_every: 8
    
    update_interval: 5s

    current:
      name: Current
      icon: mdi:current-ac
      unit_of_measurement: A

    voltage:
      name: Voltage
      filters:
        - skip_initial: 1

    power:
      id: power
      name: Power
      filters:
        - skip_initial: 1
        - multiply: 0.97
        - lambda: if (x < 0.01) {return 0;} else {return x;}

    energy:
      name: Energy
      filters:
        - skip_initial: 1
    
    current_resistor: ${current_res}
    voltage_divider: ${voltage_div}

  # Reports the total Power so-far each day, resets at midnight, see https://esphome.io/components/sensor/total_daily_energy.html
  - platform: total_daily_energy
    name: Total Daily Energy
    icon: mdi:circle-slice-3
    power_id: power
    filters:
      # Multiplication factor from W to kW is 0.001
      - multiply: 0.001
    unit_of_measurement: kWh

binary_sensor:
# Button on the front is pressed and then toggle relay
  - platform: gpio
    device_class: power
    pin:
      number: GPIO10
      mode: INPUT_PULLUP
      inverted: True
    name: Button # Name to make button visible in HA
    on_press:
      - switch.toggle: relay

text_sensor:
# Reports the ESPHome Version with compile date
  - platform: version
    name: ESPHome Version
  - platform: libretiny
    version:
      name: LibreTiny Version

# Reports detailed wifi info, can be commented out
  - platform: wifi_info
    ip_address:
      name: IP Address
    # ssid: # Some additional wifi info that is not normally needed
    #   name: Connected SSID
    # bssid:
    #   name: Connected BSSID

switch:
# Relay itself
  - platform: gpio
    name: ${friendly_name}
    pin: GPIO26
    id: relay
    restore_mode: RESTORE_DEFAULT_OFF #Try to restore relay state after reboot/power-loss event.
    #RESTORE_DEFAULT_OFF (Default) - Attempt to restore state and default to OFF if not possible to restore. Uses flash write cycles.
    #RESTORE_DEFAULT_ON - Attempt to restore state and default to ON. Uses flash write cycles.
    #ALWAYS_OFF - Always initialize the pin as OFF on bootup. Does not use flash write cycles.
    #ALWAYS_ON - Always initialize the pin as ON on bootup. Does not use flash write cycles.
2 Likes