turbokongen / hass-AMS

Custom component reading AMS through MBus adapter into HomeAssistant
42 stars 10 forks source link

Restore state #24

Closed Hellowlol closed 4 years ago

Hellowlol commented 4 years ago

Untested, at work so i cant test now, but in theory, restarting ha should restore tha latest state of the sensor. Just need a list off all the hours sensor names and check if last update was less one hour ago

Hellowlol commented 4 years ago

@turbokongen Do you got a list of the hourly sensors (without the meter_id)

turbokongen commented 4 years ago

@turbokongen Do you got a list of the hourly sensors (without the meter_id)

ams_active_energy_import ams_active_energy_export ams_reactive_energy_import ams_reactive_energy_export

Hellowlol commented 4 years ago

What type of behavior do we want here (?)

I rather have unknown values then incorrect one. So my plan was just to only restore hourly sensors if they where still valid. (less then 1 hour since last update). This will allow for hourly sensors be available together with the rest of sensors when a user restart ha

Thoughts? @turbokongen @jnrksv

turbokongen commented 4 years ago

I agree with you @Hellowlol No need to restore 10sec sensors as they quickly gets populated. I think 1 hour sensors should be restored no matter how long ago they last got updated. Say by accident the 1hour package fail any of the checks and are discarded as a failed package it will be 2hours between last and next update. Maybe use 2hours since last and not 1 hour?

Hellowlol commented 4 years ago

So this seems to work now. (Only for kamstrup atm) I have used way to much time to tracking time a bug relating to parser. the parser seems mutate self.sensor_data but it dont seems to use it the way i expected.

Hellowlol commented 4 years ago

This is ready for a spin now, still got some minor clean up to do, but everthing works (on my computer atleast 🤞 )

@turbokongen @jnrksv

Since this also includes some small changes to the parser: @jorgen666 - Kaifa meter @jgallis - Aidon meter

Would anybody mind testing it?

Delete the __pycache__ folder in both the parser and ams folder and just copy the content. Verify that all the sensors works as it should. The hourly sensors might still be unknown, (depending on some conditions), however the sensors should have values after the first hour is passed. Restart home assistant and verify that the hourly sensors is not unknown.

jnrksv commented 4 years ago

Have done some testing this morning. It seems that restore state works for ams_active_energy_import_57065672xxxxxxxx and ams_reactive_energy_export_57065672xxxxxxxx whenever HA is restarted. While ams_active_energy_export_570656728xxxxxxxx and ams_reactive_energy_import_57065672xxxxxxxx are stated as "unknown" until the next hourly readout.

Hellowlol commented 4 years ago

@jnrksv thanks. Can you try to restart ha after the next hourly read and see if all sensors comes back? Also enable debug incase it don’t work, so I can have that as well.

turbokongen commented 4 years ago

@Hellowlol It works perfect at my end.

jnrksv commented 4 years ago

@Hellowlol - same findings as in my previous post. Attched debug log from restarting HA a few minutes ago. hass-ams-patch3-restart-log.txt

turbokongen commented 4 years ago

@jnrksv Did you remove the integration, and deleted pycache before you Installed this Version?

jnrksv commented 4 years ago

Yes, infact I deleted the ams folder and did a new installation. I'll try once more in twenty minutes. And to be completely sure I'm deleting pycache right now.

Hellowlol commented 4 years ago

Remember the the pycache inside the parser folder.

jnrksv commented 4 years ago

Both pycache deleted. For me, this is starting to get interesting. This is the state after last houly readout: hass-ams-screendump-1201 And this is the state after restarting HA a few minutes later: hass-ams-screendump-1204-restart When pycache is deleted, where does the restored states come from? hass-ams-patch3-restart-log-1204.txt

jgallis commented 4 years ago

EDIT: It works for Aidon, but it dos not take the latest hour value, it is taking the previous hour value. I tok a restart 13:05, and the sensor has this timestamp: meter_timestamp: 2020-4-10 12:00:00

Hellowlol commented 4 years ago

@jgallis internaly ha uses utc so the time stamp is correct (Norway is one ~two hour ahead of utc)

Hellowlol commented 4 years ago

@jnrksv _pycache__ is a just a folder where the python interpreter stores bytecode (helps programs start faster)

All the states are stored in the home assistant database.

I suspects the code is working as it should. The state might not be written to the state machine/ db so when we try to restore the state is unknown. (Not really sure but that the only reasonable I can think of) Can you test again? But this time let ha run for 10 minutes after the new hours values are in before you restart.

jnrksv commented 4 years ago

Can you test again? But this time let ha run for 10 minutes after the new hours values are in before you restart

I let HA run for 15 minutes after hourly readout, restarted and now every state restores as expected. Why wait 10-15 minutes? @Hellowlol - I reckon you have an explanation for this too?

Hellowlol commented 4 years ago

It was just to to test a theory. I suspected you restarted ha right after the state changed. We schedule the update to the state machine. So if somebody restarts really quick, it might not be written to db/state machine before ha is shutdown.

Hellowlol commented 4 years ago

@turbokongen Think this should do it.