no message

This commit is contained in:
belevo\mh
2026-01-22 18:40:22 +01:00
parent 225e254366
commit 3390330a82

View File

@@ -64,12 +64,14 @@ public function Update()
} }
$batteries = json_decode($this->ReadPropertyString("Batteries"), true); $batteries = json_decode($this->ReadPropertyString("Batteries"), true);
if (!is_array($batteries)) $batteries = []; if (!is_array($batteries)) {
$batteries = [];
}
$sdlTotalW = (float)$this->ReadPropertyInteger("SDL_Leistung"); // W $sdlTotalW = (float)$this->ReadPropertyInteger("SDL_Leistung"); // W
if ($sdlTotalW < 0) $sdlTotalW = 0; if ($sdlTotalW < 0) $sdlTotalW = 0;
// 30 Minuten // 30 Minuten Fenster
$hours = 0.5; $hours = 0.5;
// Summe Batterie-Maxleistungen // Summe Batterie-Maxleistungen
@@ -78,6 +80,7 @@ public function Update()
$p = (float)($b["powerbat"] ?? 0); $p = (float)($b["powerbat"] ?? 0);
if ($p > 0) $sumBatPowerW += $p; if ($p > 0) $sumBatPowerW += $p;
} }
if ($sumBatPowerW <= 0.0) { if ($sumBatPowerW <= 0.0) {
$this->WriteAllZero("sumBatPowerW=0"); $this->WriteAllZero("sumBatPowerW=0");
return; return;
@@ -92,15 +95,22 @@ public function Update()
$sdlNeedTotalKWh = ($sdlTotalW / 1000.0) * $hours; $sdlNeedTotalKWh = ($sdlTotalW / 1000.0) * $hours;
$sumSdlAvailKWh = 0.0; // wirklich lieferbar (energiebegrenzt) // Energie totals
$sumSdlAvailKWh = 0.0; // tatsächlich lieferbare SDL-Energie (energiebegrenzt)
$sumEvKWh = 0.0; // Restenergie nach SDL $sumEvKWh = 0.0; // Restenergie nach SDL
// Leistung totals (kW)
$sumSdlChargeKW = 0.0; $sumSdlChargeKW = 0.0;
$sumEvChargeKW = 0.0; $sumEvChargeKW = 0.0;
$sumSdlDisKW = 0.0; $sumSdlDisKW = 0.0;
$sumEvDisKW = 0.0; $sumEvDisKW = 0.0;
$calc = [ $calc = [
"inputs" => [
"SDL_Leistung_W" => round($sdlTotalW, 0),
"SumBatPower_W" => round($sumBatPowerW, 0),
"hours" => $hours
],
"batteries" => [], "batteries" => [],
"total" => [] "total" => []
]; ];
@@ -108,12 +118,11 @@ public function Update()
foreach ($batteries as $idx => $b) { foreach ($batteries as $idx => $b) {
$pBatW = max(0.0, (float)($b["powerbat"] ?? 0)); $pBatW = max(0.0, (float)($b["powerbat"] ?? 0));
$capKWh = max(0.0, (float)($b["capazity"] ?? 0)); $capKWh = max(0.0, (float)($b["capazity"] ?? 0));
// soc ist SelectVariable -> Variable-ID
$socVarId = (int)($b["soc"] ?? 0); $socVarId = (int)($b["soc"] ?? 0);
$socPct = $this->ReadSocPercent($socVarId); $socPct = $this->ReadSocPercent($socVarId);
if ($socPct < 0) $socPct = 0;
if ($socPct > 100) $socPct = 100;
$totalCapKWh += $capKWh; $totalCapKWh += $capKWh;
// gespeicherte Energie // gespeicherte Energie
@@ -133,25 +142,25 @@ public function Update()
$sdlShareKW = $sdlShareW / 1000.0; $sdlShareKW = $sdlShareW / 1000.0;
$evShareKW = $evShareW / 1000.0; $evShareKW = $evShareW / 1000.0;
// under_grenze = SDL Energiebedarf in 30min // under_grenze_kWh = SDL Energiebedarf in 30min
$underKWh = $sdlShareKW * $hours; $underKWh = $sdlShareKW * $hours;
// tatsächlich für SDL verfügbare Energie // tatsächlich für SDL verfügbare Energie (energiebegrenzt)
$sdlAvailKWh = min($storedKWh, $underKWh); $sdlAvailKWh = min($storedKWh, $underKWh);
// EV Energie = Restenergie nach SDL (aber nicht negativ) // up_grenze_kWh = EV Energie (Restenergie nach SDL)
$upKWh = max(0.0, $storedKWh - $sdlAvailKWh); $upKWh = max(0.0, $storedKWh - $sdlAvailKWh);
// --- RUNTERREGELN (Discharge) --- // Runterregeln Discharge anhand Energie im Zeitfenster
// Max kW aus Energie in 30min: P = E / hours // Pmax(kW) = E(kWh) / hours
$sdlDisKW = ($hours > 0) ? min($sdlShareKW, $sdlAvailKWh / $hours) : 0.0; $sdlDisKW = ($hours > 0) ? min($sdlShareKW, $sdlAvailKWh / $hours) : 0.0;
$evDisKW = ($hours > 0) ? min($evShareKW, $upKWh / $hours) : 0.0; $evDisKW = ($hours > 0) ? min($evShareKW, $upKWh / $hours) : 0.0;
// Charge ist einfach die reservierte Leistung // Charge = reservierte Leistungsanteile
$sdlChKW = $sdlShareKW; $sdlChKW = $sdlShareKW;
$evChKW = $evShareKW; $evChKW = $evShareKW;
// Totals sammeln // Totals
$sumSdlAvailKWh += $sdlAvailKWh; $sumSdlAvailKWh += $sdlAvailKWh;
$sumEvKWh += $upKWh; $sumEvKWh += $upKWh;
@@ -166,7 +175,10 @@ public function Update()
"P_bat_W" => round($pBatW, 0), "P_bat_W" => round($pBatW, 0),
"Cap_kWh" => round($capKWh, 3), "Cap_kWh" => round($capKWh, 3),
"SoC_varId" => $socVarId,
"SoC_pct" => round($socPct, 3), "SoC_pct" => round($socPct, 3),
"Stored_kWh"=> round($storedKWh, 3),
"SDL_Power_kW" => round($sdlShareKW, 3), "SDL_Power_kW" => round($sdlShareKW, 3),
"EV_Power_kW" => round($evShareKW, 3), "EV_Power_kW" => round($evShareKW, 3),
@@ -179,10 +191,6 @@ public function Update()
"EV_Charge_kW" => round($evChKW, 3), "EV_Charge_kW" => round($evChKW, 3),
"EV_Discharge_kW" => round($evDisKW, 3), "EV_Discharge_kW" => round($evDisKW, 3),
"SoC_varId" => $socVarId,
"SoC_pct" => round($socPct, 3),
"Stored_kWh"=> round($storedKWh, 3),
]; ];
} }
@@ -192,9 +200,9 @@ public function Update()
$sdlPosPct = ($sumSdlAvailKWh / $sdlNeedTotalKWh) * 100.0; $sdlPosPct = ($sumSdlAvailKWh / $sdlNeedTotalKWh) * 100.0;
} }
// EV%: ich nehme "EV verfügbar / (SumCap - SDL_need)" wie wir zuletzt hatten // EV%: Anteil der EV-Restenergie bezogen auf "max EV Energie" im gespeicherten Zustand.
// Wenn du lieber EV% auf Basis "SumStored - SDL_avail" willst, sag Bescheid. // Max EV Energie (im Speicher) = totalStored - SDL_needTotal (aber nicht <0)
$evMaxKWh = max(0.0, $totalCapKWh - $sdlNeedTotalKWh); $evMaxKWh = max(0.0, $totalStoredKWh - $sdlNeedTotalKWh);
$evPosPct = 0.0; $evPosPct = 0.0;
if ($evMaxKWh > 0.000001) { if ($evMaxKWh > 0.000001) {
$evPosPct = ($sumEvKWh / $evMaxKWh) * 100.0; $evPosPct = ($sumEvKWh / $evMaxKWh) * 100.0;
@@ -204,7 +212,7 @@ public function Update()
$this->SetIdentValue("SDL_Pos", round($sdlPosPct, 3)); $this->SetIdentValue("SDL_Pos", round($sdlPosPct, 3));
$this->SetIdentValue("SoC_EV", round($evPosPct, 3)); $this->SetIdentValue("SoC_EV", round($evPosPct, 3));
// W-Variablen im Objekt: wir geben die totals in W aus // W-Variablen
$this->SetIdentValue("P_SDL_max", round($sumSdlChargeKW * 1000.0, 0)); $this->SetIdentValue("P_SDL_max", round($sumSdlChargeKW * 1000.0, 0));
$this->SetIdentValue("P_SDL_laden", round($sumSdlChargeKW * 1000.0, 0)); $this->SetIdentValue("P_SDL_laden", round($sumSdlChargeKW * 1000.0, 0));
$this->SetIdentValue("P_SDL_entladen", round($sumSdlDisKW * 1000.0, 0)); $this->SetIdentValue("P_SDL_entladen", round($sumSdlDisKW * 1000.0, 0));
@@ -226,13 +234,16 @@ public function Update()
"SDL_needTotal_kWh" => round($sdlNeedTotalKWh, 3), "SDL_needTotal_kWh" => round($sdlNeedTotalKWh, 3),
"SDL_availTotal_kWh" => round($sumSdlAvailKWh, 3), "SDL_availTotal_kWh" => round($sumSdlAvailKWh, 3),
"EV_total_kWh" => round($sumEvKWh, 3), "EV_total_kWh" => round($sumEvKWh, 3),
"totalCap_kWh" => round($totalCapKWh, 3), "totalCap_kWh" => round($totalCapKWh, 3),
"totalStored_kWh" => round($totalStoredKWh, 3) "totalStored_kWh" => round($totalStoredKWh, 3),
"EV_max_kWh" => round($evMaxKWh, 3)
]; ];
$this->SetIdentValue("CalcJSON", json_encode($calc, JSON_PRETTY_PRINT)); $this->SetIdentValue("CalcJSON", json_encode($calc, JSON_PRETTY_PRINT));
} }
// ---------------- Helpers ----------------
private function WriteAllZero(string $reason): void private function WriteAllZero(string $reason): void
{ {
@@ -259,4 +270,33 @@ public function Update()
} }
SetValue($id, $value); SetValue($id, $value);
} }
private function ReadSocPercent(int $varId): float
{
// Falls jemand statt Variable direkt 0..100 einträgt
if ($varId >= 0 && $varId <= 100) {
return (float)$varId;
}
if ($varId <= 0 || !IPS_VariableExists($varId)) {
return 0.0;
}
$v = GetValue($varId);
if (!is_numeric($v)) {
return 0.0;
}
$soc = (float)$v;
// 0..1 -> 0..100
if ($soc >= 0.0 && $soc <= 1.0) {
$soc *= 100.0;
}
if ($soc < 0.0) $soc = 0.0;
if ($soc > 100.0) $soc = 100.0;
return $soc;
}
} }