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

Model S REST API

This site may earn commission on affiliate links.
I have read lots (certainly not all) of this thread and have a few questions for anyone looking to help me out....
Disclaimer :I am not a coding expert. I mostly write windows based command line scripts, some perl, some VBA.

  1. Will this API work on the Model 3?
  2. If my needs are simple ... I just want to log my vehicle charging data, is this solution of scripts already exist somewhere?
I have a server that I can schedule scripts to run at any interval, but I was thinking of just logging right before my scheduled charging starts and then right before I leave when I am sure that the charging is complete? I would like to write some scripts using cURL to accomplish this.... Sorry if this is a newb post!

Yes, the API works with Model 3.

I would just sign up with Teslafi.com and let the site take care of all the hard work, 24x7. Well worth the $50/year fee.
 
Yes, the API works with Model 3.

I would just sign up with Teslafi.com and let the site take care of all the hard work, 24x7. Well worth the $50/year fee.
That's one approach. I have a few reservations about Teslafi and vampire drain.... it seems like it gets an awful lot of data. Also $50/year is not free and too much for me (I'm a model 3 owner not an MS/MX owner ;). If I keep my vehicle for 10 years that's $500 to Tesalfi. I think I've done most of the hard work.....I can pull the logs I'm interested in, I just need to understand why my wake_up command isn't working. If I can get that licked, I think I can just wake it up and grab my log data once or twice a day.
 
That's one approach. I have a few reservations about Teslafi and vampire drain.... it seems like it gets an awful lot of data. Also $50/year is not free and too much for me (I'm a model 3 owner not an MS/MX owner ;). If I keep my vehicle for 10 years that's $500 to Tesalfi. I think I've done most of the hard work.....I can pull the logs I'm interested in, I just need to understand why my wake_up command isn't working. If I can get that licked, I think I can just wake it up and grab my log data once or twice a day.

If you have a windows PC, my app will do what you need. Supports Model 3 too.
 
I have read lots (certainly not all) of this thread and have a few questions for anyone looking to help me out....
Disclaimer :I am not a coding expert. I mostly write windows based command line scripts, some perl, some VBA.

  1. Will this API work on the Model 3?
  2. If my needs are simple ... I just want to log my vehicle charging data, is this solution of scripts already exist somewhere?
I have a server that I can schedule scripts to run at any interval, but I was thinking of just logging right before my scheduled charging starts and then right before I leave when I am sure that the charging is complete? I would like to write some scripts using cURL to accomplish this.... Sorry if this is a newb post!

Do you know PowerShell? I have a script that queries all the data points on the API and writes the result to SQL.
 
Very interested!

Here's the code. I removed the libraries and made it all functions, changed the switches to static variables at the top you'll need to set with your information. It's very repetitive now but it'll make it easier to understand. The workflow is pretty simple.

Check if there is an encrypted token saved
Generate one, encrypt it and save it locally if there isn't one already
Decrypt the token from file and verify it hasn't expired
Query the state of the vehicle (quit if asleep)
Write results to SQL

Let me know if you get stuck.

EDIT: Forums is doing some weird stuff with the PasteBin link (Remove the spaces from below)
h t t p s : / / pastebin.com/NxJs7i8M

EDIT on the EDIT: I also forgot to mention, I have an S so i'm only saving the metrics I care about to SQL, I know the API returns more than is being written but I simplified the code to where you can easily add more metrics for an X or 3 if you wanted to, oh and change the encryption key after you save it locally since it's posted on the internet (just update the numbers on the encryption and decryption functions).
 
Last edited:
  • Helpful
Reactions: insaneoctane
I have read lots (certainly not all) of this thread and have a few questions for anyone looking to help me out....
Disclaimer :I am not a coding expert. I mostly write windows based command line scripts, some perl, some VBA.

  1. Will this API work on the Model 3?
  2. If my needs are simple ... I just want to log my vehicle charging data, is this solution of scripts already exist somewhere?
I have a server that I can schedule scripts to run at any interval, but I was thinking of just logging right before my scheduled charging starts and then right before I leave when I am sure that the charging is complete? I would like to write some scripts using cURL to accomplish this.... Sorry if this is a newb post!
Here's the code. I removed the libraries and made it all functions, changed the switches to static variables at the top you'll need to set with your information. It's very repetitive now but it'll make it easier to understand. The workflow is pretty simple.

Check if there is an encrypted token saved
Generate one, encrypt it and save it locally if there isn't one already
Decrypt the token from file and verify it hasn't expired
Query the state of the vehicle (quit if asleep)
Write results to SQL

Let me know if you get stuck.

EDIT: Forums is doing some weird stuff with the PasteBin link (Remove the spaces from below)
h t t p s : / / pastebin.com/NxJs7i8M

EDIT on the EDIT: I also forgot to mention, I have an S so i'm only saving the metrics I care about to SQL, I know the API returns more than is being written but I simplified the code to where you can easily add more metrics for an X or 3 if you wanted to, oh and change the encryption key after you save it locally since it's posted on the internet (just update the numbers on the encryption and decryption functions).
Thank you for sharing! I hope to dig into it this weekend.
 
You may be interested in my app (no subscription; $10 one-time; does not increase phantom drain beyond what Tesla default app does; lots of stats): Stats: For Model S/X/3 cars on the App Store
Interesting app. I have 2 questions for you ...
  1. How can your app gather all this data without increasing drain? I notice that my car sleeps a lot. Almost every time I try an API data collect, the initial response is that the car is unreachable. Only after I send the wake up command will the data collect execute. Every time I wake up the car, I feel that I am increasing drain (I'm not speculating how much, but it's not zero)
  2. Is it also available for Android?
 
Here's the code. I removed the libraries and made it all functions, changed the switches to static variables at the top you'll need to set with your information. It's very repetitive now but it'll make it easier to understand. The workflow is pretty simple.

Check if there is an encrypted token saved
Generate one, encrypt it and save it locally if there isn't one already
Decrypt the token from file and verify it hasn't expired
Query the state of the vehicle (quit if asleep)
Write results to SQL

Let me know if you get stuck.

EDIT: Forums is doing some weird stuff with the PasteBin link (Remove the spaces from below)
h t t p s : / / pastebin.com/NxJs7i8M

EDIT on the EDIT: I also forgot to mention, I have an S so i'm only saving the metrics I care about to SQL, I know the API returns more than is being written but I simplified the code to where you can easily add more metrics for an X or 3 if you wanted to, oh and change the encryption key after you save it locally since it's posted on the internet (just update the numbers on the encryption and decryption functions).
So I read through your code and it looks pretty clear and valuable to my effort. How often do you run this script? Do you have any concerns about hitting the server too much? I think you had a copy paste error in the last function for climate because you were returning $teslaChargeState?
Also why do you go to the trouble to encrypt the keys when the password and email are stored in plain text in the script? Hope these aren't bone headed questions! Thanks for your help!
 
So I read through your code and it looks pretty clear and valuable to my effort. How often do you run this script? Do you have any concerns about hitting the server too much? I think you had a copy paste error in the last function for climate because you were returning $teslaChargeState?
Also why do you go to the trouble to encrypt the keys when the password and email are stored in plain text in the script? Hope these aren't bone headed questions! Thanks for your help!

I run it every 5 minutes, since it checks if the car is asleep and doesn't wake it up it doesn't cause unnecessary battery drain on the car.

Yea sorry about the typo, I modified a lot of the code to work independently since in my system it runs as a module. This is also the reason for the password hard-coded. On my system the credentials are saved as an encrypted file but they're passed as variables to this module, if I didn't hardcode them at the top it'd be hard to figure out how they got into the code without seeing the whole system and not just the module itself.
 
I run it every 5 minutes, since it checks if the car is asleep and doesn't wake it up it doesn't cause unnecessary battery drain on the car.

Yea sorry about the typo, I modified a lot of the code to work independently since in my system it runs as a module. This is also the reason for the password hard-coded. On my system the credentials are saved as an encrypted file but they're passed as variables to this module, if I didn't hardcode them at the top it'd be hard to figure out how they got into the code without seeing the whole system and not just the module itself.
Every 5 minutes ... Is there any guidance for what frequency to ping the Tesla server before it becomes a problem?

I'm trying to decide if I adapt your powershell script to write simply to a flat dB for use in, say, excel or point it to my SQL server which only exists for KODI profile syncing. I am leaning towards the Excel because I am more comfortable with it. For my use case I need to pull kwh usage from my evse CT taps. Again, thanks for the great head start on whatever I end up doing
 
At 5 minutes I never had an issue getting black listed. I did purposely black list myself to see what the process was like, it took a good couple minutes of hammering the API without any delays or pauses and the API returned something to the note of "blacklisted", I simply waited about 30min to an hour and everything went back to normal.

Back when I wrote that code try-catch wasn't part of PS so I had to do some rudimentary error catching with counting loops. You could improve on that to catch errors if you're really worried about getting blacklisted.

As for where to store that data that's totally up to you, I chose SQL so I could very easily do complex queries on the fly and use PowerBI to visualize the data without having to use SSRS. You can always pipe all the data to Export-CSV and save it to a flat file instead if you want.

Edit: Forgot to mention, there is no real guidance as to anything on the API since we're not really supposed to be using it. Timdorr was one of the first ones to reverse engineer the API, since then we simply decompile the APKs or do MITM with Fiddler/Wireshark to figure out the endpoints ergo zero official documentation.
 
Last edited:
Here's another newb question. Am I correct in assuming that if I want to check if I remembered to plug my car in at night at, say, 11pm.....That if the car is "asleep" (which mine seems to usually be), I will have to wake it up in order to successfully query it in order to find out that it's plugged in? I think this is true, but I'm being very careful to not wake the car unless absolutely necessary to avoid unwanted vampires.
 
Anyone here care to tell me how to turn the timestamp in the query responses into anything useful? (timestamp":1526441782406). I'd love to hear what math/function to use to make it a real time/date that I can use.....

It's the number of milliseconds since January 1 1970 00:00:00 UTC

Like a Unix Timestamp but with 3 more digits.

Here's a converter

1526441782
Is equivalent to:
2018-05-16T03:36:22+00:00 in ISO 8601
 
Anyone here care to tell me how to turn the timestamp in the query responses into anything useful? (timestamp":1526441782406). I'd love to hear what math/function to use to make it a real time/date that I can use.....

Take a look at the code I sent you, inside the Verify-Token function you'll see how to convert the epoch time into a standard date. Line 91 and 94 to be exact.
 
  • Like
Reactions: insaneoctane