Aqara Smart Plug reporting energy unavailable

Hi Home Assistant Gurus!

it as been hours that I research to fix this issue but I think is out of my knowledge so if someone can help I will rely appreciate!

2 months ago a bought 2 Aqara Smart Plug the EU version and connect these to HA with ZHA. Everything work as expected! I decide to buy another but this time for a different seller (same reference but on Amazon). And when I connect this one Smart Energy Monitoring is not available. I contact the Amazon support and they send me a new and same problem happen…

I go deep on ZHA and Zigpy to understand what is going on and their is my findings:

  • Both working and not working device have the same Zigbee Signature and same Firmware
  • Both report the sum of energy delivered on Metering cluster (0x0702) attribute current_summ_delivered (0x0000). Yes crazy this one
  • And error show-up in the log for the failing device Error adding entities for domain sensor with platform zha. Details after the list
  • Both report the same value for Metering cluster (0x0702) attribute summa_formatting (0x0303)
  • Another error show-up on the log that is pretty strange about a foreign key contraints violation in the zigbee.db. I try to run the query by hands and it works without violation

Error log in ZHA

Logger: homeassistant.components.sensor
Source: components/zha/core/channels/smartenergy.py:196
Integration: Sensor (documentation, issues)
First occurred: 8:25:15 PM (1 occurrences)
Last logged: 8:25:15 PM

Error adding entities for domain sensor with platform zha
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 382, in async_add_entities
    await asyncio.gather(*tasks)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 614, in _async_add_entity
    await entity.add_to_platform_finish()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 782, in add_to_platform_finish
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 539, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 572, in _async_write_ha_state
    state = self._stringify_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 545, in _stringify_state
    if (state := self.state) is None:
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 371, in state
    value = self.native_value
  File "/usr/src/homeassistant/homeassistant/components/zha/sensor.py", line 174, in native_value
    return self.formatter(raw_state)
  File "/usr/src/homeassistant/homeassistant/components/zha/sensor.py", line 471, in formatter
    return self._channel.summa_formatter(value)
  File "/usr/src/homeassistant/homeassistant/components/zha/core/channels/smartenergy.py", line 196, in _formatter_function
    return self._summa_format.format(value).lstrip()
AttributeError: 'NoneType' object has no attribute 'format'

Query with the violation that I couldn’t reproduce:

INSERT INTO attributes_cache_v7 VALUES ("54:ef:44:10:00:21:2f:3c", 1, 0, 4, "LUMI")
  ON CONFLICT (ieee, endpoint_id, cluster, attrid)
  DO UPDATE SET
    value=excluded.value;

I don’t understand how this query can fail with an ON CONFLICT statements …

After all these research I’m stuck in debugging it so a bit of help will much appricate :smiley:

Thanks in Advance

PS: I already look at this Is Aqara EU smart plug supposed to support Energy metering? and my Home Assistant setup is up to date with the last version of everything

Did you solve the issue?