Tesla API Access


#42

I try Sending external web request to: owner-api.teslamotors.com/api/1/vehicles/Id/command/set_temps?driverTemp=25


#43

You can’t send a command with a simple web request. You first need to ensure that you vehicle is awake and accepting commands, then a POST request with the parameter(s) exactly as written in the API and a proper authorization header are required.

I’m going to assume you have the first piston at the top of this thread working and correctly populating your global variables: @accessToken and @vehicleID. If not, STOP, and don’t proceed until you do as I won’t be able to help you.

Once you have that piston working correctly, this piston will set the driver_temp to 25C as requested.

As written, there is no trigger for the piston, but you can use the TEST button to execute it, call it from another piston, or add a trigger of your own.


#44

This is great work, thank you for doing this!

I am uncomfortable entering my username and password in a piston, so I decided to make a small tweak. Rather than having to enter username/password, I changed the first (authentication) piston to accept a refresh token instead.

To use it, create a global variable named @refreshToken, entering a valid refresh token in it. The updated code is below for anyone interested. It replaces the code that takes email and password (piston zb8d3, above). Then run it once (via the Test button). After that point, the other pistons, to access car data, change climate, etc., work with this new code just like it does with the old (email/password) code.

I also had a problem with the vehicle ID starting with null so I made a tweak to fix that. It should work fine as long as there is one or more vehicles on the account.


#45

Excellent!


#46

@bthrock this has stopped working for me this last week and i’ve no idea why. Is yours still working?

I’m getting the following when I run the token request piston for instance (i’ve changed the token btw)

07/09/2020, 12:11:25 +586ms
+0ms ╔Received event [Home].test = 1599477085585 with a delay of 0ms
+90ms ║RunTime Analysis CS > 20ms > PS > 43ms > PE > 27ms > CE
+92ms ║Runtime (50315 bytes) successfully initialized in 43ms (v0.3.110.20191009) (91ms)
+94ms ║╔Execution stage started
+100ms ║║Cancelling statement #2's schedules...
+131ms ║║Sending external web request to: owner-api.teslamotors.com/oauth/token/
+1046ms ║║Executed virtual command httpRequest (936ms)
+1057ms ║║Calculating (integer) 1 + (integer) 13 >> (integer) 14
+1061ms ║║Executed virtual command setVariable (1ms)
+1073ms ║║Calculating (integer) 84 + (integer) 11 >> (integer) 95
+1086ms ║║Calculating (integer) 107 + (integer) 11 >> (integer) 118
+1091ms ║║Calculating (decimal) 1.599477086E9 + (decimal) 3888000.0 >> (decimal) 1.603365086E9
+1095ms ║║Calculating (decimal) 1.603365086E9 * (decimal) 1000.0 >> (decimal) 1.603365086E12
+1098ms ║║Executed virtual command setVariable (1ms)
+1100ms ║║Cancelling statement #15's schedules...
+1111ms ║║Calculating (string) Bearer + (string) qts-7ba2640dfab4ede614daf71aa38aa6292c15bf4d5c63b6f0ef7f709f9ce9 >> (string) Bearer qts-7ba2640dfab4ede614daf71aa38aa6292c15bf4d5c63b6f0ef7f709f9ce9
+1114ms ║║Sending external web request to: owner-api.teslamotors.com/api/1/vehicles/
+1727ms ║║Error executing external web request: groovyx.net.http.ResponseParseException: Unauthorized
+1728ms ║║Executed virtual command httpRequest (615ms)
+1756ms ║╚Execution stage complete. (1663ms)
+1759ms ║Setting up scheduled job for Tue, Sep 8 2020 @ 12:00:00 AM BST (in 42512.656s)
+1767ms ╚Event processed successfully (1767ms)

#47

Hmm it’s really strange. I can get the API working in my API testing program, so it appears to be a part of the code - but it’s affecting all pistons I’ve built that use the Tesla API.

I just keep getting
Error executing external web request: groovyx.net.http.ResponseParseException: Unauthorized

I can generate a new token and then the second part of the token piston causes the same error. I can’t work out why - nothing appears to have changed in the API, and nothing has changed in my piston - is it just me having this?


#48

I’m seeing this error as well:

Error executing external web request: groovyx.net.http.ResponseParseException: Unauthorized

This, of course, prevents the Vehicle ID from updating properly.

I’ll have to take a closer look at this, although I seem to remember this happening elsewhere in the past.

Edit: Found it


#49

It then seems to happen in every command too - I can’t currently issue any commands to the car (even if I hard program the Vehicle ID as 1)

I don’t’ see a fix in that thread? Just talking about it…


#50

Just a thought, it might be that 2FA has been activated on the server? Although if that’s the case wouldn’t it have broken TeslaFi too?

It’s imminent but i’m not sure if it would affect us or not?


#53

Ah, it was the piston after all. It’s been so long since I wrote the original piston, I forgot how sloppily I originally parsed the tokens. Looks like Tesla made a minor change and killed it. This should fix it.


#54

Thanks! That sorted the token generation (although i’m getting a vehicle id of “null3691219499162923”) but no error when it’s generated.

All my buttons and actions are still broken though. What’s the changed I need to make here so they work?


#55

Ah ok, it was vehicle ID issue - it shouldn’t have “null” in front of it - any idea how to filter that out or why it’s happening?


#56

I’m not seeing the null issue here, but I have a guess as why it might be happening. Do you or have you had multiple cars in your account at any time?

Either way, it might be helpful if you logged the full $response after the GET request to TeslaApiURL and forwarded it to me by PM. For security, you can mask or replace any private data if you want to, just don’t change the structure of the JSON.


#57

Only ever one car. I’ve tried to adjust things in the expression but it always gives me a null followed the the number.

Let me try and get the full response now from Webcore


#58

I have never done anything with an API before but I have been trying to figure out a way to get Alexa to control my climate! Would I be able to use this piston to do that?


#59

You can control climate with the API, however, the pistons posted here aren’t currently working and I am currently attempting to determine the reason and come up with a solution, but I don’t have a time frame on that just yet.


#60

Hi @bthrock, I imported your code but nothing happens when I hit the test button. The variables don’t populate and the last execute time doesn’t change. Did I do something wrong? I imported the file three times and the only thing I changed was the username and password field.

Any assistance is appreciated.

Thanks in advance!


#61

Known issue, as yet unresolved. See Web Requests are not working, can't access API's etc?


#62

Thanks for letting me know @bthrock. I seriously thought I broke something :smiley: