no message

This commit is contained in:
mh
2026-04-17 15:59:45 +02:00
parent 6f4cbb798b
commit f54d3dabeb
+79 -76
View File
@@ -67,21 +67,15 @@ class Bat_EV_SDL_V3_Beta extends IPSModule
}
public function Update()
{
{
// Reentranzschutz – verhindert parallele Update()-Läufe
$semKey = 'BatEVSDL_Update_' . $this->InstanceID;
if (!IPS_SemaphoreEnter($semKey, 5000)) {
// Überspringen, wenn bereits ein Lauf aktiv ist
$this->SendDebug("Update", "SKIP (Semaphore locked)", 0);
return;
}
try {
if (!GetValue($this->GetIDForIdent("State"))) {
@@ -97,10 +91,10 @@ class Bat_EV_SDL_V3_Beta extends IPSModule
return;
}
// =====================
// Integrator: State laden
// =====================
// Optional für 2s besser: microtime(true) statt time() verwenden
$now = time();
$lastTs = (int)$this->GetBufferSafe("Int_LastTs");
@@ -120,10 +114,7 @@ class Bat_EV_SDL_V3_Beta extends IPSModule
$dtH = ($now - $lastTs) / 3600.0; // Stunden
if ($dtH < 0) $dtH = 0.0;
// wenn IPS mal hängt, keine riesen Sprünge integrieren:
if ($dtH > 1.0) $dtH = 1.0; // max 1h, kannst du anpassen
if ($dtH > 1.0) $dtH = 1.0; // Sicherheitskappe
$calc = [
"inputs" => $cache["inputs"] ?? [],
@@ -131,7 +122,9 @@ class Bat_EV_SDL_V3_Beta extends IPSModule
"total" => []
];
// =====================
// Summen
// =====================
$sdlDisKW_ges = 0.0;
$evDisKW_ges = 0.0;
$sdlChKW_ges = 0.0;
@@ -144,6 +137,10 @@ class Bat_EV_SDL_V3_Beta extends IPSModule
$EV_kWh_ges = 0.0;
$totalCapKWh = 0.0;
// Für Reset bei SoC=50% (mehrere Batterien): kapazitätsgewichteter Durchschnitt
$socCapSum = 0.0;
$socWeighted = 0.0;
foreach ($cache["bats"] as $i => $c) {
$capKWh = (float)($c["capKWh"] ?? 0.0);
@@ -157,6 +154,10 @@ class Bat_EV_SDL_V3_Beta extends IPSModule
$real_kWh = $capKWh / 100.0 * $socPct;
// SoC für Reset mitteln (gewichtet)
$socWeighted += $socPct * $capKWh;
$socCapSum += $capKWh;
// vorkalkuliert:
$typ = (string)($c["typ"] ?? ("Bat " . ($i + 1)));
$underKWh = (float)($c["underKWh"] ?? 0.0);
@@ -169,8 +170,6 @@ class Bat_EV_SDL_V3_Beta extends IPSModule
$evShareKW_laden = (float)($c["evShareKW_laden"] ?? 0.0);
$evShareKW_entladen = (float)($c["evShareKW_entladen"] ?? 0.0);
// Defaults
$EV_SOC = 0.0;
$SDL_SOC = 0.0;
@@ -181,31 +180,26 @@ class Bat_EV_SDL_V3_Beta extends IPSModule
$real_kWh_ev = 0.0;
$real_kWh_sdl = 0.0;
// --- Deine 3 Fälle ---
// --- 3 Fälle ---
if ($underKWh <= $real_kWh && $upKWh >= $real_kWh) {
// SDL_SOC im Fenster dynamisch (0..100)
$win = ($upKWh - $underKWh);
if ($win > 0.0) {
$SDL_SOC = 100.0 * ($real_kWh - $underKWh) / $win; // 0..100 innerhalb Fenster
$SDL_SOC = 100.0 * ($real_kWh - $underKWh) / $win;
} else {
$SDL_SOC = 0.0;
}
$SDL_SOC = is_finite($SDL_SOC) ? max(0.0, min(100.0, $SDL_SOC)) : 0.0;
// EV_SOC im Fenster dynamisch
if ($EV_kWh > 0.0) {
$EV_SOC = 100.0 * ($real_kWh - $underKWh) / $EV_kWh;
} else {
$EV_SOC = 0.0; // definierter Fallback
$EV_SOC = 0.0;
}
$EV_SOC = is_finite($EV_SOC) ? max(0.0, min(100.0, $EV_SOC)) : 0.0;
$sdlDisKW = $sdlShareKW_entladen;
$evDisKW = $evShareKW_entladen;
$sdlChKW = $sdlShareKW_laden;
@@ -260,9 +254,6 @@ class Bat_EV_SDL_V3_Beta extends IPSModule
$real_kWh_sdl = 0.0;
} elseif ($real_kWh >= $capKWh) {
$sdlChKW = 0.0;
//$real_kWh_ev = $capKWh - ($upKWh + $underKWh);
//$real_kWh_sdl = $upKWh + $underKWh;
$real_kWh_ev = $EV_kWh;
$real_kWh_sdl = $SDL_kWh;
}
@@ -307,64 +298,71 @@ class Bat_EV_SDL_V3_Beta extends IPSModule
];
}
// ===================================================
// Integrator: nur laufen lassen wenn Soll != 0W
// ===================================================
$pEvSoll = (float)GetValue($this->GetIDForIdent("Nennleistung_Soll_EV"));
$pSdlSoll = (float)GetValue($this->GetIDForIdent("Nennleistung_Soll_SDL"));
// =====================
// Nach foreach: Durchschnitts-SoC + Reset-Logik
// =====================
$socAvg = ($socCapSum > 0.0) ? ($socWeighted / $socCapSum) : 0.0;
$eps = 1.0; // 1W-Schwellwert
$evActive = (abs($pEvSoll) > $eps);
$sdlActive = (abs($pSdlSoll) > $eps);
$resetTarget = 50.0;
$resetTol = 0.5; // bei 2s Update: 0.2..0.5 je nach SoC-Auflösung
// 1) Initialisierung: Konto beim ersten Mal auf Referenz setzen
if ($this->GetBufferSafe("Int_Init") !== "1") {
$inWindow = (abs($socAvg - $resetTarget) <= $resetTol);
$armedStr = $this->GetBufferSafe("Int_ResetArmed");
$armed = ($armedStr === "") ? true : ($armedStr === "1");
$doReset = false;
if ($armed && $inWindow) {
$doReset = true;
$armed = false;
}
if (!$inWindow) {
$armed = true;
}
$this->SetBuffer("Int_ResetArmed", $armed ? "1" : "0");
// ===================================================
// Integrator: NUR integrieren wenn Soll != 0W
// Angleichen ist raus: wir setzen bei Soll=0 hart auf Referenz
// ===================================================
$pEvSoll = (float)GetValue($this->GetIDForIdent("Nennleistung_Soll_EV"));
$pSdlSoll = (float)GetValue($this->GetIDForIdent("Nennleistung_Soll_SDL"));
$eps = 1.0;
$evActive = (abs($pEvSoll) > $eps);
$sdlActive = (abs($pSdlSoll) > $eps);
// Initialisierung beim ersten Mal
if ($this->GetBufferSafe("Int_Init") !== "1") {
$Eev = $real_kWh_ev_ges;
$Esdl = $real_kWh_sdl_ges;
$this->SetBuffer("Int_Init", "1");
}
}
// 2) Integration (kW*h = kWh) mit LETZTER Leistung aus dem vorherigen Intervall
// (+) laden -> steigt, (-) entladen -> fällt
if ($evActive) {
$Eev += (($lastPEV / 1000.0) * $dtH);
} else {
// Wenn EV aus: sofort auf physikalische Referenz
// Reset bei SoC~50%: hart auf Referenz (du akzeptierst Sprung)
if ($doReset) {
$Eev = $real_kWh_ev_ges;
}
if ($sdlActive) {
$Esdl += (($lastPSDL / 1000.0) * $dtH);
} else {
// Wenn SDL aus: sofort auf physikalische Referenz
$Esdl = $real_kWh_sdl_ges;
}
// 3) Grenzen einhalten (Clamp)
$Eev = ($EV_kWh_ges > 0.0) ? max(0.0, min($EV_kWh_ges, $Eev)) : 0.0;
$Esdl = ($SDL_kWh_ges > 0.0) ? max(0.0, min($SDL_kWh_ges, $Esdl)) : 0.0;
// 4) Sanftes Wieder-Angleichen (nur wenn aktiv), damit es nicht wegdriftet
// Idee: E := E + alpha*(E_ref - E)
$tauH = 30.0 / 3600.0; // 30 Sekunden Zeitkonstante (weil dein Update in Sekunden ist)
if ($tauH > 0.0) {
$alpha = 1.0 - exp(-$dtH / $tauH);
$alpha = max(0.0, min(0.2, $alpha)); // max 20% pro Update
}
// Integration nur wenn aktiv, sonst hart auf Referenz
if ($evActive) {
$Eev += $alpha * ($real_kWh_ev_ges - $Eev);
}
if ($sdlActive) {
$Esdl += $alpha * ($real_kWh_sdl_ges - $Esdl);
$Eev += (($lastPEV / 1000.0) * $dtH);
} else {
$Eev = $real_kWh_ev_ges;
}
// nach dem angleichen nochmals clamp
if ($sdlActive) {
$Esdl += (($lastPSDL / 1000.0) * $dtH);
} else {
$Esdl = $real_kWh_sdl_ges;
}
// Clamp: Grenzen strikt einhalten (SDL reserviert!)
$Eev = ($EV_kWh_ges > 0.0) ? max(0.0, min($EV_kWh_ges, $Eev)) : 0.0;
$Esdl = ($SDL_kWh_ges > 0.0) ? max(0.0, min($SDL_kWh_ges, $Esdl)) : 0.0;
}
// 5) Prozentwerte aus den integrierten Konten
// Prozentwerte aus den Konten
$evPosPct = ($EV_kWh_ges > 0.0) ? ($Eev / $EV_kWh_ges * 100.0) : 0.0;
$sdlPosPct = ($SDL_kWh_ges > 0.0) ? ($Esdl / $SDL_kWh_ges * 100.0) : 0.0;
@@ -372,6 +370,7 @@ if ($sdlActive) {
$this->SetIdentValue("SDL_Pos", round($sdlPosPct, 3));
$this->SetIdentValue("SoC_EV", round($evPosPct, 3));
// Maximalleistungen setzen (wie gehabt)
$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));
@@ -389,11 +388,17 @@ if ($sdlActive) {
"EV_Charge_kW" => round($evChKW_ges, 3),
"EV_Discharge_kW" => round($evDisKW_ges, 3),
"totalCap_kWh" => round($totalCapKWh, 3)
"totalCap_kWh" => round($totalCapKWh, 3),
"socAvg" => round($socAvg, 3),
"doReset" => $doReset ? 1 : 0
];
$this->SetIdentValue("CalcJSON", json_encode($calc, JSON_PRETTY_PRINT));
// Setpoints schreiben
$this->ApplySetpoints();
// ==================================
// Integrator: neue LastP für nächstes Intervall speichern
// ==================================
@@ -407,15 +412,13 @@ if ($sdlActive) {
$this->SetBuffer("Int_E_EV_kWh", (string)$Eev);
$this->SetBuffer("Int_E_SDL_kWh", (string)$Esdl);
} catch (Throwable $e) {
$this->SendDebug("Update ERROR", $e->getMessage() . " @ " . $e->getFile() . ":" . $e->getLine(), 0);
$this->WriteAllZero("Exception: " . $e->getMessage());
} finally {
IPS_SemaphoreLeave($semKey);
}
finally {
IPS_SemaphoreLeave($semKey); // ✅ Tür immer wieder aufschließen
}
}
}
private function BuildBatteryCache(bool $force): void
{