alandtse / tesla

Tesla custom integration for Home Assistant. This requires a refresh token be generated by third-party apps to login.
Apache License 2.0
594 stars 100 forks source link

Powerwall gets http 410 response with null data and does not update due to API change #724

Closed Viss closed 1 year ago

Viss commented 1 year ago

Version of the custom_component

3.17.1 - current as of yesterday

Configuration

Describe the bug

I updated this integration yesterday, and noticed that the automations I have to switch my powerwalls configs around never fired, and upon investigation I found the integration had stopped working:

image

I am thinking that it just needs to have a token refresh happen, but it appears that I can't update the refresh token without completely removing and reconfiguring my powerwall. Is there a way to update the refresh token without tearing out the integration?

Debug log

2023-10-05 17:53:37.518 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/powerwalls/STE20201009-00492 {}
2023-10-05 17:53:37.780 DEBUG (MainThread) [teslajsonpy.connection] 410: {"response":null,"error":"https://powergate.prd.sn.tesla.services:443/api/powerwalls/1232100-00-E--TG1202500029KL/fullstatus => Gone","error_description":""}
2023-10-05 17:53:37.781 WARNING (MainThread) [teslajsonpy.controller] Unable to get battery data during setup, battery will still be added. 410: UNKNOWN_ERROR_410
2023-10-05 17:53:37.781 DEBUG (MainThread) [teslajsonpy.connection] Token expiration in 2:57:54
2023-10-05 17:53:37.782 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/powerwalls/STE20201009-00492/status {}
2023-10-05 17:53:37.898 DEBUG (MainThread) [teslajsonpy.connection] 410: {"response":null,"error":"https://powergate.prd.sn.tesla.services:443/api/powerwalls/1232100-00-E--TG1202500029KL/status => Gone","error_description":""}
2023-10-05 17:53:39.062 DEBUG (MainThread) [teslajsonpy.connection] Token expiration in 2:57:52
2023-10-05 17:53:39.062 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/powerwalls/STE20201009-00492/status {}
2023-10-05 17:53:39.157 DEBUG (MainThread) [teslajsonpy.connection] 410: {"response":null,"error":"https://powergate.prd.sn.tesla.services:443/api/powerwalls/1232100-00-E--TG1202500029KL/status => Gone","error_description":""}
2023-10-05 17:53:42.013 DEBUG (MainThread) [teslajsonpy.connection] Token expiration in 2:57:49
2023-10-05 17:53:42.015 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/powerwalls/STE20201009-00492/status {}
2023-10-05 17:53:42.120 DEBUG (MainThread) [teslajsonpy.connection] 410: {"response":null,"error":"https://powergate.prd.sn.tesla.services:443/api/powerwalls/1232100-00-E--TG1202500029KL/status => Gone","error_description":""}
2023-10-05 17:53:46.235 DEBUG (MainThread) [teslajsonpy.connection] Token expiration in 2:57:45
2023-10-05 17:53:46.236 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/powerwalls/STE20201009-00492/status {}
2023-10-05 17:53:46.328 DEBUG (MainThread) [teslajsonpy.connection] 410: {"response":null,"error":"https://powergate.prd.sn.tesla.services:443/api/powerwalls/1232100-00-E--TG1202500029KL/status => Gone","error_description":""}
2023-10-05 17:53:52.783 DEBUG (MainThread) [teslajsonpy.connection] Token expiration in 2:57:39
2023-10-05 17:53:52.784 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/powerwalls/STE20201009-00492/status {}
2023-10-05 17:53:53.071 DEBUG (MainThread) [teslajsonpy.connection] 410: {"response":null,"error":"https://powergate.prd.sn.tesla.services:443/api/powerwalls/1232100-00-E--TG1202500029KL/status => Gone","error_description":""}
purcell-lab commented 1 year ago

Does anyone know if the Grid Charging switch is available at all on the local Gateway API?

Unfortunately the localAPI no longer (over 12 months ago) allows changing modes with the exception of going off-grid, changing modes needs to occur through the cloudAPI.

Jonesnetwork commented 1 year ago

I’m just trying to decipher how the app communicates with Tesla and the powerwall at the minute. Although it looks like the app actually tunnels the traffic.

the only api call I’m seeing is to: owner-api.teslamotors.com

prob not helpful but I’ll trying to see if I can provide anything of use to help get this back up and running

Viss commented 1 year ago

ownerapi_endpoints.json.txt

A colleague on mastodon estracted all of the api calls from the app into a file and posted it for me!

davidb187 commented 1 year ago

I'm using this integration and the local Powerwall integration to manage charge and discharge inline with Octopus Agile half hourly pricing. The only Powerwall switch that's missing from the local integration that I need is the 'Grid Charging' switch. Without it I can't force charge by setting the the backup reserve to 100% and setting Grid Charge on.

Does anyone know if the Grid Charging switch is available at all on the local Gateway API?

I'm using this integration and the local Powerwall integration to manage charge and discharge inline with Octopus Agile half hourly pricing. The only Powerwall switch that's missing from the local integration that I need is the 'Grid Charging' switch. Without it I can't force charge by setting the the backup reserve to 100% and setting Grid Charge on.

Does anyone know if the Grid Charging switch is available at all on the local Gateway API?

I'm using this integration and the local Powerwall integration to manage charge and discharge inline with Octopus Agile half hourly pricing. The only Powerwall switch that's missing from the local integration that I need is the 'Grid Charging' switch. Without it I can't force charge by setting the the backup reserve to 100% and setting Grid Charge on.

Does anyone know if the Grid Charging switch is available at all on the local Gateway API?

I'm doing much the save with agile. Charging in cheapest slots if not enough predicted solar the next day... Are you saying you are currently able to change your back up reserve value from home assistant some how at the moment?

I just leave grid charging on all the time and change the back up reserve from 8% to 100% when I want to charge (well I did until this morning when the API died)

purcell-lab commented 1 year ago

A colleague on mastodon extracted all of the api calls from the app into a file and posted it for me!

Interestingly all the BATTERY endpoints are not there.

purcell-lab commented 1 year ago

If you have both vehicles and energy products this custom integration will not load.

You can restore partial functionality (vehicles only) if you delete the integration and then add excluding energy sites in the configuration dialog.

image

NickSills1 commented 1 year ago

I'm using this integration and the local Powerwall integration to manage charge and discharge inline with Octopus Agile I'm doing much the save with agile. Charging in cheapest slots if not enough predicted solar the next day... Are you saying you are currently able to change your back up reserve value from home assistant some how at the moment?

I just leave grid charging on all the time and change the back up reserve from 8% to 100% when I want to charge (well I did until this morning when the API died)

Appologies, my post was confusing, I just leave the reserve at 100%. The combination of on/off grid, and grid charging on/off enables forced charge/discharge and synchronisation with the Agile pricing. I've only just added solar so I'm still experimenting with the PW charge limit, but that can be resolved with the current charge sensor in an automation, it doesn't need the limit to be changed. If that makes sense.

Edit: To qualify what I mean. If current battery charge > x And Solar prediction is > y Then set Grid Charging to Off. That way, if the reserve is 100%, HA has total control over Solar and Agile and there's no setting in the Tesla App, other than the changing the time based setting ( though I'm not sure that's relevant ), that overrides.

BrettLynch123 commented 1 year ago

If you have both vehicles and energy products this custom integration will not load.

You can restore partial functionality (vehicles only) if you delete the integration and then add excluding energy sites in the configuration dialog.

image

Thank you. Didn't realize that really helpful and much appreciated.

peter-mc commented 1 year ago

I have the same problem too... following...

xcuHSCL

jaymunro commented 1 year ago

@Viss The comments about the API still working for car only, show that the issue is likely not to do with the host, but the API deprecating the /battery_data endpoint.

This is something that I brought up in June and created a branch to make a PR however I struck a snag and need help with it. If anyone can help with getting my system running with an updated teslajsonpy so I can test my changes, I may be able to help move this forward. https://github.com/zabuldon/teslajsonpy/issues/410#issuecomment-1751459486

coolscotty commented 1 year ago

wouldn't just using the "https://owner-api.teslamotors.com/api/1/energy_sites/" end points instead resolve the issue?

https://www.teslaapi.io/energy-sites/commands

I've fallen back to my old code with Node-Red and i cant still set the reserve via this end point, although Tesla had updated my site id for my powerwall, not sure when as i had not used this code for ages.

gkwok1 commented 1 year ago

Found this post on teslamotorsclub that might be helpful.

https://teslamotorsclub.com/tmc/threads/powerwall-2-using-grid-power-in-time-based-control-mode.298299/#post-7850813

This is the part that is interesting to me and maybe someone that knows more than me can make sense of it.

Just had to edit my scripts since the Tesla API changed last night and api/1/powerwalls/ endpoints no longer work, now it is set as default_real_mode and backup_reserve_percent in energy_sites data.

gkwok1 commented 1 year ago

@Viss The comments about the API still working for car only, show that the issue is likely not to do with the host, but the API deprecating the /battery_data endpoint.

This is something that I brought up in June and created a branch to make a PR however I struck a snag and need help with it. If anyone can help with getting my system running with an updated teslajsonpy so I can test my changes, I may be able to help move this forward. zabuldon/teslajsonpy#410 (comment)

I actually used your code so I could pull operations mode on start up by modifying the code directly in /usr/local/lib/python3.11/site-packages/teslajsonpy and restarted HA.

Here is the code I used to get to the folder. docker exec -it homeassistant bash cd /usr/local/lib/python3.11/site-packages/teslajsonpy

Alternatively, you can also copy the teslajsonpy folder to /config and make your modifications there.

Viss commented 1 year ago

@Viss The comments about the API still working for car only, show that the issue is likely not to do with the host, but the API deprecating the /battery_data endpoint.

This is something that I brought up in June and created a branch to make a PR however I struck a snag and need help with it. If anyone can help with getting my system running with an updated teslajsonpy so I can test my changes, I may be able to help move this forward. zabuldon/teslajsonpy#410 (comment)

image

sure looks like they removed the subdomain to me

alexguichet commented 1 year ago
image

sure looks like they removed the subdomain to me

You are on the wrong track by poking at that url, keep using owner-api. /powerwalls routes were disused by Tesla several years ago, and the /energy_sites routes are what the app has been using for quite a long time. My previous comment:

The /powerwalls routes are no longer available, but /energy_sites routes should still be. Integrations should probably move to that!

alexguichet commented 1 year ago

wouldn't just using the "https://owner-api.teslamotors.com/api/1/energy_sites/" end points instead resolve the issue?

https://www.teslaapi.io/energy-sites/commands

I've fallen back to my old code with Node-Red and i cant still set the reserve via this end point, although Tesla had updated my site id for my powerwall, not sure when as i had not used this code for ages.

This is the way

Jonesnetwork commented 1 year ago

wouldn't just using the "https://owner-api.teslamotors.com/api/1/energy_sites/" end points instead resolve the issue? https://www.teslaapi.io/energy-sites/commands I've fallen back to my old code with Node-Red and i cant still set the reserve via this end point, although Tesla had updated my site id for my powerwall, not sure when as i had not used this code for ages.

This is the way

So i have just updated the endpoint - however still have the same issue :(

''' 2023-10-07 07:53:17.462 WARNING (MainThread) [teslajsonpy.controller] Unable to get battery data during setup, battery will still be added. 404: NOT_FOUND 2023-10-07 07:53:17.463 DEBUG (MainThread) [teslajsonpy.connection] Token expiration in 5:43:54 2023-10-07 07:53:17.464 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/energy_sites/STE20220921-00213/status {} 2023-10-07 07:53:17.536 DEBUG (MainThread) [teslajsonpy.connection] 404: <!DOCTYPE html> '''

coolscotty commented 1 year ago

get: https://owner-api.teslamotors.com/api/1/energy_sites/STE20220921-00213/status {}

the siteid is wrong for the powerwall. you need to use the one listed in https://owner-api.teslamotors.com/api/1/products

instead of STE20220921-00213 use the energy_site_id from the above URL

Jonesnetwork commented 1 year ago

get: https://owner-api.teslamotors.com/api/1/energy_sites/STE20220921-00213/status {}

the siteid is wrong for the powerwall. you need to use the one listed in https://owner-api.teslamotors.com/api/1/products

instead of STE20220921-00213 use the energy_site_id from the above URL

Ill have another go - ive manged to replace the endpoints, however its pulling the STE from else where - slightly above me this one but ill continue to help try and figure it out.

coolscotty commented 1 year ago

@Jonesnetwork if its referencing the STE than its most likely coming from the same link that i said but its using warp_site_number instead. its all above my head how it works aswell.

i can confirm that using the /energy_sites points, i can change my backup reserve that i use in my node red scripts.

Jonesnetwork commented 1 year ago

@Jonesnetwork if its referencing the STE than its most likely coming from the same link that i said but its using warp_site_number instead. its all above my head how it works aswell.

i can confirm that using the /energy_sites points, i can change my backup reserve that i use in my node red scripts.

Thanks - ive got a little further - but its still pulling in "powerwalls"

Would you be able to share the API commands your pulling in nodered please? i can see if i can match up whats missing.

alandtse commented 1 year ago

This file likely needs to be updated. Other implementations may have a newer one and maybe it's in the APK. Any PR for telsajsonpy will need to update that and also use the appropriate command and not try to access raw urls.

EDIT: In fact, https://github.com/timdorr/tesla-api/blob/master/ownerapi_endpoints.json

coolscotty commented 1 year ago

https://owner-api.teslamotors.com/api/1/energy_sites/:site_id/site_status

response: object resource_type: "battery" site_name: "xxx" gateway_id: "xxx" energy_left: 6718.526315789474 total_pack_energy: 13108 percentage_charged: 51.25515956507075 battery_type: "ac_powerwall" backup_capable: true battery_power: 1030 go_off_grid_test_banner_enabled: null storm_mode_enabled: false powerwall_onboarding_settings_set: true powerwall_tesla_electric_interested_in: null sync_grid_alert_enabled: true breaker_alert_enabled: true

https://owner-api.teslamotors.com/api/1/energy_sites/:site_id/live_status

solar_power: 244.8699951171875 energy_left: 6718.526315789474 total_pack_energy: 13108 percentage_charged: 51.25515956507075 backup_capable: true battery_power: 940 load_power: 1184.8699951171875 grid_status: "Active" grid_services_active: false grid_power: 0 grid_services_power: 0 generator_power: 0 island_status: "on_grid" storm_mode_active: false timestamp: "2023-10-07T18:38:49+11:00" wall_connectors: array[0]

the complete list is here https://www.teslaapi.io/energy-sites/state-and-settings and https://www.teslaapi.io/energy-sites/commands

for the post commands, i assume they are the same as the old /battery end points

Jonesnetwork commented 1 year ago

updated

Yeah - this is the file im currently working on at the minute - even after updating anything "powerwalls/" it still appears to be using the powerwalls url.

Its a learning curve either way lol and im sure someone will beat me to it :)

coolscotty commented 1 year ago

above my skills but maybe @purcell-lab could look at this

Jonesnetwork commented 1 year ago

above my skills but maybe @purcell-lab could look at this

Iv e matched the API's from both endpoints and it does appear to match - although not making much of a difference

So im wondering if im putting the telsajson file in the correct place?

alandtse commented 1 year ago

Probably needs to just update get_site_config and get_site_data to use the new endpoints.

https://github.com/zabuldon/teslajsonpy/blob/master/teslajsonpy/controller.py#L406-L413

EDIT: Or it's get_battery_data and get_battery_summary. Have no idea how powerwalls work. https://github.com/zabuldon/teslajsonpy/blob/master/teslajsonpy/controller.py#L435-L446

bmsrecycle commented 1 year ago

Would it be possible to make the Backup Reserve a figure which could be adjusted rather than just a sensor reading, under the Tesla Powerwall integration:

Intergration Untitled

gkwok1 commented 1 year ago

Probably needs to just update get_site_config and get_site_data to use the new endpoints.

https://github.com/zabuldon/teslajsonpy/blob/master/teslajsonpy/controller.py#L406-L413

EDIT: Or it's get_battery_data and get_battery_summary. Have no idea how powerwalls work. https://github.com/zabuldon/teslajsonpy/blob/master/teslajsonpy/controller.py#L435-L446

looks like power_reading is removed and everything inside it moved up to the root response in /api/1/energy_sites/{site_id}/live_status is already mapped to "SITE_DATA" in the endpoints file.

https://tesla-api.timdorr.com/energy-products/energy

mcdhome commented 1 year ago

I have same issue, and I have 3 energy installs, and 2 tesla cars. What I did notice is the last solar+battery installation last year now appears in my Tesla account when it never did as it's own entity before (I only saw the 3 Tesla Solar installs combined into 1 entity). So Tesla has been making changes for sure, and it happened at the same time we lost api access.

d0dge commented 1 year ago

Oh man, this is bad news!

I noticed my integration wasn't capturing the energy usage figures, so I reloaded and now of course I've lost the control services.

My main automation figures out how much to charge the Powerwall overnight from the grid based on the upcoming day's solar forecast. It was pretty much the main reason I started using HA.

Keeping fingers crossed for a solution!

tannerwastaken commented 1 year ago

Oh man, this is bad news!

I noticed my integration wasn't capturing the energy usage figures, so I reloaded and now of course I've lost the control services.

My main automation figures out how much to charge the Powerwall overnight from the grid based on the upcoming day's solar forecast. It was pretty much the main reason I started using HA.

Keeping fingers crossed for a solution!

I’m there with you… except I use mine to determine energy production surplus and use it to charge my car. Kind of like Tesla’s drive on solar feature, only much more dynamic/accurate.

AndrewTapp commented 1 year ago

Hiya

Have reloaded this integration within HomeAssistant with the code changes from an hour or so ago and I'm still getting a "Failed to set up" error.

Digging deeper I get these messages;

image image

...line 272 seems to point to this image

Regards.

Andrew

rm-rf-asterisk commented 1 year ago

My buddy is a solar installer and does Tesla jobs. Would there be any use for me to bug him if they get access to some new app etc? I would need to know the question to ask.

alexguichet commented 1 year ago

My buddy is a solar installer and does Tesla jobs. Would there be any use for me to bug him if they get access to some new app etc? I would need to know the question to ask.

No.

gkwok1 commented 1 year ago

I think I got everything working again by migrating all battery_data and battery_summary to site_config, site_summary, and site data.

Here is my fork and please let me know if something is not working. This is my first attempt at python.

https://github.com/gkwok1/teslajsonpy

image

Alona23 commented 1 year ago

Would a custom repository be coming to update the current one without losing all automations?

Al

Sent from Outlook for Androidhttps://aka.ms/AAb9ysg


From: gkwok1 @.> Sent: Saturday, October 7, 2023 9:10:26 PM To: alandtse/tesla @.> Cc: Alona23 @.>; Comment @.> Subject: Re: [alandtse/tesla] Powerwall gets http 410 response with null data and does not update due to API change (Issue #724)

I think I got everything working again by migrating all battery_data and battery_summary to site_config, site_summary, and site data.

Here is my fork and please let me know if something is not working. This is my first attempt at python.

https://github.com/gkwok1/teslajsonpy

[image]https://user-images.githubusercontent.com/51801924/273407086-43c4100f-6534-46de-a589-c1cbe99e0cdf.png

— Reply to this email directly, view it on GitHubhttps://github.com/alandtse/tesla/issues/724#issuecomment-1751808022, or unsubscribehttps://github.com/notifications/unsubscribe-auth/A6BA7KKGPXTG7CP7WJQGMILX6GZLFAVCNFSM6AAAAAA5VCLEBCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONJRHAYDQMBSGI. You are receiving this because you commented.Message ID: @.***>

Jonesnetwork commented 1 year ago

I think I got everything working again by migrating all battery_data and battery_summary to site_config, site_summary, and site data.

Here is my fork and please let me know if something is not working. This is my first attempt at python.

https://github.com/gkwok1/teslajsonpy

image

Thanks gkwok1

Im just going to give this a try now, are you putting the teslajsonapi folder in custom config or at the root of config?

thanks

gkwok1 commented 1 year ago

Im just going to give this a try now, are you putting the teslajsonapi folder in custom config or at the root of config?

thanks

drop the teslajsonpy folder in /config and restart HA.

image

gkwok1 commented 1 year ago

Would a custom repository be coming to update the current one without losing all automations? Al Sent from Outlook for Androidhttps://aka.ms/AAb9ysg

I have no plans to create a custom repository for this and hoping someone with more knowledge can review and merge this once it's has been tested. I am not sure what I broke if anything.

Alona23 commented 1 year ago

Still trying to get my head round coding and where to paste an drop😵‍💫

Al

Sent from Outlook for Androidhttps://aka.ms/AAb9ysg


From: gkwok1 @.> Sent: Saturday, October 7, 2023 9:36:46 PM To: alandtse/tesla @.> Cc: Alona23 @.>; Comment @.> Subject: Re: [alandtse/tesla] Powerwall gets http 410 response with null data and does not update due to API change (Issue #724)

Would a custom repository be coming to update the current one without losing all automations? Al Sent from Outlook for Androidhttps://aka.ms/AAb9ysg …

I have no plans to create a custom repository for this and hoping someone with more knowledge can review and merge this once it's has been tested. I am not sure what I broke if anything.

— Reply to this email directly, view it on GitHubhttps://github.com/alandtse/tesla/issues/724#issuecomment-1751812843, or unsubscribehttps://github.com/notifications/unsubscribe-auth/A6BA7KKCHLESYC4IK6RHDKTX6G4N5AVCNFSM6AAAAAA5VCLEBCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONJRHAYTEOBUGM. You are receiving this because you commented.Message ID: @.***>

Jonesnetwork commented 1 year ago

Im just going to give this a try now, are you putting the teslajsonapi folder in custom config or at the root of config? thanks

drop the teslajsonpy folder in /config and restart HA.

image

Amazing work :) - thats now working for me and im back in business

gkwok1 commented 1 year ago

Still trying to get my head round coding and where to paste an drop😵‍💫

1) Download this file: https://github.com/gkwok1/teslajsonpy/archive/refs/heads/dev.zip 2) Extract teslajsonpy folder in zip to /config in HA 3) Restart HA

JohnKG72 commented 1 year ago

I just extracted the teslajsonpy folder and created a new folder in root config/ called teslajsonpy and copied the files into it.

Is this correct?

Screenshot 2023-10-07 at 21 56 04
gkwok1 commented 1 year ago

I just extracted the teslajsonpy folder and created a new folder in root config/ called teslajsonpy and copied the files into it.

Is this correct? Screenshot 2023-10-07 at 21 56 04

That looks correct to me.

Viss commented 1 year ago

I downloaded the zip, extracted it - it created a teslajsonpy-dev folder. I renamed it to teslajsonpy, and rebooted HA, but the integration didn't come back to life. are there other code changes required, to point to the new location of teslajsonpy?

gkwok1 commented 1 year ago

@Alona23 @Viss @Jonesnetwork @JohnKG72

I just fixed a sync bug, you will have to re-pull and restart HA for all battery items to sync.

@Viss there is a teslajsonpy inside the teslajsonpy-dev folder. Copy the teslajsonpy folder to /config and restart HA.

JohnKG72 commented 1 year ago

Is that an update to the code so delete those files and do the same process? Thanks.

gkwok1 commented 1 year ago

Is that an update to the code so delete those files and do the same process? Thanks.

Only the controller.py file was updated so you can only upload that file or redo the entire process.

JohnKG72 commented 1 year ago

Is that an update to the code so delete those files and do the same process? Thanks.

Only the controller.py file was updated so you can only upload that file or redo the entire process.

Brilliant. Thanks.