nfarina / homebridge-tesla

Tesla plugin for homebridge: https://github.com/nfarina/homebridge
154 stars 38 forks source link

Unable to wake up car – plugin falsely sees it as awake #109

Closed FlyingLemming closed 1 year ago

FlyingLemming commented 1 year ago

While my Model S is awake, the plugin works perfectly. I can see the online status via the Connection switch as well as the status of HVAC and steering wheel heater (the only accessories I configured active). Everything reacts and syncs back and forth as intended, whether I change settings in the Home app or in the Tesla app.

When the car is asleep, however, no action within the Home app is able to wake it. Even when activating the Connection switch, the switch will change to off again after updating the display in the app, e.g. by switching to another room and back. Via other means I can confirm that the car indeed remains asleep all the time. By using either the official Tesla iOS app, the Tesla Remote iOS app (which I use for Siri Shortcuts), or Watch for Tesla on my Apple Watch, the car wakes up within 10–15 seconds and afterwards all controls are updated and become functional in the Home app – until it falls asleep again.

The strange thing is that the Homebridge log repeatedly states "Tesla reports vehicle is awake" – even when it obviously is not and the Connection switch is off in the Home app. I guess this is the reason why the plugin does not even bother waking up the car as it thinks it already is awake. Here's an excerpt of the log: I tried waking up the car twice via the Connection switch (at 20:00:54 and 20:06:39) and once tried to directly activate the HVAC at 20:13:51. In between, I frequently updated the Home app by either changing to another room and back or by force-shutting and restarting it. I also checked the actual status of the car frequently and can confirm that it never woke up.

Feb 04 19:57:52 HomePi1 homebridge[23303]: [2/4/2023, 19:57:52] [Bruce] Getting latest vehicle data from Tesla…
Feb 04 20:00:51 HomePi1 homebridge[23303]: [2/4/2023, 20:00:51] [Bruce] Tesla reports vehicle is awake.
Feb 04 20:00:51 HomePi1 homebridge[23303]: [2/4/2023, 20:00:51] [Bruce] Getting latest vehicle data from Tesla…
Feb 04 20:00:54 HomePi1 homebridge[23303]: [2/4/2023, 20:00:54] [Bruce] Tesla reports vehicle is awake.
Feb 04 20:00:54 HomePi1 homebridge[23303]: [2/4/2023, 20:00:54] [Bruce] Vehicle is awake.
Feb 04 20:00:54 HomePi1 homebridge[23303]: [2/4/2023, 20:00:54] [Bruce] Tesla reports vehicle is awake.
Feb 04 20:00:54 HomePi1 homebridge[23303]: [2/4/2023, 20:00:54] [Bruce] Getting latest vehicle data from Tesla (forced update)…
Feb 04 20:01:34 HomePi1 homebridge[23303]: [2/4/2023, 20:01:34] [Bruce] Tesla reports vehicle is awake.
Feb 04 20:01:34 HomePi1 homebridge[23303]: [2/4/2023, 20:01:34] [Bruce] Getting latest vehicle data from Tesla…
Feb 04 20:06:16 HomePi1 homebridge[23303]: [2/4/2023, 20:06:16] [Bruce] Tesla reports vehicle is awake.
Feb 04 20:06:16 HomePi1 homebridge[23303]: [2/4/2023, 20:06:16] [Bruce] Getting latest vehicle data from Tesla…
Feb 04 20:06:32 HomePi1 homebridge[23303]: [2/4/2023, 20:06:32] [Bruce] Tesla reports vehicle is awake.
Feb 04 20:06:32 HomePi1 homebridge[23303]: [2/4/2023, 20:06:32] [Bruce] Getting latest vehicle data from Tesla…
Feb 04 20:06:37 HomePi1 homebridge[23303]: [2/4/2023, 20:06:37] [Bruce] Tesla reports vehicle is awake.
Feb 04 20:06:37 HomePi1 homebridge[23303]: [2/4/2023, 20:06:37] [Bruce] Getting latest vehicle data from Tesla…
Feb 04 20:06:39 HomePi1 homebridge[23303]: [2/4/2023, 20:06:39] [Bruce] Vehicle is awake.
Feb 04 20:06:40 HomePi1 homebridge[23303]: [2/4/2023, 20:06:40] [Bruce] Tesla reports vehicle is awake.
Feb 04 20:06:40 HomePi1 homebridge[23303]: [2/4/2023, 20:06:40] [Bruce] Getting latest vehicle data from Tesla (forced update)…
Feb 04 20:07:07 HomePi1 homebridge[23303]: [2/4/2023, 20:07:07] [Bruce] Tesla reports vehicle is awake.
Feb 04 20:07:07 HomePi1 homebridge[23303]: [2/4/2023, 20:07:07] [Bruce] Getting latest vehicle data from Tesla…
Feb 04 20:13:42 HomePi1 homebridge[23303]: [2/4/2023, 20:13:42] [Bruce] Tesla reports vehicle is awake.
Feb 04 20:13:42 HomePi1 homebridge[23303]: [2/4/2023, 20:13:42] [Bruce] Getting latest vehicle data from Tesla…
Feb 04 20:13:51 HomePi1 homebridge[23303]: [2/4/2023, 20:13:51] [Bruce] Tesla reports vehicle is awake.
Feb 04 20:13:51 HomePi1 homebridge[23303]: [2/4/2023, 20:13:51] [Bruce] Turning on climate control…
Feb 04 20:13:51 HomePi1 homebridge[23303]: [2/4/2023, 20:13:51] [Bruce] Error while executing command: undefined
Feb 04 20:14:26 HomePi1 homebridge[23303]: [2/4/2023, 20:14:26] [Bruce] Tesla reports vehicle is awake.
Feb 04 20:14:26 HomePi1 homebridge[23303]: [2/4/2023, 20:14:26] [Bruce] Getting latest vehicle data from Tesla…
Feb 04 20:14:39 HomePi1 homebridge[23303]: [2/4/2023, 20:14:39] [Bruce] Tesla reports vehicle is awake.
Feb 04 20:14:39 HomePi1 homebridge[23303]: [2/4/2023, 20:14:39] [Bruce] Getting latest vehicle data from Tesla…
Feb 04 20:14:49 HomePi1 homebridge[23303]: [2/4/2023, 20:14:49] [Bruce] Tesla reports vehicle is awake.

I have a feeling that there's a little glitch within the plugin which falsely treats the car as always awake and, therefore, never attempts to wake it before executing commands.

FlyingLemming commented 1 year ago

I just noticed that there is an error message upon restarting Homebridge:

Feb 04 21:17:20 HomePi1 homebridge[13920]: [2/4/2023, 21:17:20] [Bruce] Initializing Tesla accessory...
Feb 04 21:17:21 HomePi1 homebridge[13920]: [2/4/2023, 21:17:21] [Bruce] Tesla reports vehicle is awake.
Feb 04 21:17:21 HomePi1 homebridge[13920]: [2/4/2023, 21:17:21] [Bruce] Getting latest vehicle data from Tesla…
Feb 04 21:17:21 HomePi1 homebridge[13920]: TeslaJS error while executing "vehicleData": undefined Error response: 408

No idea if this is connected to the mentioned issue.

nfarina commented 1 year ago

So when the plugin says "Tesla reports vehicle is awake" that is reading a value from Tesla after calling their API directly. I do cache the result of this API call for 2.5 seconds, so it's possible that it would give the wrong answer very rarely, but typically the car doesn't "flip flop" from awake to sleep quickly, so answers are generally reliable.

I can't say exactly why this is happening to you but it feels very similar to the kind of behavior I had when my car didn't have a great connection to wifi. I have a Model 3 and a Model Y and the Y in particular required a very strong wifi signal - like, I had to physically put an access point right inside the driveway-facing wall of my house. And I considered my wifi network to be very good already (none of my other 100s of wifi clients had any problems whatsoever). But moving the AP fixed the problem for good.

So if it's possible to get an access point closer to the car even temporarily, it would be worth testing.

FlyingLemming commented 1 year ago

Hello Nick,

I guess I found the issue.

Unfortunately, the JS code resulting from the Babel trans-compilation is very hard to read. But I finally found the right spot for inserting one line into index.js to check the raw data:

  2483: _this.log(`Tesla reports vehicle is ${options.isAsleep ? "sleeping" : "awake"}.`);
+ 2484: _this.log(`Debug: options.isAsleep has value ${options.isAsleep} - state has value ${state}`);

Here's the result:

Feb 08 20:13:34 HomePi1 homebridge[21792]: [2/8/2023, 20:13:34] [Bruce] Tesla reports vehicle is awake.
Feb 08 20:13:34 HomePi1 homebridge[21792]: [2/8/2023, 20:13:34] [Bruce] Debug: options.isAsleep has value false - state has value offline

When looking at api.ts it is obvious what's going wrong. There you check state for being "asleep" or not:

  65: const options = { authToken, vehicleID, isAsleep: state === "asleep" };

However, my car reports "offline" instead of "asleep". This sets options.isAsleep the wrong way round and this causes the false log entries as well as the missing wake-up call before executing commands.

After waking up the car via the Tesla iOS app, the plugins reports this:

Feb 08 20:29:10 HomePi1 homebridge[25957]: [2/8/2023, 20:29:10] [Bruce] Tesla reports vehicle is awake.
Feb 08 20:29:10 HomePi1 homebridge[25957]: [2/8/2023, 20:29:10] [Bruce] Debug: isAsleep has value false - state has value online

Maybe this is a Model S/X-thing to report online/offline instead of asleep/awake? Anyway, in ConnectionServices.ts the status received from the Tesla API is processed differently:

  30: const { state } = await tesla.getVehicle();
  31: const on = state === "online";

That's the reason why the "Connection" button always shows the correct status even though the rest of the plugin got it wrong.

Consequently, I modified this line in index.ts as a temporary workaround:

  2481: isAsleep: state === "offline"

As expected, my car reports being sleeping in the logs now and wakes up nicely when I change the HVAC settings within the Home app. :-)

Feb 08 20:47:21 HomePi1 homebridge[29857]: [2/8/2023, 20:47:21] [Bruce] Tesla reports vehicle is sleeping.
Feb 08 20:47:21 HomePi1 homebridge[29857]: [2/8/2023, 20:47:21] [Bruce] Vehicle is asleep; using default state.
Feb 08 20:47:23 HomePi1 homebridge[29857]: [2/8/2023, 20:47:23] [Bruce] Tesla reports vehicle is sleeping.
Feb 08 20:47:23 HomePi1 homebridge[29857]: [2/8/2023, 20:47:23] [Bruce] Sending wakeup command…
Feb 08 20:47:24 HomePi1 homebridge[29857]: [2/8/2023, 20:47:24] [Bruce] Waiting for vehicle to wake up...
Feb 08 20:47:25 HomePi1 homebridge[29857]: [2/8/2023, 20:47:25] [Bruce] Waiting for vehicle to wake up...
Feb 08 20:47:27 HomePi1 homebridge[29857]: [2/8/2023, 20:47:27] [Bruce] Waiting for vehicle to wake up...
Feb 08 20:47:29 HomePi1 homebridge[29857]: [2/8/2023, 20:47:29] [Bruce] Tesla reports vehicle is sleeping.
Feb 08 20:47:29 HomePi1 homebridge[29857]: [2/8/2023, 20:47:29] [Bruce] Sending wakeup command…
Feb 08 20:47:29 HomePi1 homebridge[29857]: [2/8/2023, 20:47:29] [Bruce] Waiting for vehicle to wake up...
Feb 08 20:47:30 HomePi1 homebridge[29857]: [2/8/2023, 20:47:30] [Bruce] Vehicle is now awake.
Feb 08 20:47:30 HomePi1 homebridge[29857]: [2/8/2023, 20:47:30] [Bruce] Setting climate temperature to 21.5°C…
Feb 08 20:47:31 HomePi1 homebridge[29857]: [2/8/2023, 20:47:31] [Bruce] Tesla reports vehicle is awake.
Feb 08 20:47:31 HomePi1 homebridge[29857]: [2/8/2023, 20:47:31] [Bruce] Getting latest vehicle data from Tesla (forced update)…
Feb 08 20:47:31 HomePi1 homebridge[29857]: [2/8/2023, 20:47:31] [Bruce] Vehicle is now awake.
Feb 08 20:47:31 HomePi1 homebridge[29857]: [2/8/2023, 20:47:31] [Bruce] Turning on climate control…
Feb 08 20:47:35 HomePi1 homebridge[29857]: [2/8/2023, 20:47:35] [Bruce] Vehicle data updated.
Feb 08 20:47:35 HomePi1 homebridge[29857]: [2/8/2023, 20:47:35] [Bruce] Tesla reports vehicle is awake.

I have no idea whether other or newer Teslas report their state differently. You probably want to check for both "asleep" and "offline" for setting options.isAsleep in order to cover both versions.

nfarina commented 1 year ago

Wow! Thank you so much for doing that detective work! I have never seen the value "offline" before and from some light Googling it seems to be a newer (and confusing) state for other apps as well.

I just published a new version of the plugin that unifies all checks to be state === "online" and any other value is treated as asleep/offline.

FlyingLemming commented 1 year ago

Awesome, thank you for the quick response! And for the whole plugin, of course. Never mind Homebridge itself. 😊

nfarina commented 1 year ago

Happy to oblige, and glad you are finding the whole thing useful!