Skip to content

Commit

Permalink
Merge pull request #226 from mcbirse/main
Browse files Browse the repository at this point in the history
Fix timezone for solar only sites using an offset
  • Loading branch information
jasonacox authored Mar 28, 2023
2 parents 1ac10b1 + 4d948f9 commit 7628d99
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion tools/solar-only/tesla-history/tesla-history.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@
reservedata = []
sitetz = None
tzname = None
tzoffset = False
power = None
soe = None
backup = None
Expand Down Expand Up @@ -399,7 +400,7 @@ def get_power_history(start, end):
Adds data points to 'powerdata' in InfluxDB Line Protocol format with tag source='cloud'
"""
global sitetz, tzname, dayloaded, power, soe
global sitetz, tzname, tzoffset, dayloaded, power, soe

print(f"Retrieving data for gap: [{start.astimezone(influxtz)}] - [{end.astimezone(influxtz)}] ({str(end - start)}s)")

Expand Down Expand Up @@ -438,9 +439,11 @@ def get_power_history(start, end):
histtz = tz.tzoffset(None, tzdata * 60)
offset = datetime.now(tz=histtz).strftime('%z')
tzdata = f"UTC{offset[:3]}:{offset[3:]}"
tzoffset = True
else:
# Get timezone from timezone name
histtz = tz.gettz(tzdata)
tzoffset = False

if histtz is None:
sys.exit(f"ERROR: Invalid timezone for history data - {tzdata}")
Expand Down Expand Up @@ -480,6 +483,11 @@ def get_power_history(start, end):
for d in power['time_series']:
timestamp = isoparse(d['timestamp']).astimezone(utctz)
nextstart = timestamp + timedelta(minutes=5)

# Check if solar only site timezone is using an offset and replace with InfluxDB timezone
if isinstance(site, SolarPanel) and tzoffset:
timestamp = isoparse(d['timestamp']).replace(tzinfo=influxtz).astimezone(utctz)

# Save data point when within start/end range only
if timestamp >= start and timestamp <= end:
# Calculate power usage values
Expand Down

0 comments on commit 7628d99

Please sign in to comment.