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

Automating your Tesla from your phone using Automate

This site may earn commission on affiliate links.
The Tesla app uses an open API that allows you to control aspects of your vehicle remotely. However, you can't automate or schedule operations using the official app. For example, I'd like the HVAC system in my vehicle to turn on at 4 pm ONLY on a weekday and if I'm at work. There's an app called Tasker and a plug-in for the Telsa API available on the Google Play Store that can probably accomplish this. However, I wanted to see if I could come up with my own solution.

There's a free app called Automate that can natively do JSON calls and is easy to use. I created a flow that runs in the app that will remotely turn on my HVAC based on my (phone's) location and time of day. I'd like to see if anyone is interested in learning how to access the API through Automate before I go through the trouble of posting how it's done.
 
There's a free app called Automate that can natively do JSON calls and is easy to use. I created a flow that runs in the app that will remotely turn on my HVAC based on my (phone's) location and time of day. I'd like to see if anyone is interested in learning how to access the API through Automate before I go through the trouble of posting how it's done.

I'm not necessarily interested in setting up an automated solution like you, but I would like to be able to create a widget to turn on the AC (or open the frunk) vs. having to go into the Tesla app, wait for it to wake up, and then hit the appropriate button. Does automate support the creation of Android widgets that can be used to do tasks like that?
 
There's a free app called Automate that can natively do JSON calls and is easy to use. I created a flow that runs in the app that will remotely turn on my HVAC based on my (phone's) location and time of day. I'd like to see if anyone is interested in learning how to access the API through Automate before I go through the trouble of posting how it's done.

Hi @GPinzone, I’d be interested. I’ve been considering some type of coding project, perhaps a web-based home-grown app similar to teslafi once I get my car. I’d like to see what you’ve discovered and prototyped on the handset side.
 
Yes. You can create a shortcut on your phone that will execute a flow. However, there are other 3rd part apps that offer a nice widget for remotely controlling a Tesla.
I tried Dashboard for Tesla. It didn't really work. Half the time it lost contact with the car and the widgets were grayed out.

I understand that sometimes the car is going to be out of contact and unreachable, but I expect that to be fairly rare. And even when the car is out of contact, I want the widget to still be active and when I press it I want the app to go off in the background and do what it needs to to re-establish contact with the car.

The Nissan LEAF app acted like the Tesla app does (and Dashboard for Tesla) in that it would appear to be out of contact with the car. The only way to get it back in contact was to force stop it and re-run, and then magically it would connect right away (just like the Tesla app, and just like, I presume, Dashboard for Tesla). But somebody wrote a nice app that not only automated that basic procedure in the background, but added some very useful widgets (and individual widgets too, not just a 4x1 row of them that I'm stuck with, even if I only need 2).
 
I'll post something up tomorrow. My example flow can be used for almost any operation. I solved the hard stuff like getting connected to the Tesla servers and retrieve the important data for access like creating the login token and retrieving the vehicle ID number. With that info, you can send any command the API supports. I only have one Tesla vehicle, so I don't know what the output will look like if there were two cars under my account.
 
  • Like
Reactions: DaddyP713
The iPhone (as of iOS 12) has Shortcuts (fka Workflow), which is able to make API calls. I'd be interested in exchanging techniques for getting this done.

(There's also IFTTT, come to think of it; can you do JSON calls from IFTTT?)

Further on this after about an hour of experimenting (and reminding myself JSON syntax.) I have just successfully turned on the A/C from an iOS Shortcut.

(And to answer my own question re. IFTTT: yes, with Webhooks, but it doesn't seem to support the necessary ability to include the Authorization token in the header.)
 
I was able to create this flow in Automate thanks to the Tesla Model S JSON API here: Tesla Model S JSON API · Apiary

The initial steps to connect to the car are as follows:
  1. Get an Access Token. This does not need to be repeated until the token provided expires. Tokens last approximately 45 days.
  2. List all Vehicles. This will send an array of vehicles that provide their identifying information ,like its VIN, Name, options, and internal ID number. (My flow only deals with the first vehicle returned.)
Once we have the information provided by these two steps (token and ID), we can get status and change settings through the API.

Communicating with the API uses standard http POST and GET commands. The preferred method to send and receive information is through JSON (JavaScript Object Notation). JSON allows us to interchange data that is easy to parse. The majority of the flow connects to the Tesla servers and collects data. The actual command to turn on the HVAC is done in a single step! Other functions like determining the location of the phone and the time of day is handled by Automate.
 
Last edited:
Sorry, I posted the wrong workflow. I'll re-post it soon. Here's a preview of how to program the "Time await" block:

First block waits for the right time to occur and then let the flow continue. Here's what its configuration screen looks like:

Time-await.png


The next block checks the location of the phone. If it's within 250m of my office, it will pass to the next block down. Otherwise a toast message and notification will pop-up informing me that no change was made since I'm not at work. (This doesn't run on the weekends, so no message will appear
 
@GPinzone (and anybody else working on it), FYI that you may need logic to check sleep state, or just unilaterally call /api/.../wake_up before calling /api/.../auto_conditioning_start -- I think that's what I'm seeing. (Although I think calling auto_conditioning_start might fail but result in a wake-up, in which case you could loop it a time or three until success.)
 
@GPinzone (and anybody else working on it), FYI that you may need logic to check sleep state, or just unilaterally call /api/.../wake_up before calling /api/.../auto_conditioning_start -- I think that's what I'm seeing. (Although I think calling auto_conditioning_start might fail but result in a wake-up, in which case you could loop it a time or three until success.)

I call it first, but I'm not sure it matters. If you send a command like starting the HVAC, it seems to wake up the vehicle, too. BTW, the commands sent to a sleeping vehicle (including the wake up) often return as failed. You have to check status and loop. I use a 10 second delay in between retries. What I should do is add a counter to give up after a certain number of retries, but I haven't done that yet.
 
One thing to do in Automate before we begin: Go to the settings and enable the "Google Play Services" checkbox to "Use Google Play Services location API." The least reliable aspect of this is the ability for the phone to get an accurate location indoors. Using the Google's API instead of relying on gps or whatever the phone uses natively makes it more reliable.

Here's the complete workflow:

Tesla Start HVAC.png

All the settings from the login and status is stored in a variable called "tesla" in a JSON dictionary format. I store this in the file "tesla.txt" at the root of the internal memory. The flow will attempt to load the previous settings when first run. This way important information like the vehicle name and the access token don't have to be recalculated.

The output from the "Get Access Token" API call yields a timestamp (created_at) and a delta (expires_in) to calculate the token expiration date. If the token has expired, we need to log in and get a new one (or if it's missing). The "Expression true?" block checks the current date/time with the calculated expiration date:

(tesla["created_at"] + tesla["expires_in"]) > Now

If it fails, an HTTP Request block logs into the Tesla server:

Request URL:
https://private-anon-480f8ee801-timdorr.apiary-proxy.com/oauth/token
Request Method:
POST
Request Content Type:
JSON
Request Content:
{
"grant_type": "password",
"client_id": "81527cff06843c8634fdc09e8ac0abefb46ac849f38fe1e431c2ef2106796384",
"client_secret": "c7257eb71a564034f9419ee651c7d0e5f7aa6bfbd18bafb5c5c033b093bb2fa3",
"email": "[email protected]",
"password": "XXXXXXXXXXXX"
}

Save Response
Save to variable as text
Output Variables
statuscode
output


Obviously you need to change the email and password values above.

Next step is to take the JSON output and save only the values we want into the tesla variable. Remember, tesla is a dictionary. We want to keep the previous entries (if any) and overwrite with the new values. The variable set block below is:

Variable
tesla
Input arguments
Value

extend(tesla, sift(jsonDecode(output), ["access_token", "created_at", "expires_in"]))

Lastly, we save the updated telsa variable to the tesla.txt file:

Content
{jsonEncode(tesla)}
File
tesla.txt
Encoding
UTF-8
 
Now that we have the token, we need to get the vehicle ID (and Name, VIN, etc.) The List All Vehicles call will dump this info:
List-all-Vehicles-1.png


The output is written to the tesla dictionary:
List-all-Vehicles-2.png

The tesla dictionary then gets written to the file. (Known issue: If the flow never completes through the normal path at least once, the vehicle name won't be stored in the tesla.txt file. Any notifications referencing the display_name entry will be blank.)

It should be noted that I am ONLY storing the info for the first vehicle that gets returned. That's what the [0] signifies. I would wager that the order of the vehicles remain constant, so if you have multiple vehicles and you want to run this on your third one, you can probably hardcode in a [2] and it will work consistently. Nevertheless, I suggest modifying my code to store the entire array of vehicles in the tesla dictionary.