diff --git a/Bat_EV_SDL_V3_Beta/module.php b/Bat_EV_SDL_V3_Beta/module.php index c5ca9ed..6d83f6c 100644 --- a/Bat_EV_SDL_V3_Beta/module.php +++ b/Bat_EV_SDL_V3_Beta/module.php @@ -27,6 +27,10 @@ class Bat_EV_SDL_V3_Beta extends IPSModule $this->RegisterVariableInteger("P_EV_laden", "P EV laden max (W)", "", 31); $this->RegisterVariableInteger("P_EV_entladen", "P EV entladen max (W)", "", 32); $this->RegisterVariableFloat("SDL_Start_Pos", "SDL Start SoC (%)", "", 35); + + $this->RegisterVariableBoolean("SDL_Reset", "SDL Konto Reset (Start)", "~Switch", 36); + $this->EnableAction("SDL_Reset"); + // Debug $this->RegisterVariableString("CalcJSON", "Berechnung (JSON)", "", 99); // Timer: wichtig -> Prefix muss passen @@ -42,6 +46,7 @@ class Bat_EV_SDL_V3_Beta extends IPSModule $this->Update(); } + public function RequestAction($Ident, $Value) { if ($Ident === "State") { @@ -51,8 +56,21 @@ class Bat_EV_SDL_V3_Beta extends IPSModule } return; } + + if ($Ident === "SDL_Reset") { + if ((bool)$Value) { + $this->ResetSDLToStartPoint(); // setzt Buffer auf Startzustand + SetValue($this->GetIDForIdent("SDL_Reset"), false); // Schalter automatisch zurück + $this->Update(); // optional: sofort Anzeige aktualisieren + } else { + SetValue($this->GetIDForIdent("SDL_Reset"), false); + } + return; + } + throw new Exception("Invalid Ident: " . $Ident); } + public function Update() { $semKey = 'BatEVSDL_Update_' . $this->InstanceID; @@ -972,6 +990,49 @@ private function WriteByVendorRegistersSingleMode(string $typ, array $cfg, float } +private function ResetSDLToStartPoint(): void +{ + // Cache sicherstellen (damit underKWh/SDL_kWh_total aktuell sind) + $this->BuildBatteryCache(true); + + $cache = json_decode($this->GetBufferSafe("BatCacheJSON"), true); + if (!is_array($cache) || empty($cache["bats"])) { + $this->SendDebug("SDL_Reset", "Cache leer/invalid", 0); + return; + } + + $sdlKWhTotal = 0.0; + $startKWh = 0.0; + + foreach ($cache["bats"] as $bat) { + $sdlKWhTotal += (float)($bat["SDL_kWh_total"] ?? 0.0); + $startKWh += (float)($bat["underKWh"] ?? 0.0); + } + + if ($sdlKWhTotal <= 0.0) { + $this->SendDebug("SDL_Reset", "SDL_kWh_total = 0", 0); + return; + } + + // Startzustand = Summe underKWh (bei symmetrisch => exakt 50%) + $startKWh = max(0.0, min($sdlKWhTotal, $startKWh)); + + $this->SetBuffer("Int_E_SDL_kWh", (string)$startKWh); + $this->SetBuffer("Int_Init_SDL", "1"); + $this->SetBuffer("Int_LastTs", (string)microtime(true)); + + // Hash merken, damit dein "ApplyChanges-Resync" nicht direkt wieder reinfunkt + $cacheHash = (string)$this->GetBufferSafe("BatCacheHash"); + if ($cacheHash !== "") { + $this->SetBuffer("Int_CFG_HASH", $cacheHash); + } + + // Optional: direkt Debug ausgeben + $startPct = ($sdlKWhTotal > 0.0) ? ($startKWh / $sdlKWhTotal * 100.0) : 0.0; + $this->SendDebug("SDL_Reset", "StartKWh=" . round($startKWh,3) . " (" . round($startPct,3) . "%)", 0); +} +`` + private function GetTotalBatteryPowerIstW(): float {