Ho to correct strange behaviuor on total consumatin

Hello all and first of all sorry for my bad English.
I need help with a strange behavior that I don’t know how to work around.
I have an entity X that counts the total kilowatts, always grows and is ok.
When there is no current, the value goes to zero for a moment and then returns to its incremental value.
Example if it was 200, and there is a power failure, it goes to zero for a few seconds then returns to 200 and continues to increase. I see this from sensor itself into HA checkin the graph.
In fact, entity X measures correctly
Based on this entity I put counters helpers (daily and monthly). The problem is that when there is no electricity, the meters see this change of state as a new total and, for example, a few seconds after the return of the electricity, the value of the the meters (daily and monthly) will be increased by 200, so if X was 200 before, then it will be 400 for the daily and 400 for the monthly. The day after of course daily start from 0 but monthly restet the value only at end of the month.

does anyone have a good suggestion?

thanks to all in advance.

Tz