jghaanstra / cloud.shelly

Homey app to control Shelly Cloud devices
GNU General Public License v3.0
21 stars 14 forks source link

Keep power meter values as they reset when power is lost to the shelly device #102

Open XxXHunter opened 3 years ago

XxXHunter commented 3 years ago

Dear Developer,

when the power is lost to a shelly plug S (at least) the power meter is reset to 0. the power in the homey insights gets back to 0 in this case as well. (and also "power by the hour" suffers from this.) is there a possibility to keep track of the last value say every hour or so and whenever there is a value reported by the device that is lower than the last value, have an option to add the older higher value to the one that is currently showing? It may even be a manual value add to the value of the plug but should preferably be an automated process with a manual override if needed to intervene manually.

could you consider this as after a power outage all usage values of the shelly plugs look bad.

thanks in advance!

Screen Shot 2020-12-05 at 14 52 13
jghaanstra commented 3 years ago

You should ask Allterco Robotics (the company behind Shelly) about this. My app just read the values which are returned by the Shelly. I'm not planning on solving this on my end.

XxXHunter commented 3 years ago

Dear Jelger,

I logged a case but that did not go as planned. the support engineer just pointed me to the API documentation that states that the counters get reset to 0 https://shelly-api-docs.shelly.cloud/#shelly-plug-plugs-settings I was wondering if more people would have the same issues and maybe it is not that hard to put a loop in the code to check for the last value and compare it to the current value and if lower add the last value to the current value and make that the energy value until the current value drops below the last value again at which time the then last value should be added to the current value again making the energy value only go up and never down. If you don't have the time to alter this but are willing to allow this into the app code then maybe someone else could contribute to this with a little bit of code. Please let me know what you think of this.

Thanks in advance for your time taken! King regards, XxXHunter

jghaanstra commented 3 years ago

That's a shame. Implementing this in my app for each driver with the meter_power capability is not hard but a lot of work. I'd like to spend my time on feature requests that benefit more people. So if you can find more people interested in this I will consider it.

And of course I'm open to pull requests from other developers that want to help out develop the app. That's the whole idea of open source software.

I'll open this this ticket and label it as feature request.

mhoogenbosch commented 2 years ago

I'd like to have this feature too. I'm using it together with Power by the Hour app. This to measure the power usage over the last week / month / year for my dryer , laundry machine and my dishwasher. Now it constantly resets and cannot do anything with the data. Thanks!

jghaanstra commented 2 years ago

I was thinking about this the other day but I cant really come up with a proper solution. If either of you can come up with a bulletproof logic on how to functionally achieve this I'm willing to look into it. You will need to describe how I can maintain a seperate count of the power meter independent of the value returned by the Shelly itself. If you can not come up with a solution as well I'll close this issue again.

mhoogenbosch commented 2 years ago

If I'm not mistaken this is a feature request at Shelly too. I have been thinking of this a bit but only think I can come up with is writing everything to a value and importing it if there has been a moment of connection loss (FW update for example) measuring the difference and if the value is 0 import the previous value. But there are lot's of if's.

BuyerJ commented 2 years ago

Hi Jelger, the only other solution i can think of is rebooting all the shellys every night at 00:00 and have power by the hour set to nightly reset of the values but that feels like a cannon as rebooting shellys is not something you would want every night (dont know what the lifespan of the shellys will be if we implement this....) Is there a way to just reset the value of the shelly meter on the shelly itself without rebooting it? that might also work for the integration with power by the hour but still feels like something that the power by the hour and shelly developers should have a brief discussion on.... although that might actually be a workable solution if settable @ 00:00 i wonder if that might break something on the shelly cloud end. Maybe a soft restart is an option that can be provided to soft restart all shellys without altering the relay status so only the counters are reset but again something that needs discussion with Robin Gruijter of the power by the hour app so we won't miss any data when rebooting or setting the value to 0 at midnight.....

mhoogenbosch commented 2 years ago

Hi Jelger, the only other solution i can think of is rebooting all the shellys every night at 00:00 and have power by the hour set to nightly reset of the values but that feels like a cannon as rebooting shellys is not something you would want every night (dont know what the lifespan of the shellys will be if we implement this....) Is there a way to just reset the value of the shelly meter on the shelly itself without rebooting it? that might also work for the integration with power by the hour but still feels like something that the power by the hour and shelly developers should have a brief discussion on.... although that might actually be a workable solution if settable @ 00:00 i wonder if that might break something on the shelly cloud end. Maybe a soft restart is an option that can be provided to soft restart all shellys without altering the relay status so only the counters are reset but again something that needs discussion with Robin Gruijter of the power by the hour app so we won't miss any data when rebooting or setting the value to 0 at midnight.....

That is a option indeed, but that doesn't solve the FW upgrade gap, or these should be scheduled at midnight too.

jghaanstra commented 2 years ago

Is there a way to just reset the value of the shelly meter on the shelly itself without rebooting it?

No

still feels like something that the power by the hour and shelly developers should have a brief discussion on....

What is there to discuss? As mentioned I need a bulletproof and stand-alone solution before I will implement this as it will affect all users and should not raise issues. And as @mhoogenbosch mentioned there are still a lot of if's that do not give me this garantee.

although that might actually be a workable solution

The reboot solution or even maintaining your own power meter value in a variable with some custom logic is already possible with the current app and some next level flow building.

For now the only proper solution is to solve this in the Shelly firmware and I dont see that happening and there probably is a good reason why this value does not survive a reboot.

BuyerJ commented 2 years ago

That is a option indeed, but that doesn't solve the FW upgrade gap, or these should be scheduled at midnight too.

that would be the solution then i guess.... just rebooting all shellys to have something workable from a kwh counting perspective.... the app also has a feature to upgrade all shellys and that could also be scheduled @ 00:00. wonder what will happen if there is some firmware bug and all shellys of a certain type have a problem at the same time ;)

@jghaanstra i don't see a lot of if's but that might be from my perspective to the issue if there would be a counter that would maintain the value and create a higher value being the sum of the new lower value and the latest higher value. this will require 2 values to be maintained, the current value, the previous high value and the total sum value that is displayed. the only time the high value would be overwritten with the total sum value is if the current value collected is lower than the previous current value. that should make it bulletproof as you call it. this is probably usable in every driver that has this problem so a one time fix and a lot of copy paste to the rest of the drivers. If you need someone to test let me know ;)

jghaanstra commented 2 years ago

i don't see a lot of if's but that might be from my perspective to the issue if there would be a counter that would maintain the value and create a higher value being the sum of the new lower value and the latest higher value. this will require 2 values to be maintained, the current value, the previous high value and the total sum value that is displayed.

So the logic would look like this?

BuyerJ commented 2 years ago

Maybe more like this as we need one more failsafe value to capture another occurrence after the first one:

this way the first line would hit if it is business as usual and the second would hit if there is a firmware upgrade or a reboot after the long term value has been created. The total sum would be the value to show on the shelly cumulative power meter.

my programming is rusty so you might have an easier way to determine the states but this is what i was thinking of...

jghaanstra commented 2 years ago

Ok, seems viable. Do all GEN1 Shelly devices loose the power meter value after a reboot?

BuyerJ commented 2 years ago

according to the API documentation it looks that way. The plug S does it for sure (have lots of those....). The ones that do somehow have a surviving power meter will not run into the else step as the meter will always be higher ;)

joachimvenaas commented 10 months ago

Maybe more like this as we need one more failsafe value to capture another occurrence after the first one:

  • If power meter value from Shelly has changed
  • and value is higher than previously saved value
  • than total sum = (longterm value + value) and value = previously saved value
  • else total sum = (longterm value + previously saved value) and value = previously saved value and total sum = longterm value

this way the first line would hit if it is business as usual and the second would hit if there is a firmware upgrade or a reboot after the long term value has been created. The total sum would be the value to show on the shelly cumulative power meter.

my programming is rusty so you might have an easier way to determine the states but this is what i was thinking of...

@BuyerJ Tryed it your way on my own "app" for testing. Did not work as excepted the way to described. I ended up with a solution that increment the positive difference between current and previous:

fetch('http://x.x.x.x/rpc/PM1.GetStatus?id=0')
  .then((res) => res.json())
  .then((json) => {
    const current = json.aenergy.total || 0; // Wh
    const longterm = this.getStoreValue('longterm') || 0;
    const previous = this.getStoreValue('previous_power') || current;
    const change = current - previous;

    if (change > 0) {
      const totalWh = longterm + change;

      this.log('incremented', change);
      this.setStoreValue('longterm', totalWh);
      this.setCapabilityValue('meter_power', totalWh / 1000);
    }

    this.setStoreValue('previous_power', current);
  })
  .catch((err) => {
    // do stuff
  });

Seems like its working with my limited testing

ezwep commented 4 months ago

Maybe more like this as we need one more failsafe value to capture another occurrence after the first one:

  • If power meter value from Shelly has changed
  • and value is higher than previously saved value
  • than total sum = (longterm value + value) and value = previously saved value
  • else total sum = (longterm value + previously saved value) and value = previously saved value and total sum = longterm value

this way the first line would hit if it is business as usual and the second would hit if there is a firmware upgrade or a reboot after the long term value has been created. The total sum would be the value to show on the shelly cumulative power meter. my programming is rusty so you might have an easier way to determine the states but this is what i was thinking of...

@BuyerJ Tryed it your way on my own "app" for testing. Did not work as excepted the way to described. I ended up with a solution that increment the positive difference between current and previous:

fetch('http://x.x.x.x/rpc/PM1.GetStatus?id=0')
  .then((res) => res.json())
  .then((json) => {
    const current = json.aenergy.total || 0; // Wh
    const longterm = this.getStoreValue('longterm') || 0;
    const previous = this.getStoreValue('previous_power') || current;
    const change = current - previous;

    if (change > 0) {
      const totalWh = longterm + change;

      this.log('incremented', change);
      this.setStoreValue('longterm', totalWh);
      this.setCapabilityValue('meter_power', totalWh / 1000);
    }

    this.setStoreValue('previous_power', current);
  })
  .catch((err) => {
    // do stuff
  });

Seems like its working with my limited testing

Who can help me set this up? I get frustrated every time after each power outage that the values are reset.

Should I set this as a HomeyScript for each Shelly device? I've added it, but then I get this as a result:

✅ Script Success ↩️ Returned: undefined

Or, how can I set this up via an advanced flow?

joachimvenaas commented 4 months ago

Who can help me set this up? I get frustrated every time after each power outage that the values are reset.

Should I set this as a HomeyScript for each Shelly device? I've added it, but then I get this as a result:

✅ Script Success ↩️ Returned: undefined

Or, how can I set this up via an advanced flow?

I think i created my own homey app with this one. The setCapabilityValue and setStoreValue indicates that. I'm not sure if the store function is accessible through advanced flow

ezwep commented 3 months ago

Who can help me set this up? I get frustrated every time after each power outage that the values are reset.

Should I set this as a HomeyScript for each Shelly device? I've added it, but then I get this as a result:

✅ Script Success ↩️ Returned: undefined

Or, how can I set this up via an advanced flow?

I think i created my own homey app with this one. The setCapabilityValue and setStoreValue indicates that. I'm not sure if the store function is accessible through advanced flow

Sure, can you help me a little bit with how I can do this myself?

joachimvenaas commented 3 months ago

Sure, can you help me a little bit with how I can do this myself?

I have the code i used here: https://github.com/joachimvenaas/site.venaas.diydevices/tree/main/drivers/heatpump