Welcome to Tesla Motors Club
Discuss Tesla's Model S, Model 3, Model X, Model Y, Cybertruck, Roadster and More.
Register

Trouble Waking Model 3 with API

This site may earn commission on affiliate links.
I've been building a python script to allow me to schedule charging to begin based on a target completion time. It usually works but cannot be relied on. My flow is to issue the wake_up command, wait 30 seconds, check if car is asleep or online, re-issue the wake_up if it's asleep. Loop until it comes back as online then proceed with other API calls. The car usually wakes up within 2-3 loops, especially when testing.

At night, when I'm in bed and it HAS to work in order to start charging it sometimes works but often fails. My logging shows me 20+ iterations with the API returning "asleep" every time. I've tried building in periods of longer wait times between iterations (5 min.) Last night I started trying to wake the car at 3:12am and it tried until 4:31am when the car's scheduled charging function woke it up (not the API call). Model 3 just wouldn't wake up by API.

Since my code works most of the time I'm unsure what else I can do to wake the car. Question for anybody using anything to automate some API calls (doesn't have to be Python, it could be automate, tasker, Dashboard for Tesla etc) is whether or not you've experienced difficulty waking Model 3 and more importantly did you resolve it?
 
  • Informative
Reactions: Missile Toad
Please do not take this as a negative comment at all. I’m intrigued by what you are trying to do. Just curious; why not just use the car’s built in charge timer to start charging sometime in the evening. Thank you for letting me know and good luck in making your application work.
Ah fair question! I'm in Canada where it's a touch chillier. Ideally I'd like to time my charging to finish just before I'm ready to leave in the morning so the battery is warm. I use the built-in start-time scheduler as a backup since my script/API is unreliable but it would require that I manually calculate and adjust the start time every day.

Also, I'm not a programmer so I used this as a learning exercise with a practical purpose.
 
Ah fair question! I'm in Canada where it's a touch chillier. Ideally I'd like to time my charging to finish just before I'm ready to leave in the morning so the battery is warm. I use the built-in start-time scheduler as a backup since my script/API is unreliable but it would require that I manually calculate and adjust the start time every day.

Also, I'm not a programmer so I used this as a learning exercise with a practical purpose.

Sounds like that would be a good feature for Tesla to add - e.g. instead of setting the start time - set the end time and have the car calculate approximately when it would need to wake up to charge to current limit set. Course I am not holding my breath on that one - but it is a great idea for those who are in the colder climates.
 
@Bandit I'm also writing a Python script so that I can collect statistics concerning my charging habits. I wonder if you would be willing to share your code used to present the bearer token since I'm currently stuck at that point? I have been able to present my login credentials and obtain the token -- I'm just unable to actually do anything productive now that I have the token. I've visited the following sites that give ample details about how the unofficial API works. But they are skimpy on Python implementations:
  1. Vehicles - Tesla JSON API (Unofficial)
  2. Why does Bearer not work in requests authorization header?
  3. WWW.getpostman.com
  4. A Beginner’s Guide to the Unofficial Tesla API – Jimmy Huang – Medium
Also, my vague impression is that very little heat is added to the car when it tapers down to 2-3 kW. As someone who likely experiences 32ºF (0ºC) overnight, what is the minimum power-level for the battery to appreciably warm-up (and enjoy, at least less, problems with regeneration and initially drive)?
 
I intend to share the entire program openly, it's currently in a bad state though, I need to really clean it up. Besides, if it doesn't work 100% it's not useful. The code from Cold Temperatures - how much does it effect charge times? was my starting point but I wanted to extend it to eliminate the need to ever provide username & password.

Re the bearer token, I create my headers using the following two lines:
accessToken_hdr = 'Bearer ' + access_token
head = {'Authorization': accessToken_hdr, 'Content-Type': 'application/json; charset=utf-8'}

Sounds like that would be a good feature for Tesla to add - e.g. instead of setting the start time - set the end time and have the car calculate approximately when it would need to wake up to charge to current limit set. Course I am not holding my breath on that one - but it is a great idea for those who are in the colder climates.

Fully agree! There are a number of threads suggesting that dating back years. So what a great excuse to have fun and try to hack something up. I've never had a car with a RESTful API before.

There are a ton of discussions going on in the Canada forum around Model 3s that are having door handles, windows, and charge ports all freeze up (even in garaged cars) so I may need to extend the logic to initiate pre-heat automatically as well.
 
  • Informative
Reactions: Missile Toad
I intend to share the entire program openly, it's currently in a bad state though, I need to really clean it up. Besides, if it doesn't work 100% it's not useful. The code from Cold Temperatures - how much does it effect charge times? was my starting point but I wanted to extend it to eliminate the need to ever provide username & password.

Re the bearer token, I create my headers using the following two lines:
accessToken_hdr = 'Bearer ' + access_token
head = {'Authorization': accessToken_hdr, 'Content-Type': 'application/json; charset=utf-8'}



Fully agree! There are a number of threads suggesting that dating back years. So what a great excuse to have fun and try to hack something up. I've never had a car with a RESTful API before.

There are a ton of discussions going on in the Canada forum around Model 3s that are having door handles, windows, and charge ports all freeze up (even in garaged cars) so I may need to extend the logic to initiate pre-heat automatically as well.

Yep - very cool that the car has a programming interface - hope you are able to get your code working - I would offer assistance but my expertise is not Python unfortunately - although I have coded in Python in the past - not something I have worked with in the last few years unfortunately.
 
I’m interested in how you are calculating the start time. Since the length of time is highly dependent on the outdoor air temp. Sometimes when it is near or below 0 deg F, it takes 20-30 minutes before it will even start to take a charge.
 
I just started playing with the scheduler in TeslaFi and ran into this issue. Seems able to send commands to an asleep car during the day, but with the overnight sleep it would not wake up this morning to charge. TeslaFi will make 5 attempts before excepting defeat. I was hoping issuing a wakeup command before hand may help this issue, but your experience isn't giving me much hope. Currently on 42.2.1.

I also have issues waking up the car with the app at certain times, only fixed by toggling the lock over bluetooth.
 
I’m interested in how you are calculating the start time. Since the length of time is highly dependent on the outdoor air temp. Sometimes when it is near or below 0 deg F, it takes 20-30 minutes before it will even start to take a charge.
I've been using the simple formula shared in this post Battery preheat released and found it to be pretty accurate so far.

I just started playing with the scheduler in TeslaFi and ran into this issue. Seems able to send commands to an asleep car during the day, but with the overnight sleep it would not wake up this morning to charge. TeslaFi will make 5 attempts before excepting defeat. I was hoping issuing a wakeup command before hand may help this issue, but your experience isn't giving me much hope. Currently on 42.2.1.

I also have issues waking up the car with the app at certain times, only fixed by toggling the lock over bluetooth.

This observation about TeslaFi as well as another thread I saw with reports of being unable to wake the car using the app at all lead me to believe the issue isn't something I can correct. After all, a couple of nights ago my app sent the wake up command 30ish times over an hour or more and it still didn't wake up until the pre-configured scheduled charging time arrived.
 
I just started creating a Python script using this library gglockner/teslajson

And so far so good.

Had no issues waking up the car right at the first call. But right now my script doesn't need to wake up since it's purpose is to collect battery data.

One thing I noticed is that the login session looks to expire when car goes to sleep. So a fresh login is required to communicate with the car.
 
This observation about TeslaFi as well as another thread I saw with reports of being unable to wake the car using the app at all lead me to believe the issue isn't something I can correct. After all, a couple of nights ago my app sent the wake up command 30ish times over an hour or more and it still didn't wake up until the pre-configured scheduled charging time arrived.

Yes, the current (and last few?) firmware has a bug where the car will sometimes permanently disconnect from LTE and wifi during sleep, so it can't be woken up over the internet. Once in this state, the only way to wake the car is to interact with it physically or over bluetooth---go out and pull a door handle (even if the door doesn't open) or let the phone key get into the "Connected" state, and unlock/lock the car from the app. A few seconds after that the car can be reached through the Internet.

This happens to me in my garage with the car plugged in (after charging completes), in my work parking lot, and anywhere else it is parked for a few hours. It doesn't always happen. For example, my Tasker based automation to accomplish the same thing as your script fired this morning without a problem, but now, a few hours later, the car is in the sleep of death.

Please don't let this discourage you from completing your python script, as it sounds extremely useful. It is something I would like to use. Hopefully Tesla will have this bug squashed by the time your script is ready.
 
I'm wondering is someone would be willing to help out a Python rookie at all this. I know others have already created a teslajson library that would handle all this for me, but I'm actually trying not to use those libraries to force myself how to learn all the gory details around how to actually do the fundamental response.post and response.get calls. Yeah, I know I'm reinventing the wheel, but if I don't force myself to really learn this stuff, then I won't get to where I want.

I've successfully generated a token via the request.post to https://owner-api.teslamotors.com/oauth/token using my username and password. Where I'm stuck is on how to pass the token in the auth section to get my vehicle data.

My basic script have variables defined for the client_id, client_secret, email, password, and the oauth_url. From there here's how I request the token and then put it into a variable token. Then try to use that

req = requests.post(oauth_url, data={'grant_type': 'password', 'client_id': client_id, 'client_secret': client_secret, 'email': email, 'password': password})
oauth_resp = req.json()
token = oauth_resp['access_token']​

That all seems to work fine, but I'm stuck with how to correctly pass the token when I try to get my list of vehicles.

vehicle_url = 'https://owner-api.teslamotors.com/api/1/vehicles'
vehicle = requests.get('https://owner-api.teslamotors.com/api/1/vehicles', auth=("bearer", token))​

Python takes this requests.get but I get back a 401 status_code indicating to me that I'm doing something wrong with getting it to accept the authentication.

Any help pointing out the basic errors for the newbie at this would be much appreciated.

Paul.
 
I'm wondering is someone would be willing to help out a Python rookie at all this. I know others have already created a teslajson library that would handle all this for me, but I'm actually trying not to use those libraries to force myself how to learn all the gory details around how to actually do the fundamental response.post and response.get calls. Yeah, I know I'm reinventing the wheel, but if I don't force myself to really learn this stuff, then I won't get to where I want.

I've successfully generated a token via the request.post to https://owner-api.teslamotors.com/oauth/token using my username and password. Where I'm stuck is on how to pass the token in the auth section to get my vehicle data.

My basic script have variables defined for the client_id, client_secret, email, password, and the oauth_url. From there here's how I request the token and then put it into a variable token. Then try to use that

req = requests.post(oauth_url, data={'grant_type': 'password', 'client_id': client_id, 'client_secret': client_secret, 'email': email, 'password': password})
oauth_resp = req.json()
token = oauth_resp['access_token']​

That all seems to work fine, but I'm stuck with how to correctly pass the token when I try to get my list of vehicles.

vehicle_url = 'https://owner-api.teslamotors.com/api/1/vehicles'
vehicle = requests.get('https://owner-api.teslamotors.com/api/1/vehicles', auth=("bearer", token))​

Python takes this requests.get but I get back a 401 status_code indicating to me that I'm doing something wrong with getting it to accept the authentication.

Any help pointing out the basic errors for the newbie at this would be much appreciated.

Paul.

I have limited Python experience but from my tinkering with the API with postman, I'm able to make requests with the token in the request headers. Authorization : Bearer {your token}.
 
I'm wondering is someone would be willing to help out a Python rookie at all this. I know others have already created a teslajson library that would handle all this for me, but I'm actually trying not to use those libraries to force myself how to learn all the gory details around how to actually do the fundamental response.post and response.get calls. Yeah, I know I'm reinventing the wheel, but if I don't force myself to really learn this stuff, then I won't get to where I want.

I've successfully generated a token via the request.post to https://owner-api.teslamotors.com/oauth/token using my username and password. Where I'm stuck is on how to pass the token in the auth section to get my vehicle data.

My basic script have variables defined for the client_id, client_secret, email, password, and the oauth_url. From there here's how I request the token and then put it into a variable token. Then try to use that

req = requests.post(oauth_url, data={'grant_type': 'password', 'client_id': client_id, 'client_secret': client_secret, 'email': email, 'password': password})
oauth_resp = req.json()
token = oauth_resp['access_token']​

That all seems to work fine, but I'm stuck with how to correctly pass the token when I try to get my list of vehicles.

vehicle_url = 'https://owner-api.teslamotors.com/api/1/vehicles'
vehicle = requests.get('https://owner-api.teslamotors.com/api/1/vehicles', auth=("bearer", token))​

Python takes this requests.get but I get back a 401 status_code indicating to me that I'm doing something wrong with getting it to accept the authentication.

Any help pointing out the basic errors for the newbie at this would be much appreciated.

Paul.
You are using the same library, requests, that I am using. For me, I simply add this to my header dictionary (which also has the User-Agent header): self.headers['Authorization'] = 'Bearer ' + self.accessToken and then add headers=self.headers to the get method.
 
WRT the original OP, I am having the same issue but I one extra observation. On one occasion, with the car plugged in, the app did wake-up the car. I then let it go asleep (few minutes) and try my python program, which failed. From that moment, the app is also not able to wake-up the car.

Is the fact that the app was first able to wake-up the car indicate that Tesla is now using a slightly different API call?
 
I seem to be having this same problem as well. All post calls to /wake_up return the 408 error, no matter how much I mash the "Send" button in Postman.

Code:
{"response":null,"error":"vehicle unavailable: {:error=>\"vehicle unavailable:\"}","error_description":""}

The car is reachable because the app wakes it up instantly, I just can't wake up through the API. All other endpoints that I've tried (e.g. refresh token, list vehicles, drive state, charge state, open/close charge port, etc.) function as desired when the car is awake.

Is there any other settings besides the standard auth token that I should be doing/sending?
 
FWIW I have had 100% success so far in waking the car via API. Could you confirm what endpoint you’re hitting?
Code:
https://owner-api.teslamotors.com/api/1/vehicles/{{Vehicle ID}}/command/wake_up
I requested help in the master Tesla Model S REST API thread, and tried out TeslaJS, which worked perfectly (but I am not going to try to put TeslaJS on arduino). I'm using Postman for testing before programming on the arduino, and haven't been able to figure out why it works with one but not the other.

I may try to use Fiddler to find what the full requests being sent are, and how they are different, but haven't gotten back to working on software yet; shifted to the hardware design/building for a robot to charge the car automatically when it pulls into the garage. The API usage is to support that.