Summarizing consumption data on a device which is always reset

I use a bunch of Shelly Plug-S devices for measuring consumption. Generally they work fine.
However, I have one issue: every time the device reboots, consumption information reset.

So whenever there is a small power outage, or I just change something in the network configuration, and I need to reboot the device to apply the new config, the data is lost.

This is unfortunate, but I can’t change this on the device level. The question is, how do I fix in HA?
What I would like to do is every time we read the sensor, we detect if it was reset (ie. the value is lower than what we saw last time), and in that case, just increase it with the last known value.

That would, in effect, undo the resetting.

Is there a configuration setting for this?

Or should I maybe create a new entity for “shadowing” this value, and I should update it’s value manually, from code, based on the actual sensor readings? How do I set out to do that?

Thank you for your help.

You could use the integral sensor in home assistant to integrate the device’s reported instantaneous power consumption to energy. The advantage being that home assistant will restore the sensor state after a restart,

Make sure you use the left method.

Looking into the same issue - not quite sure how to configure it.
Should I use the _energy sensor (total_increasing in kWh which is being reset on power loss), or the _power sensor (current consumption in watt).
Also, should the prefix be none or k?
And the time unit - what should this de set to ?