no message

This commit is contained in:
belevo\mh
2026-04-30 14:04:04 +02:00
parent 6af357ae95
commit 43939e8a03
+137 -78
View File
@@ -67,6 +67,7 @@ public function Update()
return;
}
// Cache nur neu bauen, wenn nötig
$this->BuildBatteryCache(false);
$cache = json_decode($this->GetBufferSafe("BatCacheJSON"), true);
@@ -75,6 +76,7 @@ public function Update()
return;
}
// Zeitbasis
$now = microtime(true);
$lastTs = (float)$this->GetBufferSafe("Int_LastTs");
@@ -87,10 +89,11 @@ public function Update()
$dtSec = $now - $lastTs;
if ($dtSec < 0.0) $dtSec = 0.0;
if ($dtSec > 10.0) $dtSec = 10.0;
$dtH = $dtSec / 3600.0;
// Sollwerte lesen
$pSdlSollW = (float)GetValue($this->GetIDForIdent("Nennleistung_Soll_SDL"));
$pEvSollW = (float)GetValue($this->GetIDForIdent("Nennleistung_Soll_EV"));
$calc = [
"inputs" => $cache["inputs"] ?? [],
@@ -98,6 +101,7 @@ public function Update()
"total" => []
];
// Summen
$sdlDisKW_ges = 0.0;
$evDisKW_ges = 0.0;
$sdlChKW_ges = 0.0;
@@ -110,6 +114,7 @@ public function Update()
$EV_kWh_ges = 0.0;
$totalCapKWh = 0.0;
// CFG-Summen (für Startpunkt / Anzeige)
$SDL_kWh_ges_cfg = 0.0;
$SDL_start_kWh_cfg = 0.0;
@@ -121,139 +126,130 @@ public function Update()
$startPct = ($SDL_kWh_ges_cfg > 0.0)
? ($SDL_start_kWh_cfg / $SDL_kWh_ges_cfg * 100.0)
: 0.0;
$startPct = max(0.0, min(100.0, $startPct));
$this->SetIdentValue("SDL_Start_Pos", round($startPct, 3));
// Init SDL-Konto einmalig auf Startpunkt (untere Reserve)
if ($this->GetBufferSafe("Int_Init_SDL") !== "1") {
$Esdl_kWh = max(0.0, min($SDL_kWh_ges_cfg, $SDL_start_kWh_cfg));
$this->SetBuffer("Int_Init_SDL", "1");
}
// Virtueller Stand in %
$sdlVirtPct = ($SDL_kWh_ges_cfg > 0.0)
? ($Esdl_kWh / $SDL_kWh_ges_cfg * 100.0)
: 0.0;
$sdlVirtPct = max(0.0, min(100.0, $sdlVirtPct));
$sdlIsEmpty = ($sdlVirtPct <= 0.1);
$sdlIsFull = ($sdlVirtPct >= 99.9);
// Flags für Grenz-Anker (deine Logik)
$sdlHitUpperBySDL = false;
$sdlHitLowerBySDL = false;
// ============================
// Battery Loop
// ============================
foreach ($cache["bats"] as $i => $c) {
$capKWh = (float)($c["capKWh"] ?? 0.0);
if ($capKWh <= 0.0) {
continue;
}
// SoC lesen
$socVarId = (int)($c["socVarId"] ?? 0);
$socPct = $this->ReadSocPercent($socVarId);
$real_kWh = $capKWh / 100.0 * $socPct;
// vorkalkuliert
$typ = (string)($c["typ"] ?? ("Bat " . ($i + 1)));
$underKWh = (float)($c["underKWh"] ?? 0.0);
$upKWh = (float)($c["upKWh"] ?? 0.0);
// Grenz-Anker nur wenn SDL selbst Richtung Grenze fährt (dein Wunsch)
if ($real_kWh > $upKWh && $pSdlSollW > 0.0) {
$sdlHitUpperBySDL = true;
}
if ($real_kWh < $underKWh && $pSdlSollW < 0.0) {
$sdlHitLowerBySDL = true;
}
// EV-Fenster (dynamisch erweitert bei SDL virt leer/voll)
$evUnderKWh = $underKWh;
$evUpKWh = $upKWh;
if ($sdlIsEmpty) {
$evUnderKWh = 0.0;
}
if ($sdlIsFull) {
$evUpKWh = $capKWh;
}
$evWindowKWh = max(0.0, $evUpKWh - $evUnderKWh);
$SDL_kWh = (float)($c["SDL_kWh_total"] ?? 0.0);
$EV_kWh = (float)($c["EV_kWh_total"] ?? 0.0);
// Fensterkapazitäten / Limits aus Cache
$SDL_kWh = (float)($c["SDL_kWh_total"] ?? 0.0); // SDL-Fenster total (kWh)
$EV_kWh = (float)($c["EV_kWh_total"] ?? 0.0);
$sdlShareKW_laden = (float)($c["sdlShareKW_laden"] ?? 0.0);
$sdlShareKW_entladen = (float)($c["sdlShareKW_entladen"] ?? 0.0);
$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;
$sdlDisKW = 0.0;
$evDisKW = 0.0;
$sdlChKW = 0.0;
$evChKW = 0.0;
$sdlDisKW = 0.0; $evDisKW = 0.0;
$sdlChKW = 0.0; $evChKW = 0.0;
$real_kWh_ev = 0.0;
$real_kWh_sdl = 0.0;
// --- Fälle ---
if ($underKWh <= $real_kWh && $real_kWh <= $upKWh) {
// EV_SOC im Fenster (0..100)
if ($evWindowKWh > 0.0) {
$EV_SOC = 100.0 * ($real_kWh - $evUnderKWh) / $evWindowKWh;
} else {
$EV_SOC = 0.0;
}
$EV_SOC = is_finite($EV_SOC) ? max(0.0, min(100.0, $EV_SOC)) : 0.0;
$denSDL = ($capKWh - $upKWh + $underKWh);
if ($denSDL > 0.0) {
$SDL_SOC = 100.0 * $underKWh / $denSDL;
} else {
$SDL_SOC = 0.0;
}
$SDL_SOC = is_finite($SDL_SOC) ? max(0.0, min(100.0, $SDL_SOC)) : 0.0;
// Leistungen
$sdlDisKW = $sdlShareKW_entladen;
$evDisKW = $evShareKW_entladen;
$sdlChKW = $sdlShareKW_laden;
$evChKW = $evShareKW_laden;
// Energieaufteilung (EV im Fenster, Rest SDL)
$real_kWh_ev = max(0.0, min($evWindowKWh, $real_kWh - $evUnderKWh));
$real_kWh_sdl = $real_kWh - $real_kWh_ev;
} elseif ($real_kWh > $evUpKWh) {
// oberhalb EV-Fenster
$EV_SOC = 100.0;
$den1 = ($capKWh - $real_kWh + $underKWh);
$den2 = (2.0 * $underKWh);
if ($den1 > 0.0 && $den2 > 0.0) {
$SDL_SOC = min(100.0, ($den1 / $den2) * 100.0);
} else {
$SDL_SOC = 0.0;
}
$SDL_SOC = is_finite($SDL_SOC) ? max(0.0, min(100.0, $SDL_SOC)) : 0.0;
$sdlDisKW = $sdlShareKW_entladen;
$evDisKW = $evShareKW_entladen;
$sdlChKW = $sdlShareKW_laden;
$evChKW = 0.0;
$evChKW = 0.0; // EV darf oben nicht laden
// EV voll im Fenster, Rest ist SDL
$real_kWh_ev = $evWindowKWh;
$real_kWh_sdl = max(0.0, $real_kWh - $real_kWh_ev);
} elseif ($real_kWh < $evUnderKWh) {
// unterhalb EV-Fenster
$EV_SOC = 0.0;
$den = $upKWh + $underKWh;
$SDL_SOC = ($den > 0.0) ? ($real_kWh * 100.0 / $den) : 0.0;
$SDL_SOC = is_finite($SDL_SOC) ? max(0.0, min(100.0, $SDL_SOC)) : 0.0;
$sdlDisKW = $sdlShareKW_entladen;
$evDisKW = 0.0;
$evDisKW = 0.0; // EV darf unten nicht entladen
$sdlChKW = $sdlShareKW_laden;
$evChKW = $evShareKW_laden;
@@ -261,10 +257,10 @@ public function Update()
$real_kWh_sdl = $real_kWh;
} else {
// Fallback
if ($evWindowKWh > 0.0) {
$EV_SOC = 100.0 * ($real_kWh - $evUnderKWh) / $evWindowKWh;
}
$EV_SOC = is_finite($EV_SOC) ? max(0.0, min(100.0, $EV_SOC)) : 0.0;
$sdlDisKW = $sdlShareKW_entladen;
@@ -276,6 +272,7 @@ public function Update()
$real_kWh_sdl = $real_kWh - $real_kWh_ev;
}
// Null/Full Schutz
if ($real_kWh <= 0.0) {
$sdlDisKW = 0.0;
$real_kWh_ev = 0.0;
@@ -289,6 +286,13 @@ public function Update()
$real_kWh_ev = max(0.0, $real_kWh_ev);
$real_kWh_sdl = max(0.0, $real_kWh_sdl);
// ==========================================================
// FIX #1: SDL_SOC als echter SDL-Füllstand (passt zur Int-Logik)
// ==========================================================
$SDL_SOC = ($SDL_kWh > 0.0) ? (100.0 * $real_kWh_sdl / $SDL_kWh) : 0.0;
$SDL_SOC = is_finite($SDL_SOC) ? max(0.0, min(100.0, $SDL_SOC)) : 0.0;
// Summen
$totalCapKWh += $capKWh;
$sdlDisKW_ges += $sdlDisKW;
@@ -302,6 +306,7 @@ public function Update()
$SDL_kWh_ges += $SDL_kWh;
$EV_kWh_ges += $EV_kWh;
// JSON pro Batterie
$calc["batteries"][] = [
"idx" => $c["idx"] ?? $i,
"typ" => $typ,
@@ -309,37 +314,41 @@ public function Update()
"SoC_pct" => round($socPct, 3),
"Effektive kWh" => round($real_kWh, 3),
"EV_SOC" => round($EV_SOC, 3),
"EV_SOC" => round($EV_SOC, 3),
"SDL_SOC" => round($SDL_SOC, 3),
"EV_kWh" => round($real_kWh_ev, 3),
"EV_kWh" => round($real_kWh_ev, 3),
"SDL_kWh" => round($real_kWh_sdl, 3),
"under_grenze_kWh" => round($underKWh, 3),
"up_grenze_kWh" => round($upKWh, 3),
"up_grenze_kWh" => round($upKWh, 3),
"SDL_Charge_kW" => round($sdlChKW, 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),
"EV_Charge_kW" => round($evChKW, 3),
"EV_Discharge_kW" => round($evDisKW, 3),
];
}
$evPosPct = ($EV_kWh_ges > 0.0)
? ($real_kWh_ev_ges / $EV_kWh_ges * 100.0)
: 0.0;
// EV Anzeige
$evPosPct = ($EV_kWh_ges > 0.0) ? ($real_kWh_ev_ges / $EV_kWh_ges * 100.0) : 0.0;
$evPosPct = max(0.0, min(100.0, $evPosPct));
$this->SetIdentValue("SoC_EV", round($evPosPct, 3));
// ======================================
// SDL aktiv?
// ======================================
$epsW = 1.0;
$sdlActive = (abs($pSdlSollW) > $epsW);
// Maximalleistungen
$maxSDL_ch = $sdlChKW_ges * 1000.0;
$maxSDL_dis = $sdlDisKW_ges * 1000.0;
// Soll clamp
$pSdlSollW = max(-$maxSDL_dis, min($maxSDL_ch, $pSdlSollW));
// CacheHash Change Reset
$cacheHash = (string)$this->GetBufferSafe("BatCacheHash");
$intHash = (string)$this->GetBufferSafe("Int_CFG_HASH");
@@ -351,9 +360,7 @@ public function Update()
if ($cacheHash !== "" && $cacheHash !== $intHash) {
$Esdl_kWh = max(0.0, min($SDL_kWh_ges_cfg, $SDL_start_kWh_cfg));
$lastTs = $now;
$dtH = 0.0;
$this->SetBuffer("Int_LastTs", (string)$now);
$this->SetBuffer("Int_CFG_HASH", $cacheHash);
$this->SetBuffer("Int_Init_SDL", "1");
@@ -361,13 +368,7 @@ public function Update()
$this->SendDebug("SDL", "Reset wegen CacheHash-Change auf Startpunkt: Esdl_kWh=" . round($Esdl_kWh, 3), 0);
}
/*
* WICHTIG:
* Nur wenn SDL selbst in Richtung Grenze fährt, wird der Integrator
* auf den physikalischen Grenzpunkt gesetzt.
*
* EV überschreitet Grenze -> KEIN Sprung von SDL_Pos.
*/
// Physikalischer Grenz-Anker (deine Logik)
if ($sdlHitUpperBySDL || $sdlHitLowerBySDL) {
$Esdl_kWh = max(0.0, min($SDL_kWh_ges, $real_kWh_sdl_ges));
$dtH = 0.0;
@@ -383,52 +384,84 @@ public function Update()
);
}
// ==========================================================
// FIX #2: Integrator mit IST-Leistung statt SOLL-Leistung
// ==========================================================
$totalIstW = $this->GetTotalBatteryPowerIstW(); // Gesamt-Batterie-Istleistung (W)
$pSdlIstW = 0.0;
if ($sdlActive) {
$Esdl_kWh += (($pSdlSollW / 1000.0) * $dtH);
// Wenn EV nicht aktiv: SDL = gesamte Istleistung
if (abs($pEvSollW) <= $epsW) {
$pSdlIstW = $totalIstW;
} else {
// EV und SDL beide aktiv:
$sameDir = (($pSdlSollW > 0 && $pEvSollW > 0) || ($pSdlSollW < 0 && $pEvSollW < 0));
if ($sameDir) {
$share = abs($pSdlSollW) / (abs($pSdlSollW) + abs($pEvSollW));
$pSdlIstW = $totalIstW * $share;
} else {
// gegenläufig: aus Gesamt-Ist nicht sauber trennbar -> nicht integrieren
$pSdlIstW = 0.0;
}
}
}
$Esdl_kWh = ($SDL_kWh_ges > 0.0)
? max(0.0, min($SDL_kWh_ges, $Esdl_kWh))
: 0.0;
// Ist clamp (wichtig)
$pSdlIstW = max(-$maxSDL_dis, min($maxSDL_ch, $pSdlIstW));
$sdlPosPct = ($SDL_kWh_ges > 0.0)
? ($Esdl_kWh / $SDL_kWh_ges * 100.0)
: 0.0;
// Integration
if ($sdlActive) {
$Esdl_kWh += (($pSdlIstW / 1000.0) * $dtH);
}
// Clamp SDL-Konto
$Esdl_kWh = ($SDL_kWh_ges > 0.0) ? max(0.0, min($SDL_kWh_ges, $Esdl_kWh)) : 0.0;
// SDL Anzeige
$sdlPosPct = ($SDL_kWh_ges > 0.0) ? ($Esdl_kWh / $SDL_kWh_ges * 100.0) : 0.0;
$sdlPosPct = max(0.0, min(100.0, $sdlPosPct));
$this->SetIdentValue("SDL_Pos", round($sdlPosPct, 3));
$this->SetIdentValue("P_SDL_laden", round($sdlChKW_ges * 1000.0, 0));
// Maximalleistungen anzeigen
$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", (int)round($evChKW_ges * 1000.0, 0));
$this->SetIdentValue("P_EV_entladen", (int)round($evDisKW_ges * 1000.0, 0));
$this->SetIdentValue("P_EV_laden", (int)round($evChKW_ges * 1000.0, 0));
$this->SetIdentValue("P_EV_entladen", (int)round($evDisKW_ges * 1000.0, 0));
// Total JSON (inkl. Istwerte zur Kontrolle)
$calc["total"] = [
"SDL_SoC_pct" => round($sdlPosPct, 3),
"EV_SoC_pct" => round($evPosPct, 3),
"EV_SoC_pct" => round($evPosPct, 3),
"SDL_kWh_total" => round($SDL_kWh_ges, 3),
"EV_kWh_total" => round($EV_kWh_ges, 3),
"EV_kWh_total" => round($EV_kWh_ges, 3),
"SDL_Charge_kW" => round($sdlChKW_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),
"EV_Charge_kW" => round($evChKW_ges, 3),
"EV_Discharge_kW" => round($evDisKW_ges, 3),
"totalCap_kWh" => round($totalCapKWh, 3),
"SDL_Start_pct" => round($startPct, 3),
"SDL_IsEmpty" => $sdlIsEmpty,
"SDL_IsFull" => $sdlIsFull,
"SDL_IsEmpty" => $sdlIsEmpty,
"SDL_IsFull" => $sdlIsFull,
"SDL_HitUpperBySDL" => $sdlHitUpperBySDL,
"SDL_HitLowerBySDL" => $sdlHitLowerBySDL
"SDL_HitLowerBySDL" => $sdlHitLowerBySDL,
// Debug / Kontrolle für Ist-Integration:
"SDL_Soll_W" => round($pSdlSollW, 0),
"SDL_Ist_W" => round($pSdlIstW, 0),
"Bat_Ist_W_total" => round($totalIstW, 0)
];
$this->SetIdentValue("CalcJSON", json_encode($calc, JSON_PRETTY_PRINT));
// Setpoints wie gehabt
$this->ApplySetpoints();
// Integrator State speichern
$this->SetBuffer("Int_LastTs", (string)$now);
$this->SetBuffer("Int_E_SDL_kWh", (string)$Esdl_kWh);
@@ -439,6 +472,11 @@ public function Update()
IPS_SemaphoreLeave($semKey);
}
}
private function BuildBatteryCache(bool $force): void
{
$batteriesRaw = $this->ReadPropertyString("Batteries");
@@ -1022,5 +1060,26 @@ private function ResetSDLTo50Percent(): void
}
}
private function GetTotalBatteryPowerIstW(): float
{
$cfg = json_decode($this->ReadPropertyString("Batteries"), true);
if (!is_array($cfg)) {
return 0.0;
}
$sum = 0.0;
foreach ($cfg as $b) {
// Key muss in deiner Batteries-Konfig existieren:
$varId = (int)($b["register_bat_power"] ?? 0);
if ($varId > 0 && IPS_VariableExists($varId)) {
$raw = (float)GetValue($varId);
// Vorzeichen wie bei dir: totalPower_ist += (-1)*GetValue(...)
$sum += (-1.0) * $raw;
}
}
return $sum;
}
}
?>