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.
Posted this up on Hacker News: Tesla's Model S has a REST API | Hacker News Incoming viewers!

image.jpg
 
I was expecting that max_range_counter to be related to the 'you've charged in max range mode 3 times in a row' and it starts warning you to switch to standard? Not the count of lifetime max range charges

That seems to be the case. I did a full standard charge last night and it reset to 0 at the point the charge completed. That still seems to indicate that the did range charges before I picked it up.
 
This is great stuff. But for some reason the token field is now coming up empty for me. I used to get it?

The only way I can get a token is to first login using my Android phone and then call the API from my app. I wonder if there is a step I am missing to trigger to creation of the token?

I also get an HTTP timeout if I don't drive the car for a few minutes. I tried setting --keepalive-time on CURL but my stupid windows OS doesn't support it. Will try again with Linux or MacOS.

8474759116_9687ccd307.jpg


Values shown are for streaming API query for speed,odometer,soc,elevation,est_heading,est_lat,est_lng,power,shift_state

First field is clearly #milliseconds since the Unix epoch. I did check the timezone to see if it's local or in UTC and it is in Coordinated Universal Time.
Valid shift states are D(rive), R(everse), P(ark)
When you go in reverse the speed is still positive but the shift state is "R"

Speed and Odometer appear to be in miles but I didn't test with the car set for metric to see if it changes.
est_heading is in degrees (compass direction) so their must be a magnetometer in the car somewhere. Perhaps the module is installed backwards or something because I appear to get the direction of the ass end of the car (rather than the frunk)

No idea what "power" is or whether it goes to 11 ;-)

My goal is to finish a node.js app that republishes the live stream of telemetry via PubNub or a websocket server of my own.
 
Last edited:
The only way I can get a token is to first login using my Android phone and then call the API from my app. I wonder if there is a step I am missing to trigger to creation of the token?

I also get an HTTP timeout if I don't drive the car for a few minutes. I tried setting --keepalive-time on CURL but my stupid windows OS doesn't support it. Will try again with Linux or MacOS.

I'm sending in the token, but it's failing with an "HTTP/1.1 401 Unauthorized" :(
 
The only way I can get a token is to first login using my Android phone and then call the API from my app. I wonder if there is a step I am missing to trigger to creation of the token?

I also get an HTTP timeout if I don't drive the car for a few minutes. I tried setting --keepalive-time on CURL but my stupid windows OS doesn't support it. Will try again with Linux or MacOS.

No idea what "power" is or whether it goes to 11 ;-)

My goal is to finish a node.js app that republishes the live stream of telemetry via PubNub or a websocket server of my own.

Try invoking GET on /vehicles/{id}/command/wake_up via the REST api, seems to fix the missing tokens issue.

The streaming connection will eventually timeout or be closed by their servers, and you will have to re-establish. This happens even with the mobile app, when I watch the traffic via the proxy. You will notice a little pause in the live updating of the vehicle on the location map when this happens.

The "power" setting seems to correspond to the power being used or generated by the vehicle and is equivalent to the right-part of the dial on the car's speedometer. I think the intent was for the mobile app's speedometer to mirror the car's because it has the same colors in the outer circle, but it is just not drawing anything yet.
 
Yep, I've tired both in desperation. I've observed that the second token is the previous token, so they roll to the right.

Are you using the correct vehicle id? For the streaming API it is the "vehicle_id" field in the vehicle's JSON description not the "id" field. The ids are not the same for the REST and streaming APIs. For the streaming API mine is a 10-digit number, compared to a 3-digit number ror the rest API.
 
Are you using the correct vehicle id? For the streaming API it is the "vehicle_id" field in the vehicle's JSON description not the "id" field. The ids are not the same for the REST and streaming APIs. For the streaming API mine is a 10-digit number, compared to a 3-digit number ror the rest API.

:redface: just figured that out - and it works !! yea!.

I thought it was the "." in my email address.


Another interesting side note the JSON "id" field changes if you login with your username instead of your email address. (Both seem to log you in though)

Thanks for your help.
 
Forgive me if wrong terminology, or if a dumb question, but is there API access to the media player in the car? can it switch inputs, set channels or sources, or select songs to play? if so, there could be some cool things done where the car starts my favorite podcasts or streams at certain times of the day?
 
est_heading is in degrees (compass direction) so their must be a magnetometer in the car somewhere. Perhaps the module is installed backwards or something because I appear to get the direction of the ass end of the car (rather than the frunk)

I guess the downside of living close to sea level is that it easy to get elevation and compass bearing measurements mixed up ;-)
Sorry for the false alarm but the est_heading values are indeed correct and indicate the direction the front of the car is facing.

Power is clearly in kilowatts and a negative number indicates regen (also in kilowatts)

The lat/long values are extremely accurate and could pinpoint the part of my driveway where the car was located. That NYT journalist better quit making up stories about what he did/didn't do as it's clear to me that Tesla can tell exactly how many times he circled the parking lot and in which parking spot he eventually parked.

The streaming interface is very basic. It's a HTTP long polling interface so all you need to do is read the body of the HTTP response and then poll again to get more data.

I put the output into a file and read it into Excel as a CSV file for more advanced graphing and analytics. I was hoping to get more accurate 0-60 mph data but since the samples are 250 ms apart I can only get a 1/4 of a second resolution. I was looking for 1/10 second or better.
 
Last edited:
A negative number also can indicate charging.

So it is! You can tell the difference between regen and charging because when the car is charging, the speed is "null" (not 0).
Usually, but not always, the shift_state is also "null" but I have seen a shift_state of "P" for a few samples while the charge_state was being switched on and off using the REST API.
 
Last edited:
When the car is fully charged in standard range mode, has anyone figured out how to get it to start charging again in max range mode?

I can set the range to max_range successfully but the car rejects any attempt to start charging again with a result code of "charge complete".
 
Could someone please give a simply layperson's explanation of this thread? I last programmed a computer using Fortran 4 and punch cards about 40 years ago. Have you folks invented a new app that non programmers can use or do you have to be able to write code to do these things? Not even sure what these things are. I looked up several of the terms, like REST API, in wikipedia, but I can't make heads or tails of what they are.

Thanks from a senior citizen Model S owner.