From 18cbf73cd0a76e98e2a207ef27b0b084c744a4a8 Mon Sep 17 00:00:00 2001 From: "belevo\\mh" Date: Thu, 22 Jan 2026 16:57:48 +0100 Subject: [PATCH] no message --- Bat_EV_SDL/module.php | 104 ++++++++++++++++-------------------------- 1 file changed, 40 insertions(+), 64 deletions(-) diff --git a/Bat_EV_SDL/module.php b/Bat_EV_SDL/module.php index 4a5e3cc..fbc6acf 100644 --- a/Bat_EV_SDL/module.php +++ b/Bat_EV_SDL/module.php @@ -6,20 +6,16 @@ class Bat_EV_SDL extends IPSModule { parent::Create(); - // Properties $this->RegisterPropertyString("Batteries", "[]"); $this->RegisterPropertyInteger("SDL_Leistung", 0); // W $this->RegisterPropertyInteger("UpdateInterval", 5); // Minuten - // Status $this->RegisterVariableBoolean("State", "Aktiv", "~Switch", 1); $this->EnableAction("State"); - // Fenster-Zustände $this->RegisterVariableFloat("SDL_Pos", "SDL Fensterposition (%)", "", 10); $this->RegisterVariableFloat("SoC_EV", "EV Fenster-Füllstand (%)", "", 11); - // Leistungsgrenzen $this->RegisterVariableFloat("P_SDL_max", "SDL max (W)", "", 20); $this->RegisterVariableFloat("P_SDL_laden", "P SDL laden max (W)", "", 21); $this->RegisterVariableFloat("P_SDL_entladen", "P SDL entladen max (W)", "", 22); @@ -28,10 +24,8 @@ class Bat_EV_SDL extends IPSModule $this->RegisterVariableFloat("P_EV_laden", "P EV laden max (W)", "", 31); $this->RegisterVariableFloat("P_EV_entladen", "P EV entladen max (W)", "", 32); - // Debug $this->RegisterVariableString("CalcJSON", "Berechnung (JSON)", "", 99); - // Timer (Prefix: GEF) $this->RegisterTimer("UpdateTimer", 0, 'GEF_Update($_IPS["TARGET"]);'); } @@ -40,11 +34,7 @@ class Bat_EV_SDL extends IPSModule parent::ApplyChanges(); $intervalMin = (int)$this->ReadPropertyInteger("UpdateInterval"); - if ($intervalMin > 0) { - $this->SetTimerInterval("UpdateTimer", $intervalMin * 60 * 1000); - } else { - $this->SetTimerInterval("UpdateTimer", 0); - } + $this->SetTimerInterval("UpdateTimer", ($intervalMin > 0) ? $intervalMin * 60 * 1000 : 0); $this->Update(); } @@ -53,32 +43,21 @@ class Bat_EV_SDL extends IPSModule { if ($Ident === "State") { SetValue($this->GetIDForIdent("State"), (bool)$Value); - if ((bool)$Value) { - $this->Update(); - } + if ((bool)$Value) $this->Update(); return; } throw new Exception("Invalid Ident: " . $Ident); } - /** - * Fenster-Modell: - * Emin = Ereq, Emax = Cap - Ereq - * EV darf nur zwischen Emin..Emax arbeiten - */ public function Update() { - if (!GetValue($this->GetIDForIdent("State"))) { - return; - } + if (!GetValue($this->GetIDForIdent("State"))) return; $batteries = json_decode($this->ReadPropertyString("Batteries"), true); - if (!is_array($batteries) || count($batteries) === 0) { - return; - } + if (!is_array($batteries) || count($batteries) === 0) return; $sdlPowerW = max(0, (int)$this->ReadPropertyInteger("SDL_Leistung")); - $reserveH = 0.5; // 30 Minuten fix + $reserveH = 0.5; $eps = 0.0001; // Summe Max-Leistungen @@ -88,25 +67,18 @@ class Bat_EV_SDL extends IPSModule if ($p > 0) $sumBatPowerW += $p; } if ($sumBatPowerW <= 0) return; - - // SDL begrenzen if ($sdlPowerW > $sumBatPowerW) $sdlPowerW = $sumBatPowerW; // Summen Leistung - $P_SDL_max = 0.0; - $P_EV_max = 0.0; - $P_SDL_laden = 0.0; - $P_SDL_entladen = 0.0; - $P_EV_laden = 0.0; - $P_EV_entladen = 0.0; + $P_SDL_max = 0.0; $P_EV_max = 0.0; + $P_SDL_laden = 0.0; $P_SDL_entladen = 0.0; + $P_EV_laden = 0.0; $P_EV_entladen = 0.0; - // EV-Fenster-Füllstand (systemweit gewichtet) - $sumEV = 0.0; - $sumEVcap = 0.0; + // EV-Füllstand (gewichtet über FensterkWh) + $sumEV = 0.0; $sumEVcap = 0.0; - // SDL Fensterposition (JETZT GEWICHTET nach Fenstergröße) - $sumPosWeighted = 0.0; - $sumPosWeight = 0.0; + // SDL_Pos (gewichtet über pSDL!) + $sumPos_pSDL = 0.0; $sum_pSDL = 0.0; $calc = []; @@ -123,53 +95,59 @@ class Bat_EV_SDL extends IPSModule // SDL-Leistungsanteil $pSDL = min(($sdlPowerW * $pBat) / $sumBatPowerW, (float)$pBat); if ($pSDL < 0) $pSDL = 0.0; - - $pEV = max(0.0, $pBat - $pSDL); + $pEV = max(0.0, $pBat - $pSDL); $P_SDL_max += $pSDL; $P_EV_max += $pEV; - // Reserveenergie für 30 min + // Reserveenergie 30min $Ereq = ($pSDL * $reserveH) / 1000.0; // Fenster $Emin = $Ereq; $Emax = $cap - $Ereq; - $range = $Emax - $Emin; // = cap - 2*Ereq + $range = $Emax - $Emin; $EVcap = max(0.0, $range); // SDL möglich? - if ($E >= $Emin) $P_SDL_entladen += $pSDL; - if (($cap - $E) >= $Ereq) $P_SDL_laden += $pSDL; + $canSDLdis = ($E + $eps) >= $Emin; + $canSDLch = (($cap - $E) + $eps) >= $Ereq; + + if ($canSDLdis) $P_SDL_entladen += $pSDL; + if ($canSDLch) $P_SDL_laden += $pSDL; // EV möglich? - if ($E > $Emin) $P_EV_entladen += $pEV; - if ($E < $Emax) $P_EV_laden += $pEV; + if ($E > $Emin + $eps) $P_EV_entladen += $pEV; + if ($E < $Emax - $eps) $P_EV_laden += $pEV; - // EV Energie im Fenster (für SoC_EV) + // EV-Füllstand $EV = 0.0; + $EVpct = 0.0; if ($EVcap > $eps) { $EV = max(0.0, min($E - $Emin, $EVcap)); $sumEV += $EV; $sumEVcap += $EVcap; + $EVpct = ($EV / $EVcap) * 100.0; } - // SDL Fensterposition (0..100), GEWICHTET nach EVcap - if ($EVcap > $eps) { - $pos = ($E - $Emin) / $EVcap; // 0..1 + // SDL Fensterposition (0..100) und gewichtete Summe über pSDL + $posPct = 0.0; + if ($range > $eps) { + $pos = ($E - $Emin) / $range; // 0..1 $pos = max(0.0, min(1.0, $pos)); $posPct = $pos * 100.0; + } - $sumPosWeighted += $posPct * $EVcap; - $sumPosWeight += $EVcap; - } else { - $posPct = 0.0; + if ($pSDL > $eps) { + $sumPos_pSDL += $posPct * $pSDL; + $sum_pSDL += $pSDL; } $calc[] = [ "typ" => $typ, "soc_pct" => round($socPct, 2), "E_kWh" => round($E, 3), + "cap_kWh" => round($cap, 3), "pSDL_W" => round($pSDL, 0), "pEV_W" => round($pEV, 0), @@ -179,12 +157,15 @@ class Bat_EV_SDL extends IPSModule "Emax_kWh" => round($Emax, 3), "EVcap_kWh" => round($EVcap, 3), - "SDL_pos_pct" => round($posPct, 2) + "SDL_pos_pct" => round($posPct, 2), + "EV_fill_pct" => round($EVpct, 2), + + "canSDLcharge" => $canSDLch, + "canSDLdischarge" => $canSDLdis ]; } - // Ergebnisse - $SDL_Pos = ($sumPosWeight > $eps) ? ($sumPosWeighted / $sumPosWeight) : 0.0; + $SDL_Pos = ($sum_pSDL > $eps) ? ($sumPos_pSDL / $sum_pSDL) : 0.0; $SoC_EV = ($sumEVcap > $eps) ? (($sumEV / $sumEVcap) * 100.0) : 0.0; SetValue($this->GetIDForIdent("SDL_Pos"), round($SDL_Pos, 2)); @@ -201,19 +182,14 @@ class Bat_EV_SDL extends IPSModule SetValue($this->GetIDForIdent("CalcJSON"), json_encode($calc, JSON_PRETTY_PRINT)); } - /** - * SoC in Prozent (0..100). Akzeptiert 0..1 als 0..100. - */ private function ReadSocPercent(int $varId): float { if ($varId <= 0 || !IPS_VariableExists($varId)) return 0.0; - $v = GetValue($varId); if (!is_numeric($v)) return 0.0; $f = (float)$v; if ($f >= 0.0 && $f <= 1.0) $f *= 100.0; - return max(0.0, min(100.0, $f)); } }