no message

This commit is contained in:
belevo\mh
2026-01-27 12:47:44 +01:00
parent 09962a8cd0
commit 9bbbad260a

View File

@@ -262,6 +262,8 @@ class Bat_EV_SDL_V2 extends IPSModule
];
$this->SetIdentValue("CalcJSON", json_encode($calc, JSON_PRETTY_PRINT));
$this->ApplySetpoints();
} catch (Throwable $e) {
$this->SendDebug("Update ERROR", $e->getMessage() . " @ " . $e->getFile() . ":" . $e->getLine(), 0);
@@ -437,6 +439,121 @@ class Bat_EV_SDL_V2 extends IPSModule
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
{
// Holt das berechnete Json aus, also das Debug json, wo in der Konsole angezeigt wird
$calcJsonId = $this->GetIDForIdent("CalcJSON");
$calc = json_decode((string)GetValue($calcJsonId), true);
if (!is_array($calc) || empty($calc["batteries"])) {
return [];
}
foreach ($calc["batteries"] as $bat) {
$idx = (int)$bat["idx"];
$typ = (string)$bat["typ"];
$evSoc = (float)$bat["EV_SOC"];
$sdlSoc = (float)$bat["SDL_SOC"];
$sdlChargeKW = (float)$bat["SDL_Charge_kW"];
$sdlDischargeKW = (float)$bat["SDL_Discharge_kW"];
$evChargeKW = (float)$bat["EV_Charge_kW"];
$evDischargeKW = (float)$bat["EV_Discharge_kW"];
}
/*
// Return sieht so aus:
return :[
"idx" => 0,
"typ":"1",
"chargeW" => 0.0,
"dischargeW" => 3000.0
],
[
"idx" => 1,
"typ":"2",
"chargeW" => 0.0,
"dischargeW" => 1000.0
];
*/
}
private function WriteBatteryPowerSetpoints(array $distribution): void
{
// chargeW und dischargeW sind in W. Wenn good hat muss in kW umrechnen
// Batteries-Config laden (für Variable-IDs + typ)
$batteriesCfg = json_decode($this->ReadPropertyString("Batteries"), true);
if (!is_array($batteriesCfg) || empty($batteriesCfg)) {
return;
}
foreach ($distribution as $d) {
// --- Batterie bestimmen ---
$idx = (int)($d["idx"] ?? -1);
if ($idx < 0 || !isset($batteriesCfg[$idx])) {
continue;
}
$cfg = $batteriesCfg[$idx];
$typ = (string)($d["typ"] ?? ($cfg["typ"] ?? ("Bat " . ($idx + 1))));
// --- Sollwerte (Watt) ---
$chargeW = max(0.0, (float)($d["chargeW"] ?? 0.0));
$dischargeW = max(0.0, (float)($d["dischargeW"] ?? 0.0));
// Sicherheitsnetz: nie gleichzeitig laden & entladen
if ($chargeW > 0.0 && $dischargeW > 0.0) {
// Debug-Hinweis, falls Strategie Mist gebaut hat
$this->SendDebug(
"WriteBatteryPowerSetpoints",
"WARN: charge & discharge gleichzeitig > 0 bei $typ (idx=$idx)",
0
);
$chargeW = 0.0;
$dischargeW = 0.0;
}
// --- Ziel-Variablen ---
$varCharge = (int)($cfg["powerbat_laden"] ?? 0);
$varDischarge = (int)($cfg["powerbat_entladen"] ?? 0);
// --- Schreiben ---
if ($varCharge > 0 && IPS_VariableExists($varCharge)) {
SetValue($varCharge, (int)round($chargeW, 0));
}
if ($varDischarge > 0 && IPS_VariableExists($varDischarge)) {
SetValue($varDischarge, (int)round($dischargeW, 0));
}
}
}
}
?>