From b79e0d143020b4a19cc36bcc2f56877ab01517a5 Mon Sep 17 00:00:00 2001 From: "belevo\\mh" Date: Fri, 23 Jan 2026 10:31:11 +0100 Subject: [PATCH] no message --- Bat_EV_SDL/module.php | 476 +++++++++++++++++++++++++----------------- 1 file changed, 282 insertions(+), 194 deletions(-) diff --git a/Bat_EV_SDL/module.php b/Bat_EV_SDL/module.php index 284ed9b..7f7a82a 100644 --- a/Bat_EV_SDL/module.php +++ b/Bat_EV_SDL/module.php @@ -1,38 +1,47 @@ - RegisterPropertyString("Batteries", "[]"); $this->RegisterPropertyInteger("SDL_Leistung", 0); // W $this->RegisterPropertyInteger("UpdateInterval", 5); // Minuten + // Status $this->RegisterVariableBoolean("State", "Aktiv", "~Switch", 1); $this->EnableAction("State"); + // Prozentwerte $this->RegisterVariableFloat("SDL_Pos", "SDL Energie verfügbar (%)", "", 10); $this->RegisterVariableFloat("SoC_EV", "EV Energie verfügbar (%)", "", 11); + // Leistungsgrenzen - $this->RegisterVariableFloat("P_SDL_laden", "P SDL laden max (W)", "", 21); $this->RegisterVariableFloat("P_SDL_entladen", "P SDL entladen max (W)", "", 22); $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 + + // Timer (korrekt: Klassenname_Update) $this->RegisterTimer("UpdateTimer", 0, 'Bat_EV_SDL_Update($_IPS["TARGET"]);'); } + public function ApplyChanges() { parent::ApplyChanges(); + $intervalMin = (int)$this->ReadPropertyInteger("UpdateInterval"); $this->SetTimerInterval("UpdateTimer", ($intervalMin > 0) ? $intervalMin * 60 * 1000 : 0); + $this->Update(); } + public function RequestAction($Ident, $Value) { if ($Ident === "State") { @@ -42,217 +51,288 @@ class Bat_EV_SDL extends IPSModule } return; } + throw new Exception("Invalid Ident: " . $Ident); } + public function Update() { - if (!GetValue($this->GetIDForIdent("State"))) { - return; - } - $batteries = json_decode($this->ReadPropertyString("Batteries"), true); - if (!is_array($batteries)) { - $batteries = []; - } - $sdlTotalW = (float)$this->ReadPropertyInteger("SDL_Leistung"); // W - if ($sdlTotalW < 0) $sdlTotalW = 0; - $sdlTotalkW = $sdlTotalW/1000; - // 30 Minuten Fenster - $hours = 0.5; - // Summe Batterie-Maxleistungen - $sumBatPowerW = 0.0; - foreach ($batteries as $b) { - $p = (float)($b["powerbat"] ?? 0); - if ($p > 0) $sumBatPowerW += $p; - } - if ($sumBatPowerW <= 0.0) { - $this->WriteAllZero("sumBatPowerW=0"); - return; - } - $sumBatPowerkW = $sumBatPowerW/1000; + // Damit dir ein Fehler nicht wieder als -32603 "durchschlägt" + try { - //-------------------------------------------- - $real_kWh = 0.0; - //------------------------------- - $calc = [ - "inputs" => [ - "SDL_Leistung_W" => round($sdlTotalW, 0), - "SumBatPower_W" => round($sumBatPowerW, 0), - "hours" => $hours - ], - "batteries" => [], - "total" => [] - ]; - // Meine initalisierte Variablen - $EV_SOC = 0.0; - $SDL_SOC =0.0; - $sdlDisKW = 0.0; // SDL- - $evDisKW = 0.0; // EV- - $sdlChKW = 0.0; // SDL+ - $evChKW = 0.0; //EV+ - $sdlDisKW_ges = 0.0; // SDL- gesamt - $evDisKW_ges = 0.0; // EV- gesamt - $sdlChKW_ges = 0.0; // SDL+ gesamt - $evChKW_ges = 0.0; //EV+ gesamt - $real_kWh_ev_ges = 0.0; - $real_kWh_sdl_ges = 0.0; - $SDL_kWh_ges = 0.0; - $EV_kWh_ges = 0.0; - $real_kWh_ev = 0.0; - $real_kWh_sdl = 0.0; - $EV_SOC = 0.0; - $SDL_SOC = 0.0; - $sdlDisKW = 0.0; - $evDisKW = 0.0; - $sdlChKW = 0.0; - $evChKW = 0.0; - $totalCapKWh = 0.0; - foreach ($batteries as $idx => $b) { - $pBatW = max(0.0, (float)($b["powerbat"] ?? 0)); - $pBatkW = $pBatW/1000; - $capKWh = max(0.0, (float)($b["capazity"] ?? 0)); - // soc ist SelectVariable -> Variable-ID - $socVarId = (int)($b["soc"] ?? 0); - $socPct = $this->ReadSocPercent($socVarId); - - - //--------------Meine Berechnung, so wie ich es möchte------------------------ - - $sdlShareKW = $sdlTotalkW/$sumBatPowerkW*$pBatkW; // SDL Power - $evShareKW = $pBatkW - $sdlShareKW ; // EV Power - $underKWh = $sdlShareKW * $hours;// untere GRenze für SDL - $underKWh = max(0.0, min($underKWh, $capKWh / 2.0)); - $upKWh = $capKWh - $underKWh ; // obere Grenze für SDL - $SDL_kWh = 2*$underKWh; //SDL kWh - //$EV_kWH = $capKWh - 2*$underKWh; // EV kWh - $EV_kWH = max(0.0, $capKWh - $SDL_kWh); - $real_kWh = $capKWh/100 * $socPct;// Jetziger Füllstand in kWh - - if ($underKWh < $real_kWh && $upKWh > $real_kWh ){ - $SDL_SOC = 50; - $EV_SOC = ($real_kWh - $underKWh) * 100 / $capKWh; - $sdlDisKW = $sdlShareKW; // SDL- - $evDisKW = $evShareKW; // EV- - $sdlChKW = $sdlShareKW; // SDL+ - $evChKW = $evShareKW; //EV+ - // Reale kWh für EV und SDl - $real_kWh_ev = $real_kWh - $underKWh; - $real_kWh_sdl = $underKWh; - } elseif ($upKWh < $real_kWh) { - $EV_SOC = 100; - - //$SDL_SOC = (2*$underKWh) / ($capKWh - $real_kWh + $underKWh) * 100; - $den = ($capKWh - $real_kWh + $underKWh); - $SDL_SOC = ($den > 0) ? ((2*$underKWh) / $den * 100.0) : 0.0; - - $sdlDisKW = $sdlShareKW; // SDL- - $evDisKW = $evShareKW; // EV- - $sdlChKW = $sdlShareKW; // SDL+ - $evChKW = 0; //EV+ - // Reale kWh für EV und SDl - $real_kWh_ev = $real_kWh - 2*$underKWh; - $real_kWh_sdl = (2 * $underKWh) - ($capKWh - $real_kWh); - } elseif ($underKWh > $real_kWh) { - $EV_SOC = 0; - $den = 2 * $underKWh; - $SDL_SOC = ($den > 0) ? ($real_kWh * 100 / $den) : 0.0; - $sdlDisKW = $sdlShareKW; // SDL- - $evDisKW = 0; // EV- - $sdlChKW = $sdlShareKW; // SDL+ - $evChKW = $evShareKW; //EV+ - // Reale kWh für EV und SDl - $real_kWh_ev = 0; - $real_kWh_sdl = $real_kWh; + if (!GetValue($this->GetIDForIdent("State"))) { + return; } - - // Null SoC für sdl Power abdecken - if ($real_kWh <= 0) { - $sdlDisKW = 0; - // Reale kWh für EV und SDl - $real_kWh_ev = 0; - $real_kWh_sdl = 0; - } elseif ($real_kWh >= $capKWh) { - $sdlChKW = 0; - // Reale kWh für EV und SDl - $real_kWh_ev = $capKWh - 2* $underKWh; - $real_kWh_sdl = 2 * $underKWh ; + + $batteries = json_decode($this->ReadPropertyString("Batteries"), true); + if (!is_array($batteries)) { + $batteries = []; } - $totalCapKWh += $capKWh; - // Berechnungen für Leistungen laden und entladen gesamt haft - $sdlDisKW_ges += $sdlDisKW; - $evDisKW_ges += $evDisKW; - $sdlChKW_ges += $sdlChKW; - $evChKW_ges += $evChKW; - $real_kWh_ev_ges += $real_kWh_ev; - $real_kWh_sdl_ges += $real_kWh_sdl ; - // gesamte kWh über alle Batterien für EV und SDL berechnet - $SDL_kWh_ges += $SDL_kWh; //SDL kWh geamt - $EV_kWh_ges += $EV_kWH; // EV kWh - //-------------------------------------------------------------------------------- - // von mir angepasst - $calc["batteries"][] = [ - "idx" => $idx, - "typ" => (string)($b["typ"] ?? ("Bat " . ($idx + 1))), - "P_bat_W" => round($pBatW, 0), - "Cap_kWh" => round($capKWh, 3), - "SoC_varId" => $socVarId, - "SoC_pct" => round($socPct, 3), - - "EV_SOC"=> round($EV_SOC, 3), - "SDL_SOC"=> round($SDL_SOC, 3), - - "EV_kWh"=> round($real_kWh_ev, 3), - "SDL_kWh"=> round($real_kWh_sdl, 3), - "SDL_Power_kW" => round($sdlShareKW, 3), - "EV_Power_kW" => round($evShareKW, 3), - "under_grenze_kWh" => round($underKWh, 3), - "up_grenze_kWh" => round($upKWh, 3), - "SDL_Charge_kW" => round($sdlChKW, 3), - "SDL_Discharge_kW" => round($sdlDisKW, 3), - "EV_Charge_kW" => round($evChKW, 3), - "EV_Discharge_kW" => round($evDisKW, 3), + + $sdlTotalW = (float)$this->ReadPropertyInteger("SDL_Leistung"); // W + if ($sdlTotalW < 0) $sdlTotalW = 0; + + $sdlTotalkW = $sdlTotalW / 1000.0; + + // 30 Minuten Fenster + $hours = 0.5; + + // Summe Batterie-Maxleistungen + $sumBatPowerW = 0.0; + foreach ($batteries as $b) { + $p = (float)($b["powerbat"] ?? 0); + if ($p > 0) $sumBatPowerW += $p; + } + + if ($sumBatPowerW <= 0.0) { + $this->WriteAllZero("sumBatPowerW=0"); + return; + } + + $sumBatPowerkW = $sumBatPowerW / 1000.0; + + $calc = [ + "inputs" => [ + "SDL_Leistung_W" => round($sdlTotalW, 0), + "SumBatPower_W" => round($sumBatPowerW, 0), + "hours" => $hours + ], + "batteries" => [], + "total" => [] ]; + + // Summen + $sdlDisKW_ges = 0.0; + $evDisKW_ges = 0.0; + $sdlChKW_ges = 0.0; + $evChKW_ges = 0.0; + + $real_kWh_ev_ges = 0.0; + $real_kWh_sdl_ges = 0.0; + + $SDL_kWh_ges = 0.0; + $EV_kWh_ges = 0.0; + $totalCapKWh = 0.0; + + foreach ($batteries as $idx => $b) { + + $pBatW = max(0.0, (float)($b["powerbat"] ?? 0)); + $pBatkW = $pBatW / 1000.0; + + $capKWh = max(0.0, (float)($b["capazity"] ?? 0)); + + // soc ist SelectVariable -> Variable-ID + $socVarId = (int)($b["soc"] ?? 0); + $socPct = $this->ReadSocPercent($socVarId); + + // Defaults pro Batterie (wichtig, damit nix "hängenbleibt") + $EV_SOC = 0.0; + $SDL_SOC = 0.0; + + $sdlDisKW = 0.0; + $evDisKW = 0.0; + $sdlChKW = 0.0; + $evChKW = 0.0; + + $real_kWh_ev = 0.0; + $real_kWh_sdl = 0.0; + + // Wenn Kapazität 0 ist: Batterie überspringen (sonst Divisionen/Unsinn) + if ($capKWh <= 0.0) { + $calc["batteries"][] = [ + "idx" => $idx, + "typ" => (string)($b["typ"] ?? ("Bat " . ($idx + 1))), + "P_bat_W" => round($pBatW, 0), + "Cap_kWh" => round($capKWh, 3), + "SoC_varId" => $socVarId, + "SoC_pct" => round($socPct, 3), + "error" => "capazity=0" + ]; + continue; + } + + // Deine Logik: SDL/EV Aufteilung über Leistungsanteil + $sdlShareKW = ($sumBatPowerkW > 0.0) ? ($sdlTotalkW / $sumBatPowerkW * $pBatkW) : 0.0; + $evShareKW = $pBatkW - $sdlShareKW; + + // untere Grenze für SDL (wichtig: zuerst setzen, dann clamp) + $underKWh = $sdlShareKW * $hours; + $underKWh = max(0.0, min($underKWh, $capKWh / 2.0)); + + $upKWh = $capKWh - $underKWh; + $SDL_kWh = 2.0 * $underKWh; + $EV_kWH = max(0.0, $capKWh - $SDL_kWh); + + $real_kWh = $capKWh / 100.0 * $socPct; + + // --- Deine 3 Fälle (minimal abgesichert, keine Änderung der Grundfunktion) --- + if ($underKWh < $real_kWh && $upKWh > $real_kWh) { + + $SDL_SOC = 50.0; + $EV_SOC = ($capKWh > 0.0) ? (($real_kWh - $underKWh) * 100.0 / $capKWh) : 0.0; + + $sdlDisKW = $sdlShareKW; + $evDisKW = $evShareKW; + $sdlChKW = $sdlShareKW; + $evChKW = $evShareKW; + + $real_kWh_ev = $real_kWh - $underKWh; + $real_kWh_sdl = $underKWh; + + } elseif ($upKWh < $real_kWh) { + + $EV_SOC = 100.0; + + // Division absichern + $den = ($capKWh - $real_kWh + $underKWh); + $SDL_SOC = ($den > 0.0) ? ((2.0 * $underKWh) / $den * 100.0) : 0.0; + + $sdlDisKW = $sdlShareKW; + $evDisKW = $evShareKW; + $sdlChKW = $sdlShareKW; + $evChKW = 0.0; + + $real_kWh_ev = $real_kWh - 2.0 * $underKWh; + $real_kWh_sdl = (2.0 * $underKWh) - ($capKWh - $real_kWh); + + } elseif ($underKWh > $real_kWh) { + + $EV_SOC = 0.0; + + $den = 2.0 * $underKWh; + $SDL_SOC = ($den > 0.0) ? ($real_kWh * 100.0 / $den) : 0.0; + + $sdlDisKW = $sdlShareKW; + $evDisKW = 0.0; + $sdlChKW = $sdlShareKW; + $evChKW = $evShareKW; + + $real_kWh_ev = 0.0; + $real_kWh_sdl = $real_kWh; + + } else { + // Grenzfall (==): damit nie "uninitialisiert/komisch" + // Verhalten: neutral, ohne deine Logik zu verbiegen + $SDL_SOC = 50.0; + $EV_SOC = ($capKWh > 0.0) ? (($real_kWh - $underKWh) * 100.0 / $capKWh) : 0.0; + + $sdlDisKW = $sdlShareKW; + $evDisKW = $evShareKW; + $sdlChKW = $sdlShareKW; + $evChKW = $evShareKW; + + $real_kWh_ev = max(0.0, $real_kWh - $underKWh); + $real_kWh_sdl = min($real_kWh, $underKWh); + } + + // Null/Full Abdeckung (dein Verhalten beibehalten, aber clampen) + if ($real_kWh <= 0.0) { + $sdlDisKW = 0.0; + $real_kWh_ev = 0.0; + $real_kWh_sdl = 0.0; + } elseif ($real_kWh >= $capKWh) { + $sdlChKW = 0.0; + + $real_kWh_ev = $capKWh - 2.0 * $underKWh; + $real_kWh_sdl = 2.0 * $underKWh; + } + + // Negative "real"-kWh verhindern (nur Sicherheit, sonst knallt später %) + $real_kWh_ev = max(0.0, $real_kWh_ev); + $real_kWh_sdl = max(0.0, $real_kWh_sdl); + + // Summen + $totalCapKWh += $capKWh; + + $sdlDisKW_ges += $sdlDisKW; + $evDisKW_ges += $evDisKW; + $sdlChKW_ges += $sdlChKW; + $evChKW_ges += $evChKW; + + $real_kWh_ev_ges += $real_kWh_ev; + $real_kWh_sdl_ges += $real_kWh_sdl; + + $SDL_kWh_ges += $SDL_kWh; + $EV_kWh_ges += $EV_kWH; + + // Debug pro Batterie + $calc["batteries"][] = [ + "idx" => $idx, + "typ" => (string)($b["typ"] ?? ("Bat " . ($idx + 1))), + "P_bat_W" => round($pBatW, 0), + "Cap_kWh" => round($capKWh, 3), + "SoC_varId" => $socVarId, + "SoC_pct" => round($socPct, 3), + + "EV_SOC" => round($EV_SOC, 3), + "SDL_SOC" => round($SDL_SOC, 3), + + "EV_kWh" => round($real_kWh_ev, 3), + "SDL_kWh" => round($real_kWh_sdl, 3), + + "SDL_Power_kW" => round($sdlShareKW, 3), + "EV_Power_kW" => round($evShareKW, 3), + + "under_grenze_kWh" => round($underKWh, 3), + "up_grenze_kWh" => round($upKWh, 3), + + "SDL_Charge_kW" => round($sdlChKW, 3), + "SDL_Discharge_kW" => round($sdlDisKW, 3), + "EV_Charge_kW" => round($evChKW, 3), + "EV_Discharge_kW" => round($evDisKW, 3), + ]; + } + + // Prozent gesamt (mit Guards) + $evPosPct = ($EV_kWh_ges > 0.0) ? ($real_kWh_ev_ges / $EV_kWh_ges * 100.0) : 0.0; + $sdlPosPct = ($SDL_kWh_ges > 0.0) ? ($real_kWh_sdl_ges / $SDL_kWh_ges * 100.0) : 0.0; + + // Werte setzen + $this->SetIdentValue("SDL_Pos", round($sdlPosPct, 3)); + $this->SetIdentValue("SoC_EV", round($evPosPct, 3)); + + $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)); + $this->SetIdentValue("P_EV_entladen", round($evDisKW_ges * 1000.0, 0)); + + $calc["total"] = [ + "SDL_SoC_pct" => round($sdlPosPct, 3), + "EV_SoC_pct" => round($evPosPct, 3), + "SDL_kWh_pct" => round($SDL_kWh_ges, 3), + "EV_kWh_pct" => round($EV_kWh_ges, 3), + "SDL_Charge_kW" => round($sdlChKW_ges, 3), + "SDL_Discharge_kW" => round($sdlDisKW_ges, 3), + "EV_Charge_kW" => round($evChKW_ges, 3), + "EV_Discharge_kW" => round($evDisKW_ges, 3), + "totalCap_kWh" => round($totalCapKWh, 3) + ]; + + $this->SetIdentValue("CalcJSON", json_encode($calc, JSON_PRETTY_PRINT)); + + } catch (Throwable $e) { + // Damit du den echten Fehler siehst (statt -32603) + $this->SendDebug("Update ERROR", $e->getMessage() . " @ " . $e->getFile() . ":" . $e->getLine(), 0); + $this->WriteAllZero("Exception: " . $e->getMessage()); } - - - //$evPosPct = $real_kWh_ev_ges/ $EV_kWh_ges *100 ; - //$sdlPosPct = $real_kWh_sdl_ges/ $SDL_kWh_ges *100 ; - $evPosPct = ($EV_kWh_ges > 0) ? ($real_kWh_ev_ges / $EV_kWh_ges * 100.0) : 0.0; - $sdlPosPct = ($SDL_kWh_ges > 0) ? ($real_kWh_sdl_ges / $SDL_kWh_ges * 100.0) : 0.0; - // Variablen setzen - $this->SetIdentValue("SDL_Pos", round($sdlPosPct, 3)); - $this->SetIdentValue("SoC_EV", round($evPosPct, 3)); - // W-Variablen - - $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)); - $this->SetIdentValue("P_EV_entladen", round($evDisKW_ges * 1000.0, 0)); - $calc["total"] = [ - "SDL_SoC_pct" => round($sdlPosPct, 3), - "EV_SoC_pct" => round($evPosPct, 3), - "SDL_kWh_pct" => round($SDL_kWh_ges, 3), - "EV_kWh_pct" => round($EV_kWh_ges, 3), - "SDL_Charge_kW" => round($sdlChKW_ges, 3), - "SDL_Discharge_kW" => round($sdlDisKW_ges, 3), - "EV_Charge_kW" => round($evChKW_ges, 3), - "EV_Discharge_kW" => round($evDisKW_ges, 3), - "totalCap_kWh" => round($totalCapKWh, 3) - ]; - $this->SetIdentValue("CalcJSON", json_encode($calc, JSON_PRETTY_PRINT)); } + // ---------------- Helpers ---------------- + private function WriteAllZero(string $reason): void { $this->SetIdentValue("SDL_Pos", 0.0); $this->SetIdentValue("SoC_EV", 0.0); - + $this->SetIdentValue("P_SDL_laden", 0.0); $this->SetIdentValue("P_SDL_entladen", 0.0); $this->SetIdentValue("P_EV_laden", 0.0); $this->SetIdentValue("P_EV_entladen", 0.0); + $this->SetIdentValue("CalcJSON", json_encode(["error" => $reason], JSON_PRETTY_PRINT)); } + private function SetIdentValue(string $ident, $value): void { $id = @$this->GetIDForIdent($ident); @@ -262,27 +342,35 @@ class Bat_EV_SDL extends IPSModule } SetValue($id, $value); } + private function ReadSocPercent(int $varId): float { // Falls jemand statt Variable direkt 0..100 einträgt - if ($varId >= 0 && $varId <= 100) { + if ($varId >= 0 && $varId <= 100 && !IPS_VariableExists($varId)) { return (float)$varId; } + if ($varId <= 0 || !IPS_VariableExists($varId)) { return 0.0; } + $v = GetValue($varId); if (!is_numeric($v)) { return 0.0; } + $soc = (float)$v; + // 0..1 -> 0..100 if ($soc >= 0.0 && $soc <= 1.0) { $soc *= 100.0; } + if ($soc < 0.0) $soc = 0.0; if ($soc > 100.0) $soc = 100.0; + return $soc; } } + ?>