diff --git a/Bat_EV_SDL _V2/module.php b/Bat_EV_SDL _V2/module.php index 09bec22..7182d69 100644 --- a/Bat_EV_SDL _V2/module.php +++ b/Bat_EV_SDL _V2/module.php @@ -667,8 +667,56 @@ private function CalculateBatteryDistribution(float $pEvW, float $pSdlW): array - $this->SetValue("Aktuelle_Leistung_SDL", $pSdlW); - $this->SetValue("Aktuelle_Leistung_EV", $totalPower_ist + $pSdlW); + + $eps = 0.01; + + $aktEV = 0.0; + $aktSDL = 0.0; + + // Optional: Wenn einer 0 ist -> aktiver Kanal bekommt totalPower_ist + if (abs($pEvW) < $eps && abs($pSdlW) < $eps) { + + $aktEV = 0.0; + $aktSDL = 0.0; + + } elseif (abs($pEvW) < $eps) { + + $aktEV = 0.0; + $aktSDL = $totalPower_ist; + + } elseif (abs($pSdlW) < $eps) { + + $aktEV = $totalPower_ist; + $aktSDL = 0.0; + + } else { + + // Beide aktiv + $sameDirection = (($pEvW > 0 && $pSdlW > 0) || ($pEvW < 0 && $pSdlW < 0)); + + if ($sameDirection) { + // Regel A: gleiche Richtung -> Ist proportional nach |Soll| aufteilen + $sumAbs = abs($pEvW) + abs($pSdlW); + + if ($sumAbs < $eps) { + // Fallback + $aktEV = $totalPower_ist / 2.0; + $aktSDL = $totalPower_ist / 2.0; + } else { + $aktEV = $totalPower_ist * (abs($pEvW) / $sumAbs); + $aktSDL = $totalPower_ist - $aktEV; // Summe bleibt exakt totalPower_ist + } + + } else { + // Regel B: gegensätzliche Richtung -> Sollwerte anzeigen + $aktEV = $pEvW; + $aktSDL = $pSdlW; + } + } + + $this->SetValue("Aktuelle_Leistung_EV", (float)$aktEV); + $this->SetValue("Aktuelle_Leistung_SDL", (float)$aktSDL); +