hirakujira / homebridge-ambiclimate-platform

Apache License 2.0
5 stars 2 forks source link

Are there anyway to delay the polling action of the plugin? #6

Closed Aekung closed 3 years ago

Aekung commented 3 years ago

Describe The Bug:

The plugin stops working from time to time. I guess the problem is from the polling interval which is too often that the api is blocked by Ambi Climate. Once when the problem happens, the error below would show up in log file

[20/04/2021, 15:19:50] [homebridge-ambiclimate-platform] Get current relative humidity failed.{"description":"unauthorized","error_code":401,"errors":[{"message":"The request failed to provide valid credentials"}]}
(node:27443) UnhandledPromiseRejectionWarning: Error: Too Many Requests

To Reproduce: It happens randomly.

Expected behavior: Extending the polling interval time might solve the problem.

Logs:

[20/04/2021, 15:19:50] [homebridge-ambiclimate-platform] Get current relative humidity failed.{"description":"unauthorized","error_code":401,"errors":[{"message":"The request failed to provide valid credentials"}]}
(node:27443) UnhandledPromiseRejectionWarning: Error: Too Many Requests

Environment:

Raspberry Pi Node.js Version | v14.16.0 NPM Version | v7.6.2 Homebridge Version | 1.3.4 Plugin Version | 1.1.5

hirakujira commented 3 years ago

Actually I don't know why this happens. If you open Home.app and close it again and again, it doesn't show "Too Many Requests" when you do that. Instead, this happens randomly.

Aekung commented 3 years ago

Actually I don't know why this happens. If you open Home.app and close it again and again, it doesn't show "Too Many Requests" when you do that. Instead, this happens randomly.

Does it also happen to you? Or it only happens to me. If so, I might have done something wrong in the setting the causes the problem to occur?

hirakujira commented 3 years ago

Yes this also happens to me. I'm trying to figure out the reason

Aekung commented 3 years ago

Yes this also happens to me. I'm trying to figure out the reason

Thanks for the reply. Hope you could figure out the reason this happens.

My thought would be on the API rate limit. Since Ambi Climate will have to pull the temperature data from the Ambi server now and then (I don't know how often) it might be possible that the plugin tries to pull the data too often that it returns 429 status with too many requests. (I think temperature update is done in the background without having to close and open the Home.app again and again)

According to this document from Ambi Climte https://api.ambiclimate.com/doc/introduction

Rate Limiting
The rate limiting is based on either per-user or per-OAuth-client. For example, assuming the rate limit is 20, the rate limit window is 10 minutes, the last rate limit window ends 10:30 AM, and you make a new request at 10:35 AM.

Per-user

You can make 20 requests from 10:35 AM to 10:45 AM.

Per-OAuth-client

Each of your OAuth clients can make 20 requests from 10:35 AM to 10:45 AM.

If the access exceeds the limit, the server will return 429 HTTP status code.

Standard API rate limits per window
The following table shows the settings for each endpoint.

/login

POST

20 access within 600 seconds. Per-user.

/oauth2/token

GET, POST

10 access within 600 seconds. Per-OAuth-client.

This limit is shared by both token creation and refresh.

I assume that the plugin uses Oauth Clients as a polling method. With this, we can only have 10 access within 600 seconds (10 access within 10 minutes, 1 access / minute) In this case, if we keep pulling the temperature data within limiting the polling interval even 1 minute per time, accessing Home.app once again during that time window could cause the problem?

rn1604 commented 3 years ago

Hi, any solutions to this issue? After a while, I am also unable to control ambi climate via homekit (homebridge) anymore.

rn1604 commented 3 years ago

Hi, any solutions to this issue? After a while, I am also unable to control ambi climate via homekit (homebridge) anymore.

My current solution is to use a smart plug and set a schedule to turn off and on hoobs once every 24h.

hirakujira commented 3 years ago

I saw the API document and I know there's API limit. However I don't think it's behavior follows the document.

You can try this: Open and close Home.app 20 times in 5 minutes (So it calls the API more than the limits, obviously), and you would notice that the plugin still works.

I think it's a bug of API itself. You can try the old plugin, which seems also have same issue, but when it failed, it crashes homebridge so it would restart automatically.

Aekung commented 3 years ago

Hi, any solutions to this issue? After a while, I am also unable to control ambi climate via homekit (homebridge) anymore.

My current solution is to use a smart plug and set a schedule to turn off and on hoobs once every 24h.

You can just cron job homebridge to restart itself every 24Hr if doing so could solve your problem. No need to use additional smart plug for that.

Sadly, the solution wouldn't work for me since the problem happens randomly and most of the time can't be fix by a single restart of homebridge. I need to restart the bridge for few times (3 - 4 times) before the problem is solved.

Aekung commented 3 years ago

I saw the API document and I know there's API limit. However I don't think it's behavior follows the document.

You can try this: Open and close Home.app 20 times in 5 minutes (So it calls the API more than the limits, obviously), and you would notice that the plugin still works.

I think it's a bug of API itself. You can try the old plugin, which seems also have same issue, but when it failed, it crashes homebridge so it would restart automatically.

Yeah, should be raise concern to Ambi Climate team regarding this?

Aekung commented 3 years ago

Hi, any solutions to this issue? After a while, I am also unable to control ambi climate via homekit (homebridge) anymore.

My current solution is to use a smart plug and set a schedule to turn off and on hoobs once every 24h.

With your idea of solving the problem, I twisted it a bit to check if it would work for me

I set cron job to restart homebridge-ambilicate-platform only on every 5 hours with

0 */5 * * * kill -15 $(pidof 'homebridge: homebridge-ambiclimate-platform')

Will see if doing this would prevent the plugin to not working properly as it is now. Will keep you guys update here.

Thank you.

Aekung commented 3 years ago

Just want to update that restarting the plugin with cron job for every 5 hours seems to fix the problem. The plugin works very well without crashing for 3 days and this had never happened before implementing the cron job reset schedule.

I would suggest this as a temporarily solution.

hirakujira commented 3 years ago

I believe that the latest version (v1.1.6) fixes this issue. :)

Aekung commented 3 years ago

I believe that the latest version (v1.1.6) fixes this issue. :)

I updated since day 1 you released but I still have my conjob set to restart plugin in interval and havent had any issue since then so I didnt notice that the latest update is meant to fix this issue.

If it’s okay to say, can you tell what’s the cause of the problem?

hirakujira commented 3 years ago

Yes, I didn't mention that until now because I was checking whether it fixed that bug. I just use your great idea. Now the plugin re-logins and gets new tokens every 6 hours. https://github.com/hirakujira/homebridge-ambiclimate-platform/blob/master/src/platform.ts#L54

Aekung commented 3 years ago

Oh wow. Glad the plugin finally works without a problem now. You are the best!

I’m gonna remove the cronjob and try the plugin alone. Thanks ^^

hirakujira commented 3 years ago

Yeah, please try it and tell me whether it works for you, too. And sorry for the delay for this simple fix because I moved to another place few weeks ago and just setup the AmbiClimate device in my new place recently.

Aekung commented 3 years ago

Done! Will check for a while and feedback to you later =)