Skip to content

Commit

Permalink
Merge pull request #133 from fboundy/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
fboundy authored Feb 23, 2024
2 parents 0977f12 + 12f6a85 commit 0c3fdf5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# PV Opt: Home Assistant Solar/Battery Optimiser v3.8.10
# PV Opt: Home Assistant Solar/Battery Optimiser v3.8.14

Solar / Battery Charging Optimisation for Home Assistant. This appDaemon application attempts to optimise charging and discharging of a home solar/battery system to minimise cost electricity cost on a daily basis using freely available solar forecast data from SolCast. This is particularly beneficial for Octopus Agile but is also benefeficial for other time-of-use tariffs such as Octopus Flux or simple Economy 7.

Expand Down
28 changes: 19 additions & 9 deletions apps/pv_opt/pv_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#
USE_TARIFF = True

VERSION = "3.8.10"
VERSION = "3.8.14"
DEBUG = False

DATE_TIME_FORMAT_LONG = "%Y-%m-%d %H:%M:%S%z"
Expand Down Expand Up @@ -274,7 +274,7 @@ def hass2df(self, entity_id, days=2, log=False):

hist = self.get_history(entity_id=entity_id, days=days)

if len(hist) >0:
if (hist is not None) and (len(hist) >0):
df = pd.DataFrame(hist[0]).set_index("last_updated")["state"]
df.index = pd.to_datetime(df.index, format="ISO8601")

Expand All @@ -283,7 +283,7 @@ def hass2df(self, entity_id, days=2, log=False):
df = df[df != "unknown"]

else:
raise ValueError(f"No data returned from HASS entity {entity_id}")
self.log(f"No data returned from HASS entity {entity_id}", level="ERROR")
df = None

return df
Expand Down Expand Up @@ -819,16 +819,21 @@ def get_ha_value(self, entity_id):

# if the state is None return None
if state is not None:
if (state in ['unknown', 'unavailable']) and (entity_id[:6] != 'button'):
e = f"HA returned {state} for state of {entity_id}"
self._status(f"ERROR: {e}")
raise ValueError(e)
# if the state is 'on' or 'off' then it's a bool
if state.lower() in ["on", "off", "true", "false"]:
elif state.lower() in ["on", "off", "true", "false"]:
value = state.lower() in ["on", "true"]

# see if we can coerce it into an int 1st and then a floar
for t in [int, float]:
try:
value = t(state)
except:
pass
else:
for t in [int, float]:
try:
value = t(state)
except:
pass

# if none of the above return a string
if value is None:
Expand Down Expand Up @@ -1380,6 +1385,11 @@ def optimise(self):
self.soc_now = self.get_config("id_battery_soc")
x = self.hass2df(self.config["id_battery_soc"], days=1, log=self.debug)

if x is None:
self.log("")
self.log("Unable to get SOC at start of current window.", level="ERROR")
return

if self.debug:
self.log(f">>> soc_now: {self.soc_now}")
self.log(f">>> x: {x}")
Expand Down
18 changes: 12 additions & 6 deletions apps/pv_opt/solis.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,17 +400,23 @@ def _solis_control_charge_discharge(self, direction, enable, **kwargs):
entity_id = self.host.config["id_timed_charge_discharge_button"]
self.host.call_service("button/press", entity_id=entity_id)
time.sleep(0.5)
time_pressed = pd.Timestamp(self.host.get_state(entity_id))
try:
time_pressed = pd.Timestamp(self.host.get_state(entity_id))

dt = (pd.Timestamp.now(self.host.tz) - time_pressed).total_seconds()
if dt < 10:
self.log(f"Successfully pressed button {entity_id}")
dt = (pd.Timestamp.now(self.host.tz) - time_pressed).total_seconds()
if dt < 10:
self.log(f"Successfully pressed button {entity_id}")

else:
else:
self.log(
f"Failed to press button {entity_id}. Last pressed at {time_pressed.strftime(TIMEFORMAT)} ({dt:0.2f} seconds ago)"
)
except:
self.log(
f"Failed to press button {entity_id}. Last pressed at {time_pressed.strftime(TIMEFORMAT)} ({dt:0.2f} seconds ago)"
f"Failed to press button {entity_id}: it appears to never have been pressed."
)


else:
self.log("Inverter already at correct time settings")

Expand Down

0 comments on commit 0c3fdf5

Please sign in to comment.