Tesla Integration to Query Status and Send Commands to Your Car

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

The real change here was the app creating an endpoint that a separate system could push the token to. The variable thing was an interim step to getting there -- no longer being used.

The token length exceeded 255 (by a considerable bit) so when trying to set a string variable to the token value it would error out and truncate, was actually easier to do the endpoint once the end goal was understood.

I have a small request, if possible. So far everything has been working good today. It automatically continues to update, and I am receiving notifications confirming the update is successful.

Though, since I am receiving notifications, I am now getting 2 notifications every 6 hours. For the time being, this is totally fine, as I want to continue monitoring the situation to see if there is failure. Though, at some point I assume this will continue working well without the need for continuous monitoring.

Would it be possible to have some toggle buttons in the UI to select the notifications we receive? More specifically, I would much rather get a notification if it attempts and fails to grab a token. This way I know I need to look at things, and don't realize it isn't working a day or two later.

Not sure if things are setup to accommodate this, so I figured I would ask. If the current notification scheme gets annoying, I will just disable notifications.

Super huge thanks to everyone involved getting this back and operational again! Let's hope Tesla doesn't change anything again for a long while :joy:

the notificatoin is just for token renewals regardless of method. it is not for failures as there are tons of ways the app can fail usually doing a refresh or a query.. It is unlikely you will see the failure in the token renewal more likely a missing car or unauthorized message in the mormal query. I will look at an option to flag those, but in reality there are a lot of places an error can pop up and it would be ungainly to put a notification in every error handler.

those come out as errors in the logs.. if you want to monitor those and get an alert i recommend you look at.

the other issue is if it does start failing.. and refresh keeps happening you are going to get slammed with tons of messages for error as they will continue to happen.. maybe some flag to altert when an error occurs and a toggle you have to turn off or somehting. i will think about it.

1 Like

Ok listen up peeps , i have integrated brads code which adds the option for the endpoint token update as well as turning on/off valet mode.

However it was not so simple because the endpoint code uses oauth which uses the hardcoded name state.accessToken which is already in use throughout the app.

In order not to break peoples apps that are working and also using the new version that is auto refreshing I chose to add a new function from the car device panel
called transitionAccessToken. This will copy the old name state.accessToken to the new name state.teslaAccessToken.

So ffor anyone with an already running system, immediately after installing the code for the new driver first, then the app below.

You MUST go to the driver ie your car device panel and run the transitionAcesssToken function. If not you may have to unstiall everything and start over.

Also need this and the endpoint token code tested before i wrap this whole thing in the packagemanager app.

You can also always check the next token refresh is scheduled in the APP (not car device) by going to the settings icon in the app on the right top and checking the bottom for the function (see images)

Also, I added a panel function called scheduleTokenRefresh which you can run at anytime fo force a refresh or for some reason if you find doing the steps above that no refresh function is scheduled, say for instance if hub was down for a long time etc.



finally here is the new code to test thanks....

2 Likes

Updated to newest code. Slight issue, but was able to get around it without reinstalling everything.

**EDIT: Re-reading your post, I think I messed up the order. I did the transition access token, then installed the app. Should have been the other way around. I screwed up, but I guess I got lucky

Here's what I did:

Copied driver over into the driver code.
**Went to car page, and pressed Transition Access Token
**Copied app over into the app code.
Opened App, grabbed new tokens, and copied them over
Upon submitting tokens, it couldn't find any car. I just hit next
Opened the app again, and copied in the exact same tokens
This time my car showed up and was already selected like normal
Hit next and left app.
Everything seems to be working fine, and even though it didn't show up the first time, it didn't wipe everything. Refreshes seem to be responding as well

I left endpoint disabled, as I don't plan to use that method. I am also seeing the scheduled event showing up as expected.

Might have been a fluke that it didn't show up the first time, but I figured I would share. Hopefully the steps I took were in the correct order

I figured it wasn't setup this way, as it is hard to capture errors and act on them accurately. Not a big deal if this doesn't happen though. If it continues working well thru the next few days I will probably disable notifications and just go on as normal. If it stops working at any point, I'll let you know

need to copy driver and app over before hitting transition.. just said driver first as there will be less time between saving the app and you hitting transition where an auto refresh of the vehicle could possibly run.

Also you shouldnt have to submit any new tokens.. if you just install both and hit transition you should be able to hit wake/refresh and see it work in the logs with not even touching the new app panel if you dont want to.

Yeah, just realized and edited my post. Luckily I didn't have to reinstall

1 Like

@kahn-Hubitat - I updated to the most recent version, and initial results are good. (Sorry for the delay... busy week and I wasn't wasn't watching this post for some reason... missed the recent updates). Once I installed the driver, app, and then transitioned token, I just had to do a manual refresh to the endpoint to update the app. I do want to move to the new Access/Refresh Token option in the app so all the other external junk isn't needed, but I'll continue to use the endpoint for a bit to test. Also, I just want to make sure that proper credit is given and call out that "brad's code" referenced above was 99% done by @thebearmay.

3 additional items to note:

1. Window attributes types need to be updated. The values are open and closed, so these should be strings instead of numbers. You can't use these correctly in rule machine if they are set as numbers (and this actually broke my rules that use it):

        attribute "front_drivers_window" , "number"
        attribute "front_pass_window" , "number"
        attribute "rear_drivers_window" , "number"
        attribute "rear_pass_window" , "number"

2. Commands are still failing when the car is asleep. There is only a 2 sec pause, so I'm getting Request failed for path: /api/1/vehicles/1234567890/command/window_control. [response:null, error:vehicle unavailable: {:error=>"vehicle unavailable:"}, error_description:]
To fix this and check for online state, @thebearmay helped me replace the commands to include a delay (example below), but as we discussed in our direct message, I agree a better option would be to wake and then retry every 5-10 seconds until success since the 30 seconds used here could be too little or too much, depending.

def ventWindows(child) {
    wake(child)
    pause(2000)
	return executeApiCommand(child, "window_control", body: [command: "vent"])
}

with

def ventWindows(child) {
    def data = [:]
	def id = child.device.deviceNetworkId
    authorizedHttpRequest("/api/1/vehicles/${id}", "GET", { resp ->
        data = transformVehicleResponse(resp)
    })
    if (data.state == "online") {
       pause(2000)}
    else {
        wake(child)
        pause(30000)}
	return executeApiCommand(child, "window_control", body: [command: "vent"])
}

3. A nit-pic, but unlockandOpenChargePort results in a joined command because the A is not uppercase. I had updated my versions to simply openChargePort
image

Device

command "openChargePort"
def openChargePort() {
	log.debug "Executing 'unock and open charge port'"
    def result = parent.openChargePort(this)
}  

App

def openChargePort(child) {
    wake(child)
    pause(2000)
return executeApiCommand(child, "charge_port_door_open")
}

Not sure what u.mean but it.is two.commands.. the tesla command is.unlock and open

The Tesla command is charge_port_door_open. I’m just pointing out that the lowercase a in unlockandOpenChargePoint is causing the label on the command button to join Unlock and and… “Unlockand Open Charge Port” as shown in the image I inserted above.

@brad good catch on the Window attributes as I originally provided that section of the code to @kahn-hubitat back in Dec. 2020 and didn't do much with it outside of execute the "vent" command that was working at that time. @kahn-hubitat I changed the attributes to string on your latest version and have tested my car and windows open and close correctly so I think it is safe to make this update to the next version you publish on Github.

1 Like

I had the same experience took two tries to get the car to show-up. Everything after that appears to be working well.

re taking multiple attempts that is the way the apis work.. it is going to take some doing to change functions to attempt .. query and refresh and see if the command worked and not all commands can be done this way.. for now you will need to manually do a refresh to see if the command worked (that is the way this port has been for years)

eventually i will get around to trying to implement some querying and refreshing but for now that is the way it works..

as for the other couple of typos etc those have been fixed new driver(not app) is posted on github

1 Like

Thanks again!

windows always opened and closed correctly at least for me close etc work.. the error was just in using the attributes in rules for me. which i never do anyway. i have a dashboard with all the tesla options...

i dont normally write rules except to remind me if i forgot to plug in.. ie hence the name of this thread.

It has become so much more, hasn’t it… Can you rename it? :laughing: