Xiaomi smart plug (ZNCZ02LM) doesn't report energy usage

My Xiaomi smart plugs are discovered fine by zigbee2mqtt, and appear fine in HA, but the energy (total kWh consumed) always shows “Unknown”.
It is a mystery, because the device configuration in HA looks correct (the topics/template for the energy sensor look correct) and I see the MQTT messages have the correct attributes.

Any ideas?

The MQTT messages have 2 attributes describing this, “consumption” and “energy”.
The configuration (from discovery) for this sensor has:

availability:
  - topic: zigbee2mqtt/bridge/state
device:
  identifiers:
    - zigbee2mqtt_0x00158d000216768a
  manufacturer: Xiaomi
  model: Mi power plug ZigBee (ZNCZ02LM)
  name: '0x00158d000216768a'
  sw_version: Zigbee2MQTT 1.21.0
device_class: energy
json_attributes_topic: zigbee2mqtt/0x00158d000216768a
last_reset_topic: zigbee2mqtt/0x00158d000216768a
last_reset_value_template: '1970-01-01T00:00:00+00:00'
name: 0x00158d000216768a energy
state_class: measurement
state_topic: zigbee2mqtt/0x00158d000216768a
unique_id: 0x00158d000216768a_energy_zigbee2mqtt
unit_of_measurement: kWh
value_template: '{{ value_json.energy }}'
platform: mqtt

The incoming MQTT message looks like:

Message 11 received on zigbee2mqtt/0x00158d000216768a at 8:53 AM:

{
    "consumption": 2.58,
    "energy": 2.58,
    "linkquality": 57,
    "power": 156.28,
    "power_outage_memory": true,
    "state": "ON",
    "temperature": 24
}

OK I think this is related to the handling of last_reset_topic and last_reset_value_template in some way. MQTT Sensor : how to define last_reset_value_template

It doesn’t report energy usage as far as I know.
I’m using it with ZHA and I get only on/off switch and current power. Don’t know if there’s any workaround to calculate consumption…

use the rieman integration with the left method.

Wow. That was pretty simple :wink:
It seems to be working. Thank you very much.
For everyone else who would like to calculate energy consumption based on current power usage, this is all it takes:

- platform: integration
    source: sensor.load_power_lumi_158d000268dd8d
    name: energy_spent
    unit_prefix: k
    round: 2
    method: left

This model does report it. You can see the value in the MQTT message (the “power” and “consumption” fields), and in the attributes for the sensor, but the actual sensor value is undefined.

Actually I’m using it through Xiaomi Gateway integration, and it’s not reporting it that way…

ZHA and Zigbee2MQTT doesn’t work the same when it comes to reading sensors and handling device quirks, so if a device works in Zigbee2MQTT but not in ZHA or vice versa does not really say that much.

FYI, when specifially talking about the ZHA integration component for Home Assistant there are loads of related discussions/explainations here about this and the current ZHA limitation of 1 sensor per cluster:

and here

There is also an issue posted to the HA core about ZHA energy consumption sensors not showing up:

All those applies to most Zigbee power plugs or other Zigbee devices with integrated energy meters:

  1. Tuya TS0121 → https://github.com/zigpy/zha-device-handlers/issues/605
  2. BlitzWolf BW-SHP13 (OEM version of Tuya TS0121) → https://github.com/zigpy/zha-device-handlers/issues/605
  3. Develco Norwegian HAN Adapter (Develco EMIZB-132) → https://github.com/zigpy/zha-device-handlers/issues/267
  4. Develco Prosumer Meter (Develco SMMZB-310101)
  5. Develco Smart Plug Mini (Develco Frient SPLZB-131) → https://github.com/zigpy/zha-device-handlers/issues/857
  6. Immax Plug-230V-ZB3.0 Smart Plug
  7. Innr SP 120 (a.k.a. Innr SP120) → https://github.com/home-assistant/core/issues/34090
  8. Lonsonho 16A Energy Monitoring Plug (OEM version of Tuya TS0121) → https://github.com/zigpy/zha-device-handlers/issues/605
  9. SmartThings Smart Plug (UK)
  10. Schwaiger ZHS15 → https://github.com/home-assistant/core/issues/44539
  11. Rehentele 16A Energy Monitoring Plug (OEM version of Tuya TS0121) → https://github.com/zigpy/zha-device-handlers/issues/605
  12. Aqara LUMI lumi.plug → ZHA plugs - smartenergy_metering > wrong cluster #44539

and a possible workaround being working on here

This discussion is specifically about these devices with zigbee2mqtt. The issue is that the energy usage is listed in the MQTT message correctly, but it doesn’t show in the UI.

Ah I just upgraded Home Assistant to 2021.9.6 and the energy usage is being reported correctly now.