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.
Good point. It might be because of that German teen who hacked 3rd party apps that held people's access token.

I read the article. It was not "hacked third party appS" it was " Found people who had used Teslamate, which is a SELF HOSTED tool, who had set it up incorrectly (likely not changing default passwords)".

I dont use any third party apps to access my car, but these were not the issue, this was a "self own" from people using a self hosted tool and not setting it up correctly, but of course the story is "Teen hacks teslas!"
 
  • Informative
Reactions: sorka
I patched the data holes by downloading from the app for those days which was tedious and the resolution is in 100 watt increments so I'm likely to be off a few percent for those days unless they are leveling between the 5 minute increments with carryover and carryunder NOT to cross one hour boundaries.
 
I can't get time_of_use_energy to be recognized with start_date. It just sends "upstream internal errror" back.

kind=power still works for me except for the ever widening hole in the beginning of December.
Make sure you're using UTC with the dates:

Python 2.7:
url = ('https://owner-api.teslamotors.com/api/1/energy_sites/' + SITE_ID + '/calendar_history' + '?kind=time_of_use_energy' + '&period=' + period + '&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') )
 
  • Informative
Reactions: xWren
Make sure you're using UTC with the dates:

Python 2.7:
url = ('https://owner-api.teslamotors.com/api/1/energy_sites/' + SITE_ID + '/calendar_history' + '?kind=time_of_use_energy' + '&period=' + period + '&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') )

Can you turn that into sample URL minus your token so that I can curl it. I'm using UTC times with Z on the end of the date/time string. Same as I'm using for end_date with kind=power.
 
Can you turn that into sample URL minus your token so that I can curl it. I'm using UTC times with Z on the end of the date/time string. Same as I'm using for end_date with kind=power.
So this is using a start_date in local (Pacific) date/time Jan 12, 2022 00:00:00 and an end_date in local (Pacific) date/time Jan 12, 2022 23:59:59 then converting it to UTC into the URL below:

https://owner-api.teslamotors.com/api/1/energy_sites/<SITE_ID>/calendar_history?kind=time_of_use_energy&period=day&start_date=2022-01-12T08:00:00Z&end_date=2022-01-13T07:59:59Z
 
So this is using a start_date in local (Pacific) date/time Jan 12, 2022 00:00:00 and an end_date in local (Pacific) date/time Jan 12, 2022 23:59:59 then converting it to UTC into the URL below:

https://owner-api.teslamotors.com/api/1/energy_sites/<SITE_ID>/calendar_history?kind=time_of_use_energy&period=day&start_date=2022-01-12T08:00:00Z&end_date=2022-01-13T07:59:59Z

For me this "kind" isn't producing hourly data.
 
I experienced the same. The access and refresh tokens are a much longer length and the access token expires in 8 hours instead of 45 days. You can use the same refresh token to generate a new access token more frequently now but the URL and parameters are different.

Bummer... not that the tokens are longer, but they only last for 8 hours. Can you still use refresh tokens to generate new access tokens? That's easy enough to script. But the full reauthentication to generate new tokens is a pain due to the Captcha.
 
I patched the data holes by downloading from the app for those days which was tedious and the resolution is in 100 watt increments so I'm likely to be off a few percent for those days unless they are leveling between the 5 minute increments with carryover and carryunder NOT to cross one hour boundaries.

Yep. That lousy 100W resolution is what motivated me to learn the Tesla API and write my own scripts to get the data at full precision. Also to have the ability to retrieve any day's data at whim, rather than manually scrolling through days in the App and clicking "Download my data".
 
  • Like
Reactions: sorka
Bummer... not that the tokens are longer, but they only last for 8 hours. Can you still use refresh tokens to generate new access tokens? That's easy enough to script. But the full reauthentication to generate new tokens is a pain due to the Captcha.

It's not 8 hours. Someone thought it was because they renewed and then 8 hours later it was expired but that's because tesla expired them due to an "event".
 
  • Like
Reactions: Vostok
I keep checking several times a day to see if the missing data is restored. i.e. maybe they had some sort of corruption and they just need to restore from backups? One can hope. I'd rather get my 1 watt resolution data back for those missing days.
 
Bummer... not that the tokens are longer, but they only last for 8 hours. Can you still use refresh tokens to generate new access tokens? That's easy enough to script. But the full reauthentication to generate new tokens is a pain due to the Captcha.
Yes though it's a bit different than before:

Python:
url = 'https://auth.tesla.com/oauth2/v3/token'
payload = {
  'grant_type': 'refresh_token',
  'client_id': 'ownerapi',
  'refresh_token': REFRESH_TOKEN,
  'scope': 'openid email offline_access'
}

The response is a bit different too.
 
  • Informative
Reactions: Vostok
The data loss continues. Now the December 9th and 10th are gone.
Though I don't use calendar_history, kind=power, I played around with it and I was able to get data for period=day in 5 minute increments. Oddly enough nothing came up for Dec 9 but Dec 10 had values. Here's what I used:

local = pytz.timezone(TIME_ZONE) date = local.localize(datetime( date.year, date.month, date.day, 23, 59, 59, 0 ), is_dst=None) url = ('https://owner-api.teslamotors.com/api/1/energy_sites/' + SITE_ID + '/calendar_history' + '?kind=power' + '&end_date=' + datetime.strftime(date.astimezone(pytz.utc), '%Y-%m-%dT%H:%M:%SZ') + '&period=' + period)
 
Though I don't use calendar_history, kind=power, I played around with it and I was able to get data for period=day in 5 minute increments. Oddly enough nothing came up for Dec 9 but Dec 10 had values. Here's what I used:

local = pytz.timezone(TIME_ZONE) date = local.localize(datetime( date.year, date.month, date.day, 23, 59, 59, 0 ), is_dst=None) url = ('https://owner-api.teslamotors.com/api/1/energy_sites/' + SITE_ID + '/calendar_history' + '?kind=power' + '&end_date=' + datetime.strftime(date.astimezone(pytz.utc), '%Y-%m-%dT%H:%M:%SZ') + '&period=' + period)

Tomorrow afternoon, Dec 10th will also vanish. But everything from November and before still appears. It's some sort of december only data hole.
 
The data loss continues. Now the December 9th and 10th are gone.

Hmm… I have data for both those days downloaded via the API, but 28 Nov has no data for me.

If you scroll back in the App is it there?

If this is something that really annoys you, you could write a script to poll the site status and record whatever data you want, and run it every 5 minutes via a cron job. I do that to record battery capacity and charge state (%), since the App &/or calendar_history endpoint only provides battery charge level every 15 minutes not 5, and does not return battery capacity at all.

You do, however, get missing data with that method too, since Tesla occasionally takes the backend offline presumably for maintenance updates for 10-20 minutes in the middle of the night USA time (which is afternoon for us) and no data is returned during those periods.

But during those periods the Gateway will still record data and upload it to the servers once it comes back online. So it is possible to stitch together a pretty complete set of data using both methods.
 
Yes though it's a bit different than before

I'm having trouble generating a new access token from the full 4 step login process outlined on this page:


Has Tesla changed this as well? It fails at Step 2. I do the login and get the Captcha at Step 1, but Step 2 to obtain an authorisation code returns

Access Denied​

You don't have permission to access "http://auth.tesla.com/oauth2/v3/authorize?" on this server.
 
I'm having trouble generating a new access token from the full 4 step login process outlined on this page:


Has Tesla changed this as well? It fails at Step 2. I do the login and get the Captcha at Step 1, but Step 2 to obtain an authorisation code returns

Access Denied​

You don't have permission to access "http://auth.tesla.com/oauth2/v3/authorize?" on this server.
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.