The last few blogs had a “not my usual content” text here, but this one is about software development. As it turns out Tesla is missing a -for me- very critical piece of functionality; being able to start the airconditioning (AC) system at a certain time. It is possible to turn on the AC immediately via the app using the fan icon (see the image below), but not automatically at 06:00, while I’m still counting sheep in my bed.
But to my amazement, every Tesla car is accessible via the internet through a REST API. This API is what the Tesla app uses to access the vehicle. Using this API is not officially supported, but Tesla does not seem to mind people accessing it.
Okay! That is interesting! I’m a software engineer, I can handle a REST API…
Interestingly the first reaction people give when you talk about this is “hey, write an app”, but that is not the best solution for this problem. The most important aspect is that there needs to be something waiting for that specific moment a call needs to go out to the car. The car itself usually is in sleep mode, so something else needs to keep an eye on the clock. A mobile app is not the best thing to do that. It needs to be something that should have minimal connectivity issues or cannot be turned off easily… Something like a cloud service.
Besides calling out to the vehicle at the appropriate time, there also is a need to keep track on when to turn the AC on and off. Naturally one could write an app, or webapp, that stores that information in a database. But there are existing tools that are used to keep track of when something should start and end, they are called calendars. And Google has a great, free and online implementation.
So the approach I decided to take was to use one of my Google Calendars, brilliantly named ‘Tesla’, to trigger starting and stopping the AC of my Tesla. My co-JavaFX enthusiast Jonathan Giles pointed me to towards Microsoft’s Azure Logic app; a workflow solution capable of triggering on a Google Calendar.
For completeness let me show the code for StartHVAC:
public class StartHVAC { private static final String USERNAME = "yeah sure"; private static final String PASSWORD = "yeah sure"; private static final String VIN = "yeah sure"; /** * */ @FunctionName("StartHVAC") public HttpResponseMessage run ( @HttpTrigger(name = "req", methods = { HttpMethod.POST }, authLevel = AuthorizationLevel.FUNCTION) HttpRequestMessage <optional> request, final ExecutionContext context ) { try { // Login to Tesla and find the vehicle TeslaAPI teslaAPI = new TeslaAPI(); teslaAPI.login(USERNAME, PASSWORD); Vehicle vehicle = teslaAPI.getVehicleByVIN(VIN); // The vehicle may be in sleep mode teslaAPI.wakeUp(vehicle.id); // Do the thing you're here for teslaAPI.startAutoConditioning(vehicle.id); // Done return request.createResponseBuilder(HttpStatus.OK).build(); } catch (Exception e) { return request.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR).body(ExceptionUtils.getStackTrace(e)).build(); } } }
The implementation above was made for one car. The Google calendar integration is hardcoded, and the emails are sent using my private email address. But there is nothing stopping me from putting it all in a database, using a timer and a for-loop, to loop over a number of Google calendars, and make this something that other people can use as well.
Yes, I know there are existing ways to schedule the AC, but I like the combination with Google Calendar from a UX (user experience) perspective. So it’s time to start stabbing at that keyboard!