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.

S3anB

New Member
Apr 13, 2020
2
0
Oxford
Good afternoon,

I am in the process of designing a display for my Netatmo weather station using python on a raspberry Pi+screen. along side the information from the weather station I wanted to display some information from our solar and battery setup.

I am successfully reading the json data from the Tesla Gateway and have the battery percentage, house usage and current solar generation that i was after. The only other thing i wanted to display was the total generation so far that day like you can get in the Tesla app but i cannot see this in the information i have.

Does anyone know if there is a way to get this out other than calculating it from the readings throughout the day?

Many thanks
 
Good afternoon,

I am in the process of designing a display for my Netatmo weather station using python on a raspberry Pi+screen. along side the information from the weather station I wanted to display some information from our solar and battery setup.

I am successfully reading the json data from the Tesla Gateway and have the battery percentage, house usage and current solar generation that i was after. The only other thing i wanted to display was the total generation so far that day like you can get in the Tesla app but i cannot see this in the information i have.

Does anyone know if there is a way to get this out other than calculating it from the readings throughout the day?

Many thanks

The way I did this was to grab energy_exported and energy_imported fields from the meter aggregate you're interested in (in your case it'd just be "solar"). Save these values at the start of the day; if you compare the current values with the start-of-day values, that should give you the numbers you want. As far as I'm aware, there isn't anything that directly gives you the total generation for a day.

Bruce.
 
Good afternoon,

I am in the process of designing a display for my Netatmo weather station using python on a raspberry Pi+screen. along side the information from the weather station I wanted to display some information from our solar and battery setup.

I am successfully reading the json data from the Tesla Gateway and have the battery percentage, house usage and current solar generation that i was after. The only other thing i wanted to display was the total generation so far that day like you can get in the Tesla app but i cannot see this in the information i have.

Does anyone know if there is a way to get this out other than calculating it from the readings throughout the day?

Many thanks

You might want to contact this site: Monitoring & Ownership
 
The way I did this was to grab energy_exported and energy_imported fields from the meter aggregate you're interested in (in your case it'd just be "solar"). Save these values at the start of the day; if you compare the current values with the start-of-day values, that should give you the numbers you want. As far as I'm aware, there isn't anything that directly gives you the total generation for a day.

Bruce.

Thanks Bruce I went with summing up the contribution from each reading so it will work from power on.

GoGo, his implementation looks much fancier than what i think can be achieve in python tkinter however i am an engineer and part of this is me learning to write GUI's which isn't something i have done before being more from an electronics background but i do now have all of the info i wanted from my weather station and tesla displayed on a screen.

Here is my simple python code for fetching the data if anyone is intrested:
Code:
import requests
from datetime import datetime

now = datetime.now()
last_time = (now - now.replace(hour=0, minute=0, second=0, microsecond=0)).total_seconds()
daily_production = 0

def GetData():
    r = requests.get('http://192.168.1.104/api/meters/aggregates', verify=False)
    responce = r.json()
    generation = responce["solar"]['instant_power']
    usage = responce["load"]['instant_power']

    r = requests.get('http://192.168.1.104/api/system_status/soe', verify=False)
    responce = r.json()
    battery = responce['percentage']
        
    now = datetime.now()
    seconds_since_midnight = (now - now.replace(hour=0, minute=0, second=0, microsecond=0)).total_seconds()
    
    global last_time
    global daily_production
    daily_production
    elapsed = seconds_since_midnight - last_time
    if(elapsed > 0):
        daily_production += generation * (float(elapsed)/3600.0)
    else:
        daily_production = -0
    last_time = seconds_since_midnight
    
    return generation, usage, daily_production, battery

#[gen, usage, prod, battery] = GetData()
#print('{:.1f} {:.1f} {:}%'.format(generated/1000, usage/1000, int(battery)))
 
What params for getting hourly home usage (the blue house on the app)?

Looks like it would be the sum of
consumer_energy_imported_from_grid +
consumer_energy_imported_from_solar +
consumer_energy_imported_from_battery ?

Just need to figure out how to get hourly increments and how far back I can go.
 
If I use kind=power, then I get something like this every 5 minutes since midnight regardless of "period".

{

"solar_power" : 2854.55555555556,

"grid_services_power" : 0,

"timestamp" : "2021-01-22T12:20:00-08:00",

"generator_power" : 0,

"grid_power" : 1053.03222223123,

"battery_power" : -2827.22222222222

}

Can I assume the sum of these would be the home usage for that 5 minute period?
 
Seems like calendar_history does the trick if you use end_date. start_date is ignored so you can't do ranges of days in a single call with what I can figure out so far. I'm sure there's a way.

The end_date gives you 5 minute increments from midnight of the same time you enter up to that time of day so if you use say 4pm, you'll get the usage from midnight to 4 pm. It returns times in your current timezone but requires using UTC time in the query.

So to get a full days worth of data for any calendar day:

curl --request GET --header 'Authorization: Bearer <insert token here> 'https://owner-api.teslamotors.com/api/1/energy_sites/<site id here>/calendar_history?kind=power&end_date=2020-12-17T07%3A59%3A59.999Z'

I'd rather get it in 1 hour increments so I don't have to do my own integration since I just want home usage (blue house icon) usage by time so I can make a dashboard that will show me how much it would have cost without solar or timeshifting with my PWs.
 
One thing I'll note is that the time specified does matter. In my testing I've found that even with very old dates, asking for 00:59:00 vs: 00:59:59.999 will give you different answers for that final 5 minute interval. It seems like Tesla never consolidates the data, and has like every reading the Gateway ever sent to them, which has to be a pretty impressive database I think.
 
Seems like calendar_history does the trick if you use end_date. start_date is ignored so you can't do ranges of days in a single call with what I can figure out so far. I'm sure there's a way.

The end_date gives you 5 minute increments from midnight of the same time you enter up to that time of day so if you use say 4pm, you'll get the usage from midnight to 4 pm. It returns times in your current timezone but requires using UTC time in the query.

So to get a full days worth of data for any calendar day:

curl --request GET --header 'Authorization: Bearer <insert token here> 'https://owner-api.teslamotors.com/api/1/energy_sites/<site id here>/calendar_history?kind=power&end_date=2020-12-17T07%3A59%3A59.999Z'

I'd rather get it in 1 hour increments so I don't have to do my own integration since I just want home usage (blue house icon) usage by time so I can make a dashboard that will show me how much it would have cost without solar or timeshifting with my PWs.
Sorka,
This is a golden nugget. Where did you find the information about calendar_history? I have been poking around many sites, and most Tesla API documentation either does not mention calendar_history, or is so thinly documented as to be useless.

So this has really helped me. I am on a TOU program in Florida and FPL cannot produce realtime meter data (they need to manually read my meter -- go figure) nor able to produce Peak and OffPeak banked kWh in a usable fashion. So I need to integrate at the granular level to find out how much peak and offpeak I have banked/used to control my usage pattern. The Tesla app cannot do it, because it does not allow double peaks times, like we have in winter in Florida, so it lacks the information to make an accurate count even if it tried. No way I am losing my banked peak hours to FPL in the New Year, only to get 2 cents a kWh for what costs me 20 cents to consume. So I need to know what is left in the tank and use it or lose it. Again, this is golden information, that should be handed over to teslaapi.io or getpostman, cuz they don't have it.

Frankly, I don't know how Tesla is going to solve FSD if this app is indicative of their software prowess. Cheers.
 
So it appears calendar_history is somewhat broken. I haven't updated in a month or so and trying to update yesterday had holes from 12/4 to 12/6. Now the holes are 12/1 to 12/7 but all over november and previous months working fine. Not sure if this is some sort of data corruption on the back end or not.

Are there any other APIs that can return hourly data for any day given?
 
So it appears calendar_history is somewhat broken. I haven't updated in a month or so and trying to update yesterday had holes from 12/4 to 12/6. Now the holes are 12/1 to 12/7 but all over november and previous months working fine. Not sure if this is some sort of data corruption on the back end or not.

Are there any other APIs that can return hourly data for any day given?

Well I had calendar_history working fine yesterday and downloaded all the data for November and December.

However at 06:30 UTC yesterday all my API scripts stopped working. Going to https://owner-api.teslamotors.com/ in a browser returns "page not found".

Has Tesla made an urgent patch or change to the API gateway to "fix" the alleged hack reported in InsideEVs? The timing of the API ceasing to work, and publication of this report is, shall we say, oddly coincidental?

 
The api and refresh tokens both were invalidated yesterday at that time requiring a full login flow. It's working but you need a new access token.
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.
 
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.

My API token that I refreshed yesterday is still working today but I had only refreshed it 2 days earlier and it normally is good for 45 days. I'm assuming there was a security issue and Tesla plugged that and then invalided the tokens forcing a full login that would now block any hackers that had gained tokens through a security hole.
 
So it appears calendar_history is somewhat broken. I haven't updated in a month or so and trying to update yesterday had holes from 12/4 to 12/6. Now the holes are 12/1 to 12/7 but all over november and previous months working fine. Not sure if this is some sort of data corruption on the back end or not.

Are there any other APIs that can return hourly data for any day given?
Not sure if this is exactly what you're talking about but back in late Nov calendar_history and kind=time_of_use_energy stopped working for me and I noticed that it worked when I also passed the start_date parameter. Previously I had it working with just the end_date.
 
My API token that I refreshed yesterday is still working today but I had only refreshed it 2 days earlier and it normally is good for 45 days. I'm assuming there was a security issue and Tesla plugged that and then invalided the tokens forcing a full login that would now block any hackers that had gained tokens through a security hole.
Good point. It might be because of that German teen who hacked 3rd party apps that held people's access token.