Skip to content

Commit

Permalink
Fix for homeassistant not showing the most recent energy data. Reduce…
Browse files Browse the repository at this point in the history
…d hourly data fetch to 4 days to the stop API from trimming the latest data from the response
  • Loading branch information
brunsy committed Aug 17, 2024
1 parent 39109af commit 6c63668
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ View your energy usage from Frank Energy (NZ) in homeassistant.

## Data

Imports the last week of hourly energy data & costs from Frank Energy.
Imports the last few days of hourly energy data & costs from Frank Energy.

![Energy Useage PNG](/homeassistant-energy-graph.png "Energy Dashboard Reporting")

## Getting started

Expand Down
13 changes: 7 additions & 6 deletions custom_components/frankenergy/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,15 +199,16 @@ async def get_data(self):
"mobile-build-number": "1"
}

today = datetime.now()
seven_days_ago = today - timedelta(days=7) # fetch 7 days worth of data
from_date = seven_days_ago.strftime("%Y-%m-%d")
to_date = today.strftime("%Y-%m-%d")
# API only returns 120 usage items (starting from the from_date).
# If you want to query more than ~4 days of hourly data, at a time you need to split it into multiple requests
# or the the latest usage will not be returned.
to_date = datetime.now()
from_date = to_date - timedelta(days=4) # fetch 4 days worth of data

url = f"{self._url_data_base}/v2/private/usage/electricity/aggregatedSiteUsage/hourly"
params = {
'startDate': from_date,
'endDate': to_date,
'startDate': from_date.strftime("%Y-%m-%d"),
'endDate': to_date.strftime("%Y-%m-%d"),
}

async with aiohttp.ClientSession() as session, \
Expand Down
Binary file added homeassistant-energy-graph.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 6c63668

Please sign in to comment.