Tesla Integration to Query Status and Send Commands to Your Car

Triggering a pull from a globalVariable on change is fairly easy just a subscribe and fetch. Also need to provide code to register the app as using the variable, and remove the registration if the app is removed, but also very easy.

It looks like Darwin's Powerwall app (Pulling data out Tesla Powerwall - #62 by Darwin) has the Groovy code to take a Tesla login and generate a token, and then keep refreshing it (using the refresh token). Before attempting to use python scripts and external webservers, it might be much simpler to use that logic in this app.

I was going to look into a solution as well that would work for Android via Tasker. If I could get it working, I would be able to upload the token to a global variable in Hubitat.

Just need to talk with the dev for the tesla tokens app to see if they can incorporate some sort of sharing procedure to communicate with Tasker. Currently you have to open the app and copy to clipboard (which could be done with tasker, but is a lot more work).

Might be able to set this up on windows as well, given decent enough working support to run Android apps. Its been a while since I've played with any of that, so idk if that would work or not

Agreed, if it is still working for the most recent Tesla API config. I’ll dig into it this weekend, but it will be painful due to my skill level. May be able to figure it out.

@thebearmay is working with me (he’s working, I’m testing) in a private thread to pull the global variable into the app, and it’s looking promising!

2 Likes

I normally connect to my Powerwall locally, but I when I get home I was going to add a 2nd instance that would pull via the API just to be able to check if it was still working.

:tada: Ok, some great news for those of us who are still refreshing our access tokens manually every 8 hours - @thebearmay incorporated the ability to send a refreshed token via URL (cloud/local) to the app, which updates the access token in the app seamlessly. As noted above, I'm using iOS shortcuts to generate a new token and send it to the hub... I have now scheduled this to happen every 6 hours.

If anyone would like to help test, DM me, and I'll share the code privately (I aim to keep this with @kahn-Hubitat in his repository, so it doesn't get splintered; therefore, I don't want to post broadly). If these updates are approved, we can move forward with this version and work to also incorporate the integrated token refresh option that @Darwin is doing with the Tesla Powerwall.

A few notes on the updates:

  • After installing the App and Driver, open the app and turn on the option to Activate Endpoint for Updating Token
  • You should then refresh by clicking the Reset Endpoint Token button. When you do this you're taken to a blank page (trying to iron this out), but the token will be refreshed. You simply need to return to apps and reopen Tesla Connect, where you will see the new endpoint URL
  • The URL provided by the app will look like this: https://cloud.hubitat.com/api/43hsl42j-8402-98gz-d67o-8hk24j2ps5k3/apps/2141/setToken?teslaToken=[Tesla Token Value]&access_token=65893616-6382-93vs-9713-6739fd93l9gd7
  • You simply need to schedule your preferred program to regularly send the token via the URL. My tokens are expiring every 8 hours, but you may need to adjust how frequently you're sending depending on what you're seeing. 4x/day gives me enough overlap to alert me in time if the refresh doesn't happen.
  • This update also includes the ability to set valet mode

Shout-out to @thebearmay for this contribution. It's awesome devs like this that make the community so great for all of us.

5 Likes

I've had some inquiries, so for those of you who intend to use iOS to send the URL to the app, here are the instructions. Please note that you need to do the scheduling (step 3) specifically on an iPhone, because Apple (because they are Apple) will not allow you to create/run a personal request on an iPad or other type of device.

  1. Install and set up Auth for Tesla. This is what I use, but another app may work. The important thing here is that it exposes refresh and get token actions to Shortcuts.
  2. Create a new shortcut (Shortcuts app > My Shortcuts) and configure as shown in the image below. Here is a template: Refresh Get Send Tesla Token to Hubitat
    a. Be sure to turn off Show When Run, which defaults to on.
    b. The URL you will use here is what is provided in the app. Just insert the Token variable from Auth for Tesla where [Tesla Token Value] is shown in the Tesla Connect app-provided URL.
  3. Once your Shortcut is created, you can run it manually, but you'll need schedule it to run regularly to fully automate this
    a. On your iPhone, go to the Automation tab in the Shortcuts app
    b. Select Create Personal Automation
    c. Use Time of Day to set your first preferred time of day to refresh
    d. Add Action and search for Run Shortcut
    e. Select the Refresh Get Send Tesla Token to Hubitat shortcut that you created using the steps above, select Next and Done.
    f. Repeat above for how every many daily refreshes you want. I chose to refresh every 6 hours because the tokens seemed to expire w/in 8 hours. If you like to live on the edge, you can probably get away with 3x/day.

That should be it. Your schedule will run your shortcut, which will refresh and grab the token and send it to the Tesla Connect app where it will be updated for your Tesla API calls. I've had mine running through a few cycles now, and its working great. Please let me know if you have any questions.

Bonus Config:
I also created this Tesla Token Refresh Monitor rule with an endpoint that I hit from my shortcut at the same time I hit the app. Thus, if the shortcut doesn't run for some reason, I'm notified... else the notification is cancelled. This alert functionality could probably be added into the app at some point.

1 Like

i cannot get the new tokens to work period. has anyone been able to use either the new format tesla fi token or the one generated by tesla_auth i get the following error when putting into the "input new access token when expired"
this has always worked in the past?
errorRequest failed for path: /api/1/vehicles. [error:invalid bearer token]

solution add entire new app instance... don't delete old one it will overwrite it.. ok now at least i have it working .. will look at login code in future

this is not true. i looked at the source and there is no username and password for login.. it is basically doing the same we are .. here is the page that comes up

the username and password login function is only to login to your local powerwall. not the tesla server to generate a token.

Using this site Tesla API Token and manually copy/pasting in the token each day into Hubitat is working for me

1 Like

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