Tesla Integration to Query Status and Send Commands to Your Car

it only seems to work for a token that is actually associated with a tesla phone app .

i have other logins i generated that are not associated with a tesla app on a phone and they no longer work no matter how i generate the token

Once you give it a refresh token, it will actually refresh the token automatically when it expires, so I believe you only need to supply the token once.

No. I copied code and tried that. The refresh token works but it fails getting the new access token. I.can post the version for your testing.

Url returns unauthorized

1 Like

I think this works for the refresh token, but not the access token. So it works fine for powerwalls, but not the cars. Not sure how teslafi gets away with it though, since it seems to auto-update some way and still keep access to the cars.

@kahn-hubitat would there be a way to use the refresh token to at least get the data from the car (but not control it via Hubitat)? This would be mostly beneficial for me, as I use the car's data for certain automations. Then, really all I would be out is being able to start the climate via google assistant or the button on my phone.

I don't know if this is a possible request, as it seems the refresh token is meant for powerwalls only, and the access token is meant for cars only. Though, I don't know how Tesla is feeding the data from their API, so I figured I would ask

TeslaFi uses the access token for API queries, and the refresh token to get a new pair of tokens when they expire. At least, that's how it worked 2 years ago.

i am working on using the tesla_auih exe on a pc right now and modifying the output to generate a json file that can be put on any webserver and use the url option already in the current app..

however still testing as the new token format is not url encoded and can contain cr and lf.. what a pain..

1 Like

If you get this working, let me know and I can test it out. I have a windows PC that is always running that I can run it from. I might need a quick guide on how to set everything up though

so far so good .. actually got first renewal to work via url generated on pc and pushed to website..

it is a little kludgy you will need some unix like utilies xxd and sed . but are readily available nad i have .exe command line versions..

in addition you ned to run tesla_auth once initially to get the username pwd in..(I had to actually change screen resolutions and tesla now added a captch and the window was too small to get to the submit button.)

since tesla_auth opens a window you then kill it after 10 seconds and then use the xxd and sed programs to modify the resulting generated tokens to be in the .json format thnat the web request expects.

in addition, you will need to use a 3rd party task launcher to launch it as it opens windows and i cannot get the built in wwindows one to work with the pop ups.

so
tesla_auth.exe
sed.exe
xxd.exe
system scheduler profesional task scheduler

i then use ftp witl and input file to push it to my webserver..

i will test for a few more days as parsing the generated token and getting the correctd formet is complicaqted.. i had to use xxd to convert to binary as there were cr lf in the file and sed (stream editor) doesnt work wi;th thos.

here is the current batch file..
c:
cd c:\teslatoken
start /B tesla_auth > token
sleep 10
taskkill /IM "tesla_auth.exe" /F
rem
copy token token.orig
rem
xxd -p <token | sed s/0a2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d// | sed s/2d2d2d2d2041434345535320544f4b454e202d2d2d2d2d2d2d2d2d2d2d2d// | sed s/2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d0a0a/7b226163636573735f746f6b656e3a22/ | xxd -p -r > token1.tmp
xxd -p <token1.tmp | sed s/0a0a2d2d2d2d2d2d2d2d2d2d/220d0a/ | xxd -p -r > token2.tmp
xxd -p <token2.tmp | sed s/2d2d2d2d2d2d2d2d2d2d2d2d2d2d// | sed s/2d2d2d2d2d2d2d2d2d2d205245465245534820544f4b454e202d2d2d2d2d// | xxd -p -r > token3.tmp
xxd -p <token3.tmp | sed s/0d0a2d2d2d2d2d2d2d2d2d205245465245/2c22726566726573685f746f6b656e223a22/ | xxd -p -r > token4.tmp
xxd -p <token4.tmp | sed s/534820544f4b454e202d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d0a0a// | xxd -p -r > token5.tmp
xxd -p <token5.tmp | sed s/0a0a2d2d2d2d2d2d2d2056414c494420// | sed s/464f52202d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d0a0a3820686f// | sed s/7572730a202020202020202020202020202020200a/227d0d0a/ | xxd -p -r > token6.tmp
xxd -p <token6.tmp | sed s/7b226163636573735f746f6b656e3a22/7b226163636573735f746f6b656e223a22/ | xxd -p -r > teslatoken
rem
copy teslatoken teslatoken2
ftp -s:pushtoken.txt 192.168.11.79
erase token*.tmp

final token file looks like this
C:>cd teslatoken

C:\teslatoken>cat teslatoken
{"access_token":"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IlR3bjV2bmNQUHhYNmprc2w5SUYyNnF4VVFfdyJ......................","refresh_token":"eyJhbGci..."}

C:\teslatoken>

modified to not show my tokens oviously.

i have asked the author of tesla_auth to generate or have an option to generate in .json format but so far no response.
the source is avail. if someone wants to take it on.. it is in rust , and i don;t have a rust compile environment set up and dont feel like going down that route right now. but the code changes should be minor to generate it in .json format. Probably actually much easier than the crap abov e..

you also need to follow the directions to get webkit installed on windows (install devcleopment version of edge) .. i cannot get this working on my qnap unis box as there is no webkit library i can find that runs on it.. but most of it similiarly should run on a debian unix if you install tesla_auth and webkit.. the patterys for the xxd / sed should be the same.

1 Like

here is the first sucessfull auth via url since the older python token generation stopped working..

It's the same API/token, so using the refresh token logic from the Powerwall Manager app should work here for vehicles. Here is a fork of the Tesla Connect app with the Powerwall Manager app refresh token logic pulled in. You can enter a refresh token in lieu of an access token and the app should keep an access token refreshed.

https://github.com/DarwinsDen/hubitat-1/blob/master/teslaconnect.groovy

1 Like

What is being logged as an error in your version on line 657 is just an attempt to get a 45 day token in case that method starts working again at some point. That is just a debug log in the Powerwall Manager and shouldn't be hurting anything here.

However, when I run your version I do see a couple of exceptions from your port that would cause issues.

  1. There are a couple of unreferenced variables that can be fixed by adding this to the end:
@Field static final Long oneHourMs = 1000*60*60
@Field static final Long oneDayMs = 1000*60*60*24
  1. And there are references to the Powerwall Manager 'logger' method on lines 578 and 583 that need to be changed over or commented out.

When I make those two changes,your version appears to work for me.

2 Likes

ok trying it again.. i assumed that error was causing issues.. changed it to info. maybe will comment the whole thing out.. anyway it was not working as everytime i tried to access the care i got unauthorized..

ok so far so good.. you need to put both normal and refresh tokens in the first time.. then it seems to work only with refresh in the futre.. i will let it run for a few days..

thanks for all your help.

seems to be working.. will post new version to git hub soon.

thanks to darwin here is a version to test with both refresh and normal token.

you will need to generate both tokens for the initial install. and put them in the appropriate fields..

after that it should only have the refresh token in when you look at it should work.

you will also either need to delete or disable your old tesla connect and car device under devices..

if you don't delete it and are only disabling it during testing.. you will need to also edit the dna to chnge the number or the driver will not be able to create the car with the same dni..

recommend for now you just disable and change the dni. as this driver is in early testing phases but I know how much off a pain it is to cut and past our token in multple times a day.. so I am releasing this early for testing.

There are also new debug options. at a miniumum recommend you leave descLog on for now.

  • you still need to generate the initial token and refresh token.. recommend you create a separate login on tesla for it, as if
  • you use your teslafi or other app token that uses differing login (ie regenerates the normal token) they will step on each other
  • and it wont work.
  • also added new debug and desc log settings.
  • use tesla_auth.exe or something else to generate initial token pairs.
3 Likes

Copied code from here and replaced my current app. Got fresh tokens and added access and refresh tokens into the menu. It seems to be updating as normal for now. I will check again tomorrow morning and see if things are still cooperating

1 Like

Threre was a bug in the previous posted version that would only sfchedule the refresh call once ... so only about 12 hours it should have worked..

I have fixed it and posted a newer version so if you installed it go back and get the latest.. Mine now has refreshed the token 3 times so seems to be working.

1 Like

Thanks, I've updated my app. I checked it this morning, and it seemed to not update. Not sure if it was bug related or not.

If it fails to update again I am going to remove the app and then fresh install. Since I just updated the code from the old app, it kept my car as remembered and I didn't have to rebuild anything. Figured I would try that first, and see if it works. If not, I can delete and rebuild and see if it continues to refresh.

I'll let you know if it still is failing even after a uninstall/reinstall

1 Like

send me the updated app so i can add it to the mainline code with an option to enable.. version 3 right now is in testing and working to refresh the token every 6 hours without any 3rd party apps.. just initially gnerate the two tokens.

Slight change to add notification for successful refresh and scheduling if notifications are enabled..

Also will pull in Brads code with the option for push token update from a variable in the next day or two.. But if this new version continues to work well should not be needed but you know tesla they change this #@@#$ every month.

2 Likes

Well my last post was over 9 hours ago, and I am still connected! Updated code again just to keep up with the latest versions for debugging. Will be nice to get notifications to ensure things are updating properly.

Good to see this seems to be working automatically again! Hopefully Tesla will stop changing things so we can enjoy it for a few months without disruption :joy:

1 Like