PortiBlog

Pre-heat or cool your Tesla with Microsoft Power Automate and the Tesla API

6 mei 2020

Since almost a year, I am the proud driver of a Tesla Model 3! The car is of course loaded with gizmo’s and software gadgets… I think I have checked and tested them all out by now :-).  

20200504_PortiBlog_image01aLast month I bought a NewMotion Smart charging station on the 2nd hand marketplace named Marktplaats.nl (famous Dutch site) and I installed the charging station on the wall of my house.

With the ability to charge my car, I thought; hey, how nice would it be to pre-heat or cool the car based on my calendar? I searched the web and found some examples of communicating through the Tesla API with the vehicle. But almost all examples didn’t meet my requirements. For example: I would only like to pre-heat or cool, depending on the outside weather temperature. I also only would like to use the HVAC system when the batteries are charged to a minimum level (let’s say 40%).

As an Office 365 (Security) Consultant, I work a lot with Microsoft Power Automate (Flow). So, by using the right Lego-bricks (named actions and triggers) I would be able read some information from my car and I would also be able to send commands to it.

How to communicate with the Tesla?

Like most software vendors do, also Tesla provides a so-called API (Application Programming Interface) which gives 3rd party developers access to several things of the car. There is unofficial API documentation available at: https://www.teslaapi.io/

Disclaimer: is this API Official? Absolutely not. These endpoints are a result of reverse engineering Tesla’s mobile applications and vehicle software. 😉

I have provided an export of the Microsoft Power Automate Flow in this Post (see links in the footer of this page). So, you will be able to import and use the Flow.

Things to set-up

Once you have imported the ZIP/Flow in Microsoft Power Automate (see this link how to do it), you can easily setup the flow to make it work with your Tesla.

The current Flow (1.0) version has the following settings:

  • Username (of your Tesla account)
  • Password (of your Tesla account)
    Caution: please read the “Things to consider in next version“, because storing credentials in plaintext is not recommended!
  • Minimal battery power (percentage of minimal battery-level to allow pre-heat/cooling)
  • HeaterCoolingTime (how long should it heat or cool?)
  • MinTemp (if outside temperature is below this threshold, it will start heating)
  • MaxTemp (if outside temperature is above this threshold, it will start cooling)
  • Temperature (what cabin temperature do you want to set? 20.0 or perhaps 21.5?
  • Coordinates (coordinates to check current outside temperature)

How the flow works

The flow has been set-up to check your Office 365 calendar. If there is a new event in the next 15 minutes, and it contains “Reizen” or “Rijden” (which means travel/driving in Dutch), then it should proceed with the flow.
The next thing to check is the current weather condition. I used the MSN Weather connector to retrieve the current temperature based on GPS coordinates.

and(greaterorequals(body('Get_current_weather')?['responses']?['weather']?['current']?['temp'],variables('MaxTemp')),lessOrEquals(body('Get_current_weather')?['responses']?['weather']?['current']?['temp'],variables('MinTemp')))

When the temperature is within the boundaries of the Min/Max temp, you will receive a push-notification that the cooling did not start due to the fact the outside temperature is within the set threshold.

Once the weather conditions are fine and I am about to travel, the flow will do a HTTP request to the car. I tried to limit the number of calls to the car, to prevent waking it up unnecessarily. So everything until now is done without waking up the car.

The image below illustrates how to do a HTTP POST to get the access-token. You can request an access token only by providing your Tesla username/password. The response of this request will contain the Access token and some more information.

20200504_PortiBlog_image02
Now that the Access token is available, we can fire more commands to the vehicle. But first we need to list all the associated vehicles to this account. The HTTP POST will use this API end-point: https://owner-api.teslamotors.com/api/1/vehicles to list the vehicles.

In my example flow, I assume you have 1 Tesla vehicle. The flow goes through a loop, so it will eventually wake-up every car and start the pre-heat/cooling 😉 If you don’t want this to happen, please change this in the flow example! By listing the vehicles that are connected to this account, you will receive a MyCarID. This ID is stored in a variable for later use.

20200504_PortiBlog_image03

Via this step we will receive the list of vehicles. If OK, (status 200) the flow will continue. Now that we have received the CarID, we can start waking it up. As I mentioned earlier, this action is placed in a “For each” so every car in the account will be accessed. To wake up the car, we can now use the following API endpoint with the “MyCarID” variable in it: https://owner-api.teslamotors.com/api/1/vehicles/@{variables(‘MyCarID’)}/wake_up.

I built in a “delay” action, because waking up the car can take some time (it is set to 20 seconds). You can adjust this of course in the flow.

Now that the car is awake, we first want to know its battery condition. There is another API endpoint to retrieve all technical (charging related) information from the vehicle. See this url for more information: https://www.teslaapi.io/vehicles/state-and-settings#charge-state

The JSON response will contain a lot of information about the charging state and current battery state. As defined in the settings of the flow, we only want to pre-heat/cool if the battery is equal to or greater than 50% range. If the battery charge status is below 50%, you will receive a push-notification that the pre-heat/cooling could not start due to the set threshold.

20200504_PortiBlog_image04

Now that we have checked the calendar, weather condition AND the battery-status, it is time to start the HVAC! This is also achieved by using the API of course. Once the HVAC is turned on, we can send 2 separate HTTP POSTs to set the driver AND passenger temperature. As a bonus I also start heating the seat at level 3, so your trip will start with a warm bum 😉.

20200504_PortiBlog_image05

As you can see above, I set the same temperature for the driver as for the passenger. You can modify and extend the flow to set different temperatures of course.

The same modification applies for the seat heater. This is turned on by default. In the next version of the flow I would suggest to turn it on only when it is freezing outside 😉.

20200504_PortiBlog_image06

Tesla App: Once the flow run, you can see that the vehicle is responding!
After 15 minutes the HVAC and Seat heater will automatically turn off via another HTTP POST action.

 

 

 

Things to consider for the next version:

  1. Storing Usernames and Passwords in a plain-text format is never a good idea. You can of course modify the flow as a “Button-flow” so it will request your username and password trough the Flow App on your phone. Or you can modify the flow, so it will only use an access token (instead of retrieving one). Via these websites you can easily obtain access tokens, but keep in mind; they will expire every 45 days!
  2. Azure Key Vault: Storing credentials in the Azure Keyvault is a much better solution! I am currently working on the next version of this flow with the ability to use the Premium Azure Key Vault Connector.
  3. Refresh the token: This flow requests a new Access Token every time it runs. Refreshing the access token should be a better solution. Every time a token is requested, the old token will expire. Each token is set to expire in 45 days.
  4. Checking Car drive status: This flow version won’t check the current drive-status of the car. The API allows to check if the car is currently driving. So, if you leave home earlier, you don’t want the flow to change your cabin temperature 😉.
  5. Check the temperature based of vehicle location: In this version, the GPS coordinates are fixed. Feel free to modify the flow so it will check the vehicle’s current location/coordinates, to determine the weather condition. It is even possible to GET the outside temperature sensor information from the vehicle. I didn’t use the vehicle sensors, because this would unnecessarily wake up the car every time.

 

This blog was just to check and see how the car can be controlled through the Tesla API with Microsoft Power Automate. If you have questions, please feel free to leave a comment down below or use the contact page!

DOWNLOAD EXPORTED EXAMPLE FLOW

 

 

Submit a comment