RobertD502 / home-assistant-petkit

Home Assistant integration for PetKit devices
MIT License
213 stars 32 forks source link

Issues with pet weight sensors #95

Open override80 opened 1 week ago

override80 commented 1 week ago

Hi @RobertD502 ,

I'm having a weird issue in the last few days:

image image image

For some reason i get spot updates in the weight sensor for my cats with really out of range values, it looks like they are 1000x the pet weight. Could it be related to latest updates? Weird values gets added to the DB (first line, which corresponds to latest peak, max is 3400):

image

This seems to happen upon Integration load/reload (in fact, i can replicate it forcing a reload, this is the value immediately after reloading:

image

pretty huge cats here :)

The weight goes back to normal (like 4.200g) after a few minutes.

RobertD502 commented 1 week ago

Turn on debug logging and share the file with me.

RobertD502 commented 1 week ago

I may know what is going on (on the Home Assistant side of things), but it will require additional logging. Could you please:

  1. Replace the current sensor.py file (located in the custom_components/petkit/ directory) with the sensor.py file I have linked at the bottom of this message.
  2. Turn on debug logging (via the UI) for the PetKit integration.
  3. Restart Home Assistant.
  4. Give it about a minute and then go back to the Petkit integration and disable debug logging.
  5. Share the log file that gets downloaded to your device.

Side note: Don't worry about the errors in the logs from the petkit integration - I added those in the file I have uploaded to help distinguish the temporary things I am logging from the regular debug logs.

File can be downloaded here: https://easyupload.io/05ifda (Github won't allow .py files to be directly uploaded to messages)

override80 commented 1 week ago

Hi @RobertD502 ,

here's the log. Could it be related to the reload of the last state?

home-assistant_petkit_2024-11-13T08-53-02.512Z.log


these are showing up in the HA log as well:

2024-11-13 11:42:43.519 ERROR (MainThread) [custom_components.petkit] {'Vaniglia': {'sorted_dict': {42015: 3860}, 'last_key': 42015, 'latest_weight': 3860, 'weight_calculation': 3.9, 'native_value': 3.9}}
2024-11-13 11:42:43.520 ERROR (MainThread) [custom_components.petkit] {'Cannella': {'sorted_dict': {18013: 4163}, 'last_key': 18013, 'latest_weight': 4163, 'weight_calculation': 4.2, 'native_value': 4.2}}
2024-11-13 11:42:43.520 ERROR (MainThread) [custom_components.petkit] {'Nespola': {'sorted_dict': {2572: 3445}, 'last_key': 2572, 'latest_weight': 3445, 'weight_calculation': 3.4, 'native_value': 3.4}}

where does the latest_weight come from? It looks like a more precise version of pet weight (not rounded to hg)

DieterKoblenz commented 1 week ago

Maybe this is a similar issue, if not I can make a new one.

My two cats don't have any entities anymore. It says they are disabled by the component. I am running the latest .24 version via HACS though.

home-assistant_petkit_2024-11-13T13-49-51.690Z.log

RobertD502 commented 1 week ago

@override80

At a quick glance, this looks to be a Home Assistant units problem. The way weight is handled (in order to preserve last non-zero weight at midnight until the litter box statistics page identifies a pet having a non-zero weight for the current day) is that Home Assistant, during boot or when the integration is reloaded, sets the state of the sensor to the last state recorded in the HA database.

Then, during the first poll, the weight is fixed because it uses the weight that is returned by the PetKit API to set the state of the sensor. The default unit used by the integration is KG.

For example, when the PetKit API is polled: 1) It returns a weight of 3200 2) I then divide this by 1000 to get a state of 3.2KG 3) You have the units set to Grams, which means your front-end shows a state of 3200.

Now, for some reason, Home Assistant seems to be saving the state in its database in the unit you selected (in grams) instead of respecting the default native unit of measurement (KG). What ends up happening, during HA start or reload of the integration, is that 3200 is seen as the last state (instead of 3.2KG) and Home Assistant says "oh wait the native unit of measurement is KG, so the state is 3200KG...the user selected a unit of grams so let's convert 3200KG to Grams", which is how you'd end up with a high weight like 3,200,000 Grams.

One way to test this theory is to have your sensors set to a unit of KG instead of Grams. Let the integration run for a few polling cycles (~10 minutes) and then reload the integration and see if you get whacky values. The only potential issue you may see is with Pets that haven't used the litter box yet.

RobertD502 commented 1 week ago

@DieterKoblenz

It is not related to this issue. Based on your logs, the PetKit API doesn't seem to be returning any Pets at the endpoint that is used to fetch pets for an account.

Shoot me a private message on discord as I'll need to investigate why your account isn't returning any pets.

override80 commented 1 week ago

One way to test this theory is to have your sensors set to a unit of KG instead of Grams. Let the integration run for a few polling cycles (~10 minutes) and then reload the integration and see if you get whacky values. The only potential issue you may see is with Pets that haven't used the litter box yet.

How do i do it :)?

RobertD502 commented 1 week ago

@override80

For each of the latest weight sensors, click on it, select the settings gear icon in the upper right hand corner, and then, in the unit of measurement field, select kilograms.

override80 commented 1 week ago

Seems like it's working! I just set the unit to kg. I will leave it like this and do some more tests.

One last question: I always get readings rounded to hg (4.200, 3.800...). Is there a way to get a more precise reading (up to grams, like 4.287 or 3.845)? Or is it a limit on the api side?

RobertD502 commented 1 week ago

@override80

I believe I've tracked down the issue. As I suspected, the way I am pulling in the last state is what causes the issue - there are two ways to do it: One way pulls in the state that reflects the value that is obtained after the conversion is done (if the user selected a unit different from KG), the other way pulls the "native value" (the pre-conversion value). I'll get a fix out that should resolve the issue for anyone that chooses to use a non-KG unit of measurement.

Essentially: Way 1 pulls the last state while Way 2 pulls the last native_value.

override80 commented 1 week ago

@RobertD502 thank you very much!

DieterKoblenz commented 1 week ago

@DieterKoblenz

It is not related to this issue. Based on your logs, the PetKit API doesn't seem to be returning any Pets at the endpoint that is used to fetch pets for an account.

Shoot me a private message on discord as I'll need to investigate why your account isn't returning any pets.

On what Discord can I find you? Sorry to spam this unrelated issue.

RobertD502 commented 6 days ago

@DieterKoblenz

It is not related to this issue. Based on your logs, the PetKit API doesn't seem to be returning any Pets at the endpoint that is used to fetch pets for an account.

Shoot me a private message on discord as I'll need to investigate why your account isn't returning any pets.

On what Discord can I find you? Sorry to spam this unrelated issue.

Same username as here.