From 7de43a42401f0eb8dad6bfe1d9e30eba378f0bb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20H=C3=A4fliger?= Date: Fri, 14 Nov 2025 10:01:17 +0100 Subject: [PATCH] no message --- Shelly_Parser_MQTT/module.php | 57 ++++++++++++++++++++++++----------- 1 file changed, 40 insertions(+), 17 deletions(-) diff --git a/Shelly_Parser_MQTT/module.php b/Shelly_Parser_MQTT/module.php index 77d007e..21ba266 100644 --- a/Shelly_Parser_MQTT/module.php +++ b/Shelly_Parser_MQTT/module.php @@ -73,25 +73,45 @@ class Shelly_Parser_MQTT extends IPSModule * ---------------------------------------------------------*/ public function RequestAction($Ident, $Value) { - // Ident ist z.B. "BE_1_3_14_output" + // Lokale Variable aktualisieren + $varID = @IPS_GetObjectIDByIdent($Ident, $this->InstanceID); + if (!$varID) { + // Falls Variable im Ordner liegt: + foreach (IPS_GetChildrenIDs($this->InstanceID) as $folder) { + foreach (IPS_GetChildrenIDs($folder) as $cid) { + if (IPS_GetObject($cid)['ObjectIdent'] === $Ident) { + $varID = $cid; + break 2; + } + } + } + } + + if ($varID) { + SetValue($varID, $Value); + } + + // Prüfen ob das ein Output ist if (str_ends_with($Ident, '_output')) { + $deviceID = substr($Ident, 0, -strlen('_output')); - // lokale Variable aktualisieren - $varID = $this->EnsureBooleanVariable($deviceID, $Ident, 'Output'); - SetValue($varID, (bool)$Value); - - // Shelly schalten $topic = $deviceID . '/rpc/Switch.Set'; + $payload = json_encode([ 'id' => 0, 'on' => (bool)$Value ]); $this->Publish($topic, $payload); + + if ($this->ReadPropertyBoolean('Debug')) { + IPS_LogMessage("Shelly_Parser_MQTT", "Switching $deviceID -> $Value"); + } } } + /* --------------------------------------------------------- * RECEIVE MQTT DATA * ---------------------------------------------------------*/ @@ -176,15 +196,12 @@ class Shelly_Parser_MQTT extends IPSModule // Output if (array_key_exists('output', $mapped)) { - $outID = $this->EnsureBooleanVariable( - $deviceID, - $deviceID . '_output', - 'Output' - ); - $this->EnableAction($deviceID . '_output'); + $outID = $this->EnsureBooleanVariable($deviceID, $deviceID . '_output', 'Output'); + IPS_SetVariableCustomAction($outID, $this->InstanceID); SetValue($outID, (bool)$mapped['output']); } + // Input (GEN4 / PRO) if (array_key_exists('input', $mapped)) { $inID = $this->EnsureBooleanVariable( @@ -213,18 +230,24 @@ class Shelly_Parser_MQTT extends IPSModule { $folderID = $this->GetDeviceFolder($deviceID); + // Suche richtige Variable im Ordner foreach (IPS_GetChildrenIDs($folderID) as $cid) { $obj = IPS_GetObject($cid); - if ($obj['ObjectIdent'] === $ident && $obj['ObjectType'] === OBJECTTYPE_VARIABLE) { - return $cid; + if ($obj['ObjectIdent'] === $ident) { + return $cid; // bestehende Variable gefunden } } - $id = $this->RegisterVariableBoolean($ident, $name); - IPS_SetParent($id, $folderID); - return $id; + // Neue Variable anlegen + $varID = IPS_CreateVariable(0); // 0 = Boolean + IPS_SetName($varID, $name); + IPS_SetIdent($varID, $ident); + IPS_SetParent($varID, $folderID); + + return $varID; } + private function EnsureFloatVariable(string $deviceID, string $ident, string $name): int { $folderID = $this->GetDeviceFolder($deviceID);