MQTT utility meter spikes throughout reboots

The problem

Hey guys, I’m in need of help for a little frustrating issue I’ve encountered with my HASS instance for a pretty long time now. I’m using two ESP8266 with ESP Easy that count the impulses of my energy meter and send these values over MQTT to HASS. Then, I use a utility meter to aggregate the total energy consumption in the course of the day. This works fine most of the time, but once in a while (especially after an update or restart of HASS, or when HASS reconnects to the MQTT broker) there is a huge spike in my utility meter reading:

At first, I suspected the ESP Easy reading to be wrong, but this is not the case, it correctly computes and sends total_kwh. The total_kwh is a counter that is calculated by subtracting the total amount of impulses by 1000 (1000 impulses = 1 kWh). These values also reach HASS.

What I’ve tried

I tried sending some manual messages over MQTT to debug the issue. Here is what I’ve found:

Let’s say the current reading from ESP Easy is 20 kWh. When I manually send 20 kWh again to the topic, the utility meter correctly does not count it. However, once I restart HASS and send 20 kWh again, even though the current utility meter is at 20 kWh, 20 gets added so the total is 40 kWh. It seems to me as that HASS does not save the last received value from MQTT throughout reboots. I tried retaining these topics in ESP Easy, but the issue persists

The question

Is there anyone who has experienced similar issues and knows a solution? Is there something that needs to be configured so that HASS persists the last received value from MQTT for the utility meter to not count it twice?

Many thanks for any hints!

Be sure to use An availability template in your mqtt sensor definition.

1 Like