krahabb / meross_lan

Home Assistant integration for Meross devices
MIT License
454 stars 47 forks source link

Home Assistant energy not reset #264

Closed 0p3t0 closed 1 year ago

0p3t0 commented 1 year ago

Hi, starting from last HA Update (march version), energy seems to be not well resetted to zero ad midnight image image This is happening only on mss310r 2.0.0 Firmware: 2.1.4 (latest today) But is working fine on mss310 6.0.0 Firmware: 6.3.6 (latest today).

I'm running MerossLAN V.3.0.2

Do you have any suggestion? Thanks a lot

krahabb commented 1 year ago

Eh..that's bad since it happens also on my mss310 (exactly same old fw 2.1.4) but I still have HA 2023.2. Also, the sensor values sent to HA are exactly the state coming from the device (This is a design decision I try to enforce almost everywhere possible). This is especally true for the device energy reading so I really guess it's a bug in the firmware.

Now this opens a question if I should fall apart a bit in the design rule and maybe process the device data to acually reset them but still, if the device doesn't report the expected consumption this might be worse than the bug itself. I guess the energy statistics are not affected though (they should reset for the day) since HA should rightly process the reset for the sensor at the first reading posted after midnight.

0p3t0 commented 1 year ago

I think identification with firmware can help. Eventually, if is possible to enter a manual number for each device, can be dynamically subtracted from the readed value, but of course would be better a restore of previous behavior. I'm part of beta program, so I'm glad to help debugging and give you feedback using beta releases, just let me know how you prefer to proceed. Thanks a lot in advance for the wonderful integration you've done :)

krahabb commented 1 year ago

Thank you, I've checked the HA statistics and they seems affected too but the problem lies in the firmware and I have to think a bit about it... sadly enough these data will 'corrupt' the long term statistics..I'll try and see if I can come up with a quick patch

krahabb commented 1 year ago

Oddly enough, I have a spare mss310 with same fw level that I use for development and its energy value got reset correctly on 1st of march. Maybe, since I keep unplugging it, a boot cycle of the device could reset the device state (I hardly think this is tru since historic energy data are surely stored in a sort of eprom/flash inside the device but this is the only thing that comes to my mind in order to explain why, this very same device model, doesn't show the issue.

You could try too to unplug and restatrt the mss310 and see what happens.

I can't power cycle my 'production' mss310 showing the issue since it's in another home...

I should have put the mss310 after a smart witch in order to be able to remotely reset it ;)

0p3t0 commented 1 year ago

I've removed for 5 minutes all of the mss310, but still have same issue unfortunately.

krahabb commented 1 year ago

I'm working on a 'quick' (not so quick tbh) fix but while working on that different things popped up in my mind... I hope to finalize that tomorrow and update the release. I'm basically building a new sensor for the energy based off device power readings. I'm still testing a bit of things but the idea is to bypass the meross integral sum (or give the option to)

krahabb commented 1 year ago

Hello @0p3t0, You could find the patched code in the 'lazy_dev' branch. If you're up to you could give it a shot and see if it works. As I've previously said, patching the original readings might lead to worse results as soon as the devices change their behavior (which happened with the bug but could happen again with unforeseen ramifications) Long story short: the original device consumption reading is still there but I've added an euristic to try reset it's daily reading when catching the behavior of this bug. If the device doesn't show the bug the patch doesnt kick in so the readings are still the 'original' one. Beside this, I've added an extra sensor ('energy_estimate') which instead calculates the integral sum of power readings in meross_lan thus emulating the energy consumption. This sensor is totally in code and, in my experiments, looks pretty promising and follows accurately the consumption readings from the device, except that, being implemented in meross_lan, any bug should be fixable in case ;) Those two sensors in the end should show 'almost' exact same values when the device is connected. Of course, when the device is not reachable, the software sensor cannot calculate any sum so it will just sit and start counting again only when the device is connected again. You could then see this sensor falling a bit behind every time the device is working (so its counter summing) while not connected to HA The software sensor sum should survive HA restarts too since it (should) restores its last good state and restart from there when HA is restarted or the configuration changes

Last thing but the most important: HA/meross_lan must be connected to the device across midnight in order to catch the first consumption reading from the device for the new day and see if it needs to 'offset' the readings for the day or not. If not, it cannot tell if the day was starting good thus relying on what the device is reporting.

That said, when you install this, you should see the energy reset kick in correctly by midnight

PS: the energy_estimate sensor starts disabled the first time it is added but should work correctly anyway since meross_lan runs it whatever its state in the HA registry. I didn't tested this anyway. Just being disabled, its state is not persisted so it looses its sum when restarted/reenabled but still it should work across midnight when it is needed for the original sensor to be 'catched'

0p3t0 commented 1 year ago

Thanks a lot, just downloaded the source and replaced custom component folder. I'll plan a midnight reboot in order to reconnect to HA all devices and start the communication in right way. I'll let you know in next days the behavior, in the meanwhile thanks a lot for the excelent work!

krahabb commented 1 year ago

You should have it running 'before midnight' (at least before the last consumption update before midnight to be more precise) so the transition could rightly be intercepted else it will not know something wrong happened from the last known value from the day before. Anyway I will post some other improvements over the day and let you know. Right now, your version should work (mine got correctly 'fixed' tonight)

0p3t0 commented 1 year ago

I confirm is now working much better! image Also new sensor Energy Estimate is working, not same values, but acceptable to have an idea about consumption :) image

krahabb commented 1 year ago

How the energy sensors work with Lazy.3

This is a recap of the features provided in Lazy.3 for energy consumption. Their final implementation is based off the current thread issue and some other tweaks which are detailed here: There are now 2 energy measures per device in meross_lan and they work as this:

Short comparison

'energy' (legacy):

krahabb commented 1 year ago

Just for future (and past) reference, I finally came across the same type of issues previously reported for the same kind of bug. It looks it is not only on 2.1.4 fw but spreads across multiple ones (see #181) Oddly enough it looks like the 1st of march is a kind of a trigger... At any rate the Lazy.3 patch would fix all of these since they all show the same pattern