Tesla Powerwall 2 Integration

Maybe you could add this to your tutorial:

Here you can get the tokens - without app.

it is working now for me.
the ios app that i had did not give me the access token but only refresh so it expired after 1 day.

The SOC Problem bothers me quite a lot.
i don’t know where to search for errors.
Maybe there are some differences in the Software between Models?
I use a PW2 and this information is given to me:

NAME DES STANDORTS

Zuhause

BACKUP-MÖGLICHKEIT

Backup möglich

MODUS

Eigenverbrauch

NOTSTROMRESERVE

5%

NETZ-STANDARD

50Hz_230V_1_VDE4105_2018_Germany

ZEITZONE

Europe/Berlin

KUNDENVERSION

22.1.1 b0ec2432

NETZWERK

ETHERNET, MOBILFUNK

POWERWALL

Teil 1092170-03-J

Perhaps enable debug logging, and then we will be able to see the responses from the Tesla API, and that will also tell us your site number so you can check values with curl.

Agree this is useful, however, in my testing, these access tokens are only lasting 8 hours :frowning:

I’m going to need to try and find a better solution.

OK I think I know what’s needed. Tesla have made a chance and access tokens which used to last for 45 days, now only last 8 hours. Refresh tokens still ‘appear’ to last for 45 days, so I think I need to:

  • Update the configuration to ask for a Refresh token, not an access token
  • Update the code, so that it converts the refresh token into an access token.
  • Update the remaining code to use this generated access token (rather than one from the configuration)
  • add code that checks for {“error”:“invalid bearer token”}, in which case we generate a new access token using the refresh token.

This shouldn’t be too hard, I just need a couple of clear hours. I need to fix this in the next 14 days as that’s when my current access_token will expire :slight_smile:

Edit: I something testing here, I need to wait 8 hours until the initial access token expires to know if it’s refreshing correctly.

Curious if you have considered merging your cloud code into the tesla binding that already exists but is only handling cars. Doing so would simplify maintenance when tesla changes the API on us and just add a simplicity of use as a whole - only one binding needed for cloud tesla. I def hope you keep the local powerwall stuff going long term though as I prefer that for my read only view as it is much less likely to be broken by updates. I don’t mind that one being independent though I suppose it could also be rolled into the other binding eventually perhaps

Hey Jared! Long time no see! I agree a single Tesla binding for Energy and Cars might make sense. In fact, when I first tried to get the PW2 supported, I started last year by modifying the Tesla binding - but then I left it a while as it wasn’t complete, meanwhile the code had marched on, and re-merging it was a nightmare.

Having everything in my own binding makes things easier for me to maintain - I don’t really consider myself a programmer - I just hack my way around until things work :stuck_out_tongue:

I can’t see me ever merging the local and cloud bindings - the real advantage of the cloud binding is being able to change reserve and mode controls.

I think things are working now - I’ll let it run overnight then post an updated cloud build.

1 Like

I am curious :slight_smile:
I love this and the only issue i have now is getting an api key that does not expire …

OK, you twisted my arm! https://smedley.id.au/tmp/org.openhab.binding.teslapowerwallcloud-3.3.0-SNAPSHOT.jar

Note: you may need to delete your ‘Thing’ and re-add it as the configuration has changed. You now need to supply an ‘SSO Refresh’ token not an ‘Access Token’

I’m not sure exactly how long these SSO Refresh tokens last - I believe it’s still ~45 days.

The code still needs cleaning up/committing to github. Currently, it renews the token every 7.5 hours.

Edit: 24 hours later and it seems to be working as intended. Now installed on my production system, and I’m happy to say that replacing the jar then going to the ‘Thing’ allowed me to add the SSO Refresh token without deleting the thing.

1 Like

Hi,

I’m just testing the local powerwall binding

https://github.com/psmedley/openhab-addons/releases/download/teslapowerwall/org.openhab.binding.teslapowerwall-3.3.0-SNAPSHOT.jar

First I found that if the password is wrong, the binding throws an error “NullPointerException”:

2022-04-08 14:55:19.035 [DEBUG] [ll.internal.TeslaPowerwallWebTargets] - java.lang.InterruptedException
java.io.IOException: java.lang.InterruptedException
at org.openhab.core.io.net.http.HttpUtil.executeUrlAndGetReponse(HttpUtil.java:260) ~[?:?]
at org.openhab.core.io.net.http.HttpUtil.executeUrl(HttpUtil.java:155) ~[?:?]
at org.openhab.core.io.net.http.HttpUtil.executeUrl(HttpUtil.java:130) ~[?:?]
at org.openhab.binding.teslapowerwall.internal.TeslaPowerwallWebTargets.invoke2(TeslaPowerwallWebTargets.java:202) ~[?:?]
at org.openhab.binding.teslapowerwall.internal.TeslaPowerwallWebTargets.getToken(TeslaPowerwallWebTargets.java:137) ~[?:?]
at org.openhab.binding.teslapowerwall.internal.TeslaPowerwallHandler.pollStatus(TeslaPowerwallHandler.java:118) ~[?:?]
at org.openhab.binding.teslapowerwall.internal.TeslaPowerwallHandler.poll(TeslaPowerwallHandler.java:96) ~[?:?]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) [?:?]
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:305) [?:?]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:305) [?:?]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) [?:?]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) [?:?]
at java.lang.Thread.run(Thread.java:834) [?:?]
Caused by: java.lang.InterruptedException
at java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireSharedInterruptibly(AbstractQueuedSynchronizer.java:1040) ~[?:?]
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireSharedInterruptibly(AbstractQueuedSynchronizer.java:1345) ~[?:?]
at java.util.concurrent.CountDownLatch.await(CountDownLatch.java:232) ~[?:?]
at org.eclipse.jetty.client.util.FutureResponseListener.get(FutureResponseListener.java:100) ~[?:?]
at org.eclipse.jetty.client.HttpRequest.send(HttpRequest.java:730) ~[?:?]
at org.openhab.core.io.net.http.HttpUtil.executeUrlAndGetReponse(HttpUtil.java:251) ~[?:?]
… 12 more
2022-04-08 14:55:19.055 [DEBUG] [rwall.internal.TeslaPowerwallHandler] - Could not connect to Tesla Powerwall
org.openhab.binding.teslapowerwall.internal.TeslaPowerwallCommunicationException: Tesla Powerwall returned error while invoking https://192.168.1.22/api/login/Basic
at org.openhab.binding.teslapowerwall.internal.TeslaPowerwallWebTargets.invoke2(TeslaPowerwallWebTargets.java:212) ~[?:?]
at org.openhab.binding.teslapowerwall.internal.TeslaPowerwallWebTargets.getToken(TeslaPowerwallWebTargets.java:137) ~[?:?]
at org.openhab.binding.teslapowerwall.internal.TeslaPowerwallHandler.pollStatus(TeslaPowerwallHandler.java:118) ~[?:?]
at org.openhab.binding.teslapowerwall.internal.TeslaPowerwallHandler.poll(TeslaPowerwallHandler.java:96) ~[?:?]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) [?:?]
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:305) [?:?]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:305) [?:?]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) [?:?]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) [?:?]
at java.lang.Thread.run(Thread.java:834) [?:?]
2022-04-08 14:55:19.061 [WARN ] [.core.thing.binding.BaseThingHandler] - Handler TeslaPowerwallHandler tried updating the thing status although the handler was already disposed.
2022-04-08 14:55:22.135 [DEBUG] [rwall.internal.TeslaPowerwallHandler] - config.hostname = 192.168.1.22, refresh = 30
2022-04-08 14:55:22.136 [DEBUG] [rwall.internal.TeslaPowerwallHandler] - Scheduling poll for 1 second out, then every 30 s
2022-04-08 14:55:23.137 [DEBUG] [rwall.internal.TeslaPowerwallHandler] - Polling for state
2022-04-08 14:55:23.139 [DEBUG] [ll.internal.TeslaPowerwallWebTargets] - logonjson = {“username":“customer”,“password”:“mypassword”,“email”:"me@nomail.com”,“force_sm_off”:false}
2022-04-08 14:55:23.141 [DEBUG] [ll.internal.TeslaPowerwallWebTargets] - Calling url: https://192.168.1.22/api/login/Basic
2022-04-08 14:55:23.265 [WARN ] [rwall.internal.TeslaPowerwallHandler] - Unexpected error connecting to Tesla Powerwall
java.lang.NullPointerException: null
at org.openhab.binding.teslapowerwall.internal.TeslaPowerwallWebTargets.getToken(TeslaPowerwallWebTargets.java:139) ~[?:?]
at org.openhab.binding.teslapowerwall.internal.TeslaPowerwallHandler.pollStatus(TeslaPowerwallHandler.java:118) ~[?:?]
at org.openhab.binding.teslapowerwall.internal.TeslaPowerwallHandler.poll(TeslaPowerwallHandler.java:96) ~[?:?]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) [?:?]
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:305) [?:?]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:305) [?:?]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) [?:?]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) [?:?]
at java.lang.Thread.run(Thread.java:834) [?:?]

After using the correct password, the binding initializes without errors.

Second I’ve seen that the binding uses the word “Instanteous”, but I think the correct word should be “Instantaneous”. Of course this is only a bagatelle.

Thank you for this binding, it helps me a lot!

Regards, Joerg

1 Like

Hi JoergDr,

“Instanteous” is definitely a typo - blame my phat fingers :slight_smile: I vaguely recall fixing it already in git.

When I get some time, I’ll see if I can handle incorrect passwords more gracefully :slight_smile:

Cheers,

Paul

I know that naughty behavior very well with my fingers… :slight_smile:

Please let me know if there is a new version available (though it is not urgent).

Greetings,
Joerg

https://smedley.id.au/tmp/org.openhab.binding.teslapowerwall-3.3.0-SNAPSHOT.jar fixes the typos

Grabbed it. Thank you!

1 Like

I am getting quite some errors in my logs regarding the PW2 binding.
Is it more robust when using the offline version?

Regards

Jonas

FYI:

2022-05-09 10:26:13.593 [WARN ] [.internal.TeslaPowerwallCloudHandler] - Unexpected error connecting to Tesla API
com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 11 path $
	at com.google.gson.JsonParser.parseReader(JsonParser.java:66) ~[?:?]
	at com.google.gson.JsonParser.parseString(JsonParser.java:47) ~[?:?]
	at org.openhab.binding.teslapowerwallcloud.internal.api.SiteInfo.parse(SiteInfo.java:42) ~[?:?]
	at org.openhab.binding.teslapowerwallcloud.internal.TeslaPowerwallCloudWebTargets.getSiteInfo(TeslaPowerwallCloudWebTargets.java:95) ~[?:?]
	at org.openhab.binding.teslapowerwallcloud.internal.TeslaPowerwallCloudHandler.pollStatus(TeslaPowerwallCloudHandler.java:183) ~[?:?]
	at org.openhab.binding.teslapowerwallcloud.internal.TeslaPowerwallCloudHandler.poll(TeslaPowerwallCloudHandler.java:154) ~[?:?]
	at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:?]
	at java.util.concurrent.FutureTask.runAndReset(Unknown Source) [?:?]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source) [?:?]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:?]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [?:?]
	at java.lang.Thread.run(Unknown Source) [?:?]
Caused by: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 11 path $
	at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1564) ~[?:?]
	at com.google.gson.stream.JsonReader.checkLenient(JsonReader.java:1405) ~[?:?]
	at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:543) ~[?:?]
	at com.google.gson.stream.JsonReader.peek(JsonReader.java:426) ~[?:?]
	at com.google.gson.JsonParser.parseReader(JsonParser.java:61) ~[?:?]
	... 11 more
==> /var/log/openhab/events.log <==
2022-05-09 10:26:13.617 [INFO ] [ab.event.ThingStatusInfoChangedEvent] - Thing 'teslapowerwallcloud:powerwallcloud:9ae60c1884' changed from ONLINE to OFFLINE (COMMUNICATION_ERROR): com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 11 path $

Yes - same experience here. To control the charging of my EV I use the other binding
org.openhab.binding.teslapowerwall-3.3.0-SNAPSHOT.jar
which is more reliable / stable.

The Cloud binding offers some tweaking of charging modes. These cannot be used in germany, Tesla switches changes to these back right away.

1 Like

The offline binding that has been mentioned here, works fine for me:

Offline Binding works fine for me too.
Will this be integrated in the next openhab release?
Still getting some null pointer exceptions from time to time.

I’d be interested to see the exceptions, and I can try and address them.

In terms of getting it into the openhab release, that will need me to dedicate some time to cleaning up the code, and getting it into a mergeable state - something I can’t commit to in the near term.

Apologies, I missed this post. Do those exceptions happen when you’re performing a particular operation, or randomly?