Tesla auto charge port unlock

Using the Tesla unofficial API and Google Cloud Functions

Matt Jeanes
6 min readSep 1, 2019

What?

This post describes how to create a personalised Google Cloud Function (GCF) which can be called by IFTTT or anything else which unlocks the charging port on any Tesla, including the Model 3.

The code checks charging status, and only if the car is plugged in and not currently charging will it unlock the charge port.

Why?

If you don’t have the official Tesla charger, you likely don’t have a button on the charger itself which tells the car to release the chargers. Tesla Superchargers also have these buttons too, and are extremely useful.

If you don’t have the button it means that to unlock the charging port you have to either go into the car and press the ‘Unlock Charge Port’ button on the central display or go into the Tesla app ➡ Charging ➡ Unlock Charge Port.

For normal daily home charging, this can get really annoying when you’re trying to leave for your commute. I decided to try and use the unofficial Tesla API (https://tesla-api.timdorr.com/) to fix this for me, as I knew if the app could do something, so could the API.

How?

To accomplish this, I decided to try out GCFs. I’d heard of the concept before, running a bit of standalone code in response to a trigger — in this case a web request. AWS Lambda and Azure Functions are very similar and could also be used here but for this tutorial I will be using GCFs.

First of all, you’ll need a Google account. Sign into the Google Cloud Platform Console and create a new project or re-use an existing one if you like. You may be required to add a card to your account, but don’t worry about being charged for this project as there is a free tier (see pricing details here) and you can set a billing budget to get notified in case you exceed the free tier (extremely unlikely for this project)

Next up, you’ll need to use the navigation menu on the top left and go to ‘Cloud Functions’. Press ‘Create Function’, give it a name, select 128MB for memory allocated, tick allow unauthenticated invocations, set source code to ‘Inline editor’, set the runtime to Node.js 10, and use the source code here for both index.js and package.json and set ‘function to execute’ to ‘run’.

It should look like this so far

Expand the ‘Environment variables, networking, timeouts and more’ section, set the region to your region, timeout to 60, and then we need to add some environment variables.

There are a few choices depending on how you want to set it up on which env variables to add but at minimum you need ‘PASSWORD’. Set this to whatever you want, as this will be needed later when calling the function from IFTTT or wherever to stop any random person on the internet calling it.

There are two ways to provide Tesla account credentials — providing a Tesla API access token directly (details here, note this will expire over time) or your Tesla account username (email) and password which the code will use to get a token automatically every time it runs.

It’s up to you whichever one you want to pick, but note that if you use an access token directly it will eventually expire (3 months after generation at time of writing) and you will need to update the GCF again. The only place your token or login details will be stored is directly in Google Cloud and not sent to any other third parties at any time.

To use an access token directly, add a env variable named ‘TESLA_ACCESS_TOKEN’ with your access token as the value. To use your Tesla account username and password, add variables ‘TESLA_USERNAME’ and ‘TESLA_PASSWORD’ with your email and password respectively.

Finally, and optionally, you can provide a car id. This is a unique identifier for your car, in the case of multiple cars on your account. If you don’t add this, the code will automatically find all vehicles on your account and pick the first one it finds. It is the ‘id_s’ field returned as part of the list vehicles API, or you can run it once and get the id from the logs, then add it to your code to save it from doing that look up each time. If you want to add this, create an env variable named ‘TESLA_CAR_ID’ with the id as the value.

Then just hit create. It’ll take a second to create. Once done, you can test it. There is a testing tab on Google Cloud Platform which you’ll see after creating your function that allows you to run it manually. Make sure it’s all working and then we’ll integrate it into IFTTT.

NOTE: A reader has informed me that the configured environment variables might not pass through when using the test button so if it doesn’t work for you either try testing it using Postman or any other API testing software.

It should look something like this when done

I’d also recommend viewing the logs so you can see what it’s up to, and this is also the perfect place to grab the car id if you haven’t set it already. Press ‘View logs’ at the top of the page and you can see how it all works. I’d highly recommend reading through the code as well to get an idea of what it’s doing.

Example of the function logs, useful for debugging in case it’s not working properly

For this example, we’ll use IFTTT to test integration but you can also call the function from something like Tasker or Automate on Android or anything else — the core principles remain the same.

Sign up to IFTTT if you haven’t already, and create a new integration. Give it a name. I’ll be using the ‘Button widget’ for this example, but you can use whatever you want here. For the action, search ‘Webhooks’ and select ‘Make a web request’. Enter the URL for your function, it’ll be something like ‘https://us-central1-home-123546.cloudfunctions.net/unlock-charging-port’.

Set method to ‘POST’, content type to ‘application/json’, body to ‘{“key”: “PASSWORD”}’ replacing PASSWORD with the value of your PASSWORD env variable from earlier. Make sure you keep the quote marks and hit finish.

To test the button you’ll need the IFTTT app on your phone. I’m using Android, but you can do this on iOS too using these instructions. Make sure you’re signed into the IFTTT app and create a new IFTTT Small widget from your home screen and select your new integration when prompted.

On Android, you should have something like this

And that’s all, just press the button and your charging port should unlock within a few seconds, a bit longer if the car is asleep and needs to wake up but should take no longer than about 20–25 seconds.

You can also try setting the trigger to perhaps a Bluetooth connection or similar to automate this process entirely. Personally, I have mine set up to trigger when my phone connects to the cars Bluetooth audio so I just pop the door a bit when I go out to the car so it all connects up and then unlocks the charge port.

If you follow this guide and it helps you or you have an idea for improvements, I’d love to know! I’m on Twitter as @mattjeanes23 — thanks for reading.

--

--