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.
@wayner , I was considering kicking off a test charge, waiting X amount of time, then reading the time to full charge, but I figured that's something we can roughly calculate with a charge rate and the current state of charge. If the time to full charge is available, I go off that, If not, I calculate it myself.

Given this limitation, the former is used when determining when to stop charging (for example to respect charge time restrictions, say 12 AM - 7 AM), and the latter is used when determining when to start charging (for example make sure charging is complete by a certain time, say 9 AM).
 
@wayner , ah yes... I understand the charge rate is variable and thus the completion time is an estimate and a pain. I do what I can to make that as accurate as possible.

When available, I use the provided 'time_to_full_charge' (in hours) field from the charge state, else I calculate the time to full charge myself using a configured charge rate for that charging location. I've concluded the 'time_to_full_charge' is read straight from the car only when charging so it knows it's charging rate better than anyone else.
I am looking at modding the old visible tesla app with similar functionality. As I see it the problem with time_to_full_charge is that, since you have to already be charging, it's only useful after-the-fact. Probably a simple calculation based on kW would work. Better yet, for a specific location it could calculate anticipated charge time based off past history.
 
I think the best way to do this is to do it off of past history or to ask the user to input a charge rate. I would think that in most instances this will be done at someone's home so they will learn the charge rate after a while. And being a bit off isn't usually a big deal, unless you need to do a full range charge.
 
I just noticed that as of today the "is_autoconditioning_on" now always returns false and we should now rely on the new field "is_climate_on" to determine climate power status. Just submitted an update for my app fixing this issue.
I haven't tested this recently so maybe it changed, but is_autoconditioning_on is most likely referring to the A/C setting in your car and not whether climate is on or not. Since it's winter now, it's very unlikely that your A/C is on, and that might explain why it's always false to you.
 
  • Like
Reactions: Dylan Diamond
Here's some helpful PHP code, as an example.

Also, the first 50 lines of every command file you have is identical or nearly identical, and could easily be incorporated into a single generic initization function.

PHP:
$TESLA_CLIENT_ID="-----------";
$TESLA_CLIENT_SECRET="#################";
$TESLA_API_URL = "https://owner-api.teslamotors.com/";
$API_LAST_ERROR="";

//get vehicle list
function commandListCars($token) {
    global $TESLA_API_URL,$API_LAST_ERROR;
    echo queryTeslaAPI($token, "api/1/vehicles/");
}

function commandUnlock($token, $VEHICLE_ID) {
    global $TESLA_API_URL,$API_LAST_ERROR;
    queryTeslaAPI($token, "api/1/vehicles/".$VEHICLE_ID."/command/door_unlock");
}
function commandLock($token, $VEHICLE_ID) {
    global $TESLA_API_URL,$API_LAST_ERROR;
    queryTeslaAPI($token, "api/1/vehicles/".$VEHICLE_ID."/command/door_lock");
}

//general function to query the tesla API
function queryTeslaAPI ($token, $url) {
    global $TESLA_API_URL,$API_LAST_ERROR;

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $TESLA_API_URL.$url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_POST, false);

    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
      "Authorization: Bearer $token"
    ));

    $response = curl_exec($ch);
    curl_close($ch);

    $response_array = json_decode ($response, true);

    $API_LAST_ERROR=$response_array;
    return $response_array["response"];
}

function ReadTeslaAPI($token,$carid) {

    global $TESLA_API_URL,$API_LAST_ERROR;

    $climate= queryTeslaAPI($token, "api/1/vehicles/".$carid."/data_request/climate_state");
    $drive= queryTeslaAPI($token, "api/1/vehicles/".$carid."/data_request/drive_state");
    $charge= queryTeslaAPI($token, "api/1/vehicles/".$carid."/data_request/charge_state");
    $state= queryTeslaAPI($token, "api/1/vehicles/".$carid."/data_request/vehicle_state");

    $api=array();
    $api["rated"]=(int)$charge["battery_range"];
    $api["soc"]=(int)$charge["battery_level"];
    $api["odometer"]=(int)$state["odometer"];
    $api["ctemp"]=$climate["outside_temp"] ;
    $api["temp"]=($climate["outside_temp"] * 9/5) + 32;

    $api["lat"]=$drive["latitude"];
    $api["lng"]=$drive["longitude"];

    return $api;
}
 
It's open source code available on GitHub. Thanks for the ideas. This was merely a rough cut.

Tesla Management Portal for the web coming soon...

C1lC_GpW8AAJwG2.jpg
 
Last edited:
I need a hint to get past the oauth API call.

The JSON API is remarkably silent about where the parameters go and in what format. One website said they went in the BODY in JSON format. A comment on this forum said they go encoded in the header. I can't get any of them to work.

A further problem is that all I can find are code samples which are useless to me because my dev tools aren't anything anybody else is using. What I need is simply the RAW message to send and where it goes. I can't even get the Tesla Model S JSON API web page to work typing in parameters manually.

Please?
 
I need a hint to get past the oauth API call.

The JSON API is remarkably silent about where the parameters go and in what format. One website said they went in the BODY in JSON format. A comment on this forum said they go encoded in the header. I can't get any of them to work.

A further problem is that all I can find are code samples which are useless to me because my dev tools aren't anything anybody else is using. What I need is simply the RAW message to send and where it goes. I can't even get the Tesla Model S JSON API web page to work typing in parameters manually.

Please?

Your comments are exactly what we faced after wading through nearly a hundred pages of random comments on this API. You will find all of the raw commands in our PHP Application Library for the Tesla API: GitHub - wardmundy/php-api-tesla: PHP Application Library for Tesla API. While it is billed as PHP, the code is plain-text, and you can simply extract the curl commands (which are HTTP requests) to get what you need. The trick to all of this is to first obtain an OAuth token using your Tesla credentials (see the token.php script). Next, obtain the ID associated with your vehicle (HINT: it's not the Vehicle ID, but the ID field - see the vehicle.php script). Once you have those two pieces, you can look through the remaining scripts to obtain the curl commands to extract anything you need. The proper syntax for most commands is available here: Tesla Model S JSON API · Apiary
 
I already knew to start with getting the token. I looked at your token.php and it might as well have been written in Chinese.

$params = array(
'grant_type' => 'password',
'client_id' => $tesla_client_id,
'client_secret' => $tesla_client_secret,
'email' => $tesla_email,
'password' => $tesla_password,
);

isn't raw text, and nothing in there tells me whether it goes in the body of the URL or the header.
 
  • Funny
Reactions: NerdUno