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_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); $this->RegisterVariableFloat("P_EV_max", "EV max (W)", "", 30); $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 $this->RegisterTimer("UpdateTimer", 0, 'GEF_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") { SetValue($this->GetIDForIdent("State"), (bool)$Value); if ((bool)$Value) { $this->Update(); } 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; // 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; } // SDL nicht größer als Summe Batterieleistung if ($sdlTotalW > $sumBatPowerW) $sdlTotalW = $sumBatPowerW; // Totals $totalCapKWh = 0.0; $totalStoredKWh = 0.0; $sdlNeedTotalKWh = ($sdlTotalW / 1000.0) * $hours; // Energie totals $sumSdlAvailKWh = 0.0; // tatsächlich lieferbare SDL-Energie (energiebegrenzt) $sumEvKWh = 0.0; // Restenergie nach SDL // Leistung totals (kW) $sumSdlChargeKW = 0.0; $sumEvChargeKW = 0.0; $sumSdlDisKW = 0.0; $sumEvDisKW = 0.0; $calc = [ "inputs" => [ "SDL_Leistung_W" => round($sdlTotalW, 0), "SumBatPower_W" => round($sumBatPowerW, 0), "hours" => $hours ], "batteries" => [], "total" => [] ]; foreach ($batteries as $idx => $b) { $pBatW = max(0.0, (float)($b["powerbat"] ?? 0)); $capKWh = max(0.0, (float)($b["capazity"] ?? 0)); // soc ist SelectVariable -> Variable-ID $socVarId = (int)($b["soc"] ?? 0); $socPct = $this->ReadSocPercent($socVarId); $totalCapKWh += $capKWh; // gespeicherte Energie $storedKWh = $capKWh * ($socPct / 100.0); $totalStoredKWh += $storedKWh; // SDL-Leistung anteilig verteilen $sdlShareW = 0.0; if ($pBatW > 0 && $sdlTotalW > 0) { $sdlShareW = $sdlTotalW * ($pBatW / $sumBatPowerW); } if ($sdlShareW > $pBatW) $sdlShareW = $pBatW; $evShareW = max(0.0, $pBatW - $sdlShareW); // in kW $sdlShareKW = $sdlShareW / 1000.0; $evShareKW = $evShareW / 1000.0; // under_grenze_kWh = SDL Energiebedarf in 30min $underKWh = $sdlShareKW * $hours; // tatsächlich für SDL verfügbare Energie (energiebegrenzt) $sdlAvailKWh = min($storedKWh, $underKWh); // up_grenze_kWh = EV Energie (Restenergie nach SDL) $upKWh = max(0.0, $storedKWh - $sdlAvailKWh); // Runterregeln Discharge anhand Energie im Zeitfenster // Pmax(kW) = E(kWh) / hours $sdlDisKW = ($hours > 0) ? min($sdlShareKW, $sdlAvailKWh / $hours) : 0.0; $evDisKW = ($hours > 0) ? min($evShareKW, $upKWh / $hours) : 0.0; // Charge = reservierte Leistungsanteile $sdlChKW = $sdlShareKW; $evChKW = $evShareKW; // Totals $sumSdlAvailKWh += $sdlAvailKWh; $sumEvKWh += $upKWh; $sumSdlChargeKW += $sdlChKW; $sumEvChargeKW += $evChKW; $sumSdlDisKW += $sdlDisKW; $sumEvDisKW += $evDisKW; $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), "Stored_kWh"=> round($storedKWh, 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), ]; } // SDL% = lieferbare SDL Energie / benötigte SDL Energie $sdlPosPct = 0.0; if ($sdlNeedTotalKWh > 0.000001) { $sdlPosPct = ($sumSdlAvailKWh / $sdlNeedTotalKWh) * 100.0; } // EV%: Anteil der EV-Restenergie bezogen auf "max EV Energie" im gespeicherten Zustand. // Max EV Energie (im Speicher) = totalStored - SDL_needTotal (aber nicht <0) $evMaxKWh = max(0.0, $totalStoredKWh - $sdlNeedTotalKWh); $evPosPct = 0.0; if ($evMaxKWh > 0.000001) { $evPosPct = ($sumEvKWh / $evMaxKWh) * 100.0; } // Variablen setzen $this->SetIdentValue("SDL_Pos", round($sdlPosPct, 3)); $this->SetIdentValue("SoC_EV", round($evPosPct, 3)); // W-Variablen $this->SetIdentValue("P_SDL_max", round($sumSdlChargeKW * 1000.0, 0)); $this->SetIdentValue("P_SDL_laden", round($sumSdlChargeKW * 1000.0, 0)); $this->SetIdentValue("P_SDL_entladen", round($sumSdlDisKW * 1000.0, 0)); $this->SetIdentValue("P_EV_max", round($sumEvChargeKW * 1000.0, 0)); $this->SetIdentValue("P_EV_laden", round($sumEvChargeKW * 1000.0, 0)); $this->SetIdentValue("P_EV_entladen", round($sumEvDisKW * 1000.0, 0)); $calc["total"] = [ "SDL_SoC_pct" => round($sdlPosPct, 3), "EV_SoC_pct" => round($evPosPct, 3), "SDL_Charge_kW" => round($sumSdlChargeKW, 3), "SDL_Discharge_kW" => round($sumSdlDisKW, 3), "EV_Charge_kW" => round($sumEvChargeKW, 3), "EV_Discharge_kW" => round($sumEvDisKW, 3), "SDL_needTotal_kWh" => round($sdlNeedTotalKWh, 3), "SDL_availTotal_kWh" => round($sumSdlAvailKWh, 3), "EV_total_kWh" => round($sumEvKWh, 3), "totalCap_kWh" => round($totalCapKWh, 3), "totalStored_kWh" => round($totalStoredKWh, 3), "EV_max_kWh" => round($evMaxKWh, 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_max", 0.0); $this->SetIdentValue("P_SDL_laden", 0.0); $this->SetIdentValue("P_SDL_entladen", 0.0); $this->SetIdentValue("P_EV_max", 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); if ($id <= 0) { $this->SendDebug(__FUNCTION__, "Ident nicht gefunden: $ident", 0); return; } SetValue($id, $value); } private function ReadSocPercent(int $varId): float { // Falls jemand statt Variable direkt 0..100 einträgt if ($varId >= 0 && $varId <= 100) { 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; } }