Skip to content

Commit

Permalink
Additional error trapping when HA returns unknown or unavailable
Browse files Browse the repository at this point in the history
  • Loading branch information
fboundy committed Feb 20, 2024
1 parent 1506c6e commit 4c2b494
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 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.11

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
19 changes: 12 additions & 7 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.11"
DEBUG = False

DATE_TIME_FORMAT_LONG = "%Y-%m-%d %H:%M:%S%z"
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']:
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

0 comments on commit 4c2b494

Please sign in to comment.