diff --git a/Bat_EV_SDL/module.php b/Bat_EV_SDL/module.php index fbc6acf..7dbb2d7 100644 --- a/Bat_EV_SDL/module.php +++ b/Bat_EV_SDL/module.php @@ -6,16 +6,22 @@ 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"); - $this->RegisterVariableFloat("SDL_Pos", "SDL Fensterposition (%)", "", 10); - $this->RegisterVariableFloat("SoC_EV", "EV Fenster-Füllstand (%)", "", 11); + // Prozentwerte nach deiner Skizze: + // SDL_% = verfügbare SDL-Energie / benötigte SDL-Energie + // EV_% = verfügbare EV-Energie / max. EV-Energie (SumCap - SumSDLreq) + $this->RegisterVariableFloat("SDL_Pos", "SDL Energie verfügbar (%)", "", 10); + $this->RegisterVariableFloat("SoC_EV", "EV Energie verfügbar (%)", "", 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); @@ -24,8 +30,10 @@ 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"]);'); } @@ -43,7 +51,9 @@ 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); @@ -51,34 +61,46 @@ class Bat_EV_SDL extends IPSModule 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; - $eps = 0.0001; + $reserveH = 0.5; // 30 Minuten + $eps = 0.0001; - // Summe Max-Leistungen + // Summe Batterie-Maxleistungen $sumBatPowerW = 0; foreach ($batteries as $b) { $p = (int)($b["powerbat"] ?? 0); 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; - // EV-Füllstand (gewichtet über FensterkWh) - $sumEV = 0.0; $sumEVcap = 0.0; + $P_SDL_laden = 0.0; + $P_SDL_entladen = 0.0; + $P_EV_laden = 0.0; + $P_EV_entladen = 0.0; - // SDL_Pos (gewichtet über pSDL!) - $sumPos_pSDL = 0.0; $sum_pSDL = 0.0; + // Summen Energie für Prozentberechnung (deine Logik) + $sumCap_kWh = 0.0; + + $sumSDLreq_kWh = 0.0; // Sum(E_req) + $sumSDLavail_kWh = 0.0; // Sum(min(E_ist, E_req)) + + $sumEVavail_kWh = 0.0; // Sum(max(E_ist - E_req, 0)) $calc = []; @@ -89,87 +111,78 @@ class Bat_EV_SDL extends IPSModule $cap = (float)($b["capazity"] ?? 0); if ($pBat <= 0 || $cap <= 0) continue; - $socPct = $this->ReadSocPercent((int)($b["soc"] ?? 0)); - $E = $cap * $socPct / 100.0; + $sumCap_kWh += $cap; - // SDL-Leistungsanteil + $socPct = $this->ReadSocPercent((int)($b["soc"] ?? 0)); + $E_ist = $cap * $socPct / 100.0; // kWh + + // SDL Anteil Leistung proportional $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 30min - $Ereq = ($pSDL * $reserveH) / 1000.0; + // E_req für 30 min SDL (kWh) + $E_req = ($pSDL * $reserveH) / 1000.0; - // Fenster - $Emin = $Ereq; - $Emax = $cap - $Ereq; - $range = $Emax - $Emin; - $EVcap = max(0.0, $range); + $sumSDLreq_kWh += $E_req; - // SDL möglich? - $canSDLdis = ($E + $eps) >= $Emin; - $canSDLch = (($cap - $E) + $eps) >= $Ereq; + // SDL verfügbar nach deiner Skizze: min(E_ist, E_req) + $SDL_avail = min($E_ist, $E_req); + $sumSDLavail_kWh += $SDL_avail; + + // EV verfügbar nach deiner Skizze: max(E_ist - E_req, 0) + $EV_avail = max($E_ist - $E_req, 0.0); + $sumEVavail_kWh += $EV_avail; + + // Leistungsmaxima (Richtung) nach Reservierungslogik + // SDL entladen geht nur, wenn genug Energie für 30min vorhanden + $canSDLdis = ($E_ist + $eps) >= $E_req; + + // SDL laden geht nur, wenn genug Platz für 30min vorhanden + $canSDLch = (($cap - $E_ist) + $eps) >= $E_req; if ($canSDLdis) $P_SDL_entladen += $pSDL; if ($canSDLch) $P_SDL_laden += $pSDL; - // EV möglich? - if ($E > $Emin + $eps) $P_EV_entladen += $pEV; - if ($E < $Emax - $eps) $P_EV_laden += $pEV; + // EV entladen geht nur, wenn Energie über SDL-Reserve liegt + if (($E_ist - $E_req) > $eps) $P_EV_entladen += $pEV; - // 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) 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; - } - - if ($pSDL > $eps) { - $sumPos_pSDL += $posPct * $pSDL; - $sum_pSDL += $pSDL; - } + // EV laden (einfach): solange noch Platz ist + if (($cap - $E_ist) > $eps) $P_EV_laden += $pEV; $calc[] = [ "typ" => $typ, - "soc_pct" => round($socPct, 2), - "E_kWh" => round($E, 3), + "pBat_W" => $pBat, "cap_kWh" => round($cap, 3), + "soc_pct" => round($socPct, 2), + "E_ist_kWh" => round($E_ist, 3), "pSDL_W" => round($pSDL, 0), + "E_req_kWh" => round($E_req, 3), + "SDL_avail_kWh" => round($SDL_avail, 3), + "pEV_W" => round($pEV, 0), - - "Ereq_kWh" => round($Ereq, 3), - "Emin_kWh" => round($Emin, 3), - "Emax_kWh" => round($Emax, 3), - "EVcap_kWh" => round($EVcap, 3), - - "SDL_pos_pct" => round($posPct, 2), - "EV_fill_pct" => round($EVpct, 2), + "EV_avail_kWh" => round($EV_avail, 3), "canSDLcharge" => $canSDLch, "canSDLdischarge" => $canSDLdis ]; } - $SDL_Pos = ($sum_pSDL > $eps) ? ($sumPos_pSDL / $sum_pSDL) : 0.0; - $SoC_EV = ($sumEVcap > $eps) ? (($sumEV / $sumEVcap) * 100.0) : 0.0; + // EV max Energie = SumCap - SumSDLreq (deine 77 - 16 = 61) + $EV_max_kWh = max(0.0, $sumCap_kWh - $sumSDLreq_kWh); - SetValue($this->GetIDForIdent("SDL_Pos"), round($SDL_Pos, 2)); - SetValue($this->GetIDForIdent("SoC_EV"), round($SoC_EV, 2)); + // SDL% und EV% (deine Darstellung) + $SDL_pct = ($sumSDLreq_kWh > $eps) ? ($sumSDLavail_kWh / $sumSDLreq_kWh) * 100.0 : 0.0; + $EV_pct = ($EV_max_kWh > $eps) ? ($sumEVavail_kWh / $EV_max_kWh) * 100.0 : 0.0; + + // Setzen Variablen + SetValue($this->GetIDForIdent("SDL_Pos"), round($SDL_pct, 2)); + SetValue($this->GetIDForIdent("SoC_EV"), round($EV_pct, 2)); SetValue($this->GetIDForIdent("P_SDL_max"), round($P_SDL_max, 0)); SetValue($this->GetIDForIdent("P_SDL_laden"), round($P_SDL_laden, 0)); @@ -179,7 +192,34 @@ class Bat_EV_SDL extends IPSModule SetValue($this->GetIDForIdent("P_EV_laden"), round($P_EV_laden, 0)); SetValue($this->GetIDForIdent("P_EV_entladen"), round($P_EV_entladen, 0)); - SetValue($this->GetIDForIdent("CalcJSON"), json_encode($calc, JSON_PRETTY_PRINT)); + // Debug JSON mit Summen + $out = [ + "SDL_W" => $sdlPowerW, + "SumBatPower_W" => $sumBatPowerW, + "Reserve_h" => $reserveH, + + "SumCap_kWh" => round($sumCap_kWh, 3), + + "SumSDLreq_kWh" => round($sumSDLreq_kWh, 3), + "SumSDLavail_kWh" => round($sumSDLavail_kWh, 3), + "SDL_pct" => round($SDL_pct, 2), + + "EV_max_kWh" => round($EV_max_kWh, 3), + "SumEVavail_kWh" => round($sumEVavail_kWh, 3), + "EV_pct" => round($EV_pct, 2), + + "P_SDL_max_W" => round($P_SDL_max, 0), + "P_SDL_laden_W" => round($P_SDL_laden, 0), + "P_SDL_entladen_W" => round($P_SDL_entladen, 0), + + "P_EV_max_W" => round($P_EV_max, 0), + "P_EV_laden_W" => round($P_EV_laden, 0), + "P_EV_entladen_W" => round($P_EV_entladen, 0), + + "batteries" => $calc + ]; + + SetValue($this->GetIDForIdent("CalcJSON"), json_encode($out, JSON_PRETTY_PRINT)); } private function ReadSocPercent(int $varId): float @@ -190,6 +230,7 @@ class Bat_EV_SDL extends IPSModule $f = (float)$v; if ($f >= 0.0 && $f <= 1.0) $f *= 100.0; + return max(0.0, min(100.0, $f)); } }