diff --git a/Bat_EV_SDL_V3_Beta/module.php b/Bat_EV_SDL_V3_Beta/module.php index 816e82e..b0665f4 100644 --- a/Bat_EV_SDL_V3_Beta/module.php +++ b/Bat_EV_SDL_V3_Beta/module.php @@ -105,7 +105,7 @@ public function Update() } $dtSec = $now - $lastTs; - if ($dtSec < 0) $dtSec = 0.0; + if ($dtSec < 0.0) $dtSec = 0.0; if ($dtSec > 3600) $dtSec = 3600.0; $dtH = $dtSec / 3600.0; @@ -160,6 +160,7 @@ public function Update() $real_kWh_ev = 0.0; $real_kWh_sdl = 0.0; + // 3 Fälle if ($underKWh <= $real_kWh && $real_kWh <= $upKWh) { $win = ($upKWh - $underKWh); @@ -184,7 +185,7 @@ public function Update() $sdlDisKW = $sdlShareKW_entladen; $evDisKW = $evShareKW_entladen; $sdlChKW = $sdlShareKW_laden; - $evChKW = 0.0; // ✅ EV darf oben nicht laden + $evChKW = 0.0; // EV darf oben nicht laden $real_kWh_ev = $capKWh - ($capKWh - $upKWh + $underKWh); $real_kWh_sdl = ($capKWh - $upKWh + $underKWh) - ($capKWh - $real_kWh); @@ -194,7 +195,7 @@ public function Update() $EV_SOC = 0.0; $sdlDisKW = $sdlShareKW_entladen; - $evDisKW = 0.0; // ✅ EV darf unten nicht entladen + $evDisKW = 0.0; // EV darf unten nicht entladen $sdlChKW = $sdlShareKW_laden; $evChKW = $evShareKW_laden; @@ -205,6 +206,7 @@ public function Update() $real_kWh_ev = max(0.0, $real_kWh_ev); $real_kWh_sdl = max(0.0, $real_kWh_sdl); + // Summen $totalCapKWh += $capKWh; $sdlDisKW_ges += $sdlDisKW; @@ -238,7 +240,7 @@ public function Update() ]; } - // Maximalleistungen setzen (für Anzeige/Debug) + // Maximalleistungen setzen (Anzeige/Debug) $this->SetIdentValue("P_SDL_laden", round($sdlChKW_ges * 1000.0, 0)); $this->SetIdentValue("P_SDL_entladen", round($sdlDisKW_ges * 1000.0, 0)); $this->SetIdentValue("P_EV_laden", round($evChKW_ges * 1000.0, 0)); @@ -246,14 +248,12 @@ public function Update() $this->SetIdentValue("CalcJSON", json_encode($calc, JSON_PRETTY_PRINT)); - // ------------------------------------------------- // Setpoints berechnen/schreiben (setzt auch "Aktuelle_Leistung_*") - // ------------------------------------------------- $this->ApplySetpoints(); - // ------------------------------------------------- - // Jetzt dynamisch integrieren mit "Aktueller Leistung" (Ist-basiert) - // ------------------------------------------------- + // ----------------------------- + // Dynamisch integrieren mit "Aktueller Leistung" (Ist-basiert) + // ----------------------------- $pEvSoll = (float)GetValue($this->GetIDForIdent("Nennleistung_Soll_EV")); $pSdlSoll = (float)GetValue($this->GetIDForIdent("Nennleistung_Soll_SDL")); @@ -265,10 +265,51 @@ public function Update() $PSDL = (float)GetValue($this->GetIDForIdent("Aktuelle_Leistung_SDL")); // Clamp auf lokale Maxima (kW->W) + $maxEV_ch = $evChKW_ges * 1000.0; + $maxEV_dis = $evDisKW_ges * 1000.0; + $maxSDL_ch = $sdlChKW_ges * 1000.0; + $maxSDL_dis = $sdlDisKW_ges * 1000.0; + $PEV = max(-$maxEV_dis, min($maxEV_ch, $PEV)); + $PSDL = max(-$maxSDL_dis, min($maxSDL_ch, $PSDL)); + + if ($evActive) { + $Eev += (($PEV / 1000.0) * $dtH); + } else { + $Eev = $real_kWh_ev_ges; + } + + if ($sdlActive) { + $Esdl += (($PSDL / 1000.0) * $dtH); + } else { + $Esdl = $real_kWh_sdl_ges; + } + + // Clamp Energiekonten + $Eev = ($EV_kWh_ges > 0.0) ? max(0.0, min($EV_kWh_ges, $Eev)) : 0.0; + $Esdl = ($SDL_kWh_ges > 0.0) ? max(0.0, min($SDL_kWh_ges, $Esdl)) : 0.0; + + // Prozentwerte + $evPosPct = ($EV_kWh_ges > 0.0) ? ($Eev / $EV_kWh_ges * 100.0) : 0.0; + $sdlPosPct = ($SDL_kWh_ges > 0.0) ? ($Esdl / $SDL_kWh_ges * 100.0) : 0.0; + + $this->SetIdentValue("SoC_EV", round($evPosPct, 3)); + $this->SetIdentValue("SDL_Pos", round($sdlPosPct, 3)); + + // Integrator speichern + $this->SetBuffer("Int_LastTs", (string)$now); + $this->SetBuffer("Int_E_EV_kWh", (string)$Eev); + $this->SetBuffer("Int_E_SDL_kWh", (string)$Esdl); + + } catch (Throwable $e) { + $this->SendDebug("Update ERROR", $e->getMessage() . " @ " . $e->getFile() . ":" . $e->getLine(), 0); + $this->WriteAllZero("Exception: " . $e->getMessage()); + } finally { + IPS_SemaphoreLeave($semKey); } - - } +} + + private function BuildBatteryCache(bool $force): void { @@ -375,6 +416,9 @@ public function Update() $this->SendDebug("Cache", "Battery cache rebuilt (" . count($cache["bats"]) . " bats)", 0); } + + + private function GetBufferSafe(string $name): string { $v = $this->GetBuffer($name); @@ -639,6 +683,8 @@ private function CalculateBatteryDistribution(float $pEvW, float $pSdlW): array $this->SetIdentValue("Aktuelle_Leistung_EV", (float)$aktEV); $this->SetIdentValue("Aktuelle_Leistung_SDL", (float)$aktSDL); + + return $finalOutput; }