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

Just spotted this opensource API scraper

This site may earn commission on affiliate links.

Glan gluaisne

Active Member
Sep 11, 2019
2,782
2,925
UK
I've been thinking about using one of the spare Raspberry Pi Zero W modules I have sitting here to make a cheap and cheerful little box that will wake the car up at midnight to allow charge point controlled charging to work properly, and also to do as @MidhurstNigel has done using TeslaFi to do this little job. TeslaFi seems to work OK, but it does have a lot of functionality I'm not that interested in (yet) plus it needs a subscription.

Whilst looking for ideas as to how to get just a basic way to use the Tesla API commands to work on a RPi Zero W (turns out to be surprisingly easy) I stumbled on this Github project: lephisto/tesla-apiscraper

There's also a chap that's put together a fairly rough and ready intro to using this Tesla Dashboard:

Looks to have similar functionality to TeslaFi, but is open source, and doesn't use tokens, so just requires that you keep the machine it's running on as physically and electronically secure as your key fob, key cards or 'phone, as that machine will have your log in credentials.

Not sure it's something I'm going to play with just now, but I do have a couple of RPi3 s around, and a spare monitor or two, so I may have a go at making a stand-alone box that's dedicated to just monitoring the car at some future date, This project looks to be a pretty interesting starting point for that.
 
Could do, but that's outwith my skill set! I'm intrinsicly a hardware person that only writes code as a means to an end.

There's also the security aspect, as the login credentials need to be held securely, or a token used.

I'm OK writing a few lines of Python, and running that script as a cron job, and can do the whole lot for under £25, or less than that as I have a couple of Raspberry Pi Zero Ws spare. This also means that I can physically secure the login credentials, by making sure the Pi Zero W is locked up somewhere.

Initially all I'm looking to do is use a few (may be just one) of the Tesla API commands to wake the car up at a set time (run as a cron job) so that I can get around the charge acceptance flaw, and allow my timed charge point to work. I reckon if I start with the very simplest task, like this, I can then have a tink about whether adding some additional functions might be useful. One idea that appeals is to look at making what amounts to a really simple car "remote control", that I can just pick up and do things like turn on preconditioning, start or stop a charge, maybe even vary the charge rate. There's no need for a display, and with luck I may be able to find a way to get the power consumption down to a low enough level for it to perform much as a TV remote. In the past, I used the remotes for the last two cars like this, from inside the house, most often to turn on cabin preconditioning.
 
  • Disagree
Reactions: T.F.S.
If all you're querying is an API to wake the car, why not just knock up a cheap Azure/AWS service to do so? Most of them run X transactions per month for free.
Could do, but that's outwith my skill set! I'm intrinsicly a hardware person that only writes code as a means to an end.

There's also the security aspect, as the login credentials need to be held securely, or a token used.

I'm OK writing a few lines of Python, and running that script as a cron job, and can do the whole lot for under £25, or less than that as I have a couple of Raspberry Pi Zero Ws spare. This also means that I can physically secure the login credentials, by making sure the Pi Zero W is locked up somewhere.

Initially all I'm looking to do is use a few (may be just one) of the Tesla API commands to wake the car up at a set time (run as a cron job) so that I can get around the charge acceptance flaw, and allow my timed charge point to work. I reckon if I start with the very simplest task, like this, I can then have a tink about whether adding some additional functions might be useful. One idea that appeals is to look at making what amounts to a really simple car "remote control", that I can just pick up and do things like turn on preconditioning, start or stop a charge, maybe even vary the charge rate. There's no need for a display, and with luck I may be able to find a way to get the power consumption down to a low enough level for it to perform much as a TV remote. In the past, I used the remotes for the last two cars like this, from inside the house, most often to turn on cabin preconditioning.

SIRRob has a point. if that is all you want to do its worth checking out AWS Lambda. Its the AWS serverless service. Its very very simple to use. It supports Python. You can literally just write the code paste it in set a timer as the trigger event and you are done. completely resilient. and 1 million invocations per month for free so it won't cost you a penny.
Its pretty secure but you would not even have to upload your credentials if you did not want to. all the Tesla APIs require a token so you could create one using a manual api call from say postman harvest the token and just use that.
 
I’m all about the pi, but what your asking could also be done with iOS shortcuts on your iPhone or Tasker on your Android phone.
I do see your point about putting logins or tokens on public infrastructure, so maybe this will work too.
 
I'm not sure if you have played with the API's at all ( I may be teaching my grandmother to suck eggs here) but if not just so you know to wake the car you will need to do the following:
1) get an API token using your email and password
2) using the token call the vehicles API to find out your id ( confusingly there are 2 id's, "vehicleId" and the "Id" it is the Id you need not the vehicleId)
3) Call the wake up api which will need the Id and the Token

I have attached some simple python that contains 3 functions, one for each step. I'm sure your Python is better than you make out but it might save you a few minutes doing it from scratch ¯\_(ツ)_/¯
 

Attachments

  • demo.txt
    1.8 KB · Views: 91
If you would like to go the raspberry pi route, you might want to look at Home Assistant. This is an open source home automation system, but also has a Tesla integration.

This would be running locally, and you can setup a display on a £35 fire tablet/old phone to do things like unlock the car or charge port. You can also setup automatons for things like charging at certain times, etc. I've got one that will remind me first thing in the morning if the battery is below a certain level in case I forgot to charge overnight...

This is a pic of my setup (with some other home automation stuff as well):

20190806_144250.jpg
 
I’m all about the pi, but what your asking could also be done with iOS shortcuts on your iPhone or Tasker on your Android phone.
I do see your point about putting logins or tokens on public infrastructure, so maybe this will work too.

Sadly no 'phone coverage here, only a landline connection, so our 'phones tend to stay off 99% of the time. All I'm looking for initially is a work around for the failure of the M3 to wake up and charge overnight, due to the bug that stops the CP waking the car when it starts advertising power. Other stuff might be handy later, maybe, but isn't the driver behind doing something right now.


I'm not sure if you have played with the API's at all ( I may be teaching my grandmother to suck eggs here) but if not just so you know to wake the car you will need to do the following:
1) get an API token using your email and password
2) using the token call the vehicles API to find out your id ( confusingly there are 2 id's, "vehicleId" and the "Id" it is the Id you need not the vehicleId)
3) Call the wake up api which will need the Id and the Token

I have attached some simple python that contains 3 functions, one for each step. I'm sure your Python is better than you make out but it might save you a few minutes doing it from scratch ¯\_(ツ)_/¯
If you would like to go the raspberry pi route, you might want to look at Home Assistant. This is an open source home automation system, but also has a Tesla integration.

This would be running locally, and you can setup a display on a £35 fire tablet/old phone to do things like unlock the car or charge port. You can also setup automatons for things like charging at certain times, etc. I've got one that will remind me first thing in the morning if the battery is below a certain level in case I forgot to charge overnight...

This is a pic of my setup (with some other home automation stuff as well):

View attachment 487672
One line api curl call run from cron job and update token once a month?

Tesla API (Unofficial) - Testing via cURL

Didn’t read above as on small screen
I can confirm the API works a treat. As has been said it’s pretty straight forward to get a token. I send a wake command, wait 45s, and then a charge command. Bingo.

Tesla API
Introduction

Thanks all, much along the lines I've been thinking, especially the simpler option of just waking the car shortly before a scheduled charge time from the charge point. The link I gave in the other thread to the chap that was using a RPi to trigger his car to give alarms from his CCTV/PIR detection system was the trigger for this.
 
  • Disagree
Reactions: T.F.S.
All I'm looking for initially is a work around for the failure of the M3 to wake up and charge overnight, due to the bug that stops the CP waking the car when it starts advertising power. Other stuff might be handy later, maybe, but isn't the driver behind doing something right now.
Hmmm, why not use the time of charge capability in the TM3 instead of the charger? I'm not familiar with the CP and reasons for using it's time of day logic instead of the car's logic so apologize for asking a simple question. If you need to continue to use it, maybe set the car to start charging 5 minutes after you "enable" the CP to advertise ?
 
Hmmm, why not use the time of charge capability in the TM3 instead of the charger? I'm not familiar with the CP and reasons for using it's time of day logic instead of the car's logic so apologize for asking a simple question. If you need to continue to use it, maybe set the car to start charging 5 minutes after you "enable" the CP to advertise ?

That sort of works for a fixed time-of-use schedule, but here we're in the process of rolling out variable electricity pricing on a 30 minute window basis, so our smart charge points (mandated here, in effect, as they are the only type that can be grant funded) have the capability to vary the charge time according to tariff variation. We're also in the process of switching to smart metering, that allows the use of constantly variable electricity tariffs, on a 30 minute window basis. The grid here are very keen to get this smart system working, as it allows EV charging (and other loads) to be either turned off, in high demand periods, or turned on, during low demand periods, to keep things in better balance.

The car is supposed to follow the IEC61851 signalling protocol, whether it's awake or asleep, so should start charging when the CP starts advertising and stop charging when it stops advertising, but sadly it doesn't, it needs to be woken up in order to respond to CP change.

Also, there's a bug in the scheduled charge setting, that may only affect UK, or may be EU, cars, in that when scheduled charging starts it initially ramps up to the maximum advertised charge current, then drops back to 16 A a few minutes plater. This can leave the car with not enough charge the next morning. One work around for this is to use something to put the car to sleep again a few minutes after it's started charging, then wake it up again, which seems to then bootstrap it into charging at the advertised charge current. What seems pretty clear is that Tesla have just not bothered to test the functionality of the charge control system on our M3s properly. My inclination is that they have done near-zero testing, as the car doesn't even do as they say it should in the owners manual.

Have a look at this branch which appears to be more up to date
adriankumpf/teslamate

Thanks for that - bookmarked for when I move on to something a bit more complex than just having a stand-alone gizmo for waking the car up on demand.
 
  • Disagree
Reactions: T.F.S.
Also, there's a bug in the scheduled charge setting, that may only affect UK, or may be EU, cars, in that when scheduled charging starts it initially ramps up to the maximum advertised charge current, then drops back to 16 A a few minutes plater.
For the record, I haven't had this problem with my Rolec charger, so may be related to specific brands or models of chargers?
 
For the record, I haven't had this problem with my Rolec charger, so may be related to specific brands or models of chargers?

Not sure whether or not it's charge point related, or a bug in the communications with the charger, TBH (the latter being in the car). Several people here, and on other fora, are reporting a fairly regular failure to charge at the set rate when using the scheduled charge function in the car, The car starts to charge at the set rate, then drops back to a lower charge rate after a few minutes, and unless some intervention takes place, it remains at the lower charge rate all night.

One reported fix (there may be others) is to force the car to stop charging shortly after the scheduled charge starts, then force it to start charging again, not using the charge point, but using something like TeslaFi. This seems to reliably restart the charge at the set rate.

I've tried forcing the car to wake up and start charging, when there is no scheduled charge set, but no matter what signal is applied to the CP (the communications line to the car from the charge point) the car refuses to wake up and start charging if it is asleep. Waking the car up by any means (unlocking it, checking its status with the app, etc) always starts the charge, if the cable is connected and the charge point is advertising that power is available.

Be interested to know exactly what settings you are using with the Rolec, and whether the Rolec app has the credentials to be able to wake the car, using the API, or not.
 
  • Disagree
Reactions: T.F.S.
I'd say 95% of the time my Ohme does successfully wake the car and start charging. (Now the Ohme is an unusual cable, and it may well be starting the charge through the Tesla API??)

There have been a few occasions where I've found the car is in a sort of super deep sleep, where charging doesnt start, and the App won't wake it up either, the only thing that works is opening the door! Then everything springs into life.
 
Be interested to know exactly what settings you are using with the Rolec, and whether the Rolec app has the credentials to be able to wake the car, using the API, or not.
The Rolec is set to “dumb”, I have never given it my Tesla credentials (so no Tesla api interaction of any kind), and I use the scheduled charge on the charging screen in the car to start at 00:30. So far it has reliably started at 00:30, and TeslaFi always reports it charged at 32A.
 
  • Informative
Reactions: Glan gluaisne
The Rolec is set to “dumb”, I have never given it my Tesla credentials (so no Tesla api interaction of any kind), and I use the scheduled charge on the charging screen in the car to start at 00:30. So far it has reliably started at 00:30, and TeslaFi always reports it charged at 32A.

Thanks, that's interesting. I wonder if the car happens to be awake enough to still respond to requests from the charge port, or whether the Rolec has a cunning way of waking the car up?

Although I know of a handful of Rolec charge points locally, they are all older, non-smart ones, using the Mainpine EPC. I have a Viridian EPC as a spare here for one of the Rolecs (the Viridian is identical to the Mainpine unit) and I'm tempted to hook it up and see if that can wake the car up. It's a while since I've played around with a Mainpine/Viridian EPC, but I seem to recall that they very closely complied with IEC61851.

I may still have contact info for Kevin Sharp, and he knew a fair bit about the Mainpine EPCs, and the implementation in Rolec products, and he may just know if Rolec are doing something differently with the smart EVSEs.
 
  • Disagree
Reactions: T.F.S.
Thanks, that's interesting. I wonder if the car happens to be awake enough to still respond to requests from the charge port, or whether the Rolec has a cunning way of waking the car up?

Although I know of a handful of Rolec charge points locally, they are all older, non-smart ones, using the Mainpine EPC. I have a Viridian EPC as a spare here for one of the Rolecs (the Viridian is identical to the Mainpine unit) and I'm tempted to hook it up and see if that can wake the car up. It's a while since I've played around with a Mainpine/Viridian EPC, but I seem to recall that they very closely complied with IEC61851.

I may still have contact info for Kevin Sharp, and he knew a fair bit about the Mainpine EPCs, and the implementation in Rolec products, and he may just know if Rolec are doing something differently with the smart EVSEs.
The Rolec isn’t waking up the car. The car is scheduled (on the car’s screen) to start charging at 00:30, so at that time it wakes itself up and starts charging!
 
The Rolec isn’t waking up the car. The car is scheduled (on the car’s screen) to start charging at 00:30, so at that time it wakes itself up and starts charging!

OK, that sort of works, but there are two potential bugs still.

One is that it seems that, when using the car scheduled start, the charge rate will sometimes arbitrarily reduce (usually to 16 A) some time shortly after charging has commenced. This has been widely reported in several other threads here, with work arounds that involve using something like Teslafi to stop charging, then restart it, which apparently then "kick starts" the car to resume charging at the full 30 A/ 32 A.

The other bug is that a scheduled charge start time cannot be used in conjunction with a scheduled charge stop time - the car doesn't have the functionality to set both the charge start and stop times together. This means that, if you're like us, with two charge points, plus some other heavy overnight loads, that limit the charge rate for a time, the car may carry on charging beyond end of the E7 period. Happened to me a couple of weeks ago, where charging started at midnight but didn't finish until mid-morning, so a good 30% of the charge was at peak rate.
 
  • Disagree
Reactions: T.F.S.