Skip to content

Commit

Permalink
Logging of Hass2DF
Browse files Browse the repository at this point in the history
  • Loading branch information
fboundy committed Dec 21, 2023
1 parent 96f4aac commit bbc8f91
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions apps/pv_opt/pv_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
#
USE_TARIFF = True

VERSION = "3.4.4"
VERSION = "3.4.5-alpha"
DEBUG = True

DATE_TIME_FORMAT_LONG = "%Y-%m-%d %H:%M:%S%z"
DATE_TIME_FORMAT_SHORT = "%d-%b %H:%M"
Expand Down Expand Up @@ -229,18 +230,28 @@ def importName(modulename, name):

class PVOpt(hass.Hass):
def hass2df(self, entity_id, days=2, log=False):
if log:
self.log(f">>> Getting {days} history for {entity_id}")
self.log(f">>> Entity exits: {self.entity_exists(entity_id)}")
hist = self.get_history(entity_id=entity_id, days=days)

if log:
self.log(hist)
self.log(f">>> {hist})
df = pd.DataFrame(hist[0]).set_index("last_updated")["state"]
df.index = pd.to_datetime(df.index, format="ISO8601")

if log:
self.log(f">>> {df}")

df = df.sort_index()
df = df[df != "unavailable"]
df = df[df != "unknown"]
if log:
self.log(f">>> {df}")
return df

def initialize(self):
self.debug = False
self.debug = DEBUG
self.config = {}
self.log("")
self.log(f"******************* PV Opt v{VERSION} *******************")
Expand Down Expand Up @@ -305,7 +316,7 @@ def _estimate_capacity(self):
if "id_battery_charge_power" in self.config:
df = pd.DataFrame(
self.hass2df(
entity_id=self.config["id_battery_charge_power"], days=7
entity_id=self.config["id_battery_charge_power"], days=7, log=self.debug
).astype(int, errors="ignore")
).set_axis(["Power"], axis=1)

Expand Down Expand Up @@ -391,7 +402,7 @@ def _cost_today(self):
grid = (
pd.concat(
[
self.hass2df(self.config[f"id_{col}_today"], days=1)
self.hass2df(self.config[f"id_{col}_today"], days=1,log=self.debug)
.astype(float)
.resample("30T")
.ffill()
Expand All @@ -413,14 +424,14 @@ def _cost_today(self):
grid = (
pd.concat(
[
self.hass2df(self.config["id_grid_import_power"], days=1)
self.hass2df(self.config["id_grid_import_power"], days=1,log=self.debug)
.astype(float)
.resample("30T")
.mean()
.reindex(index)
.fillna(0)
.reindex(index),
self.hass2df(self.config["id_grid_export_power"], days=1)
self.hass2df(self.config["id_grid_export_power"], days=1, log=self.debug)
.astype(float)
.resample("30T")
.mean()
Expand All @@ -429,14 +440,14 @@ def _cost_today(self):
],
axis=1,
)
.set_axis(["grid_import", "grid_export"], axis=1)
.set_axis(["grid_import", "grid_export"], axis=1, log=self.debug)
.loc[pd.Timestamp.now(tz="UTC").normalize() :]
)

elif "id_grid_power" in self.config:
grid = (
-(
self.hass2df(self.config["id_grid_power"], days=1)
self.hass2df(self.config["id_grid_power"], days=1, log=self.debug)
.astype(float)
.resample("30T")
.mean()
Expand Down Expand Up @@ -1263,7 +1274,7 @@ def optimise(self):

x = self.hass2df(
self.config["id_battery_soc"],
days=1,
days=1, log=self.debug
).astype(float)
x = x.loc[x.loc[: self.static.index[0]].index[-1] :]
x = pd.concat(
Expand Down Expand Up @@ -1749,6 +1760,7 @@ def load_consumption(self):
df = self.hass2df(
entity_id,
days=int(self.get_config("consumption_history_days")),
log=self.debug,
)

except Exception as e:
Expand Down

0 comments on commit bbc8f91

Please sign in to comment.