770 lines
25 KiB
PHP
770 lines
25 KiB
PHP
<?php
|
|
|
|
class Bat_EV_SDL_V3_Beta extends IPSModule
|
|
{
|
|
private const HOURS = 0.5; // 30 Minuten
|
|
|
|
public function Create()
|
|
{
|
|
parent::Create();
|
|
|
|
// Properties
|
|
$this->RegisterPropertyString("Batteries", "[]");
|
|
//$this->RegisterPropertyInteger("$upKWh + $underKWh)", 0);
|
|
$this->RegisterPropertyInteger("SDL_Leistung_Laden", 0);
|
|
$this->RegisterPropertyInteger("SDL_Leistung_Entladen", 0); // W
|
|
$this->RegisterPropertyInteger("UpdateInterval", 5); // Minuten
|
|
|
|
// Status
|
|
$this->RegisterVariableBoolean("State", "Aktiv", "~Switch", 1);
|
|
$this->EnableAction("State");
|
|
|
|
// Prozentwerte
|
|
$this->RegisterVariableFloat("SDL_Pos", "SDL Energie verfügbar (%)", "", 10);
|
|
$this->RegisterVariableFloat("SoC_EV", "EV Energie verfügbar (%)", "", 11);
|
|
|
|
// Variablen
|
|
$this->RegisterVariableFloat("Nennleistung_Soll_EV", "Nennleistung Soll EV", "", 2);
|
|
$this->RegisterVariableFloat("Nennleistung_Soll_SDL", "Nennleistung Soll SDL", "", 3);
|
|
$this->RegisterVariableFloat("Aktuelle_Leistung_EV", "Aktuelle Leistung EV", "", 5);
|
|
$this->RegisterVariableFloat("Aktuelle_Leistung_SDL", "Aktuelle Leistung SDL", "", 4);
|
|
$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_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: wichtig -> Prefix muss passen
|
|
$this->RegisterTimer("UpdateTimer", 0, 'GEF_Update($_IPS["TARGET"]);');
|
|
}
|
|
|
|
public function ApplyChanges()
|
|
{
|
|
parent::ApplyChanges();
|
|
|
|
$intervalSec = (int)$this->ReadPropertyInteger("UpdateInterval");
|
|
$this->SetTimerInterval("UpdateTimer", ($intervalSec > 0) ? $intervalSec * 1000 : 0);
|
|
|
|
// Cache neu bauen (force)
|
|
$this->BuildBatteryCache(true);
|
|
|
|
$this->Update();
|
|
}
|
|
|
|
public function RequestAction($Ident, $Value)
|
|
{
|
|
if ($Ident === "State") {
|
|
SetValue($this->GetIDForIdent("State"), (bool)$Value);
|
|
if ((bool)$Value) {
|
|
$this->Update();
|
|
}
|
|
return;
|
|
}
|
|
|
|
throw new Exception("Invalid Ident: " . $Ident);
|
|
}
|
|
|
|
public function Update()
|
|
{
|
|
$semKey = 'BatEVSDL_Update_' . $this->InstanceID;
|
|
|
|
if (!IPS_SemaphoreEnter($semKey, 5000)) {
|
|
$this->SendDebug("Update", "SKIP (Semaphore locked)", 0);
|
|
return;
|
|
}
|
|
|
|
try {
|
|
|
|
if (!GetValue($this->GetIDForIdent("State"))) {
|
|
return;
|
|
}
|
|
|
|
$this->BuildBatteryCache(false);
|
|
|
|
$cache = json_decode($this->GetBufferSafe("BatCacheJSON"), true);
|
|
if (!is_array($cache) || empty($cache["bats"])) {
|
|
$this->WriteAllZero("cache empty/invalid");
|
|
return;
|
|
}
|
|
|
|
// -----------------------------
|
|
// Zeit + Konten laden
|
|
// -----------------------------
|
|
$now = microtime(true);
|
|
|
|
$lastTs = (float)$this->GetBufferSafe("Int_LastTs");
|
|
$Eev = (float)$this->GetBufferSafe("Int_E_EV_kWh");
|
|
$Esdl = (float)$this->GetBufferSafe("Int_E_SDL_kWh");
|
|
|
|
if ($lastTs <= 0.0) {
|
|
$lastTs = $now;
|
|
$Eev = 0.0;
|
|
$Esdl = 0.0;
|
|
}
|
|
|
|
$dtSec = $now - $lastTs;
|
|
if ($dtSec < 0) $dtSec = 0.0;
|
|
if ($dtSec > 3600) $dtSec = 3600.0;
|
|
$dtH = $dtSec / 3600.0;
|
|
|
|
$calc = [
|
|
"inputs" => $cache["inputs"] ?? [],
|
|
"batteries" => [],
|
|
"total" => []
|
|
];
|
|
|
|
// -----------------------------
|
|
// Summen
|
|
// -----------------------------
|
|
$sdlDisKW_ges = 0.0;
|
|
$evDisKW_ges = 0.0;
|
|
$sdlChKW_ges = 0.0;
|
|
$evChKW_ges = 0.0;
|
|
|
|
$real_kWh_ev_ges = 0.0;
|
|
$real_kWh_sdl_ges = 0.0;
|
|
|
|
$SDL_kWh_ges = 0.0;
|
|
$EV_kWh_ges = 0.0;
|
|
$totalCapKWh = 0.0;
|
|
|
|
foreach ($cache["bats"] as $i => $c) {
|
|
|
|
$capKWh = (float)($c["capKWh"] ?? 0.0);
|
|
if ($capKWh <= 0.0) continue;
|
|
|
|
$socVarId = (int)($c["socVarId"] ?? 0);
|
|
$socPct = $this->ReadSocPercent($socVarId);
|
|
|
|
$real_kWh = $capKWh / 100.0 * $socPct;
|
|
|
|
$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);
|
|
|
|
$EV_SOC = 0.0;
|
|
$SDL_SOC = 0.0;
|
|
|
|
$sdlDisKW = 0.0; $evDisKW = 0.0;
|
|
$sdlChKW = 0.0; $evChKW = 0.0;
|
|
|
|
$real_kWh_ev = 0.0;
|
|
$real_kWh_sdl = 0.0;
|
|
|
|
if ($underKWh <= $real_kWh && $real_kWh <= $upKWh) {
|
|
|
|
$win = ($upKWh - $underKWh);
|
|
$SDL_SOC = ($win > 0.0) ? (100.0 * ($real_kWh - $underKWh) / $win) : 0.0;
|
|
$SDL_SOC = max(0.0, min(100.0, $SDL_SOC));
|
|
|
|
$EV_SOC = ($EV_kWh > 0.0) ? (100.0 * ($real_kWh - $underKWh) / $EV_kWh) : 0.0;
|
|
$EV_SOC = max(0.0, min(100.0, $EV_SOC));
|
|
|
|
$sdlDisKW = $sdlShareKW_entladen;
|
|
$evDisKW = $evShareKW_entladen;
|
|
$sdlChKW = $sdlShareKW_laden;
|
|
$evChKW = $evShareKW_laden;
|
|
|
|
$real_kWh_ev = $real_kWh - $underKWh;
|
|
$real_kWh_sdl = $underKWh;
|
|
|
|
} elseif ($real_kWh > $upKWh) {
|
|
|
|
$EV_SOC = 100.0;
|
|
|
|
$sdlDisKW = $sdlShareKW_entladen;
|
|
$evDisKW = $evShareKW_entladen;
|
|
$sdlChKW = $sdlShareKW_laden;
|
|
$evChKW = 0.0; // ✅ EV darf oben nicht laden
|
|
|
|
$real_kWh_ev = $capKWh - ($capKWh - $upKWh + $underKWh);
|
|
$real_kWh_sdl = ($capKWh - $upKWh + $underKWh) - ($capKWh - $real_kWh);
|
|
|
|
} else {
|
|
|
|
$EV_SOC = 0.0;
|
|
|
|
$sdlDisKW = $sdlShareKW_entladen;
|
|
$evDisKW = 0.0; // ✅ EV darf unten nicht entladen
|
|
$sdlChKW = $sdlShareKW_laden;
|
|
$evChKW = $evShareKW_laden;
|
|
|
|
$real_kWh_ev = 0.0;
|
|
$real_kWh_sdl = $real_kWh;
|
|
}
|
|
|
|
$real_kWh_ev = max(0.0, $real_kWh_ev);
|
|
$real_kWh_sdl = max(0.0, $real_kWh_sdl);
|
|
|
|
$totalCapKWh += $capKWh;
|
|
|
|
$sdlDisKW_ges += $sdlDisKW;
|
|
$evDisKW_ges += $evDisKW;
|
|
$sdlChKW_ges += $sdlChKW;
|
|
$evChKW_ges += $evChKW;
|
|
|
|
$real_kWh_ev_ges += $real_kWh_ev;
|
|
$real_kWh_sdl_ges += $real_kWh_sdl;
|
|
|
|
$SDL_kWh_ges += $SDL_kWh;
|
|
$EV_kWh_ges += $EV_kWh;
|
|
|
|
$calc["batteries"][] = [
|
|
"idx" => $c["idx"] ?? $i,
|
|
"typ" => $typ,
|
|
"SoC_varId" => $socVarId,
|
|
"SoC_pct" => round($socPct, 3),
|
|
"Effektive kWh" => round($real_kWh, 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),
|
|
|
|
"SDL_Charge_kW" => round($sdlChKW, 3),
|
|
"SDL_Discharge_kW" => round($sdlDisKW, 3),
|
|
"EV_Charge_kW" => round($evChKW, 3),
|
|
"EV_Discharge_kW" => round($evDisKW, 3),
|
|
];
|
|
}
|
|
|
|
// Maximalleistungen setzen (für Anzeige/Debug)
|
|
$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));
|
|
|
|
$this->SetIdentValue("CalcJSON", json_encode($calc, JSON_PRETTY_PRINT));
|
|
|
|
// -------------------------------------------------
|
|
// Setpoints berechnen/schreiben (setzt auch "Aktuelle_Leistung_*")
|
|
// -------------------------------------------------
|
|
$this->ApplySetpoints();
|
|
|
|
// -------------------------------------------------
|
|
// Jetzt dynamisch integrieren mit "Aktueller Leistung" (Ist-basiert)
|
|
// -------------------------------------------------
|
|
$pEvSoll = (float)GetValue($this->GetIDForIdent("Nennleistung_Soll_EV"));
|
|
$pSdlSoll = (float)GetValue($this->GetIDForIdent("Nennleistung_Soll_SDL"));
|
|
|
|
$epsW = 1.0;
|
|
$evActive = (abs($pEvSoll) > $epsW);
|
|
$sdlActive = (abs($pSdlSoll) > $epsW);
|
|
|
|
$PEV = (float)GetValue($this->GetIDForIdent("Aktuelle_Leistung_EV"));
|
|
$PSDL = (float)GetValue($this->GetIDForIdent("Aktuelle_Leistung_SDL"));
|
|
|
|
// Clamp auf lokale Maxima (kW->W)
|
|
|
|
|
|
|
|
private function BuildBatteryCache(bool $force): void
|
|
{
|
|
$batteriesRaw = $this->ReadPropertyString("Batteries");
|
|
//$sdlTotalW = max(0, (int)$this->ReadPropertyInteger("SDL_Leistung"));
|
|
$sdlTotalW_laden = max(0, (int)$this->ReadPropertyInteger("SDL_Leistung_Laden"));
|
|
$sdlTotalW_entladen = max(0, (int)$this->ReadPropertyInteger("SDL_Leistung_Entladen"));
|
|
$hours = self::HOURS;
|
|
|
|
$hash = md5(json_encode([
|
|
"Batteries" => $batteriesRaw,
|
|
"SDL_W_Laden" => $sdlTotalW_laden,
|
|
"SDL_W_Entladen" => $sdlTotalW_entladen,
|
|
"hours" => $hours
|
|
]));
|
|
|
|
$oldHash = $this->GetBufferSafe("BatCacheHash");
|
|
if (!$force && $oldHash === $hash) {
|
|
return;
|
|
}
|
|
|
|
$batteries = json_decode($batteriesRaw, true);
|
|
if (!is_array($batteries)) $batteries = [];
|
|
|
|
$sumBatPowerW = 0.0;
|
|
foreach ($batteries as $b) {
|
|
$p = (float)($b["powerbat"] ?? 0);
|
|
if ($p > 0) $sumBatPowerW += $p;
|
|
}
|
|
|
|
$cache = [
|
|
"inputs" => [
|
|
"SDL_Leistung_W_laden" => $sdlTotalW_laden,
|
|
"SDL_Leistung_W_entladen" => $sdlTotalW_entladen,
|
|
"SumBatPower_W" => round($sumBatPowerW, 0),
|
|
"hours" => $hours
|
|
],
|
|
"bats" => []
|
|
];
|
|
|
|
if ($sumBatPowerW <= 0.0) {
|
|
$this->SetBuffer("BatCacheHash", $hash);
|
|
$this->SetBuffer("BatCacheJSON", json_encode($cache));
|
|
$this->SendDebug("Cache", "sumBatPowerW=0 -> empty cache", 0);
|
|
return;
|
|
}
|
|
|
|
$sumBatPowerkW = $sumBatPowerW / 1000.0;
|
|
//$sdlTotalkW = $sdlTotalW / 1000.0;
|
|
|
|
$sdlTotalkW_laden = $sdlTotalW_laden / 1000.0;
|
|
$sdlTotalkW_entladen = $sdlTotalW_entladen / 1000.0;
|
|
|
|
foreach ($batteries as $idx => $b) {
|
|
|
|
$pBatW = max(0.0, (float)($b["powerbat"] ?? 0));
|
|
$pBatkW = $pBatW / 1000.0;
|
|
|
|
$capKWh = max(0.0, (float)($b["capazity"] ?? 0));
|
|
if ($capKWh <= 0.0) continue;
|
|
|
|
$socVarId = (int)($b["soc"] ?? 0);
|
|
$typ = (string)($b["typ"] ?? ("Bat " . ($idx + 1)));
|
|
|
|
|
|
// Mit laden und entladen unterschiedlich
|
|
//----------------------------------------------------
|
|
$sdlShareKW_laden = ($sumBatPowerkW > 0.0) ? ($sdlTotalkW_laden / $sumBatPowerkW * $pBatkW) : 0.0;
|
|
$evShareKW_laden = $pBatkW - $sdlShareKW_laden;
|
|
$upKWh = $capKWh - $sdlShareKW_laden * 0.5;
|
|
|
|
$sdlShareKW_entladen = ($sumBatPowerkW > 0.0) ? ($sdlTotalkW_entladen / $sumBatPowerkW * $pBatkW) : 0.0;
|
|
$evShareKW_entladen = $pBatkW - $sdlShareKW_entladen;
|
|
$underKWh = $sdlShareKW_entladen * 0.5;
|
|
|
|
$SDL_kWh = $underKWh + ($capKWh -$upKWh);
|
|
$EV_kWh = $capKWh - $SDL_kWh;
|
|
//----------------------------------------------------
|
|
|
|
$cache["bats"][] = [
|
|
"idx" => $idx,
|
|
"typ" => $typ,
|
|
|
|
"socVarId" => $socVarId,
|
|
"capKWh" => $capKWh,
|
|
|
|
"pBatW" => $pBatW,
|
|
"sdlShareKW_laden" => $sdlShareKW_laden,
|
|
"sdlShareKW_entladen" => $sdlShareKW_entladen,
|
|
"evShareKW_laden" => $evShareKW_laden,
|
|
"evShareKW_entladen" => $evShareKW_entladen,
|
|
|
|
"underKWh" => $underKWh,
|
|
"upKWh" => $upKWh,
|
|
|
|
"SDL_kWh_total" => $SDL_kWh,
|
|
"EV_kWh_total" => $EV_kWh
|
|
];
|
|
}
|
|
|
|
$this->SetBuffer("BatCacheHash", $hash);
|
|
$this->SetBuffer("BatCacheJSON", json_encode($cache));
|
|
|
|
$this->SendDebug("Cache", "Battery cache rebuilt (" . count($cache["bats"]) . " bats)", 0);
|
|
}
|
|
|
|
private function GetBufferSafe(string $name): string
|
|
{
|
|
$v = $this->GetBuffer($name);
|
|
return is_string($v) ? $v : "";
|
|
}
|
|
|
|
private function WriteAllZero(string $reason): void
|
|
{
|
|
$this->SetIdentValue("SDL_Pos", 0.0);
|
|
$this->SetIdentValue("SoC_EV", 0.0);
|
|
|
|
$this->SetIdentValue("P_SDL_laden", 0.0);
|
|
$this->SetIdentValue("P_SDL_entladen", 0.0);
|
|
|
|
$this->SetIdentValue("P_EV_laden", 0.0);
|
|
$this->SetIdentValue("P_EV_entladen", 0.0);
|
|
|
|
$this->SetIdentValue("CalcJSON", json_encode(["error" => $reason], JSON_PRETTY_PRINT));
|
|
}
|
|
|
|
private function SetIdentValue(string $ident, $value): void
|
|
{
|
|
$id = @$this->GetIDForIdent($ident);
|
|
if ($id <= 0) {
|
|
$this->SendDebug(__FUNCTION__, "Ident nicht gefunden: $ident", 0);
|
|
return;
|
|
}
|
|
SetValue($id, $value);
|
|
}
|
|
|
|
private function ReadSocPercent(int $varId): float
|
|
{
|
|
// Falls jemand statt Variable direkt 0..100 einträgt
|
|
if ($varId >= 0 && $varId <= 100 && !IPS_VariableExists($varId)) {
|
|
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;
|
|
if ($soc < 0.0) $soc = 0.0;
|
|
if ($soc > 100.0) $soc = 100.0;
|
|
|
|
return $soc;
|
|
}
|
|
|
|
public function ApplySetpoints(): void
|
|
{
|
|
$pEvW = (float) GetValue($this->GetIDForIdent("Nennleistung_Soll_EV"));
|
|
$pSdlW = (float) GetValue($this->GetIDForIdent("Nennleistung_Soll_SDL"));
|
|
|
|
//Diverenz zwischen EV und SDl berechnen
|
|
|
|
|
|
// Methode für priorisierung
|
|
$distribution = $this->CalculateBatteryDistribution($pEvW,$pSdlW);
|
|
|
|
|
|
$this->WriteBatteryPowerSetpoints($distribution);
|
|
}
|
|
|
|
|
|
|
|
|
|
private function CalculateBatteryDistribution(float $pEvW, float $pSdlW): array
|
|
{
|
|
$calcJsonId = $this->GetIDForIdent("CalcJSON");
|
|
if (!IPS_VariableExists($calcJsonId)) {
|
|
return [];
|
|
}
|
|
|
|
$rawJson = (string)GetValue($calcJsonId);
|
|
if (empty($rawJson)) {
|
|
return [];
|
|
}
|
|
|
|
$calc = json_decode($rawJson, true);
|
|
$batteries = $calc['batteries'] ?? [];
|
|
|
|
// ---------------------------------------------------------
|
|
// Hilfsfunktion: Verteilungslogik
|
|
// ---------------------------------------------------------
|
|
$distributePower = function(float $targetPower, string $mode) use ($batteries): array {
|
|
$result = [];
|
|
foreach ($batteries as $bat) {
|
|
$result[$bat['idx']] = 0.0;
|
|
}
|
|
|
|
if (abs($targetPower) < 0.01) {
|
|
return $result;
|
|
}
|
|
|
|
$isCharge = ($targetPower > 0);
|
|
$absPower = abs($targetPower);
|
|
|
|
$socKey = ($mode === 'EV') ? 'EV_SOC' : 'SDL_SOC';
|
|
$limitKey = $isCharge ? $mode . '_Charge_kW' : $mode . '_Discharge_kW';
|
|
|
|
$groups = [];
|
|
foreach ($batteries as $bat) {
|
|
$soc = (int)round($bat[$socKey]);
|
|
$maxW = ((float)$bat[$limitKey]) * 1000.0; // kW -> W
|
|
$groups[$soc][] = [
|
|
'idx' => $bat['idx'],
|
|
'maxW' => $maxW
|
|
];
|
|
}
|
|
|
|
if ($isCharge) {
|
|
ksort($groups);
|
|
} else {
|
|
krsort($groups);
|
|
}
|
|
|
|
$remainingNeeded = $absPower;
|
|
|
|
foreach ($groups as $soc => $groupBatteries) {
|
|
if ($remainingNeeded <= 0.01) break;
|
|
|
|
$groupTotalCapacity = 0.0;
|
|
foreach ($groupBatteries as $gb) {
|
|
$groupTotalCapacity += $gb['maxW'];
|
|
}
|
|
|
|
$powerForThisGroup = min($remainingNeeded, $groupTotalCapacity);
|
|
|
|
if ($groupTotalCapacity > 0) {
|
|
$ratio = $powerForThisGroup / $groupTotalCapacity;
|
|
foreach ($groupBatteries as $gb) {
|
|
$result[$gb['idx']] = $gb['maxW'] * $ratio;
|
|
}
|
|
}
|
|
|
|
$remainingNeeded -= $powerForThisGroup;
|
|
}
|
|
|
|
if (!$isCharge) {
|
|
foreach ($result as $idx => $val) {
|
|
$result[$idx] = -$val;
|
|
}
|
|
}
|
|
|
|
return $result;
|
|
};
|
|
|
|
// ---------------------------------------------------------
|
|
// 1) Verteilung EV/SDL getrennt
|
|
// ---------------------------------------------------------
|
|
$evDistribution = $distributePower($pEvW, 'EV');
|
|
$sdlDistribution = $distributePower($pSdlW, 'SDL');
|
|
|
|
// ---------------------------------------------------------
|
|
// 2) Zusammenführen pro Batterie (Netto-Setpoint)
|
|
// ---------------------------------------------------------
|
|
$finalOutput = [];
|
|
foreach ($batteries as $bat) {
|
|
$idx = $bat['idx'];
|
|
|
|
$valEv = $evDistribution[$idx] ?? 0.0;
|
|
$valSdl = $sdlDistribution[$idx] ?? 0.0;
|
|
|
|
$totalW = $valEv + $valSdl;
|
|
|
|
$chargeW = 0.0;
|
|
$dischargeW = 0.0;
|
|
|
|
if ($totalW > 0) {
|
|
$chargeW = $totalW;
|
|
} elseif ($totalW < 0) {
|
|
$dischargeW = -$totalW;
|
|
}
|
|
|
|
$finalOutput[] = [
|
|
"idx" => $idx,
|
|
"typ" => (string)$bat['typ'],
|
|
"chargeW" => round($chargeW, 0),
|
|
"dischargeW" => round($dischargeW, 0)
|
|
];
|
|
}
|
|
|
|
// ---------------------------------------------------------
|
|
// 3) Ist-Batterieleistung lesen (Summe)
|
|
// ---------------------------------------------------------
|
|
$batteriesRaw = json_decode($this->ReadPropertyString("Batteries"));
|
|
$totalPower_ist = 0.0;
|
|
|
|
if (is_array($batteriesRaw)) {
|
|
foreach ($batteriesRaw as $bat) {
|
|
// ACHTUNG: Vorzeichen-Konvention!
|
|
// Du hattest (-1)*GetValue(...) drin. Behalte das, wenn dein Register so liefert.
|
|
$totalPower_ist += (-1) * (float)GetValue($bat->register_bat_power);
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------
|
|
// 4) "Aktuelle Leistung EV/SDL" stabil aus Istleistung ableiten
|
|
// - gleiche Richtung: proportional nach |Soll|
|
|
// - nur einer aktiv: der bekommt die Istleistung
|
|
// - gegensätzliche Richtung: Sollwerte anzeigen (keine Skalierung)
|
|
// ---------------------------------------------------------
|
|
$eps = 0.01;
|
|
|
|
$aktEV = 0.0;
|
|
$aktSDL = 0.0;
|
|
|
|
$evOn = (abs($pEvW) > $eps);
|
|
$sdlOn = (abs($pSdlW) > $eps);
|
|
|
|
if (!$evOn && !$sdlOn) {
|
|
$aktEV = 0.0;
|
|
$aktSDL = 0.0;
|
|
|
|
} elseif (!$evOn && $sdlOn) {
|
|
// nur SDL aktiv -> bekommt Istleistung
|
|
$aktEV = 0.0;
|
|
$aktSDL = $totalPower_ist;
|
|
|
|
} elseif ($evOn && !$sdlOn) {
|
|
// nur EV aktiv -> bekommt Istleistung
|
|
$aktEV = $totalPower_ist;
|
|
$aktSDL = 0.0;
|
|
|
|
} else {
|
|
// beide aktiv
|
|
$sameDirection =
|
|
(($pEvW > 0 && $pSdlW > 0) || ($pEvW < 0 && $pSdlW < 0));
|
|
|
|
if ($sameDirection) {
|
|
// Ist proportional nach |Soll|
|
|
$sumAbs = abs($pEvW) + abs($pSdlW);
|
|
if ($sumAbs < $eps) {
|
|
$aktEV = $totalPower_ist / 2.0;
|
|
$aktSDL = $totalPower_ist / 2.0;
|
|
} else {
|
|
$aktEV = $totalPower_ist * (abs($pEvW) / $sumAbs);
|
|
$aktSDL = $totalPower_ist - $aktEV; // exakt Summe = totalPower_ist
|
|
// Vorzeichen an Istleistung anpassen
|
|
if ($totalPower_ist < 0) {
|
|
$aktEV = -abs($aktEV);
|
|
$aktSDL = -abs($aktSDL);
|
|
} else {
|
|
$aktEV = abs($aktEV);
|
|
$aktSDL = abs($aktSDL);
|
|
}
|
|
}
|
|
|
|
} else {
|
|
// gegensätzliche Richtung: Sollwerte anzeigen (stabil, keine Explosion)
|
|
$aktEV = $pEvW;
|
|
$aktSDL = $pSdlW;
|
|
}
|
|
}
|
|
|
|
// Stabil setzen (kein $this->SetValue!)
|
|
$this->SetIdentValue("Aktuelle_Leistung_EV", (float)$aktEV);
|
|
$this->SetIdentValue("Aktuelle_Leistung_SDL", (float)$aktSDL);
|
|
|
|
return $finalOutput;
|
|
}
|
|
|
|
|
|
|
|
private function WriteBatteryPowerSetpoints(array $distribution): void
|
|
{
|
|
IPS_LogMessage(
|
|
__FUNCTION__,
|
|
"distribution=" . json_encode($distribution, JSON_PRETTY_PRINT)
|
|
);
|
|
|
|
$batteriesCfg = json_decode($this->ReadPropertyString("Batteries"), true);
|
|
if (!is_array($batteriesCfg) || empty($batteriesCfg)) {
|
|
return;
|
|
}
|
|
|
|
foreach ($distribution as $d) {
|
|
|
|
$idx = (int)($d["idx"] ?? -1);
|
|
if ($idx < 0 || !isset($batteriesCfg[$idx])) {
|
|
continue;
|
|
}
|
|
|
|
$cfg = $batteriesCfg[$idx];
|
|
|
|
$typ = (string)($d["typ"] ?? ($cfg["typ"] ?? ("Bat " . ($idx + 1))));
|
|
|
|
// ✅ immer positiv
|
|
$chargeW = max(0.0, (float)($d["chargeW"] ?? 0.0));
|
|
$dischargeW = max(0.0, (float)($d["dischargeW"] ?? 0.0));
|
|
|
|
// nie gleichzeitig
|
|
if ($chargeW > 0.0 && $dischargeW > 0.0) {
|
|
$this->SendDebug("WriteBatteryPowerSetpoints", "WARN: both >0 for $typ (idx=$idx) -> set 0", 0);
|
|
$chargeW = 0.0;
|
|
$dischargeW = 0.0;
|
|
}
|
|
|
|
$this->WriteByVendorRegistersSingleMode($typ, $cfg, $chargeW, $dischargeW);
|
|
|
|
$this->SendDebug("Setpoints", "$typ (idx=$idx) charge={$chargeW}W discharge={$dischargeW}W", 0);
|
|
}
|
|
}
|
|
|
|
private function WriteByVendorRegistersSingleMode(string $typ, array $cfg, float $chargeW, float $dischargeW): void
|
|
{
|
|
$t = mb_strtolower($typ);
|
|
|
|
// Leistungs-Variablen
|
|
$varPowerCharge = (int)($cfg["powerbat_laden"] ?? 0);
|
|
$varPowerDisch = (int)($cfg["powerbat_entladen"] ?? 0);
|
|
|
|
// ✅ EIN Modus-Register
|
|
$varMode = (int)($cfg["register_ladenentladen_modus"] ?? 0);
|
|
|
|
// sichere Writer
|
|
$setInt = function(int $varId, int $value): void {
|
|
if ($varId > 0 && IPS_VariableExists($varId)) {
|
|
RequestAction($varId, $value);
|
|
}
|
|
};
|
|
$setW = function(int $varId, float $w): void {
|
|
if ($varId > 0 && IPS_VariableExists($varId)) {
|
|
RequestAction($varId, (int)round(max(0.0, $w), 0)); // ✅ niemals negativ
|
|
}
|
|
};
|
|
|
|
// Moduscodes je Typ
|
|
$modeCharge = 1; $modeDisch = 2; // Default
|
|
if (strpos($t, "goodwe") !== false) {
|
|
$modeCharge = 11; $modeDisch = 12;
|
|
} elseif (strpos($t, "solaredge") !== false) {
|
|
$modeCharge = 3; $modeDisch = 4;
|
|
}
|
|
|
|
// =========================
|
|
// Laden
|
|
// =========================
|
|
if ($chargeW > 0.0) {
|
|
|
|
// Modus setzen (ein Register)
|
|
$setInt($varMode, $modeCharge);
|
|
|
|
// GoodWe: nur powerbat_laden
|
|
if (strpos($t, "goodwe") !== false) {
|
|
$setW($varPowerCharge, (int)$chargeW);
|
|
$setW($varPowerDisch, (int)0);
|
|
return;
|
|
}
|
|
|
|
// SolarEdge + Default: zwei Leistungsregister
|
|
$setW($varPowerCharge, $chargeW);
|
|
$setW($varPowerDisch, 0.0);
|
|
return;
|
|
}
|
|
|
|
// =========================
|
|
// Entladen
|
|
// =========================
|
|
if ($dischargeW > 0.0) {
|
|
|
|
// Modus setzen (ein Register)
|
|
$setInt($varMode, $modeDisch);
|
|
|
|
// GoodWe: Entladen nutzt trotzdem powerbat_laden (immer positiv)
|
|
if (strpos($t, "goodwe") !== false) {
|
|
$setW($varPowerCharge, (int)$dischargeW);
|
|
$setW($varPowerDisch, (int)0);
|
|
return;
|
|
}
|
|
|
|
// SolarEdge + Default: Entladen in powerbat_entladen
|
|
$setW($varPowerDisch, $dischargeW);
|
|
$setW($varPowerCharge, 0.0);
|
|
return;
|
|
}
|
|
|
|
// =========================
|
|
// Stop / Neutral
|
|
// =========================
|
|
$setW($varPowerCharge, 0.0);
|
|
$setW($varPowerDisch, 0.0);
|
|
// optional: Modus nicht anfassen oder auf 0 setzen:
|
|
// $setInt($varMode, 0);
|
|
}
|
|
|
|
|
|
}
|
|
?>
|