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.
This is too easy! I'm thinking of setting a cron job that runs every 10 minutes - and creating dummy file to indicate whether the last time the script was run if the car was charging or not and if for some reason charging stopped since the last time the script was run, it'll email or better yet text me (google voice). This would be a great tool to notify you if either a fuse tripped overnight or someone unplugged you or the charge station shut you down or something... that will save you from a nasty surprised of an uncharged or partially charged car while on a trip :)
 
what are all the variables that can be accessed? I'd like to poll my car every 10 minutes and inject it into a mysql database. Then another script can take action based on the data analyzed.

See post #1 documentation web site for current variables and their uses. You could change the cron entry shown in post #72 to be "0-59/10" to achieve ten minute reports.
 
I just picked up my S from the factory, so now I can actually play with the API :)

I noticed something interesting in my logging app. A snippet of data:

inside_temp
outside_temp
driver_temp_setting
is_auto_conditioning_on
battery_current
25.5
19.5
22.5
false
-0.6
25.519.5
22.5false
-0.6
25.5
19.5
22.5
false
-0.6
25.5
19.5
22.5
false
-0.6
25.5

22.5

0


22.5

-0.1

It looks like even without the global "sleep" setting, there are still subsystems that go to sleep over time, and are reflected in the battery current. These are at 10 minute intervals (the timestamp columns broke the bbcode table somehow)

I also noticed that my car has had 2 max range charges already. Guess I should have picked it up sooner...
 
I just picked up my S from the factory, so now I can actually play with the API :)

I noticed something interesting in my logging app. A snippet of data:

inside_temp
outside_temp
driver_temp_setting
is_auto_conditioning_on
battery_current
25.5
19.5
22.5
false
-0.6
25.519.5
22.5false
-0.6
25.5
19.5
22.5
false
-0.6
25.5
19.5
22.5
false
-0.6
25.5

22.5

0


22.5

-0.1

It looks like even without the global "sleep" setting, there are still subsystems that go to sleep over time, and are reflected in the battery current. These are at 10 minute intervals (the timestamp columns broke the bbcode table somehow)

I also noticed that my car has had 2 max range charges already. Guess I should have picked it up sooner...

Strange, my max_range_charge_counter is at 0 and I have done a couple of range charges.
 
Calculating kWh

Anybody have any ideas how to calculate kWh remaining via the API given the car's current charge state? I'm trying to calculate Wh per mile, similar to what one finds in the trip meters in the car. I can get the odometer readings for the mileage, but not obvious how to calculate the watt hours used. Here's some relevant sample data from my car while charging:

Code:
 "battery_range"=>183.28,
 "ideal_battery_range"=>210.94,
 "est_battery_range"=>187.98,
 "battery_level"=>74

The main problem is that the "battery_level" which seems to be the same as State of Charge (SoC), has a pretty low precision (1% of total battery), which makes calculations very imprecise. So I'm hoping that the range numbers can be used to calculate remaining kWh, as they have greater precision. What is unknown is the math Tesla uses to calculate the ranges from the battery state (and other factors like temperature, etc.).

Any thoughts?

My current plan is just to log some data, and see if I can find some kind of obvious correlation between the numbers.
 
Anybody have any ideas how to calculate kWh remaining via the API given the car's current charge state? I'm trying to calculate Wh per mile, similar to what one finds in the trip meters in the car. I can get the odometer readings for the mileage, but not obvious how to calculate the watt hours used. Here's some relevant sample data from my car while charging:

Code:
 "battery_range"=>183.28,
 "ideal_battery_range"=>210.94,
 "est_battery_range"=>187.98,
 "battery_level"=>74

The main problem is that the "battery_level" which seems to be the same as State of Charge (SoC), has a pretty low precision (1% of total battery), which makes calculations very imprecise. So I'm hoping that the range numbers can be used to calculate remaining kWh, as they have greater precision. What is unknown is the math Tesla uses to calculate the ranges from the battery state (and other factors like temperature, etc.).

Any thoughts?

My current plan is just to log some data, and see if I can find some kind of obvious correlation between the numbers.

First, I'm not positive about the battery_level. Right now mine says "93" at rated 241.64. If it's really percentage of full, this would put 100% at less than 260. I will check next time I do a range charge, which puts the rated at 270+
Second, how do you get the odometer?
 
how do you get the odometer?

Via the streaming API. :)

Try this:

Code:
curl --user USERNAME:TOKEN https://streaming.vn.teslamotors.com/stream/VEHICLE_ID/?values=speed,odometer,soc,elevation,est_heading,est_lat,est_lng,power,shift_state

where USERNAME is your MyTesla username, TOKEN is the first token returned in your vehicle's JSON returned from the REST API, VEHICLE_ID is the "vehicle_id" returned in vehicle's JSON. The TOKEN value rotates every few minutes, so you will need to query your vehicle's JSON description before opening a new stream.

Fields are returned as a comma separated list of values matching the field references in the URL query string. The first field is a timestamp.

Now run that curl command, then hop in your vehicle and drive around for a while, and watch it stream. :)

For those that use Ruby, I've already developed a ruby gem that supports both the rest and streaming APIs as well as a high-level Vehicle Object API. I'll release a public version by the end of the month, just need some time to clean it up a little (and I'm traveling quite a bit this month). Here's some sample code (from my twitter client) using the API:

Code:
portal = Teslamatic::API::Portal.new(config[:username], config[:password])

vehicles = Teslamatic::Vehicle.vehicles(portal)
vehicle = vehicles.first

current_gear = vehicle.gear_selector
current_odometer = vehicle.odometer
 
  • Informative
Reactions: Jonas_man
My current plan is just to log some data, and see if I can find some kind of obvious correlation between the numbers.

I just checked my log from today and it really looks like there's just a simple linear relationship between battery_range and battery_level.

Interestingly, there seems to be quantization of the battery_range at what's the equivalent of about 1/7 of a percent, at least on my 60kwh, from looking at the numbers during "vampire load". That may be indicative of either an artifact of their measurement or of the reality of the 12V drawing down and recharging to a specific level.
 
This is great stuff. But for some reason the token field is now coming up empty for me. I used to get it?

Via the streaming API. :)

Try this:

Code:
curl --user USERNAME:TOKEN https://streaming.vn.teslamotors.com/stream/VEHICLE_ID/?values=speed,odometer,soc,elevation,est_heading,est_lat,est_lng,power,shift_state

where USERNAME is your MyTesla username, TOKEN is the first token returned in your vehicle's JSON returned from the REST API, VEHICLE_ID is the "vehicle_id" returned in vehicle's JSON. The TOKEN value rotates every few minutes, so you will need to query your vehicle's JSON description before opening a new stream.

Fields are returned as a comma separated list of values matching the field references in the URL query string. The first field is a timestamp.

Now run that curl command, then hop in your vehicle and drive around for a while, and watch it stream. :)

For those that use Ruby, I've already developed a ruby gem that supports both the rest and streaming APIs as well as a high-level Vehicle Object API. I'll release a public version by the end of the month, just need some time to clean it up a little (and I'm traveling quite a bit this month). Here's some sample code (from my twitter client) using the API:

Code:
portal = Teslamatic::API::Portal.new(config[:username], config[:password])

vehicles = Teslamatic::Vehicle.vehicles(portal)
vehicle = vehicles.first

current_gear = vehicle.gear_selector
current_odometer = vehicle.odometer