diff --git a/PV_Visu/module.php b/PV_Visu/module.php index 7e94152..920cf11 100644 --- a/PV_Visu/module.php +++ b/PV_Visu/module.php @@ -30,7 +30,52 @@ class PV_Visu extends IPSModule $this->UpdateData(); // Initial } + + public function RequestAction($Ident, $Value) + { + if ($Ident === 'update') { + $data = $this->UpdateData(); + return $data; // ← sendet Daten direkt an die Visualisierung + } + throw new \Exception("Unknown Ident: $Ident"); + } + public function UpdateData() + { + $start = strtotime('today 00:00'); + $end = time(); + + $prod = $this->GetDailyTotal($this->ReadPropertyInteger('VarProduction'), $start, $end); + $cons = $this->GetDailyTotal($this->ReadPropertyInteger('VarConsumption'), $start, $end); + $feed = $this->GetDailyTotal($this->ReadPropertyInteger('VarFeedIn'), $start, $end); + $grid = $this->GetDailyTotal($this->ReadPropertyInteger('VarGrid'), $start, $end); + + $prodCons = $prod > 0 ? (($cons - $grid) / $prod) * 100 : 0; + $prodFeed = $prod > 0 ? 100 - $prodCons : 0; + $consPV = $cons > 0 ? min($prod, ($cons - $grid)) / $cons * 100 : 0; + $consGrid = $cons > 0 ? 100 - $consPV : 0; + + $data = [ + 'prodCons' => round($prodCons, 1), + 'prodFeed' => round($prodFeed, 1), + 'consPV' => round($consPV, 1), + 'consGrid' => round($consGrid, 1), + 'value' => [ + 'prod' => round($prod, 2), + 'cons' => round($cons, 2), + 'feed' => round($feed, 2), + 'grid' => round($grid, 2), + ], + ]; + + $json = json_encode($data); + SetValueString($this->GetIDForIdent('JSONData'), $json); + + // Kein SendDataToVisualization! + IPS_LogMessage("PV_Visu", "UpdateData (JSON): $json"); + + return $data; + } public function MessageSink($TimeStamp, $SenderID, $Message, $Data) { if ($Message === VM_UPDATE) {