no message
This commit is contained in:
+280
-277
@@ -66,49 +66,43 @@ class Bat_EV_SDL_V3_Beta extends IPSModule
|
||||
throw new Exception("Invalid Ident: " . $Ident);
|
||||
}
|
||||
|
||||
public function Update()
|
||||
{
|
||||
|
||||
|
||||
public function Update()
|
||||
{
|
||||
// Reentranzschutz – verhindert parallele Update()-Läufe
|
||||
$semKey = 'BatEVSDL_Update_' . $this->InstanceID;
|
||||
|
||||
// Reentranzschutz – verhindert parallele Update()-Läufe
|
||||
$semKey = 'BatEVSDL_Update_' . $this->InstanceID;
|
||||
if (!IPS_SemaphoreEnter($semKey, 5000)) {
|
||||
$this->SendDebug("Update", "SKIP (Semaphore locked)", 0);
|
||||
return;
|
||||
}
|
||||
|
||||
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"))) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Cache nur neu bauen, wenn nötig
|
||||
$this->BuildBatteryCache(false);
|
||||
|
||||
|
||||
try {
|
||||
|
||||
if (!GetValue($this->GetIDForIdent("State"))) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Cache nur neu bauen, wenn nötig
|
||||
$this->BuildBatteryCache(false);
|
||||
|
||||
$cache = json_decode($this->GetBufferSafe("BatCacheJSON"), true);
|
||||
if (!is_array($cache) || empty($cache["bats"])) {
|
||||
$this->WriteAllZero("cache empty/invalid");
|
||||
return;
|
||||
}
|
||||
|
||||
$cache = json_decode($this->GetBufferSafe("BatCacheJSON"), true);
|
||||
if (!is_array($cache) || empty($cache["bats"])) {
|
||||
$this->WriteAllZero("cache empty/invalid");
|
||||
return;
|
||||
}
|
||||
|
||||
// =====================
|
||||
// Integrator: State laden
|
||||
// =====================
|
||||
// Optional für 2s besser: microtime(true) statt time() verwenden
|
||||
$now = time();
|
||||
|
||||
$lastTs = (int)$this->GetBufferSafe("Int_LastTs");
|
||||
$lastPEV = (float)$this->GetBufferSafe("Int_LastP_EV_W"); // W (+ laden, - entladen)
|
||||
$lastPSDL = (float)$this->GetBufferSafe("Int_LastP_SDL_W"); // W (+ laden, - entladen)
|
||||
$lastPEV = (float)$this->GetBufferSafe("Int_LastP_EV_W"); // W (+ laden, - entladen)
|
||||
$lastPSDL = (float)$this->GetBufferSafe("Int_LastP_SDL_W"); // W (+ laden, - entladen)
|
||||
|
||||
$Eev = (float)$this->GetBufferSafe("Int_E_EV_kWh"); // kWh-Konto EV
|
||||
$Esdl = (float)$this->GetBufferSafe("Int_E_SDL_kWh"); // kWh-Konto SDL
|
||||
$Eev = (float)$this->GetBufferSafe("Int_E_EV_kWh"); // kWh-Konto EV
|
||||
$Esdl = (float)$this->GetBufferSafe("Int_E_SDL_kWh"); // kWh-Konto SDL
|
||||
|
||||
if ($lastTs <= 0) {
|
||||
$lastTs = $now;
|
||||
@@ -120,302 +114,311 @@ 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"] ?? [],
|
||||
"batteries" => [],
|
||||
"total" => []
|
||||
];
|
||||
|
||||
// =====================
|
||||
// Summen
|
||||
// =====================
|
||||
$sdlDisKW_ges = 0.0;
|
||||
$evDisKW_ges = 0.0;
|
||||
$sdlChKW_ges = 0.0;
|
||||
$evChKW_ges = 0.0;
|
||||
|
||||
$calc = [
|
||||
"inputs" => $cache["inputs"] ?? [],
|
||||
"batteries" => [],
|
||||
"total" => []
|
||||
];
|
||||
$real_kWh_ev_ges = 0.0;
|
||||
$real_kWh_sdl_ges = 0.0;
|
||||
|
||||
// Summen
|
||||
$sdlDisKW_ges = 0.0;
|
||||
$evDisKW_ges = 0.0;
|
||||
$sdlChKW_ges = 0.0;
|
||||
$evChKW_ges = 0.0;
|
||||
$SDL_kWh_ges = 0.0;
|
||||
$EV_kWh_ges = 0.0;
|
||||
$totalCapKWh = 0.0;
|
||||
|
||||
$real_kWh_ev_ges = 0.0;
|
||||
$real_kWh_sdl_ges = 0.0;
|
||||
// Für Reset bei SoC=50% (mehrere Batterien): kapazitätsgewichteter Durchschnitt
|
||||
$socCapSum = 0.0;
|
||||
$socWeighted = 0.0;
|
||||
|
||||
$SDL_kWh_ges = 0.0;
|
||||
$EV_kWh_ges = 0.0;
|
||||
$totalCapKWh = 0.0;
|
||||
foreach ($cache["bats"] as $i => $c) {
|
||||
|
||||
foreach ($cache["bats"] as $i => $c) {
|
||||
$capKWh = (float)($c["capKWh"] ?? 0.0);
|
||||
if ($capKWh <= 0.0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$capKWh = (float)($c["capKWh"] ?? 0.0);
|
||||
if ($capKWh <= 0.0) {
|
||||
continue;
|
||||
}
|
||||
// dynamisch: SoC lesen
|
||||
$socVarId = (int)($c["socVarId"] ?? 0);
|
||||
$socPct = $this->ReadSocPercent($socVarId);
|
||||
|
||||
// dynamisch: SoC lesen
|
||||
$socVarId = (int)($c["socVarId"] ?? 0);
|
||||
$socPct = $this->ReadSocPercent($socVarId);
|
||||
$real_kWh = $capKWh / 100.0 * $socPct;
|
||||
|
||||
$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);
|
||||
$upKWh = (float)($c["upKWh"] ?? 0.0);
|
||||
$SDL_kWh = (float)($c["SDL_kWh_total"] ?? 0.0);
|
||||
$EV_kWh = (float)($c["EV_kWh_total"] ?? 0.0);
|
||||
// vorkalkuliert:
|
||||
$typ = (string)($c["typ"] ?? ("Bat " . ($i + 1)));
|
||||
$underKWh = (float)($c["underKWh"] ?? 0.0);
|
||||
$upKWh = (float)($c["upKWh"] ?? 0.0);
|
||||
$SDL_kWh = (float)($c["SDL_kWh_total"] ?? 0.0);
|
||||
$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);
|
||||
$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;
|
||||
|
||||
// Defaults
|
||||
$EV_SOC = 0.0;
|
||||
$SDL_SOC = 0.0;
|
||||
$real_kWh_ev = 0.0;
|
||||
$real_kWh_sdl = 0.0;
|
||||
|
||||
$sdlDisKW = 0.0; $evDisKW = 0.0;
|
||||
$sdlChKW = 0.0; $evChKW = 0.0;
|
||||
|
||||
$real_kWh_ev = 0.0;
|
||||
$real_kWh_sdl = 0.0;
|
||||
|
||||
// --- Deine 3 Fälle ---
|
||||
if ($underKWh <= $real_kWh && $upKWh >= $real_kWh) {
|
||||
|
||||
|
||||
// --- 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;
|
||||
|
||||
|
||||
|
||||
if ($EV_kWh > 0.0) {
|
||||
$EV_SOC = 100.0 * ($real_kWh - $underKWh) / $EV_kWh;
|
||||
} else {
|
||||
$EV_SOC = 0.0; // definierter Fallback
|
||||
}
|
||||
$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;
|
||||
$evChKW = $evShareKW_laden;
|
||||
|
||||
$real_kWh_ev = $real_kWh - $underKWh;
|
||||
$real_kWh_sdl = $underKWh;
|
||||
|
||||
} elseif ($upKWh < $real_kWh) {
|
||||
|
||||
$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;
|
||||
|
||||
$real_kWh_ev = $capKWh - ($capKWh - $upKWh + $underKWh);
|
||||
$real_kWh_sdl = ($capKWh - $upKWh + $underKWh) - ($capKWh - $real_kWh);
|
||||
|
||||
} elseif ($underKWh > $real_kWh) {
|
||||
|
||||
// EV_SOC im Fenster dynamisch
|
||||
if ($EV_kWh > 0.0) {
|
||||
$EV_SOC = 100.0 * ($real_kWh - $underKWh) / $EV_kWh;
|
||||
} else {
|
||||
$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;
|
||||
$sdlChKW = $sdlShareKW_laden;
|
||||
$evChKW = $evShareKW_laden;
|
||||
|
||||
$real_kWh_ev = 0.0;
|
||||
$real_kWh_sdl = $real_kWh;
|
||||
}
|
||||
$EV_SOC = is_finite($EV_SOC) ? max(0.0, min(100.0, $EV_SOC)) : 0.0;
|
||||
|
||||
// Null/Full
|
||||
if ($real_kWh <= 0.0) {
|
||||
$sdlDisKW = 0.0;
|
||||
$real_kWh_ev = 0.0;
|
||||
$real_kWh_sdl = 0.0;
|
||||
} elseif ($real_kWh >= $capKWh) {
|
||||
$sdlChKW = 0.0;
|
||||
//$real_kWh_ev = $capKWh - ($upKWh + $underKWh);
|
||||
//$real_kWh_sdl = $upKWh + $underKWh;
|
||||
$sdlDisKW = $sdlShareKW_entladen;
|
||||
$evDisKW = $evShareKW_entladen;
|
||||
$sdlChKW = $sdlShareKW_laden;
|
||||
$evChKW = $evShareKW_laden;
|
||||
|
||||
$real_kWh_ev = $EV_kWh;
|
||||
$real_kWh_sdl = $SDL_kWh;
|
||||
$real_kWh_ev = $real_kWh - $underKWh;
|
||||
$real_kWh_sdl = $underKWh;
|
||||
|
||||
} elseif ($upKWh < $real_kWh) {
|
||||
|
||||
$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;
|
||||
|
||||
$real_kWh_ev = max(0.0, $real_kWh_ev);
|
||||
$real_kWh_sdl = max(0.0, $real_kWh_sdl);
|
||||
$sdlDisKW = $sdlShareKW_entladen;
|
||||
$evDisKW = $evShareKW_entladen;
|
||||
$sdlChKW = $sdlShareKW_laden;
|
||||
$evChKW = 0.0;
|
||||
|
||||
// Summen
|
||||
$totalCapKWh += $capKWh;
|
||||
$real_kWh_ev = $capKWh - ($capKWh - $upKWh + $underKWh);
|
||||
$real_kWh_sdl = ($capKWh - $upKWh + $underKWh) - ($capKWh - $real_kWh);
|
||||
|
||||
$sdlDisKW_ges += $sdlDisKW;
|
||||
$evDisKW_ges += $evDisKW;
|
||||
$sdlChKW_ges += $sdlChKW;
|
||||
$evChKW_ges += $evChKW;
|
||||
} elseif ($underKWh > $real_kWh) {
|
||||
|
||||
$real_kWh_ev_ges += $real_kWh_ev;
|
||||
$real_kWh_sdl_ges += $real_kWh_sdl;
|
||||
$EV_SOC = 0.0;
|
||||
|
||||
$SDL_kWh_ges += $SDL_kWh;
|
||||
$EV_kWh_ges += $EV_kWh;
|
||||
$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;
|
||||
|
||||
$calc["batteries"][] = [
|
||||
"idx" => $c["idx"] ?? $i,
|
||||
"typ" => $typ,
|
||||
"SoC_varId" => $socVarId,
|
||||
"SoC_pct" => round($socPct, 3),
|
||||
"Effektive kWh" => round($real_kWh, 3),
|
||||
$sdlDisKW = $sdlShareKW_entladen;
|
||||
$evDisKW = 0.0;
|
||||
$sdlChKW = $sdlShareKW_laden;
|
||||
$evChKW = $evShareKW_laden;
|
||||
|
||||
"EV_SOC" => round($EV_SOC, 3),
|
||||
"SDL_SOC" => round($SDL_SOC, 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),
|
||||
|
||||
"SDL_Charge_kW" => round($sdlChKW, 3),
|
||||
"SDL_Discharge_kW" => round($sdlDisKW, 3),
|
||||
"EV_Charge_kW" => round($evChKW, 3),
|
||||
"EV_Discharge_kW" => round($evDisKW, 3),
|
||||
];
|
||||
$real_kWh_ev = 0.0;
|
||||
$real_kWh_sdl = $real_kWh;
|
||||
}
|
||||
|
||||
// ===================================================
|
||||
// Integrator: nur laufen lassen wenn Soll != 0W
|
||||
// ===================================================
|
||||
$pEvSoll = (float)GetValue($this->GetIDForIdent("Nennleistung_Soll_EV"));
|
||||
$pSdlSoll = (float)GetValue($this->GetIDForIdent("Nennleistung_Soll_SDL"));
|
||||
|
||||
$eps = 1.0; // 1W-Schwellwert
|
||||
$evActive = (abs($pEvSoll) > $eps);
|
||||
$sdlActive = (abs($pSdlSoll) > $eps);
|
||||
|
||||
// 1) Initialisierung: Konto beim ersten Mal auf Referenz setzen
|
||||
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
|
||||
$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
|
||||
|
||||
if ($evActive) {
|
||||
$Eev += $alpha * ($real_kWh_ev_ges - $Eev);
|
||||
}
|
||||
if ($sdlActive) {
|
||||
$Esdl += $alpha * ($real_kWh_sdl_ges - $Esdl);
|
||||
}
|
||||
|
||||
// nach dem angleichen nochmals 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;
|
||||
// Null/Full
|
||||
if ($real_kWh <= 0.0) {
|
||||
$sdlDisKW = 0.0;
|
||||
$real_kWh_ev = 0.0;
|
||||
$real_kWh_sdl = 0.0;
|
||||
} elseif ($real_kWh >= $capKWh) {
|
||||
$sdlChKW = 0.0;
|
||||
$real_kWh_ev = $EV_kWh;
|
||||
$real_kWh_sdl = $SDL_kWh;
|
||||
}
|
||||
|
||||
// 5) Prozentwerte aus den integrierten 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;
|
||||
$real_kWh_ev = max(0.0, $real_kWh_ev);
|
||||
$real_kWh_sdl = max(0.0, $real_kWh_sdl);
|
||||
|
||||
// Output setzen
|
||||
$this->SetIdentValue("SDL_Pos", round($sdlPosPct, 3));
|
||||
$this->SetIdentValue("SoC_EV", round($evPosPct, 3));
|
||||
// Summen
|
||||
$totalCapKWh += $capKWh;
|
||||
|
||||
$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));
|
||||
$this->SetIdentValue("P_EV_entladen", round($evDisKW_ges * 1000.0, 0));
|
||||
$sdlDisKW_ges += $sdlDisKW;
|
||||
$evDisKW_ges += $evDisKW;
|
||||
$sdlChKW_ges += $sdlChKW;
|
||||
$evChKW_ges += $evChKW;
|
||||
|
||||
$calc["total"] = [
|
||||
"SDL_SoC_pct" => round($sdlPosPct, 3),
|
||||
"EV_SoC_pct" => round($evPosPct, 3),
|
||||
$real_kWh_ev_ges += $real_kWh_ev;
|
||||
$real_kWh_sdl_ges += $real_kWh_sdl;
|
||||
|
||||
"SDL_kWh_total" => round($SDL_kWh_ges, 3),
|
||||
"EV_kWh_total" => round($EV_kWh_ges, 3),
|
||||
$SDL_kWh_ges += $SDL_kWh;
|
||||
$EV_kWh_ges += $EV_kWh;
|
||||
|
||||
"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),
|
||||
$calc["batteries"][] = [
|
||||
"idx" => $c["idx"] ?? $i,
|
||||
"typ" => $typ,
|
||||
"SoC_varId" => $socVarId,
|
||||
"SoC_pct" => round($socPct, 3),
|
||||
"Effektive kWh" => round($real_kWh, 3),
|
||||
|
||||
"totalCap_kWh" => round($totalCapKWh, 3)
|
||||
"EV_SOC" => round($EV_SOC, 3),
|
||||
"SDL_SOC" => round($SDL_SOC, 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),
|
||||
|
||||
"SDL_Charge_kW" => round($sdlChKW, 3),
|
||||
"SDL_Discharge_kW" => round($sdlDisKW, 3),
|
||||
"EV_Charge_kW" => round($evChKW, 3),
|
||||
"EV_Discharge_kW" => round($evDisKW, 3),
|
||||
];
|
||||
|
||||
$this->SetIdentValue("CalcJSON", json_encode($calc, JSON_PRETTY_PRINT));
|
||||
$this->ApplySetpoints();
|
||||
// ==================================
|
||||
// Integrator: neue LastP für nächstes Intervall speichern
|
||||
// ==================================
|
||||
$aktEV = (float)GetValue($this->GetIDForIdent("Aktuelle_Leistung_EV"));
|
||||
$aktSDL = (float)GetValue($this->GetIDForIdent("Aktuelle_Leistung_SDL"));
|
||||
|
||||
$this->SetBuffer("Int_LastP_EV_W", (string)$aktEV);
|
||||
$this->SetBuffer("Int_LastP_SDL_W", (string)$aktSDL);
|
||||
$this->SetBuffer("Int_LastTs", (string)$now);
|
||||
|
||||
$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); // ✅ Tür immer wieder aufschließen
|
||||
}
|
||||
|
||||
// =====================
|
||||
// Nach foreach: Durchschnitts-SoC + Reset-Logik
|
||||
// =====================
|
||||
$socAvg = ($socCapSum > 0.0) ? ($socWeighted / $socCapSum) : 0.0;
|
||||
|
||||
$resetTarget = 50.0;
|
||||
$resetTol = 0.5; // bei 2s Update: 0.2..0.5 je nach SoC-Auflösung
|
||||
|
||||
$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");
|
||||
}
|
||||
|
||||
// Reset bei SoC~50%: hart auf Referenz (du akzeptierst Sprung)
|
||||
if ($doReset) {
|
||||
$Eev = $real_kWh_ev_ges;
|
||||
$Esdl = $real_kWh_sdl_ges;
|
||||
}
|
||||
|
||||
// Integration nur wenn aktiv, sonst hart auf Referenz
|
||||
if ($evActive) {
|
||||
$Eev += (($lastPEV / 1000.0) * $dtH);
|
||||
} else {
|
||||
$Eev = $real_kWh_ev_ges;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
// 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;
|
||||
|
||||
// Output setzen
|
||||
$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));
|
||||
$this->SetIdentValue("P_EV_entladen", round($evDisKW_ges * 1000.0, 0));
|
||||
|
||||
$calc["total"] = [
|
||||
"SDL_SoC_pct" => round($sdlPosPct, 3),
|
||||
"EV_SoC_pct" => round($evPosPct, 3),
|
||||
|
||||
"SDL_kWh_total" => round($SDL_kWh_ges, 3),
|
||||
"EV_kWh_total" => round($EV_kWh_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),
|
||||
|
||||
"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
|
||||
// ==================================
|
||||
$aktEV = (float)GetValue($this->GetIDForIdent("Aktuelle_Leistung_EV"));
|
||||
$aktSDL = (float)GetValue($this->GetIDForIdent("Aktuelle_Leistung_SDL"));
|
||||
|
||||
$this->SetBuffer("Int_LastP_EV_W", (string)$aktEV);
|
||||
$this->SetBuffer("Int_LastP_SDL_W", (string)$aktSDL);
|
||||
$this->SetBuffer("Int_LastTs", (string)$now);
|
||||
|
||||
$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);
|
||||
}
|
||||
}
|
||||
|
||||
private function BuildBatteryCache(bool $force): void
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user