From 9bbbad260a2429f577ed22b766c07d9812819b85 Mon Sep 17 00:00:00 2001 From: "belevo\\mh" Date: Tue, 27 Jan 2026 12:47:44 +0100 Subject: [PATCH] no message --- Bat_EV_SDL _V2/module.php | 119 +++++++++++++++++++++++++++++++++++++- 1 file changed, 118 insertions(+), 1 deletion(-) diff --git a/Bat_EV_SDL _V2/module.php b/Bat_EV_SDL _V2/module.php index b2e3d82..9add5f9 100644 --- a/Bat_EV_SDL _V2/module.php +++ b/Bat_EV_SDL _V2/module.php @@ -262,6 +262,8 @@ class Bat_EV_SDL_V2 extends IPSModule ]; $this->SetIdentValue("CalcJSON", json_encode($calc, JSON_PRETTY_PRINT)); + $this->ApplySetpoints(); + } catch (Throwable $e) { $this->SendDebug("Update ERROR", $e->getMessage() . " @ " . $e->getFile() . ":" . $e->getLine(), 0); @@ -437,6 +439,121 @@ class Bat_EV_SDL_V2 extends IPSModule return $soc; } -} + public function ApplySetpoints(): void + { + $pEvW = (float) GetValue($this->GetIDForIdent("Nennleistung_Soll_EV")); + $pSdlW = (float) GetValue($this->GetIDForIdent("Nennleistung_Soll_SDL")); + //Diverenz zwischen EV und SDl berechnen + + + // Methode für priorisierung + $distribution = $this->CalculateBatteryDistribution($pEvW,$pSdlW); + + + $this->WriteBatteryPowerSetpoints($distribution); + } + + + + private function CalculateBatteryDistribution(float $pEvW, float $pSdlW): array + { + + // Holt das berechnete Json aus, also das Debug json, wo in der Konsole angezeigt wird + $calcJsonId = $this->GetIDForIdent("CalcJSON"); + $calc = json_decode((string)GetValue($calcJsonId), true); + + if (!is_array($calc) || empty($calc["batteries"])) { + return []; + } + + foreach ($calc["batteries"] as $bat) { + + $idx = (int)$bat["idx"]; + $typ = (string)$bat["typ"]; + + $evSoc = (float)$bat["EV_SOC"]; + $sdlSoc = (float)$bat["SDL_SOC"]; + + $sdlChargeKW = (float)$bat["SDL_Charge_kW"]; + $sdlDischargeKW = (float)$bat["SDL_Discharge_kW"]; + $evChargeKW = (float)$bat["EV_Charge_kW"]; + $evDischargeKW = (float)$bat["EV_Discharge_kW"]; + } + + + + /* + // Return sieht so aus: + return :[ + "idx" => 0, + "typ":"1", + "chargeW" => 0.0, + "dischargeW" => 3000.0 + ], + [ + "idx" => 1, + "typ":"2", + "chargeW" => 0.0, + "dischargeW" => 1000.0 + ]; + */ + + } + + private function WriteBatteryPowerSetpoints(array $distribution): void + { + // chargeW und dischargeW sind in W. Wenn good hat muss in kW umrechnen + + // Batteries-Config laden (für Variable-IDs + typ) + $batteriesCfg = json_decode($this->ReadPropertyString("Batteries"), true); + if (!is_array($batteriesCfg) || empty($batteriesCfg)) { + return; + } + + foreach ($distribution as $d) { + + // --- Batterie bestimmen --- + $idx = (int)($d["idx"] ?? -1); + if ($idx < 0 || !isset($batteriesCfg[$idx])) { + continue; + } + + $cfg = $batteriesCfg[$idx]; + $typ = (string)($d["typ"] ?? ($cfg["typ"] ?? ("Bat " . ($idx + 1)))); + + // --- Sollwerte (Watt) --- + $chargeW = max(0.0, (float)($d["chargeW"] ?? 0.0)); + $dischargeW = max(0.0, (float)($d["dischargeW"] ?? 0.0)); + + // Sicherheitsnetz: nie gleichzeitig laden & entladen + if ($chargeW > 0.0 && $dischargeW > 0.0) { + // Debug-Hinweis, falls Strategie Mist gebaut hat + $this->SendDebug( + "WriteBatteryPowerSetpoints", + "WARN: charge & discharge gleichzeitig > 0 bei $typ (idx=$idx)", + 0 + ); + $chargeW = 0.0; + $dischargeW = 0.0; + } + + // --- Ziel-Variablen --- + $varCharge = (int)($cfg["powerbat_laden"] ?? 0); + $varDischarge = (int)($cfg["powerbat_entladen"] ?? 0); + + // --- Schreiben --- + if ($varCharge > 0 && IPS_VariableExists($varCharge)) { + SetValue($varCharge, (int)round($chargeW, 0)); + } + + if ($varDischarge > 0 && IPS_VariableExists($varDischarge)) { + SetValue($varDischarge, (int)round($dischargeW, 0)); + } + + + } + } + +} ?>