Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 72 additions & 69 deletions apps/predbat/predbat.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import os
import yaml

THIS_VERSION = "v7.14.23"
THIS_VERSION = "v7.14.24"
TIME_FORMAT = "%Y-%m-%dT%H:%M:%S%z"
TIME_FORMAT_SECONDS = "%Y-%m-%dT%H:%M:%S.%f%z"
TIME_FORMAT_OCTOPUS = "%Y-%m-%d %H:%M:%S%z"
Expand Down Expand Up @@ -2504,7 +2504,7 @@ def download_predbat_releases(self):
__file__, self.releases["this"], self.releases["latest"], self.releases["latest_beta"]
)
)
PREDBAT_UPDATE_OPTIONS = []
PREDBAT_UPDATE_OPTIONS = ["main"]
this_tag = THIS_VERSION

# Find all versions for the dropdown menu
Expand Down Expand Up @@ -4813,72 +4813,73 @@ def basic_rates(self, info, rtype, prev=None):
max_minute = max(rates) + 1
midnight = datetime.strptime("00:00:00", "%H:%M:%S")
for this_rate in info:
start_str = this_rate.get("start", "00:00:00")
start_str = self.resolve_arg("start", start_str, "00:00:00")
end_str = this_rate.get("end", "00:00:00")
end_str = self.resolve_arg("end", end_str, "00:00:00")
if this_rate:
start_str = this_rate.get("start", "00:00:00")
start_str = self.resolve_arg("start", start_str, "00:00:00")
end_str = this_rate.get("end", "00:00:00")
end_str = self.resolve_arg("end", end_str, "00:00:00")

if start_str.count(":") < 2:
start_str += ":00"
if end_str.count(":") < 2:
end_str += ":00"
if start_str.count(":") < 2:
start_str += ":00"
if end_str.count(":") < 2:
end_str += ":00"

try:
start = datetime.strptime(start_str, "%H:%M:%S")
except ValueError:
self.log("WARN: Bad start time {} provided in energy rates".format(start_str))
self.record_status("Bad start time {} provided in energy rates".format(start_str), had_errors=True)
continue

try:
end = datetime.strptime(end_str, "%H:%M:%S")
except ValueError:
self.log("WARN: Bad end time {} provided in energy rates".format(end_str))
self.record_status("Bad end time {} provided in energy rates".format(end_str), had_errors=True)
continue
try:
start = datetime.strptime(start_str, "%H:%M:%S")
except ValueError:
self.log("WARN: Bad start time {} provided in energy rates".format(start_str))
self.record_status("Bad start time {} provided in energy rates".format(start_str), had_errors=True)
continue

date = None
if "date" in this_rate:
date_str = self.resolve_arg("date", this_rate["date"])
try:
date = datetime.strptime(date_str, "%Y-%m-%d")
end = datetime.strptime(end_str, "%H:%M:%S")
except ValueError:
self.log("WARN: Bad date {} provided in energy rates".format(this_rate["date"]))
self.record_status("Bad date {} provided in energy rates".format(this_rate["date"]), had_errors=True)
self.log("WARN: Bad end time {} provided in energy rates".format(end_str))
self.record_status("Bad end time {} provided in energy rates".format(end_str), had_errors=True)
continue

rate = this_rate.get("rate", 0.0)
rate = self.resolve_arg("rate", rate, 0.0)
try:
rate = float(rate)
except ValueError:
self.log("WARN: Bad rate {} provided in energy rates".format(rate))
self.record_status("Bad rate {} provided in energy rates".format(rate), had_errors=True)
continue
date = None
if "date" in this_rate:
date_str = self.resolve_arg("date", this_rate["date"])
try:
date = datetime.strptime(date_str, "%Y-%m-%d")
except ValueError:
self.log("WARN: Bad date {} provided in energy rates".format(this_rate["date"]))
self.record_status("Bad date {} provided in energy rates".format(this_rate["date"]), had_errors=True)
continue

rate = this_rate.get("rate", 0.0)
rate = self.resolve_arg("rate", rate, 0.0)
try:
rate = float(rate)
except ValueError:
self.log("WARN: Bad rate {} provided in energy rates".format(rate))
self.record_status("Bad rate {} provided in energy rates".format(rate), had_errors=True)
continue

# Time in minutes
start_minutes = max(self.minutes_to_time(start, midnight), 0)
end_minutes = min(self.minutes_to_time(end, midnight), 24 * 60 - 1)
# Time in minutes
start_minutes = max(self.minutes_to_time(start, midnight), 0)
end_minutes = min(self.minutes_to_time(end, midnight), 24 * 60 - 1)

self.log("Adding rate {} => {} to {} @ {} date {}".format(this_rate, self.time_abs_str(start_minutes), self.time_abs_str(end_minutes), rate, date))
self.log("Adding rate {} => {} to {} @ {} date {}".format(this_rate, self.time_abs_str(start_minutes), self.time_abs_str(end_minutes), rate, date))

# Make end > start
if end_minutes <= start_minutes:
end_minutes += 24 * 60
# Make end > start
if end_minutes <= start_minutes:
end_minutes += 24 * 60

# Adjust for date if specified
if date:
delta_minutes = self.minutes_to_time(date, self.midnight)
start_minutes += delta_minutes
end_minutes += delta_minutes
# Adjust for date if specified
if date:
delta_minutes = self.minutes_to_time(date, self.midnight)
start_minutes += delta_minutes
end_minutes += delta_minutes

# Store rates against range
if end_minutes >= 0 and start_minutes < max_minute:
for minute in range(start_minutes, end_minutes):
if (not date) or (minute >= 0 and minute < max_minute):
rates[minute % max_minute] = rate
if not date and not prev:
rates[(minute % max_minute) + max_minute] = rate
# Store rates against range
if end_minutes >= 0 and start_minutes < max_minute:
for minute in range(start_minutes, end_minutes):
if (not date) or (minute >= 0 and minute < max_minute):
rates[minute % max_minute] = rate
if not date and not prev:
rates[(minute % max_minute) + max_minute] = rate

return rates

Expand Down Expand Up @@ -5318,21 +5319,23 @@ def rate_minmax(self, rates):
rate_max = 0
rate_average = 0
rate_n = 0
rate = 0

# Scan rates and find min/max/average
rate = rates.get(self.minutes_now, 0)
for minute in range(self.minutes_now, self.forecast_minutes + self.minutes_now):
if minute in rates:
rate = rates[minute]
if rate > rate_max:
rate_max = rate
rate_max_minute = minute
if rate < rate_min:
rate_min = rate
rate_min_minute = minute
rate_average += rate
rate_n += 1
minute += 1
if rates:
rate = rates.get(self.minutes_now, 0)
for minute in range(self.minutes_now, self.forecast_minutes + self.minutes_now):
if minute in rates:
rate = rates[minute]
if rate > rate_max:
rate_max = rate
rate_max_minute = minute
if rate < rate_min:
rate_min = rate
rate_min_minute = minute
rate_average += rate
rate_n += 1
minute += 1
if rate_n:
rate_average /= rate_n

Expand Down