Welcome to Tesla Motors Club
Discuss Tesla's Model S, Model 3, Model X, Model Y, Cybertruck, Roadster and More.
Register

Getting daily production from Tesla Gateway API

This site may earn commission on affiliate links.
Yeah this changed a few months ago and I stopped trying to keep up with all the changes, relying on the refresh token. There's a long list of discussions on Tim Dorr's Github with varying solutions.

Well my access_token and refresh_token were both revoked by Tesla when they 'solved' this recent vulnerability, so I have to create a new token from scratch. You are very lucky if your refresh token has not been revoked and you can continue to use it.

The discussions on Tim Dorr's GitHub relate to issues that predate this latest action from Tesla.
 
Well my access_token and refresh_token were both revoked by Tesla when they 'solved' this recent vulnerability, so I have to create a new token from scratch. You are very lucky if your refresh token has not been revoked and you can continue to use it.

The discussions on Tim Dorr's GitHub relate to issues that predate this latest action from Tesla.
Well my access_token and refresh_token were both revoked by Tesla when they 'solved' this recent vulnerability, so I have to create a new token from scratch. You are very lucky if your refresh token has not been revoked and you can continue to use it.

The discussions on Tim Dorr's GitHub relate to issues that predate this latest action from Tesla.
No my refresh token stopped working as well. I used the “Auth for Tesla” iOS app to get a new set of tokens and the repeated refresh works again, with the new endpoint and parameters.
 
Oh yeah, that's a daily summary. For the 5 minute increments I use: /powerhistory but that only gets the previous day. There must be an endpoint that you can query for a particular day because it's available in the app.
Does anyone know the API endpoint and parameters to get the historic time series power data for any given date? As @mjhwa says the app can do so, although scrolling back to the day you want is tedious, so it must be possible.

I have looked at "api/1/energy_sites/{site_id}/history" and "api/1/energy_sites/{site_id}/calendar_history" but can only get power data for the current and previous day too. The cloud has the historic data, I would like to have it locally (fetched using a chron job or similar) for data mining and chart plotting.
 
  • Like
Reactions: jgleigh
Does anyone know the API endpoint and parameters to get the historic time series power data for any given date? As @mjhwa says the app can do so, although scrolling back to the day you want is tedious, so it must be possible.

I have looked at "api/1/energy_sites/{site_id}/history" and "api/1/energy_sites/{site_id}/calendar_history" but can only get power data for the current and previous day too. The cloud has the historic data, I would like to have it locally (fetched using a chron job or similar) for data mining and chart plotting.

You get the current day’s data if you don’t pass a date parameter to that endpoint. You need to pass the date you want to it, in the correct ISO 8601 format with timezone (otherwise you can get incomplete results on days with daylight saving changeovers). Some of the online resources explain how to do this. You can retrieve daily data back to the date of commissioning.
 
You get the current day’s data if you don’t pass a date parameter to that endpoint. You need to pass the date you want to it, in the correct ISO 8601 format with timezone (otherwise you can get incomplete results on days with daylight saving changeovers). Some of the online resources explain how to do this. You can retrieve daily data back to the date of commissioning.
Which endpoint do you mean @Vostok, /powerhistory for the battery or /history for the site?

As best I understand while /api/1/energy_sites/{site_id}/history retrieves the cumulative energy generation/storage (kWh) for a specified recent period, it only returns power generation/storage (watts) for the previous day. When "kind"="power" the "period" parameter is ignored, and it is the power time series that I want not the aggregated energy values. Can /powerhistory have parameters?
 
OK figured it out (I would edit my previous post if I could)

Code:
api/1/energy_sites/{SITE_ID}/calendar_history?kind=power&end_date=2022-08-15T22:59:59Z

will give me any day of power data in 5min intervals regardless of what "period" value I set.

The trick is to get the time part of end_date right. While providing end-date as UTC (Z in the string), I have to deduct an hour and request "22:59:59" (which is 23:59:59 BST) to get results for a full 24 hour period. The returned time series data is in local time so I assume that it is also being applied to the definition of a calendar day.
 
OK figured it out (I would edit my previous post if I could)

Code:
api/1/energy_sites/{SITE_ID}/calendar_history?kind=power&end_date=2022-08-15T22:59:59Z

will give me any day of power data in 5min intervals regardless of what "period" value I set.

For kind=power the 'period' parameter is ignored. You get 1 days' worth of 5-minute power data for the date passed.

If instead you request calendar_history?kind=energy&period=xxx then the period does matter because it is returning Wh not W. Period can be day, week, month, year or lifetime and reports aggregate data for each day from the start of the current period requested to the current time if no &end_date= is passed, else for the requested period up to the &end_date= passed.

The trick is to get the time part of end_date right. While providing end-date as UTC (Z in the string), I have to deduct an hour and request "22:59:59" (which is 23:59:59 BST) to get results for a full 24 hour period. The returned time series data is in local time so I assume that it is also being applied to the definition of a calendar day.

You can pass a date in the local timezone by using '+' or '-' instead of Z, e.g. 2022-08-15T23:59:59+01:00. Use of '+' has to be hex encoded as %2B in the URL. Daylight saving changeover days are tricky to get the full day of data.
 
  • Informative
Reactions: xWren
For kind=power the 'period' parameter is ignored. You get 1 days' worth of 5-minute power data for the date passed.

If instead you request calendar_history?kind=energy&period=xxx then the period does matter because it is returning Wh not W. Period can be day, week, month, year or lifetime and reports aggregate data for each day from the start of the current period requested to the current time if no &end_date= is passed, else for the requested period up to the &end_date= passed.



You can pass a date in the local timezone by using '+' or '-' instead of Z, e.g. 2022-08-15T23:59:59+01:00. Use of '+' has to be hex encoded as %2B in the URL. Daylight saving changeover days are tricky to get the full day of data.
So this is the endpoint I use (python 3):

Python:
    url = (BASE_URL
           + '/energy_sites/'
           + SITE_ID
           + '/calendar_history'
           + '?kind=power'
           + '&start_date='
           + datetime.strftime(
               s_date.astimezone(pytz.utc),
               '%Y-%m-%dT%H:%M:%SZ')
           + '&end_date='
           + datetime.strftime(
               e_date.astimezone(pytz.utc),
               '%Y-%m-%dT%H:%M:%SZ')
           + '&period=' + period)

I've noticed that this gives the best results when both the start and end date are converted to UTC. Daylight savings used to mess me up but I changed it to use the python function astimezone(pytz.utc) which automatically adjusts for that, in addition to converting my local timezone to UTC. More details here if you need it: tesla/TeslaEnergyAPI.py at master · themonomers/tesla
 
  • Like
Reactions: xWren and Vostok
The API seems to be broken. Getting unauthorized (401) from:
"https://owner-api.teslamotors.com/a...6876197/calendar_history?kind=power&end_date=" + date;

My refresh token is up to date and valid. Anyone else having issues?

Battery status API is still working fine.

API works for me. 401 implies there's an issue with the token, you might try regenerating that.

There are a few more parameters you generally pass in there (&period=day&start_date=), but leaving them out should work too. How are you formatting the date? Does a simpler API like /api/1/products work?

Here's a complete request that works for me:
/api/1/energy_sites/SITE_ID/calendar_history?kind=power&period=day&start_date=2023-11-11T00%3A00%3A00-07%3A00&end_date=2023-11-11T23%3A59%3A59-07%3A00
 
I was using the refresh token from Teslafi (yes, I'm lazy) which appeared to be working for battery status but actually wasn't. I forced a delete and refresh which now results in a refresh token many times longer than it used to be but it works and I'm able to get calendar_history values again.

However, it doesn't appear to work for:


https://owner-api.teslamotors.com/api/1/powerwalls/<battery_id>/status

Getting a 404 rather than an auth error so apparently that API doesn't exist anymore. Anyone know what url to use to get current battery capacity? (not current SOC but rather maximum capacity as reported by the BMS).
 
Getting a 404 rather than an auth error so apparently that API doesn't exist anymore. Anyone know what url to use to get current battery capacity? (not current SOC but rather maximum capacity as reported by the BMS).

/api/1/products will give you total_pack_energy (this is the sum of all Powerwalls).
I'm not sure you can get per-battery status with the Owner API.

The Gateway API will give you per-battery nominal_full_pack_energy with /api/system_status.
 
/api/1/products will give you total_pack_energy (this is the sum of all Powerwalls).
I'm not sure you can get per-battery status with the Owner API.

The Gateway API will give you per-battery nominal_full_pack_energy with /api/system_status.

https://owner-api.teslamotors.com/api/1/powerwalls/<battery_id>/status

worked until last month. I don't care about current SOC. I only care about maximum capacity as reported by the BMS. Last month it was down to 39117 watt hours from 40433 when new.

I'll mess with system_status.