From 042073e5e61c94cacd6a2ff4bf99295b1e31b4f3 Mon Sep 17 00:00:00 2001 From: Trefor Southwell Date: Sat, 31 May 2025 15:22:36 +0100 Subject: [PATCH 1/3] Db optimisations --- apps/predbat/db_engine.py | 19 ++++++++++++++++--- apps/predbat/predbat.py | 2 +- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/apps/predbat/db_engine.py b/apps/predbat/db_engine.py index 779a6142..5adca711 100644 --- a/apps/predbat/db_engine.py +++ b/apps/predbat/db_engine.py @@ -22,6 +22,7 @@ def __init__(self, base, db_days): self.db = sqlite3.connect(self.base.config_root + "/predbat.db") self.db_cursor = self.db.cursor() + self.entity_id_cache = {} self._cleanup_db() self.log("db_engine: Started") @@ -77,9 +78,13 @@ def _get_entity_index_db(self, entity_id): """ Get the entity index from the SQLLite database """ + if entity_id in self.entity_id_cache: + return self.entity_id_cache[entity_id] + self.db_cursor.execute("SELECT entity_index FROM entities WHERE entity_name=?", (entity_id,)) res = self.db_cursor.fetchone() if res: + self.entity_id_cache[entity_id] = res[0] return res[0] else: return None @@ -159,10 +164,18 @@ def _set_state_db(self, entity_id, state, attributes, timestamp): keep, ), ) - # Also update the latest table - self.db_cursor.execute("DELETE FROM latest WHERE entity_index = ?", (entity_index,)) + # Upsert into the latest table (replace if exists, insert if not) self.db_cursor.execute( - "INSERT INTO latest (entity_index, datetime, state, attributes, system, keep) VALUES (?, ?, ?, ?, ?, ?)", + """ + INSERT INTO latest (entity_index, datetime, state, attributes, system, keep) + VALUES (?, ?, ?, ?, ?, ?) + ON CONFLICT(entity_index) DO UPDATE SET + datetime=excluded.datetime, + state=excluded.state, + attributes=excluded.attributes, + system=excluded.system, + keep=excluded.keep + """, ( entity_index, now_utc_txt, diff --git a/apps/predbat/predbat.py b/apps/predbat/predbat.py index 128184ba..81bee95c 100644 --- a/apps/predbat/predbat.py +++ b/apps/predbat/predbat.py @@ -36,7 +36,7 @@ import requests import asyncio -THIS_VERSION = "v8.20.1" +THIS_VERSION = "v8.20.2" # fmt: off PREDBAT_FILES = ["predbat.py", "config.py", "prediction.py", "gecloud.py","utils.py", "inverter.py", "ha.py", "download.py", "unit_test.py", "web.py", "predheat.py", "futurerate.py", "octopus.py", "solcast.py","execute.py", "plan.py", "fetch.py", "output.py", "userinterface.py", "energydataservice.py", "alertfeed.py", "compare.py", "db_manager.py", "db_engine.py"] From f9b1ba4970839bc82fcd619d9dab6853d07cd515 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Sat, 31 May 2025 14:24:34 +0000 Subject: [PATCH 2/3] [pre-commit.ci lite] apply automatic fixes --- apps/predbat/db_engine.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/predbat/db_engine.py b/apps/predbat/db_engine.py index 5adca711..b65a5e27 100644 --- a/apps/predbat/db_engine.py +++ b/apps/predbat/db_engine.py @@ -80,7 +80,7 @@ def _get_entity_index_db(self, entity_id): """ if entity_id in self.entity_id_cache: return self.entity_id_cache[entity_id] - + self.db_cursor.execute("SELECT entity_index FROM entities WHERE entity_name=?", (entity_id,)) res = self.db_cursor.fetchone() if res: From ca4a6cf87ede946d8deba9d2b95a74b93a188b43 Mon Sep 17 00:00:00 2001 From: Trefor Southwell Date: Sat, 31 May 2025 16:04:06 +0100 Subject: [PATCH 3/3] Make swap more agressive --- apps/predbat/plan.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/predbat/plan.py b/apps/predbat/plan.py index 66bf519b..fef45eb0 100644 --- a/apps/predbat/plan.py +++ b/apps/predbat/plan.py @@ -2369,7 +2369,7 @@ def optimise_swap_export(self, record_charge_windows, record_export_windows, deb self.charge_limit_best, self.charge_window_best, self.export_window_best, self.export_limits_best, end_record=self.end_record ) - if best_metric <= selected_metric: + if best_metric <= selected_metric or (export_limit_target == 100.0 and abs(best_metric - selected_metric) <= 0.1): if self.debug_enable: self.log( "Swap export window {} {}-{} limit {} with {} => {}-{} metric {} cost {} keep {} cycle {} carbon {} import {}".format(