Shelly - Power Meter resets total consumption on power loss

  • Platform information:
    • Hardware: Raspberry Pi 3
    • OS: OpenHabianPi
    • openHAB version: 2.4.0-1

I have some Shelly Plus S wall plugs which are able to connect via MQTT and they are measuring power consumption. So far so good, but there is one problem: when there is an power loss it resets the total consumed power to zero. At the moment there is no possibility to set the value after a reset so i have to find a “software workaround” within a rule i think.

So far i think the main thing to do is:

  1. read total consumed power (tcp)
    1a. update tcp if new value is bigger then the old value
    1b. add new value to old value if new value is zero or smaller then the old one.

So far so good, but: does this make sense and how could i do this in a rule?

best regards


Not quite. What you need to do is keep track of the last reading from the device.

    if(currReading < lastReading) tcp = tcp + currReading
    else tcp = tcp + (currReading - lastReading)

The first reading after the reset will treat the currReading as the delta to add to your running total. Every reading after that we add the difference between the current reading and the last reading to the running total.

1 Like

I asked Dimitar Dimitrov from Shelly about this. The power meter counter should be stored in the flash in the next firmware.


Thanks to @rlkoshak for this hint. I will give it a try as soon as possible and give feedback :slight_smile:

And also thanks to @igi for asking Dimitar Dimitrov about this point. Maybe the workaround will not be needed anymore. :slight_smile:

Can anybody confirm, that this has been implemented yet? I’m using Firmware 1.5.7 with Shelly4 PRO

In current version of shelly 1pm it’s not in. So you have to deal with a openhab rule around.
I’m right not able to get it work… I’ve some precition problems. When I’ve 3,46kWh a day in shelly. My openhab has around 2,8kWh… try to figure out next days where my missing point is…

Hi together, this is my first post here :slight_smile:

I don’t want to open a new thread/topic but I need your knowledge about exactly this problem.
I am using a shelly1pm and have the same problem which is mentioned here: I lose the totalKWH with every update of the firmware. My problem: How can I display the total consumption respecting also updates, power loss, etc.?

I tried it already with rrd4j but most of the time the shelly is in status “off” so the strategy “everyMinute” is an oversized strategy, I guess…

I have no idea how to design a rule to store this value corretly. Could you please help?


  • Trigger the Rule every time the total KWH changes Item TotalKWH changed
  • delta = TotalKWH.state - previousState
  • If the delta is negative you know that the counter reset so just use the current state as the delta if(delta < 0) delta = TotalKWH.state
  • Add the delta to the Item that stores your running total TotalKWHProxy.postUpdate(TotalKWHProxy.state + delta)
  • Persist TotalKWHProxy, not TotalKWH

rrd4j is, by design, a database that never grows in size. Storing the value every minute is not only not “oversized” but it’s required. As data ages, every ten values gets replaced with the average of those ten so as the data ages it becomes more sparse, therefore taking up less room.

Thank you for your answer, but how to get previousState?

Hi rlkoshak, I think this can not work. Could you please review your code?
In this grafik I set a reset of two times but I guess your calculation will not work. What do you think?

Frankly, I don’t have a shelly power meter. There is no way I can test this. Also, errors in code often have as much or more to do with context. So post your code and maybe I can review that.

There’s only three lines of code here so there is not that much that can go wrong. Looking at that table I would guess that you are missing the third line of code that adds the delta to the current total: TotalKWHProxy.postUpdate(TotalKWHProxy.state + delta). Or you are not looking at the logs and there is an error with that line of code which the logs will be complaining about.

Ultimately, cargo-cult programming is not going to take you very far. You really need to have a basic understanding of what the code you are copying and pasting does, overall how it works, and how to look for errors. You also need to learn how to debug problems when you encounter them.

First: Thank you @rlkoshak for your help. I used your code snippets to build up a rule. So far i think it works when everything is “ok” but on power loss it starts to count again by 0. Maybe I have just done a little mistake.

rule "ShellyPlugS001PowerLoss"
    Item ShellyPlugS001_totalKWH received update
    logInfo("ShellyPlugS001PowerLoss", "ShellyPlugS001_totalKWH changed")
    var Number delta = (ShellyPlugS001_totalKWH.state as Number) - (ShellyPlugS001_totalKWH.previousState().state as Number)
    logInfo("ShellyPlugS001PowerLoss", "ShellyPlugS001_totalKWH_delta: " + delta)
    if( delta < 0 ) {
        delta = (ShellyPlugS001_totalKWH.state as Number)
        logInfo("ShellyPlugS001PowerLoss", "New ShellyPlugS001_totalKWH_delta: " + delta)
        ShellyPlugS001_totalKWH_persist.postUpdate((ShellyPlugS001_totalKWH_persist.state as Number) + delta)
    } else {
        logInfo("ShellyPlugS001PowerLoss", "Old ShellyPlugS001_totalKWH_delta: " + delta)
        ShellyPlugS001_totalKWH_persist.postUpdate((ShellyPlugS001_totalKWH.state as Number) + delta)

I think I’m just blind to see the problem. :smiley: Maybe someone else can see the problem. :slight_smile:

Best regards and stay healthy


On power loss for OH all variables go away and if you do not have persistence with restoreOnStartup all your Items initialize to NULL. Do you restore the state on startup?

Oh, I’m not talking about a power loss at my OpenHAB Rasperry, i talk about a power loss on the Shelly Things. They reset their power consumption store every time they are unplugged (or any other power loss). So i searched and found this thread.

All my items are stored persistent in a MariaDB Database, just to answer your question. :slight_smile:

With my rule I try to solve this problem with your snippets but i think i have done something wrong.

You are trying to do this with only one Item? As explained above, you should be using a proxy Item. You can’t do this without one. The proxy Item keeps the running total. You calculate the delta and add that to the proxy Item. If the delta is positive (i.e. the new reading is below the last reported reading) you know that the device reset.

What firmware are your Shelly devices on?
Alterco changed this a while ago.

@rlkoshak Oh i use a proxy item.

ShellyPlugS001_totalKWH: the real item
ShellyPlugS001_totalKWH_persist: the proxy item

i use the latest firmware version 1.6, but when i pull for example the ShellyPlugS it resets. Is there a setting or somethin i missed?

No setting afaik. @markus7017 have you any information from Alterco regarding this?

We requested this feature a long time ago. AFAIK they implemented a user resettable total for EM and EM3 with firmware 1.6.something. I could check the status with them, but you should post your requests here: Shelly Binding