Welcome to Tesla Motors Club
Discuss Tesla's Model S, Model 3, Model X, Model Y, Cybertruck, Roadster and More.
Register
  • We just completed a significant update, but we still have some fixes and adjustments to make, so please bear with us for the time being. Cheers!

Getting daily production from Tesla Gateway API

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
 

bmah

Moderator, Model S/X, California Forums
Mar 17, 2015
3,850
6,821
Lafayette, CA, USA
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.
 

GoGoGoMach5

Member
Mar 11, 2020
17
3
Southern California
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
 

S3anB

New Member
Apr 13, 2020
2
0
Oxford
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)))
 

sorka

Well-Known Member
Feb 28, 2015
7,622
5,653
Merced, CA
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.
 

sorka

Well-Known Member
Feb 28, 2015
7,622
5,653
Merced, CA
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?
 

sorka

Well-Known Member
Feb 28, 2015
7,622
5,653
Merced, CA
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.
 

woferry

Member
Mar 4, 2019
397
468
San Jose, CA
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.
 

About Us

Formed in 2006, Tesla Motors Club (TMC) was the first independent online Tesla community. Today it remains the largest and most dynamic community of Tesla enthusiasts. Learn more.

Do you value your experience at TMC? Consider becoming a Supporting Member of Tesla Motors Club. As a thank you for your contribution, you'll get nearly no ads in the Community and Groups sections. Additional perks are available depending on the level of contribution. Please visit the Account Upgrades page for more details.


SUPPORT TMC
Top