RobertD502 / home-assistant-petkit

Home Assistant integration for PetKit devices
MIT License
184 stars 28 forks source link

Enhanced logging around pet feeding #24

Closed npawelek closed 1 year ago

npawelek commented 1 year ago

Not sure if this is how I utilize my petkit fresh element feeder or if it's something that others are potentially dealing with too. I have feedings that are set for specific times, but what I'm seeing is that the device frequently goes unavailable because of petkit servers or the the API might be down for maintenance, resulting in the feeding never happening. Currently there's no good way to view this at an device/entity level, so I find myself checking a camera at the time of the feeding and listening for the dispensing food. This brings me to my question, is it possible to add a log entry when food is actually dispensed?

RobertD502 commented 1 year ago

Any reason you haven't programmed the feeding schedule directly on the PETKIT app? By setting a feeding schedule on the app, before using the integration, that feeding schedule gets written directly on the feeder and the feeder handles the execution locally without the need to communicate with PETKIT servers.

If you are automating feedings via Home Assistant: When a feeding is supposed to happen, you can set up a condition that checks to see if the entity is available at that time. If not, send a notification to yourself.

Sending a service call to an unavailable entity should already be logged by Home Assistant. Not sure how to incorporate the event log into a HA automation, but I do know that in Node Red you can utilize the catch Node to then trigger sending yourself a notification if a service call resulted in an error (such as sending a service call to an unavailable entity).

Edit:

Depending on what feeder you have, if a manual feeding happened (which this is if you're sending a feeding via Home Assistant), it would increment both the manually dispensed and times dispensed entities.

npawelek commented 1 year ago

Appreciate the response, @RobertD502. I didn't realize this was the case with pre-programming the feeding schedule in the Petkit app, so thanks for that piece of information!

To add a bit of detail, I'm utilizing node-red to initiate a feeding schedule based on a helper boolean that determines if I'm home/away. The boolean ultimately determines how many times food is dispensed that day. Typically, if I'm home I will supplement with soft food, which results in less feedings overall. I do have some logic to wait and retry when the entity is unavailable, but the main issue is that when calling the select entity, nothing is logged to the entity itself (which you'd typically expect). For instance, my automation initiated at 6AM, the entity was offline until 6:09AM, at which time the service call occurred. In my logbook, I see the entity become available, but nothing that indicated dispensing. I have the Fresh Element Mini Pro, which doesn't seem to have entities for dispensing.

RobertD502 commented 1 year ago

Ah yes, the mini pro doesn't return number of grams fed and number of feedings dispensed from the API like the others.

To get around your problem, set up a catch Node for a failed service call that leads into a predetermined timer after which the service call is retried. You can even have the catch Node also propagate into a notification service call to send yourself a notification if a feeding fails.

Edit:

Example of catch nodes that I have for my Soma smart shades sunrise/sunset flow (they use a local API but sometimes the service call fails).

image

npawelek commented 1 year ago

Thanks, I guess that confirms things. I'll add some logic to catch the API failure and setup a text sensor that can be updated when the call service succeeds.

RobertD502 commented 1 year ago

@npawelek Disregard. Just tested it out by unplugging one of my feeders and sending a service call to an unavailable entity. Home Assistant did not raise an error as it should have - looks like they haven't implemented the logic for raising an exception when attempting to call a service on an unavailable entity.

RobertD502 commented 1 year ago

Just attempted to raise an error myself if the entity is unavailable, a user sends a service call to select an option, and it is completely ignored. Also tried standard logging and that is ignored as well. Looks like the only thing that can be done is to have your flow check the state of the entity, if it is unavailable, wait a period to time and then resend the service call (+ possibly send yourself a notification). There is no exception that will be caught by the catch node since Home Assistant isn't properly handling unavailable entities and completely ignored if I try to raise an exception. So, ultimately, it is up to Home Assistant to fix this, but use the logic I described above to keep resending the command when the entity is unavailable (add a stop watch between sending service calls since the possibility of the entity becoming available isn't until new data is fetched based on your polling interval).

npawelek commented 1 year ago

Correct, the API failures that I noted have only been observed if the PetKit servers are down for maintenance. In terms of the unavailable status, the select doesn't have a status other than available, so checking to ensure it's not unavailable is the only thing that's been successful.

RobertD502 commented 1 year ago

That'll be the only thing that can be done for now until it is fixed on Home Assistant's side since the way they are handling service calls is also ignoring me trying to manually raise an exception within the async_select_option method for the manual feeding entity when the entity is unavailable.