SwitchBot gets an open API!

Thank you for the update. It seems like the SwitchBot cloud has been having issues based on what you and @NerdShowAndTell have been experiencing. Hopefully they are all resolved now.

So I kept having these API disconnect issues off and on for the past few days and finally got a chance to troubleshoot. I thought to myself, I bet it's hitting the API request limit.. Sure enough.. Checked the logs

"resp.data = [statusCode:190, body:[:], message:Requests reached the daily limit]"

I had the default of 300 seconds polling set, so I doubled that to 600 to see if this resolves the issues, and I'll then see what effect this has on my automations.

Good catch! They claim a limit of 10,000 interactions per day.

The polled refresh should make about 1 request per device. Any commands you send to the device make at least 2 requests (one for the command, one to refresh). The curtain driver tended to use a lot of requests due to trying to update its status while it was moving, though I have a stripped down version of that pending that should reduce its usage.

My point in all of that was that it should take a lot of polled refreshes to hit the limit, unless you have a ton of devices or maybe if you are using device commands fairly often in your automations.

What kind of devices do you have, and how many of each are there? Are you using my SwitchBot Events app to get real-time events? You could experiment with dialing the polling even farther down to see if the events are giving you the attribute updates that you need.

Hey @tomw , first I have to say, I was using v1.0 for my Switchbot curtains for quite a while, want to say about a year? and your app made having Switchbot worthwhile. THANK YOU.

That said, I had all sorts of problems with the app and Switchbot support finally sent me a sideload 6.16, which took a while, but then provided me the secret key in addition to my token.

But now, only after upgrading, am I getting the API request limit. Like immediately after. Screenshot below.

That said, I've noticed in my device page the same thing I've seen above with other folks having trouble with the 1.1 version; the state variables -- which used to have half a screen of information -- are now empty, screenshot below.

Any advice to troubleshoot from my side?

1 Like

If you clear out the secret key and save on the SwitchBot System device page, it should revert to using v1.0 of the API. Do you still hit the rate limit, and if not then do things work again as they did before you upgraded?

Great question, @tomw , with a complex answer.

Looking at the screenshot, it looks like the daily limit error stopped right at 5pm PT; maybe because of the time zone where SwitchBot is? Don't know. But after 5pm it starts reporting success, which may also be why my curtains shut at night, but don't open in the morning... maybe by then too many calls have been made? Just changed the poll refresh interval to 900 earlier this morning, so I'll see if that has an effect tomorrow morning.

Meanwhile, on the device page, the moment I cleared out the secret key, my device State Variables returned, screenshot below.

Any of this help you?

1 Like

Thanks for the detailed info and description!

The data in State comes from an API access. If you're rate limited you wouldn't get that data. When you save settings on the device page, it clears State and then tries to re-initialize everything.

So, with the rate limit error mysteriously dissipating, it is not possible to tell whether it was switching back to v1.0 or your daily rate limit getting reset that caused it to work again.

You could either stay on v1.0 and see if you get rate limited again. Or switch back to v1.1 to see if the problem returns.

I'll PM you the revised curtain driver that does less polling internally to see if that helps.

1 Like

Absolutely, @tomw, glad it helped!

Huzzah! I pasted the secret key back in and all the State Variables remained. I confirmed I could open and close at least one curtain (I have four pairs; let me know if you want me to test the rest).

So, I only open and close each pair of curtains 4 times a day -- open at sunrise, close if it's too bright, open again a fixed time later, then close at sunset. Four pairs = 8 curtains * 4 times per day = 32 actual calls.

I will definitely install the revised curtain driver, and do you think I could extend the poll time even further, say 1000 or 1200? I don't know how polling affects curtain responsiveness to open/close commands.

Appreciate your responsiveness and support!

1 Like

I posted v0.9.15, which has support for Plug Mini devices.

It has on/off switch control, as well as reporting for voltage, current, and power usage.

Thanks to @themcclure and @BallpointZen for all of your testing help.

1 Like

I can confirm I'm hitting the limit still. It looks like the api is getting polled multiple times a second with my curtains..

I have disabled all automations, and I'm not even controlling the curtains via hubitat right now.

Removed log -

1 Like

That debug print helps a lot.

It looks like their API has a bug - that 'moving' bit is supposed to be a true/false, and it is causing it to spin polling like that (because '60' is always not-false, so the logic in my driver is checking repeatedly trying to see it eventually stop moving)

My new edits to the curtain driver disregards that moving bit entirely. I'm going to push that as an update based on a few people successfully testing it privately.

I'll send it to you in PM to try in the meantime (though, you may have to try tomorrow since you might be rate limited today, ugh, sorry for the trouble).

I edited the driver to use 127.0.0.1 instead of calling their api earlier- to hopefully save some api requests to be able to test today. So I'll give it a try as long as i'm not limited!

1 Like

Ah, good catch on trying to stop the bleeding. At the rate it was running, you probably would have run out of requests between 2.5-3 hours, depending on what else you had going on.

They have been making tons of changes lately, apparently, and with new features often come regressions on existing things.

Which reminds me of some bug reports I read on their GitHub space: if anyone sees any broken behavior with real time events for things like motion and contact sensors, please let me know.

1 Like

Sanity test on the new driver by @NerdShowAndTell and @dghubfan succeeded, so I pushed v0.9.16 live with some improvements to the Curtain driver.

This reduces the internal polling by the curtain driver significantly, which should help with the rate limiting issue some users have seen.

It also adds a preferences option to invert the position and open/close reporting if it is "backwards" from the SwitchBot app, which is something I discussed with @jnosa899 and @kraegd (and possibly others) way back earlier in this thread.

3 Likes

Confirmed! The API is running flawlessly, and curtains are closing at sunset and opening at sunrise. Thanks a ton, @tomw !

2 Likes

It looks like all of the IR Device commands (other than turnOn and turnOff) are being sent with a Command Type of "customize". If they are built-in commands (i.e. from the Command set for virtual infrared remote devices) they won't work.

For example "swing" (on a remote for a fan) should be sent as
commandType="command", command="swing"
but is instead sent as
commandType="customize", command="swing"

This would only work if the user has defined a custom button called "swing" rather than using the built-in Swing button.

1 Like

Good catch.

I checked in v0.9.17 with support for those additional commands (changes to the SwitchBot IR Device driver).

Please let me know if you see any issues after the upgrade.

3 Likes

FYI, from the good folks at SwitchBot:

1 Like

Hi tomw, firstly - thank you! Curtains and Bot are working like a charm! I know this is an old comment but the Color Bulb driver would be awesome, I bought a handful a while ago as an alternative to continuing to grow my Hue collection and for the money they work brilliantly so getting them integrated would be even better

Thanks for your interest. I did write a driver for the Strip Light, which seems nearly identical to the Color Bulb except that the bulb has additional color temperature functionality.

I'll send you a modified version of the driver to test and see if it works with the bulb, and if so I'll have to restructure things a bit to support both properly.

1 Like