Tesla Integration to Query Status and Send Commands to Your Car

This process is working for me as well. But two days ago something seems to have changed with TeslaFi, and now the token is refreshing every day, which make the manual process of updating the token impractical. I’m looking into another way to generate the token externally, but it was my understanding you could only have one token at a time, which means that I’ll need to generate the token externally and update both TeslaFi and Hubitat Tesla Connect. @bjr312 I’ll post an update when I have one.

I made some progress, but I’m not quite there yet. You can create different access/refresh tokens from different Tesla accounts, so this would allow tokens for both TeslaFi and Hubitat Tesla Connect:

  1. Use TeslaFi’s process to generate a token via login to Tesla site using your regular Tesla credentials. TeslaFi will store this token and refresh it regularly as it appears to be doing now.
  2. Using the Tesla app (under the security tab) create another driver using a different email address. This is a bit convoluted because you need to have the app on another device or log out of the app to create a new account, but should be pretty easy if you follow the instructions. If you already have another driver with another login, you can use that account if they permit you access.
  3. Use an app such as ‎Auth app for Tesla on the App Store (not sure if there is an Android version) to generate another token using the additional driver account credentials you created. The access token generated here works, but the app states that it is temporary, so I’m not sure how long it’s going to last. The refresh token does not work directly in Hubitat Tesla Connect app.

So, while I’ve made it this far, I’m afraid the need to manually refresh the token frequently is going to persist. I think the solution will be to built the capability into Tesla Connect to use the refresh token to regularly generate access tokens (like TeslaFi does), but that is beyond my immediate capabilities (but I will work on it).

@kahn-hubitat - Any thoughts on how to get this done? This would basically be re-adding the auto token refresh feature to Tesla Connect. I found what appears to be a python working solution for the new V3 tokens, but this is beyond my ability. Thanks!

1 Like

Good catch needing to use 2 accounts for the two separate apps. Though personally I was only running the free trial of Teslafi just to understand how the authenticating worked better. I don't plan to continue using it after my trial expires. For me, all the useful data I need can be pulled from Tesla Connect, as well as having built up some google voice control features, and phone shortcuts (stuff I integrated into webcore) to control the car.

The android token app is called: Tesla Tokens. It is what I have been using and seems to work just fine (minus the new 8 hour refresh time). I also found this website: https://tesla-info.com/tesla-token.php that works well too.

Since this has been somewhat broken for me for the time being, I have mainly been updating my token every Sunday evening, since I log the mileage of the car into a spreadsheet every Monday morning just to keep track of how much I drive every week.

Good catch finding some working python code. I know @kahn-hubitat had a system up and working to refresh the token from a remote location (well a local server). It worked good until Tesla retired the V2 tokens. Maybe this code new working python code could be combined to automate the token grab?

The python code as well.no longer works. I had it working for awhile as.well .

Still working on this, but here’s what I’ve accomplished thus far: Auth App for Tesla has iOS Shortcuts actions, so I was able to create an iOS shortcut that periodically runs, refreshes the access token from Auth App, and sends it to a Hubitat variable as shown in the images below.

My remaining issue is how to get the new access token to automatically load into the Tesla Connect App application state for the access token (state.accessToken). @kahn-hubitat - Any ideas here? I’ll also post this generic question to the forum.


If you can upload it to the hub file manager I can work with you read it into the app.

You are right it looks like tesla has changed the format of the api token. Based in what token teslafi is showing. Is this the 4,5 time? they have done that. The tokens no longer start with qts and are much longer.

As for getting it into the system i already have the capability to pull the token from a website. The easiest way would be to use a 3rd party generator and out put it into a local web url.

Unfortunately. I am in fl right new and my main wifi router that gives out private ips apparently has died .

I can reboot it via a kasa switch (on a backup at&t access pt wifi network) remotely but that has not helped. As a workaround i turned it off and turned wifi on my main Comcast modem. Some cameras etc are back up snd reachable but not my hub and other devices.

I can see the hub is still on as lights are turning on/off on schedule.

Long and short of it... i have no access to the hub and cannot help till i get home in a week. Also I have NO apple devices nor plan to so cannot help there.

Yeah new tokens now start with eyJhb, and are very long.

I found a bit of code HERE that will decode the URL from Tesla and provide the tokens. It seems to host a webpage, and you copy and paste the URL from Tesla into it, and it gives the codes. Similar to how others seem to be doing it, but its locally hosted. I tried it out, and it is working fine, just still a manual process.

I may tinker with this code I found and see if I can automate it further so it doesn't require manually copying the URL from Tesla into it. Probably a long shot though, since I don't have much experience with stuff like this, and it seems this is what Tesla is trying to prevent.

Enjoy what is hopefully a vacation @kahn-hubitat, another week or two of downtime won't kill anyone :grinning:

If you're doing this, the Tesla Connect app could certainly read the Hub Variable and stuff it into state.

The value in the variable can be retrieved with

getGlobalVar(name).value

That would return the string, to put into the Tesla Connect app. I haven't looked at the code there, but you're very close to this working.

2 Likes

Awesome, thanks @bravenel!!

@kahn-hubitat - This sounds easy enough (for you :stuck_out_tongue_winking_eye:). Right now the app only pulls in the new token when it is manually added/updated, so I guess we need a regularly scheduled refresh or a trigger that updates the token in the app when the global variable changes. I know many won’t be using my exact process, but I think pulling a global variable into the app is generic enough for others to use any process to update the global variable. Also, I realize you’re out, so no rush… I guess I’ll just have to manually turn on the HVAC for now! :scream:

@thebearmay - with the info Bruce provided, do you think can help? LMK and I’ll jump in while @kahn-hubitat is out. Thx!

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