Files
Symcon_Belevo_Energiemanage…/Bat_EV_SDL/module.php
T
2026-01-22 16:38:00 +01:00

185 lines
5.9 KiB
PHP

<?php
class Bat_EV_SDL extends IPSModule
{
public function Create()
{
parent::Create();
// Properties
$this->RegisterPropertyString("Batteries", "[]");
$this->RegisterPropertyInteger("SDL_Leistung", 0); // W
$this->RegisterPropertyInteger("UpdateInterval", 5); // Minuten
// Status
$this->RegisterVariableBoolean("State", "Aktiv", "~Switch", 1);
$this->EnableAction("State");
// Fenster-Zustände
$this->RegisterVariableFloat("SDL_Pos", "SDL Fensterposition (%)", "", 10);
$this->RegisterVariableFloat("SoC_EV", "EV Fenster-Füllstand (%)", "", 11);
// Leistungsgrenzen
$this->RegisterVariableFloat("P_SDL_max", "SDL max (W)", "", 20);
$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_EV_max", "EV max (W)", "", 30);
$this->RegisterVariableFloat("P_EV_laden", "P EV laden max (W)", "", 31);
$this->RegisterVariableFloat("P_EV_entladen", "P EV entladen max (W)", "", 32);
// Debug
$this->RegisterVariableString("CalcJSON", "Berechnung (JSON)", "", 99);
// Timer (Prefix: GEF)
$this->RegisterTimer("UpdateTimer", 0, 'GEF_Update($_IPS["TARGET"]);');
}
public function ApplyChanges()
{
parent::ApplyChanges();
$intervalMin = (int)$this->ReadPropertyInteger("UpdateInterval");
if ($intervalMin > 0) {
$this->SetTimerInterval("UpdateTimer", $intervalMin * 60 * 1000);
} else {
$this->SetTimerInterval("UpdateTimer", 0);
}
$this->Update();
}
public function RequestAction($Ident, $Value)
{
if ($Ident === "State") {
SetValue($this->GetIDForIdent("State"), (bool)$Value);
if ($Value) {
$this->Update();
}
return;
}
throw new Exception("Invalid Ident: " . $Ident);
}
/**
* Fenster-Modell:
* Emin = Ereq, Emax = Cap - Ereq
* EV darf nur zwischen Emin..Emax arbeiten
*/
public function Update()
{
if (!GetValue($this->GetIDForIdent("State"))) {
return;
}
$batteries = json_decode($this->ReadPropertyString("Batteries"), true);
if (!is_array($batteries) || count($batteries) === 0) {
return;
}
$sdlPowerW = max(0, (int)$this->ReadPropertyInteger("SDL_Leistung"));
$reserveH = 0.5;
$eps = 0.0001;
// Summe Max-Leistungen
$sumBatPowerW = 0;
foreach ($batteries as $b) {
$p = (int)($b["powerbat"] ?? 0);
if ($p > 0) $sumBatPowerW += $p;
}
if ($sumBatPowerW <= 0) return;
if ($sdlPowerW > $sumBatPowerW) $sdlPowerW = $sumBatPowerW;
// Summen Leistung
$P_SDL_max = 0.0;
$P_EV_max = 0.0;
$P_SDL_laden = 0.0;
$P_SDL_entladen = 0.0;
$P_EV_laden = 0.0;
$P_EV_entladen = 0.0;
// Fensterposition & EV-Füllstand
$sumPos = 0.0; $cntPos = 0;
$sumEV = 0.0; $sumEVcap = 0.0;
$calc = [];
foreach ($batteries as $b) {
$pBat = (int)($b["powerbat"] ?? 0);
$cap = (float)($b["capazity"] ?? 0);
if ($pBat <= 0 || $cap <= 0) continue;
$socPct = $this->ReadSocPercent((int)($b["soc"] ?? 0));
$E = $cap * $socPct / 100.0;
// SDL-Leistungsanteil
$pSDL = min(($sdlPowerW * $pBat) / $sumBatPowerW, $pBat);
$pEV = max(0.0, $pBat - $pSDL);
$P_SDL_max += $pSDL;
$P_EV_max += $pEV;
$Ereq = ($pSDL * $reserveH) / 1000.0;
$Emin = $Ereq;
$Emax = $cap - $Ereq;
// SDL möglich?
if ($E >= $Emin) $P_SDL_entladen += $pSDL;
if (($cap - $E) >= $Ereq) $P_SDL_laden += $pSDL;
// EV möglich?
if ($E > $Emin) $P_EV_entladen += $pEV;
if ($E < $Emax) $P_EV_laden += $pEV;
// Fensterposition
$range = $Emax - $Emin;
if ($range > $eps) {
$pos = ($E - $Emin) / $range;
$pos = max(0.0, min(1.0, $pos));
$sumPos += $pos * 100.0;
$cntPos++;
}
// EV-Füllstand
$EVcap = max(0.0, $range);
$EV = max(0.0, min($E - $Emin, $EVcap));
$sumEV += $EV;
$sumEVcap += $EVcap;
$calc[] = [
"typ" => $b["typ"] ?? "",
"E_kWh" => round($E, 3),
"Emin" => round($Emin, 3),
"Emax" => round($Emax, 3),
"pSDL_W" => round($pSDL, 0),
"pEV_W" => round($pEV, 0)
];
}
// Setzen Variablen
SetValue($this->GetIDForIdent("SDL_Pos"), $cntPos ? round($sumPos / $cntPos, 2) : 0.0);
SetValue($this->GetIDForIdent("SoC_EV"), $sumEVcap > $eps ? round(($sumEV / $sumEVcap) * 100.0, 2) : 0.0);
SetValue($this->GetIDForIdent("P_SDL_max"), round($P_SDL_max, 0));
SetValue($this->GetIDForIdent("P_SDL_laden"), round($P_SDL_laden, 0));
SetValue($this->GetIDForIdent("P_SDL_entladen"), round($P_SDL_entladen, 0));
SetValue($this->GetIDForIdent("P_EV_max"), round($P_EV_max, 0));
SetValue($this->GetIDForIdent("P_EV_laden"), round($P_EV_laden, 0));
SetValue($this->GetIDForIdent("P_EV_entladen"), round($P_EV_entladen, 0));
SetValue($this->GetIDForIdent("CalcJSON"), json_encode($calc, JSON_PRETTY_PRINT));
}
private function ReadSocPercent(int $varId): float
{
if ($varId <= 0 || !IPS_VariableExists($varId)) return 0.0;
$v = GetValue($varId);
if (!is_numeric($v)) return 0.0;
$f = (float)$v;
if ($f >= 0 && $f <= 1) $f *= 100.0;
return max(0.0, min(100.0, $f));
}
}