no message

This commit is contained in:
belevo\mh
2026-01-22 16:57:48 +01:00
parent 83bf7c5907
commit 18cbf73cd0

View File

@@ -6,20 +6,16 @@ class Bat_EV_SDL extends IPSModule
{ {
parent::Create(); parent::Create();
// Properties
$this->RegisterPropertyString("Batteries", "[]"); $this->RegisterPropertyString("Batteries", "[]");
$this->RegisterPropertyInteger("SDL_Leistung", 0); // W $this->RegisterPropertyInteger("SDL_Leistung", 0); // W
$this->RegisterPropertyInteger("UpdateInterval", 5); // Minuten $this->RegisterPropertyInteger("UpdateInterval", 5); // Minuten
// Status
$this->RegisterVariableBoolean("State", "Aktiv", "~Switch", 1); $this->RegisterVariableBoolean("State", "Aktiv", "~Switch", 1);
$this->EnableAction("State"); $this->EnableAction("State");
// Fenster-Zustände
$this->RegisterVariableFloat("SDL_Pos", "SDL Fensterposition (%)", "", 10); $this->RegisterVariableFloat("SDL_Pos", "SDL Fensterposition (%)", "", 10);
$this->RegisterVariableFloat("SoC_EV", "EV Fenster-Füllstand (%)", "", 11); $this->RegisterVariableFloat("SoC_EV", "EV Fenster-Füllstand (%)", "", 11);
// Leistungsgrenzen
$this->RegisterVariableFloat("P_SDL_max", "SDL max (W)", "", 20); $this->RegisterVariableFloat("P_SDL_max", "SDL max (W)", "", 20);
$this->RegisterVariableFloat("P_SDL_laden", "P SDL laden max (W)", "", 21); $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_SDL_entladen", "P SDL entladen max (W)", "", 22);
@@ -28,10 +24,8 @@ class Bat_EV_SDL extends IPSModule
$this->RegisterVariableFloat("P_EV_laden", "P EV laden max (W)", "", 31); $this->RegisterVariableFloat("P_EV_laden", "P EV laden max (W)", "", 31);
$this->RegisterVariableFloat("P_EV_entladen", "P EV entladen max (W)", "", 32); $this->RegisterVariableFloat("P_EV_entladen", "P EV entladen max (W)", "", 32);
// Debug
$this->RegisterVariableString("CalcJSON", "Berechnung (JSON)", "", 99); $this->RegisterVariableString("CalcJSON", "Berechnung (JSON)", "", 99);
// Timer (Prefix: GEF)
$this->RegisterTimer("UpdateTimer", 0, 'GEF_Update($_IPS["TARGET"]);'); $this->RegisterTimer("UpdateTimer", 0, 'GEF_Update($_IPS["TARGET"]);');
} }
@@ -40,11 +34,7 @@ class Bat_EV_SDL extends IPSModule
parent::ApplyChanges(); parent::ApplyChanges();
$intervalMin = (int)$this->ReadPropertyInteger("UpdateInterval"); $intervalMin = (int)$this->ReadPropertyInteger("UpdateInterval");
if ($intervalMin > 0) { $this->SetTimerInterval("UpdateTimer", ($intervalMin > 0) ? $intervalMin * 60 * 1000 : 0);
$this->SetTimerInterval("UpdateTimer", $intervalMin * 60 * 1000);
} else {
$this->SetTimerInterval("UpdateTimer", 0);
}
$this->Update(); $this->Update();
} }
@@ -53,32 +43,21 @@ class Bat_EV_SDL extends IPSModule
{ {
if ($Ident === "State") { if ($Ident === "State") {
SetValue($this->GetIDForIdent("State"), (bool)$Value); SetValue($this->GetIDForIdent("State"), (bool)$Value);
if ((bool)$Value) { if ((bool)$Value) $this->Update();
$this->Update();
}
return; return;
} }
throw new Exception("Invalid Ident: " . $Ident); throw new Exception("Invalid Ident: " . $Ident);
} }
/**
* Fenster-Modell:
* Emin = Ereq, Emax = Cap - Ereq
* EV darf nur zwischen Emin..Emax arbeiten
*/
public function Update() public function Update()
{ {
if (!GetValue($this->GetIDForIdent("State"))) { if (!GetValue($this->GetIDForIdent("State"))) return;
return;
}
$batteries = json_decode($this->ReadPropertyString("Batteries"), true); $batteries = json_decode($this->ReadPropertyString("Batteries"), true);
if (!is_array($batteries) || count($batteries) === 0) { if (!is_array($batteries) || count($batteries) === 0) return;
return;
}
$sdlPowerW = max(0, (int)$this->ReadPropertyInteger("SDL_Leistung")); $sdlPowerW = max(0, (int)$this->ReadPropertyInteger("SDL_Leistung"));
$reserveH = 0.5; // 30 Minuten fix $reserveH = 0.5;
$eps = 0.0001; $eps = 0.0001;
// Summe Max-Leistungen // Summe Max-Leistungen
@@ -88,25 +67,18 @@ class Bat_EV_SDL extends IPSModule
if ($p > 0) $sumBatPowerW += $p; if ($p > 0) $sumBatPowerW += $p;
} }
if ($sumBatPowerW <= 0) return; if ($sumBatPowerW <= 0) return;
// SDL begrenzen
if ($sdlPowerW > $sumBatPowerW) $sdlPowerW = $sumBatPowerW; if ($sdlPowerW > $sumBatPowerW) $sdlPowerW = $sumBatPowerW;
// Summen Leistung // Summen Leistung
$P_SDL_max = 0.0; $P_SDL_max = 0.0; $P_EV_max = 0.0;
$P_EV_max = 0.0; $P_SDL_laden = 0.0; $P_SDL_entladen = 0.0;
$P_SDL_laden = 0.0; $P_EV_laden = 0.0; $P_EV_entladen = 0.0;
$P_SDL_entladen = 0.0;
$P_EV_laden = 0.0;
$P_EV_entladen = 0.0;
// EV-Fenster-Füllstand (systemweit gewichtet) // EV-Füllstand (gewichtet über FensterkWh)
$sumEV = 0.0; $sumEV = 0.0; $sumEVcap = 0.0;
$sumEVcap = 0.0;
// SDL Fensterposition (JETZT GEWICHTET nach Fenstergröße) // SDL_Pos (gewichtet über pSDL!)
$sumPosWeighted = 0.0; $sumPos_pSDL = 0.0; $sum_pSDL = 0.0;
$sumPosWeight = 0.0;
$calc = []; $calc = [];
@@ -123,53 +95,59 @@ class Bat_EV_SDL extends IPSModule
// SDL-Leistungsanteil // SDL-Leistungsanteil
$pSDL = min(($sdlPowerW * $pBat) / $sumBatPowerW, (float)$pBat); $pSDL = min(($sdlPowerW * $pBat) / $sumBatPowerW, (float)$pBat);
if ($pSDL < 0) $pSDL = 0.0; if ($pSDL < 0) $pSDL = 0.0;
$pEV = max(0.0, $pBat - $pSDL);
$pEV = max(0.0, $pBat - $pSDL);
$P_SDL_max += $pSDL; $P_SDL_max += $pSDL;
$P_EV_max += $pEV; $P_EV_max += $pEV;
// Reserveenergie für 30 min // Reserveenergie 30min
$Ereq = ($pSDL * $reserveH) / 1000.0; $Ereq = ($pSDL * $reserveH) / 1000.0;
// Fenster // Fenster
$Emin = $Ereq; $Emin = $Ereq;
$Emax = $cap - $Ereq; $Emax = $cap - $Ereq;
$range = $Emax - $Emin; // = cap - 2*Ereq $range = $Emax - $Emin;
$EVcap = max(0.0, $range); $EVcap = max(0.0, $range);
// SDL möglich? // SDL möglich?
if ($E >= $Emin) $P_SDL_entladen += $pSDL; $canSDLdis = ($E + $eps) >= $Emin;
if (($cap - $E) >= $Ereq) $P_SDL_laden += $pSDL; $canSDLch = (($cap - $E) + $eps) >= $Ereq;
if ($canSDLdis) $P_SDL_entladen += $pSDL;
if ($canSDLch) $P_SDL_laden += $pSDL;
// EV möglich? // EV möglich?
if ($E > $Emin) $P_EV_entladen += $pEV; if ($E > $Emin + $eps) $P_EV_entladen += $pEV;
if ($E < $Emax) $P_EV_laden += $pEV; if ($E < $Emax - $eps) $P_EV_laden += $pEV;
// EV Energie im Fenster (für SoC_EV) // EV-Füllstand
$EV = 0.0; $EV = 0.0;
$EVpct = 0.0;
if ($EVcap > $eps) { if ($EVcap > $eps) {
$EV = max(0.0, min($E - $Emin, $EVcap)); $EV = max(0.0, min($E - $Emin, $EVcap));
$sumEV += $EV; $sumEV += $EV;
$sumEVcap += $EVcap; $sumEVcap += $EVcap;
$EVpct = ($EV / $EVcap) * 100.0;
} }
// SDL Fensterposition (0..100), GEWICHTET nach EVcap // SDL Fensterposition (0..100) und gewichtete Summe über pSDL
if ($EVcap > $eps) { $posPct = 0.0;
$pos = ($E - $Emin) / $EVcap; // 0..1 if ($range > $eps) {
$pos = ($E - $Emin) / $range; // 0..1
$pos = max(0.0, min(1.0, $pos)); $pos = max(0.0, min(1.0, $pos));
$posPct = $pos * 100.0; $posPct = $pos * 100.0;
}
$sumPosWeighted += $posPct * $EVcap; if ($pSDL > $eps) {
$sumPosWeight += $EVcap; $sumPos_pSDL += $posPct * $pSDL;
} else { $sum_pSDL += $pSDL;
$posPct = 0.0;
} }
$calc[] = [ $calc[] = [
"typ" => $typ, "typ" => $typ,
"soc_pct" => round($socPct, 2), "soc_pct" => round($socPct, 2),
"E_kWh" => round($E, 3), "E_kWh" => round($E, 3),
"cap_kWh" => round($cap, 3),
"pSDL_W" => round($pSDL, 0), "pSDL_W" => round($pSDL, 0),
"pEV_W" => round($pEV, 0), "pEV_W" => round($pEV, 0),
@@ -179,12 +157,15 @@ class Bat_EV_SDL extends IPSModule
"Emax_kWh" => round($Emax, 3), "Emax_kWh" => round($Emax, 3),
"EVcap_kWh" => round($EVcap, 3), "EVcap_kWh" => round($EVcap, 3),
"SDL_pos_pct" => round($posPct, 2) "SDL_pos_pct" => round($posPct, 2),
"EV_fill_pct" => round($EVpct, 2),
"canSDLcharge" => $canSDLch,
"canSDLdischarge" => $canSDLdis
]; ];
} }
// Ergebnisse $SDL_Pos = ($sum_pSDL > $eps) ? ($sumPos_pSDL / $sum_pSDL) : 0.0;
$SDL_Pos = ($sumPosWeight > $eps) ? ($sumPosWeighted / $sumPosWeight) : 0.0;
$SoC_EV = ($sumEVcap > $eps) ? (($sumEV / $sumEVcap) * 100.0) : 0.0; $SoC_EV = ($sumEVcap > $eps) ? (($sumEV / $sumEVcap) * 100.0) : 0.0;
SetValue($this->GetIDForIdent("SDL_Pos"), round($SDL_Pos, 2)); SetValue($this->GetIDForIdent("SDL_Pos"), round($SDL_Pos, 2));
@@ -201,19 +182,14 @@ class Bat_EV_SDL extends IPSModule
SetValue($this->GetIDForIdent("CalcJSON"), json_encode($calc, JSON_PRETTY_PRINT)); SetValue($this->GetIDForIdent("CalcJSON"), json_encode($calc, JSON_PRETTY_PRINT));
} }
/**
* SoC in Prozent (0..100). Akzeptiert 0..1 als 0..100.
*/
private function ReadSocPercent(int $varId): float private function ReadSocPercent(int $varId): float
{ {
if ($varId <= 0 || !IPS_VariableExists($varId)) return 0.0; if ($varId <= 0 || !IPS_VariableExists($varId)) return 0.0;
$v = GetValue($varId); $v = GetValue($varId);
if (!is_numeric($v)) return 0.0; if (!is_numeric($v)) return 0.0;
$f = (float)$v; $f = (float)$v;
if ($f >= 0.0 && $f <= 1.0) $f *= 100.0; if ($f >= 0.0 && $f <= 1.0) $f *= 100.0;
return max(0.0, min(100.0, $f)); return max(0.0, min(100.0, $f));
} }
} }