when he screams at the camera and he ignores the rules and he clenches his fists and he ruffles his brows and his veins they swell and his eyes, they fill with drops: hydrogen hydrogen oxygen…
How to create a visual heat map of where your Tesla has been using the Tesla streaming API and RStudio.
When I first got my Tesla three years ago it was nerd heaven —like getting a computer on wheels. It could accelerate like crazy, drive itself and recharge when slowing down. A friend casually remarked that the regenerative braking made it seem like you were driving a golf cart (albeit, a 700hp golf cart). Once the novelty of testing launch control wore off and my jerky driving while testing autopilot crash sensitivity in rush hour triggered one too many motion sickness events in the kids, I realized it was time to explore some other aspects of the car.
When you buy a Tesla an account is created in their portal that allows you to track all the car information from the car’s initial VIN assignment, through manufacturing, shipping, and then ongoing service. The portal experience comes very naturally to people who use cloud-based apps and begs the question of why did it take so long for an end-to-end customer-centric experience to manifest itself in a car manufacturer.
Another benefit of having a unique account with Tesla is the ability to use the mobile application to check on your car. You can see the charging status, location, and temperature. There is also a feature where the app will alert you if the car is excessively speeding. A friend found this out the hard way when he got buzzed by the app when at an out-of-town dinner. He checked the app and was able to watch his car on a moving map speeding at 200km/h down a city highway. A quick call to the hotel valet indicated that the car had been taken out “for a wash,” and shortly after my friend was able to see the car taking a highway exit and turning back to the direction of the hotel. Valet mode as a security feature to the Tesla was added shortly after.
The app can also control your car by turning on the AC or heating, popping the trunks, or getting the car to drive itself in and out of a garage in summon mode.
As a nerd, and as it turns out there are a bunch of Tesla owners that are nerds — it doesn’t take long to realize that an app that get real-time streaming information and can exert control must have access to an API.
A guy by the name of Tim Dorr has put together a nice API reference and you can check it out below.
Another individual, Hans Jespers, pieced together some node.js code to tap into the Tesla streaming API and dump the raw data into a MongoDB database.
His code captures status and telematics information about the vehicle. The status information contains things like inside and outside temperature information, defroster, seat heaters, and preset heating information. The telematics information has the car odometer, location, speed, battery charge/drain rate, and distance remaining. Why it’s in Imperial units baffles me, the rest of the world is metric.
From the string above you get a timestamp, the total distance travelled in miles (about 67,000km in my case), various stats about ideal and estimated battery distance remaining, a latitude and longitude of the current location of the car, and whether it’s driving, parked, or charging.
Initially I was interested in charging and battery distance information and Hans’ app had some great charts with battery consumption analysis. Range anxiety is an acute syndrome for the first winter of any Tesla owner who has to deal with temperatures below 12oC (53oF). Stop and start battery performance diminishes quite considerably and it takes a while to understand the performance curve. When I first got the car, the supercharger network was much smaller in Canada and making sure you could get to a remote charging location was key. Potentially taking a one-way drive wasn’t super-exciting and certainly isn’t a way to endear yourself to the wife and kids when going to the cottage.
Admittedly, after a few weeks of analyzing charts, my interest waned and I completely forgot about using the app. The only thing I was diligent about was keeping the streaming code running on my desktop. At some point in the future, the data had to be useful.
Fast forward three years and I now have 12,960,243 geolocation data points taking up 3GB of MongoDB tablespace. The data is generally complete except for short periods where I forgot to restart the streaming collector and for seven weeks the first summer when somebody crashed into my rear wheel and bent the struts when taking too wide a turn.
I was intrigued in creating a heat map of where my car had been and with what frequency. Why? I had the data and wanted to do something with it. That, and I had a looming deadline of documenting architecture processes and needed a mental break.
Since this was a simple afternoon visualization experiment and I had no idea how to solve this problem, I didn’t want to spend oodles of time investigating a perfect solution.
After truncating the values to try and reduce the uniqueness of the car’s locations, the number of times the car had been over that point had to be calculated. This frequency per location would form the basis for colouring the heat map. What I don’t account for is traffic jams or speed. The Tesla stream will emit the same number of data points for a given time regardless of speed. So a slow drive will look more driven (that’s homework to do). Since I’d already loaded the data from MongoDB into postgres in the hopes of using PostGIS, it was a straightforward select/insert to group by latitude and longitude. The grouping created bins of the number of times a lat/long coordinate had occurred.
This truncating and grouping reduced the unique number of data points from 13MM to 656,488 (a dataset twenty times smaller). A quick query of the grouping shows that even with reducing the accuracy, the car was in one location 343,070 times over the course of three years. The first bunch of data points are the location of the car when parked on the street somewhere out front of the house (no garage for me!).
As you can well imagine, the data needs to be further binned to fit into a nice colour palette. It wouldn’t be useful to try and plot the data with 650K colours and expect to see something reasonable. I decided that a size of thirty two for a colour palette would be enough to colour the heat map for density.
R has a useful function called findInterval which will look at an entire vector (array) of data and bin it accordingly. The command below looks at the number times the car was in a location (count) and ranks that value into one of 32 bins.
At this point in time, we have a series of latitude and longitude data points with a corresponding heat map colour from a palette of thirty two colours.
The next step is to create the underlying map template using ggmap. The code below requests a map of Ontario (the province containing Toronto, where I live).
The last step in this entire process is to merge the driving data set with the map to create the heat map.
How that I’m typing this, I realize that yellow and red aren’t the best colours as overlays on a Google roadmap, but you can see the car generally stays around the Great Toronto Area with trips up north and to various family members. There are large swaths of missing data on a couple of camping trips and I don’t know if that was because of cellular connectivity issues or something else.
If somebody from Tesla does read this, please add a SuperCharger to Peterborough or Bancroft. All of Haliburton Highlands are essentially off limits during colder weather when battery range is reduced.
The number of points in the local area is too much and the ordering of the data points on the map confuses the actual driving density. Where the map looks more mushy, it really should be dark red.
Other things that can be figured out from the collected data and that I’ll potentially address when other deadlines are looming:
Thanks for reading, I’m no rocket surgeon when it comes to visualizations, but I wanted to share what you can extract from your Tesla’s API.
I chose to look at Medium’s membership program. The member motivations that it addresses are having money go back to the site’s journalism, interactions with likeminded organizations, appropriate…
When launching a new product, it can be scary not knowing how it will be received and whether or not it will be a success. We are big believers in the importance of really getting to know your…
I love Oscar Wilde but I have to call bullshit on this quote. The artist is the creator of beautiful things? Beautiful to whom? If beauty like they say, lies in the eyes of the beholder, then which…