Skip to content

Commit

Permalink
Fix bugs in auto opt-in saving event, add rate values to next charge/…
Browse files Browse the repository at this point in the history
…discharge slot sensors (#426)

* Fix bugs in auto opt-in saving event
* Add rate values to next charge/discharge slot sensors
* Change default existing install predbat state to charge & discharge
  • Loading branch information
springfall2008 authored Dec 5, 2023
1 parent 91ecb21 commit 0b89af1
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions apps/predbat/predbat.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import appdaemon.plugins.hass.hassapi as hass
import adbase as ad

THIS_VERSION = "v7.14"
THIS_VERSION = "v7.14.1"
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 @@ -402,7 +402,7 @@
"type": "select",
"options": PREDBAT_MODE_OPTIONS,
"icon": "mdi:state-machine",
"default": PREDBAT_MODE_OPTIONS[PREDBAT_MODE_MONITOR],
"default": PREDBAT_MODE_OPTIONS[PREDBAT_MODE_CONTROL_CHARGEDISCHARGE],
},
{"name": "load_filter_modal", "friendly_name": "Apply modal filter historical load", "type": "switch", "enable": "expert_mode", "default": True},
{"name": "iboost_enable", "friendly_name": "IBoost enable", "type": "switch", "default": False},
Expand Down Expand Up @@ -5730,10 +5730,12 @@ def publish_discharge_limit(self, discharge_window, discharge_limits, best):
discharge_end_str = "undefined"
discharge_start_date = None
discharge_end_date = None
discharge_average = None

if discharge_window and (discharge_window[0]["end"] < (24 * 60 + self.minutes_now)):
discharge_start_minutes = discharge_window[0]["start"]
discharge_end_minutes = discharge_window[0]["end"]
discharge_average = discharge_window[0].get("average", None)

time_format_time = "%H:%M:%S"
discharge_startt = self.midnight_utc + timedelta(minutes=discharge_start_minutes)
Expand All @@ -5760,6 +5762,7 @@ def publish_discharge_limit(self, discharge_window, discharge_limits, best):
state=discharge_limit_percent,
attributes={
"results": discharge_limit_time,
"rate" : discharge_average,
"friendly_name": "Predicted discharge limit best",
"state_class": "measurement",
"unit_of_measurement": "%",
Expand Down Expand Up @@ -5807,6 +5810,7 @@ def publish_discharge_limit(self, discharge_window, discharge_limits, best):
state=discharge_limit_percent,
attributes={
"results": discharge_limit_time,
"rate" : discharge_average,
"friendly_name": "Predicted discharge limit",
"state_class": "measurement",
"unit_of_measurement": "%",
Expand Down Expand Up @@ -5864,6 +5868,7 @@ def publish_charge_limit(self, charge_limit, charge_window, charge_limit_percent
if not SIMULATE:
charge_limit_first = 0
charge_limit_percent_first = 0
charge_average_first = None
charge_start_str = "undefined"
charge_end_str = "undefined"
charge_start_date = None
Expand All @@ -5876,6 +5881,7 @@ def publish_charge_limit(self, charge_limit, charge_window, charge_limit_percent
charge_limit_percent_first = charge_limit_percent[0]
charge_start_minutes = charge_window[0]["start"]
charge_end_minutes = charge_window[0]["end"]
charge_average_first = charge_window[0].get("average", None)

time_format_time = "%H:%M:%S"
charge_startt = self.midnight_utc + timedelta(minutes=charge_start_minutes)
Expand Down Expand Up @@ -5906,6 +5912,7 @@ def publish_charge_limit(self, charge_limit, charge_window, charge_limit_percent
"state_class": "measurement",
"unit_of_measurement": "%",
"icon": "mdi:battery-charging",
"rate" : charge_average_first,
},
)
self.dashboard_item(
Expand Down Expand Up @@ -5953,6 +5960,7 @@ def publish_charge_limit(self, charge_limit, charge_window, charge_limit_percent
"state_class": "measurement",
"unit_of_measurement": "%",
"icon": "mdi:battery-charging",
"rate" : charge_average_first,
},
)
self.dashboard_item(
Expand Down Expand Up @@ -8554,8 +8562,8 @@ def fetch_sensor_data(self):

joined_events = self.get_state(entity_id=entity_id, attribute="joined_events")
if not joined_events:
entity_event = entity_id.replace("binary_sensor.", "event.").replace("_sessions", "_session_events")
joined_events = self.get_state(entity_id=entity_event, attribute="joined_events")
entity_id = entity_id.replace("binary_sensor.", "event.").replace("_sessions", "_session_events")
joined_events = self.get_state(entity_id=entity_id, attribute="joined_events")

available_events = self.get_state(entity_id=entity_id, attribute="available_events")
if available_events:
Expand All @@ -8566,7 +8574,7 @@ def fetch_sensor_data(self):
saving_rate = event.get("octopoints_per_kwh", saving_rate * octopoints_per_penny) / octopoints_per_penny # Octopoints per pence
if code:
self.log("Joining Octopus saving event code {} start {} end {} price per kWh {}".format(code, start, end, saving_rate))
self.call_service("octopus_energy.join_octoplus_saving_session_event", event_code=code, target=entity_id)
self.call_service("octopus_energy/join_octoplus_saving_session_event", event_code = code, entity_id = entity_id)

if joined_events:
for event in joined_events:
Expand Down Expand Up @@ -9289,6 +9297,9 @@ def load_user_config(self, quiet=True, register=False):
if name == "expert_mode":
if new_install:
item["default"] = False
elif name == "mode":
if new_install:
item["default"] = PREDBAT_MODE_OPTIONS[PREDBAT_MODE_MONITOR]

# Find values and monitor config
for item in CONFIG_ITEMS:
Expand Down Expand Up @@ -9569,4 +9580,4 @@ def run_time_loop_balance(self, cb_args):
except Exception as e:
self.log("ERROR: Exception raised {}".format(e))
self.record_status("ERROR: Exception raised {}".format(e))
raise e
raise e

0 comments on commit 0b89af1

Please sign in to comment.