ESP8266 / HLW8012 resolution for power reading

Hello,

is it possible to change the resolution of the power reading of ESP8266 / HLW8012?

Ich have a “Shelly Plug” EU version (I assume technically it is called “Shelly Plug 2”). ESPHome is running fine. I use the device for switching and energy metering of the connected coffee machine that draws 2400 W at maximum and about 2 W at idle.

The issue is, that I want to be able to detect if the connected device is switched on by reading the power signal and a threshold of about 3 W. With ESPHome and HLW8012 I get a signal resolution of about 10 W what is too much to measure around 3 W. With the original Shelly software, I was able to do so with this device.

Is there any parameter for the HLW8012 platform or an alternative way for reading the values at higher resolution in ESPHome?

https://devices.esphome.io/devices/Shelly-Plug2

sensor:
  # Power sensor
  - platform: hlw8012
    sel_pin: GPIO1 # not connected, but required for compiling.
    cf_pin: GPIO5
    cf1_pin: GPIO3 # not connected, but required for compiling.
  # Current sensor: Not existing in Shelly Plug 2!
    current:
      name: "${channel_1} current"
      internal: true
  # Voltage sensor: Not existing in Shelly Plug 2!
    voltage:
      name: "${channel_1} voltage"
      internal: true
    power:
      name: "power"
      id: power
      unit_of_measurement: "W"
      accuracy_decimals: 1
      device_class: power
      state_class: measurement
      filters:
      # Callibration
        - calibrate_linear:
          - 0.0 -> 0.75
          - 21 -> 32.5
          - 165 -> 249
          - 321 -> 477
          - 755 -> 1121
    update_interval: 1s

Hey

Did you do the calibration process as detailed in ESPHome documentation ? It’s the only way to get something more precise !

Vincèn

My issue is not precision. My issue is resolution.

The calibration I did by using a power meter and different electrical devices with different power draw. The calibration in my first post are these readings at 5 points. I verified after calibration be measuring and it fits.

Ignoring the offset and my limited possibility to generate precise power draws I get a resolution of about 15.3 W in the low power range.

1.8 W → 17.2 W → 32.5 W → 47.8 W

You could do try something like this :point_down:

sensor:
    # ...
    update_interval: 0.1s

to get a 10 times higher update interval :bulb:

You might keep this sensor internal only for your productive system to don’t flood HA and have another copy sensor with some filter like throttle or some average that is exposed to HA :raised_hands:

In Tasmota all is running as expected. I flashed to Tasmota and did the calibration. All power readings are matching to the power meter and I get power values in the low range. 5 W, 9 W and 17W is what I could test with available devices connected.

Back to ESPHome it was not working as before.

I did the calibration again in a different way using urrent_resistor and voltage_divider. All OK for the high power values. But I do not get the wanted resolution in the low power range. 32 W is the first value I get after 0 W.

substitutions:
  voltage_divider: "2351"
  current_resistor: "0.0006735057983942908"
  current_multiply: "1.0"
[...]
  - platform: hlw8012
    sel_pin: GPIO1 # not connected, but required for compiling.
    cf_pin: GPIO5
    cf1_pin: GPIO3 # not connected, but required for compiling.
    current_resistor: ${current_resistor}
    voltage_divider: ${voltage_divider}
  # Current sensor: Not existing in Shelly Plug EU!
    current:
      name: "${channel_1} current"
      filters:
        - multiply: ${current_multiply}
      internal: true
  # Voltage sensor: Not existing in Shelly Plug EU!
    voltage:
      name: "${channel_1} voltage"
      internal: true
    power:
      name: "${channel_1} power"
      id: power
      unit_of_measurement: "W"
      accuracy_decimals: 1
      device_class: power
      state_class: measurement
    update_interval: 1s

Just deployed a hlw8012 based plug in a HA free enviroment and I don’t had any problems at all (detecting “standby” of an appliance when power is less than 4W and turn it off). :bar_chart:

Only thing I spot is that your calibration seems to be very far of mine :point_down:

  # Higher value gives lower watt readout
  current_res: "0.00290"
  # Lower value gives lower voltage readout
  voltage_div: "940"

As starting values before calibration I used the standard values. Not having set a value will end up in a voltage devider of 2351 and a current resistor of 1.0 Ohm. Acc. ESPHome manual these are standard values of Sonoff devices. Id did not found values for my specific device. So I used these ones. The config above are the values after calibration.

Similar comments to @orange-assistant. My resolution looks to be 0.1W on graphs and no issue detecting small power differences. Also only single value multipliers.

voltagedivider: "770"
currentresistor: "0.001"
ampsfactor: "0.4250"
wattsfactor: "0.97"

@orange-assistant @Ellcon
What device you are using?
In my case it is a ESP8266 based Shelly Plug EU Version (also called Shelly Plug 2).

Mine’s an Arlec PC191HA with Beken bk7231t chip and for the hlw8012 uses sub model bl0937.

  - platform: hlw8012
    model: BL0937
    sel_pin:
      inverted: true
      number: P24
    cf_pin: 
      inverted: true  # the logic of BL0937 is opposite from HLW8012
      number: P7
    cf1_pin: 
      inverted: true  # the logic of BL0937 is opposite from HLW8012
      number: P8
    power:
      name: "Power"
      unit_of_measurement: W
      accuracy_decimals: 1
      state_class: ''
      filters:
        - multiply: ${wattsfactor}
        - lambda: if (x < 0.01) {return 0;} else {return x;}

I believe that because of the bl0937 these devices are not comparable.

May not make a difference but you could try and add bl0937. I searched on Shelley teardown and it did mention this was used. The esphome docs are often generic and manufacturers are changing devices all the time.

Thank you for helping.

The Shelly Plus is a completely different device. This is ESP32 based. The Shelly Plug is an older generation and is ESP8266 based.

https://templates.blakadder.com/shelly_plug.html

It was an older generation blitzwolf plug (pre 2020 model) with the old/classic HLW8012 based chipset :point_down:

Thank you.

One of the next rainy days I will test and try out what will happen when I use these calibration values.

If your device is that old then maybe a limitation of the power sensor chip? Doubt it is the esp side. I have athom plugs using esp8266 but power chip cse7766. Again, this has good sensitivity & resolution.

Please have a look in my previous posts. It is working with the original Shelly software and it is working with Tasmota software. It is not working with ESPHome. So it cannot be a hardware limitation.

This either is a limitation of ESPHome in combiombination with this hardware or a misconfiguration by me. This is what I want to figure out.

It’s probably one of the more common devices - I can’t imagen esphome not working properly on that one, specially that it has an device entry an no marks whatsoever :point_down:

This is what I believe as well. And this is the reason why I am here in the hope someone can tell me what I am doing wrong.

This is the config I am using.
(The link already is included in my first post)