Tesla API Access


#1

The following provides an example of how to use webCoRE to access the Tesla API.

The first piston (Updated 3/3/21 for compatibility with Tesla API v3) uses a Refresh Token that you provide to obtain and refresh (as needed) an access token used by the two example pistons that follow to access the API.

To use this piston, you will first have to obtain a Refresh Token from Tesla. I used a python script available here . I 'm sure there are other options I’m not familiar with. .

This piston, while functional, employs a somewhat clumsy workaround for a webCoRE bug that creates problems for pistons containing multiple POST statements, so a new global variable (type: Boolean) @tokenSwitch is needed.

See the instructions within the piston itself to get started.

If you have multiple vehicles, some modification of the first piston will be required to ensure the proper Vehicle IDs are stored. I’ll try to address that in the next iteration.

Please note: if you are using TeslaFi or another third-party service that maintains the access token, do not use this piston. You will have to manually update the value of @accessToken each time those services refresh your access token. (That should occur approximately every 45 days).

The second piston will run successfully via the TEST button ONLY AFTER the first piston has been run successfully and your credentials have been stored in the global variables you created. The piston provides examples of the methodology for accessing the API and parsing the responses.

Whatever trigger you use to activate the pistons you develop from this information, remember that waking the car uses the battery. The usage is small, but waking the car too often could impact your range.

Here’s a simple climate control piston. (Again, click TEST to execute)

Of course these are only examples. More data and more commands are available for those interested in developing their own pistons. For more information on the Tesla API see: https://www.teslaapi.io/ or https://tesla-api.timdorr.com/

Let me know if you find this useful. Good luck!

Edited 12/14/18: Updated original text and piston to focus exclusively on how to access the API and the proper method of parsing the JSON response.

Edited 4/18/20: Updated example pistons to work with multiple vehicles on a single Tesla account.

Edited 4/21/20: Added example of a simple climate control piston.

Edited 3/3/21: Updated First Piston for Compatibility with Tesla API v3 Authentication

Edited 3/4/21: Tweaked First Piston


Tesla Powerwall Logging
#2

@bthrock you are killing me with this piston :joy::grin::grin:

I had two dreams,
Create a smart home and buy a Tesla.
I had forgotten about the Tesla:)

“Honey, you know how we talked about kids are going to the best colleges…let’s talk again:” :joy::joy::joy:

IF time changes to after {highschool}
and
IF kids grades are > good
and
IF We have plenty of money
Than
Send them to great colleges

“I don’t think that our piston will trigger:))))”


#3

Does this use the same API that the Powerwall systems use?


#4

Yes, you would just need to modify the URLs for the specific states, settings, and commands.


#5

Good, I can stop messing with this GoControl contact switch for a mains disconnect sensor then…


#6

I don’t have a powerwall, but I’d be happy to try to answer any questions if something’s not clear.


#7

Thanks. I want to be able to detect when we are pulling from the powerwall exclusively for night time use and when we are pulling from the powerwall due to a power outage so I can put the house into different levels of some sort of high-efficiency mode. The solar hub is a zigbee device but I have no way of knowing how to gather what I need from it for a device handler.


#8

Yeah, I’m not sure what data is available from the API but I would try logging info from the HTTP requests shown here https://www.teslaapi.io/powerwalls/state-and-settings and see if it has what you need.

To be clear, I know this API incorporates Powerwall data, but I don’t know if it’s somehow limited to Powerwalls used in conjunction with Tesla vehicles. If there’s a separate and unique Powerwall API available for Powerwall-only owners, I’m not familiar with it.


#9

Perfect. Disappointing the only way to access responses is via parsing the text.


#10

Parsing a response from at HTTP request is standard with almost any API. And in webCoRE it’s very easy to do.


#11

I see that now in your second piston. Thanks. Got it all working but there is not much I can really do with it until Tesla fixes a communication issue between one of my controllers and the hub.


#12

Building on bthrock’s good work, I have written a piston to query and log the Powerwall’s data


#14

Having a pop at this but i’m getting the following error messages when I click “Test” any ideas?

Error executing virtual command [].setVariable: (5ms) groovy.lang.MissingPropertyException:     No such property: result for class: script_app_6c4a97a42cd9c01ed79ae72c875a75bf6c50e82c665272109ecaf6627b713546
+1156ms	║Error executing virtual command [].setVariable: (1ms) groovy.lang.MissingPropertyException: No such property: result for class: script_app_6c4a97a42cd9c01ed79ae72c875a75bf6c50e82c665272109ecaf6627b713546
+1170ms	║An error occurred while executing the event: java.lang.NullPointerException: Cannot get property 'v' on null object

#15

@bthrock Any ideas bud as to why i’m getting that error message and not generating the tokens?


#16

Sorry, I’ve been out of the country for two weeks and away from any cell or wi-fi service for most of that time. I’ll try to take a look in a day or two, but I may need to see a full log along with green snapshot with trace enabled to identify the problem.


#17

I am assuming that you have not made any modifications to the piston other than to enter the values for your email and password (lines 22 and 23 in the piston as posted above). Those are the only changes necessary. If you have entered them correctly, the piston should work. If not, you should be getting an ‘unauthorized error.’

The MissingPropertyException suggests that something else has changed. You could simply reimport the piston and try again. Alternately, post a GREEN snapshot (blurring your email & password, of course) and a full log, and we should be able to get this working. :smiley:


#18

Hi!

I did try reimporting again just in case - I get this menu when first importing which i’ve just clicked ignore on.

Here is my green button output

And here is the full log

|+1ms|╔Received event [Home].test = 1575485994931 with a delay of 0ms|
| --- | --- |
|+135ms|║RunTime Analysis CS > 20ms > PS > 94ms > PE > 21ms > CE|
|+138ms|║Runtime (50659 bytes) successfully initialized in 94ms (v0.3.110.20191009) (137ms)|
|+139ms|║╔Execution stage started|
|+144ms|║║Cancelling statement #2's schedules...|
|+173ms|║║Sending external web request to: owner-api.teslamotors.com/oauth/token/|
|+1092ms|║║Executed virtual command httpRequest (938ms)|
|+1102ms|║║Calculating (integer) 1 + (integer) 13 >> (integer) 14|
|+1107ms|║║Error executing virtual command [].setVariable: (1ms) groovy.lang.MissingPropertyException: No such property: result for class: script_app_6c4a97a42cd9c01ed79ae72c875a75bf6c50e82c665272109ecaf6627b713546|
|+1119ms|║║Calculating (integer) 80 + (integer) 11 >> (integer) 91|
|+1130ms|║║Calculating (integer) 103 + (integer) 11 >> (integer) 114|
|+1134ms|║║Calculating (decimal) 1.575485995E9 + (decimal) 3888000.0 >> (decimal) 1.579373995E9|
|+1137ms|║║Calculating (decimal) 1.579373995E9 * (decimal) 1000.0 >> (decimal) 1.579373995E12|
|+1140ms|║║Error executing virtual command [].setVariable: (1ms) groovy.lang.MissingPropertyException: No such property: result for class: script_app_6c4a97a42cd9c01ed79ae72c875a75bf6c50e82c665272109ecaf6627b713546|
|+1144ms|║║Cancelling statement #15's schedules...|
|+1153ms|║║An error occurred while executing the event: java.lang.NullPointerException: Cannot get property 'v' on null object|
|+1170ms|║╚Execution stage complete. (1031ms)|
|+1180ms|║Setting up scheduled job for Thu, Dec 5 2019 @ 12:00:00 AM GMT (in 18003.889s)|
|+1190ms|╚Event processed successfully (1190ms)|
|04/12/2019, 18:59:42 +311ms|
|+715ms|║An error has occurred while subscribing: physicalgraph.exception.StateCharacterLimitExceededException: State cannot be greater than 100000.0 characters|

#19

Please use the upload button in the message editor to insert your images in place of the dropbox links.


#20

For reference:

pic


#21

I did try but unfortunately it wasn’t working in my browser