Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
Beide Seiten der vorigen Revision Vorhergehende Überarbeitung | |||
manuals:symcon:mqtt-integration [2018/09/22 18:52] admin |
manuals:symcon:mqtt-integration [2018/09/23 19:15] (aktuell) paulg4h |
||
---|---|---|---|
Zeile 65: | Zeile 65: | ||
mit folgendem Inhalt: | mit folgendem Inhalt: | ||
<code php><? | <code php><? | ||
- | $d = false; $msg = ""; | + | include_once(IPS_GetScript(42011 )["ScriptFile"]); |
+ | |||
+ | $d = true; $msg = ""; | ||
$value = ""; $run = true; | $value = ""; $run = true; | ||
- | if ($d){ $msg .= "Sender: ".$_IPS['SENDER']."\n"; } | + | |
- | //if (($_IPS['SENDER'] == "Variable") or ($_IPS['SENDER'] == "WebFront")){ | + | if ($d){ |
- | if ($_IPS['SENDER'] == "Variable"){ | + | $msg .= "Sender: ".$_IPS['SENDER']."\n"; |
- | $value = GetValue($_IPS['VARIABLE']); | + | $msg .= print_r($_IPS, true); |
- | if ($value == $_IPS['OLDVALUE']){ | + | |
- | $msg .= "Value: X".$value."X old: X".$_IPS['OLDVALUE']."X\n"; | + | |
- | $run = false; | + | |
- | } | + | |
} | } | ||
- | if (($_IPS['SENDER'] == "WebFront")){ | + | |
- | $value = $_IPS['VALUE']; | + | if ($_IPS['SENDER'] == "Execute"){ |
+ | //On Manual Execute check child events and set Event Trigger to "On Change" | ||
+ | foreach (IPS_GetChildrenIDs($_IPS['SELF']) as $key => $value){ | ||
+ | if (IPS_GetObject($value)['ObjectType'] == 4){ | ||
+ | IPS_SetEventTrigger($value, 1, IPS_GetEvent($value)['TriggerVariableID']); | ||
+ | } | ||
+ | } | ||
+ | } else { | ||
+ | if (($_IPS['SENDER'] == "WebFront") or ($_IPS['SENDER'] == "Variable")){ | ||
+ | if ($_IPS['SENDER'] == "Variable"){ | ||
+ | //$value = (GetValueBoolean($_IPS['VARIABLE'])?"true":"false"); | ||
+ | $value = ($_IPS['VALUE']=="1"?"true":"false"); | ||
+ | } | ||
+ | if (($_IPS['SENDER'] == "WebFront")){ | ||
+ | $value = ($_IPS['VALUE']=="1"?"true":"false"); | ||
+ | } | ||
+ | |||
+ | if (($value == (GetValueBoolean($_IPS['VARIABLE'])?"true":"false")) and ($_IPS['SENDER'] != "Variable")){ | ||
+ | if($d){ $msg .= " Aktuell: >".$value."< alt: >".(GetValueBoolean($_IPS['VARIABLE'])?"true":"false")."<\n"; } | ||
+ | $run = false; | ||
+ | } | ||
+ | |||
+ | if ($d){ | ||
+ | $msg .= " run: ".($run?"Ja":"Nein")."\n"; | ||
+ | } | ||
+ | |||
+ | $topic = explode(";",IPS_GetObject($_IPS['VARIABLE'])['ObjectInfo'])[1]; | ||
+ | |||
+ | if((strlen($topic) > 3) and ($run)){ | ||
+ | if ($d){ | ||
+ | $msg .= " Veröffentliche: ".$value."\n"; | ||
+ | $msg .= " Variable: ".IPS_GetName($_IPS['VARIABLE'])."\n"; | ||
+ | $msg .= " Topic: ".$topic."\n"; | ||
+ | $msg .= " Value: ".$value."\n"; | ||
+ | } | ||
+ | MQTT_Publish($idMQTTBroker , $topic ,$value ,1 , true); | ||
+ | } | ||
+ | } | ||
} | } | ||
- | if ($d){ $msg .= "Value: ".$value."\n"; } | ||
- | $id = $_IPS['VARIABLE']; | ||
- | $topic = explode(";",IPS_GetObject($id)['ObjectInfo'])[1]; | ||
- | if((strlen($topic) > 3) and ($run)){ | ||
- | switch(IPS_GetVariable($id)['VariableType']){ | ||
- | case 0: //boolean | ||
- | if($value == 1){ $value = "true"; } else { $value = "false"; } | ||
- | break; | ||
- | case 1: //Integer | ||
- | |||
- | break; | ||
- | case 2: //float | ||
- | |||
- | break; | ||
- | case 3: //string | ||
- | |||
- | break; | ||
- | } | ||
- | if($d){ $msg .= "veröffentliche: ".$value."\n"; } | ||
- | MQTT_Publish(50729 /*[MQTT_Localhost]*/, $topic ,$value ,1 , true); | ||
- | } | ||
- | |||
- | if ($d){ $msg .= "Variable: ".IPS_GetName($id)."\nTopic: ".$topic."\n Value: ".$value."\n"; } | ||
- | |||
if(strlen($msg) > 0){ | if(strlen($msg) > 0){ | ||
IPS_LogMessage("mqtt_switch_relais", $msg); | IPS_LogMessage("mqtt_switch_relais", $msg); | ||
} | } | ||
- | ?> | + | ?></code> |
- | </code> | + | |
Zeile 118: | Zeile 128: | ||
// mqtt topic to symcon variable id mapping | // mqtt topic to symcon variable id mapping | ||
$idParent = IPS_GetParent($_IPS['SELF']); | $idParent = IPS_GetParent($_IPS['SELF']); | ||
- | $archiveId = 32289 /*[Archive]*/; | + | $archiveId = 32289; |
- | $relaisScriptId = 17676 /*[Datenpunkte\MQTT\mqtt_switch_relais]*/; //Relais Script ID | + | $relaisScriptId = 17676; //Relais Script ID |
// get value from mqtt variable | // get value from mqtt variable | ||
if( array_key_exists("payload", $_IPS)){ | if( array_key_exists("payload", $_IPS)){ | ||
- | + | $topic = $_IPS['topic']; | |
- | $topic = $_IPS['topic']; | + | $topics = explode("/", $topic); |
- | $topics = explode("/", $topic); | + | $payload = $_IPS['payload']; |
- | $payload = $_IPS['payload']; | + | |
- | + | $payload = str_replace(array("\r", "\n"), '', $payload); | |
- | $payload = str_replace(array("\r", "\n"), '', $payload); | + | |
- | + | if ($d){ $msg .= "topic: $topic\npayload: $payload\n"; } | |
- | if ($d){ $msg .= "topic: $topic\npayload: $payload\n"; } | + | |
- | + | // Datentyp der payload herausfinden und ins Format für IPS Übersetzen | |
- | // Datentyp der payload herausfinden und ins Format für IPS Übersetzen | + | $contType = 99; |
- | $contType = 99; | + | if (is_float($payload+0)){ |
- | if (is_float($payload+0)){ | + | //Float |
- | //Float | + | $contType = 2; |
- | $contType = 2; | + | } |
- | } | + | if (($contType == 99) and (is_numeric($payload))){ |
- | if (($contType == 99) and (is_numeric($payload))){ | + | //Integer |
- | //Integer | + | $contType = 1; |
- | $contType = 1; | + | } |
- | } | + | if (($contType == 99) and (is_string($payload))){ |
- | if (($contType == 99) and (is_string($payload))){ | + | if ((strtolower($payload) == "true") or (strtolower($payload) == "false")){ |
- | if ((strtolower($payload) == "true") or (strtolower($payload) == "false")){ | + | //Boolean |
- | //Boolean | + | $contType = 0; |
- | $contType = 0; | + | } else { |
- | } else { | + | //String |
- | //String | + | $contType = 3; |
- | $contType = 3; | + | } |
- | } | + | } |
- | } | + | $idf = $idParent; |
- | $idf = $idParent; | + | //Pfad durchlaufen und Prüfen ob die Kategorien / Variablen vorhanden sind und bei bedarf Anlegen |
- | //Pfad durchlaufen und Prüfen ob die Kategorien / Variablen vorhanden sind und bei bedarf Anlegen | + | for($i = 0; $i < count($topics); $i++){ |
- | for($i = 0; $i < count($topics); $i++){ | + | if ($i == (count($topics) -1)){ |
- | if ($i == (count($topics) -1)){ | + | $type = 2; |
- | $type = 2; | + | } else { |
- | } else { | + | $type = 0; |
- | $type = 0; | + | } |
- | } | + | $idf = checkIfElementExists($idf, $type, $topics[$i], $contType); |
- | $idf = checkIfElementExists($idf, $type, $topics[$i], $contType); | + | } |
- | } | + | |
- | + | if($d){ | |
- | if($d){ | + | $msg .= "Variablen Type. ".$contType."\n"; |
- | $msg .= "Variablen Type. ".$contType."\n"; | + | $msg .= "Set Itend to: "."mqtt_".str_replace("/", "_", $topic)."\n"; |
- | $msg .= "Set Itend to: "."mqtt_".str_replace("/", "_", $topic)."\n"; | + | } |
- | } | + | |
- | + | IPS_SetIdent($idf, "mqtt_".str_replace("/", "_", $topic)); | |
- | IPS_SetIdent($idf, "mqtt_".str_replace("/", "_", $topic)); | + | |
- | + | //mqset($idf, $payload); | |
- | mqset($idf, $payload); | + | SetValue($idf, $payload); |
} | } | ||
if ((strlen($msg) > 0) and ($d)){ | if ((strlen($msg) > 0) and ($d)){ | ||
Zeile 185: | Zeile 195: | ||
function mqset($id, $value){ | function mqset($id, $value){ | ||
+ | Global $msg, $d; | ||
if(GetValue($id) != $value){ | if(GetValue($id) != $value){ | ||
switch(IPS_GetVariable($id)["VariableType"]){ | switch(IPS_GetVariable($id)["VariableType"]){ | ||
case 0: // boolean | case 0: // boolean | ||
- | $val = filter_var($value, FILTER_VALIDATE_BOOLEAN); | + | $val = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); |
+ | if($d){ $msg .= "val: ".$val."\n"; } | ||
SetValue($id, $val); | SetValue($id, $val); | ||
break; | break; | ||
Zeile 232: | Zeile 244: | ||
} | } | ||
IPS_SetName($id, $name); | IPS_SetName($id, $name); | ||
- | IPS_SetParent($id, $varid); | + | IPS_SetParent($id, $varid); |
- | if($type == 2){ // Variable | + | if($type == 2){ // Variable |
- | if ($contType == 0){ // Boolean | + | if ($contType == 0){ // Boolean |
- | if (strpos(" ".strtolower($name), "licht") > 0){ | + | if (strpos(" ".strtolower($name), "licht") > 0){ |
- | //Wenn der Variablen Name das wort "licht" enthält das Profil Licht zuweisen | + | //Wenn der Variablen Name das wort "licht" enthält das Profil Licht zuweisen |
- | IPS_SetVariableCustomProfile($id, "Licht"); | + | IPS_SetVariableCustomProfile($id, "Licht"); |
- | IPS_SetVariableCustomAction($id, $relaisScriptId); | + | IPS_SetVariableCustomAction($id, $relaisScriptId); |
- | } elseif(strpos(" ".strtolower($name), "motion") > 0){ | + | } elseif(strpos(" ".strtolower($name), "motion") > 0){ |
- | IPS_SetVariableCustomProfile($id, "Motion"); | + | IPS_SetVariableCustomProfile($id, "Motion"); |
- | } elseif ($name == "connected"){ | + | } elseif ($name == "connected"){ |
- | IPS_SetVariableCustomProfile($id, "Connected"); | + | IPS_SetVariableCustomProfile($id, "Connected"); |
- | } else { | + | } else { |
- | //Wenn es eine Boolean Variable ist das Profile ~Switch zuweisen | + | //Wenn es eine Boolean Variable ist das Profile ~Switch zuweisen |
- | IPS_SetVariableCustomProfile($id, "~Switch"); | + | IPS_SetVariableCustomProfile($id, "~Switch"); |
- | IPS_SetVariableCustomAction($id, $relaisScriptId); | + | IPS_SetVariableCustomAction($id, $relaisScriptId); |
- | checkCreateEvent($id, $relaisScriptId); | + | checkCreateEvent($id, $relaisScriptId); |
- | } | + | } |
- | } | + | } |
- | if (strpos(" ".strtolower($name), "temp") > 0){ | + | if (strpos(" ".strtolower($name), "temp") > 0){ |
- | IPS_SetVariableCustomProfile($id, "~Temperature"); | + | IPS_SetVariableCustomProfile($id, "~Temperature"); |
- | } | + | } |
- | if (strpos(" ".strtolower($name), "humidity") > 0){ | + | if (strpos(" ".strtolower($name), "humidity") > 0){ |
- | IPS_SetVariableCustomProfile($id, "~Humidity"); | + | IPS_SetVariableCustomProfile($id, "~Humidity"); |
- | } | + | } |
- | if (strpos(" ".strtolower($name), "rssi") > 0){ | + | if (strpos(" ".strtolower($name), "rssi") > 0){ |
- | IPS_SetVariableCustomProfile($id, "rssi"); | + | IPS_SetVariableCustomProfile($id, "rssi"); |
- | } | + | } |
IPS_SetInfo($id, "mqtt;$topic"); | IPS_SetInfo($id, "mqtt;$topic"); | ||
//Archivierung aktivieren | //Archivierung aktivieren | ||
- | AC_SetLoggingStatus($archiveId, $id, true); | + | AC_SetLoggingStatus($archiveId, $id, true); |
- | IPS_ApplyChanges($archiveId); | + | IPS_ApplyChanges($archiveId); |
} | } | ||
} | } | ||
Zeile 287: | Zeile 299: | ||
} | } | ||
if ($create){ | if ($create){ | ||
- | //Event Anlegen | + | //Event Anlegen |
- | $eid = IPS_CreateEvent(0); //Ausgelöstes Ereignis | + | $eid = IPS_CreateEvent(0); //Ausgelöstes Ereignis |
- | IPS_SetEventTrigger($eid, 0, $varid); //Bei Änderung von Variable | + | IPS_SetEventTrigger($eid, 1, $varid); //Bei Variablenaktualisierung |
- | IPS_SetParent($eid, $scriptId); //Ereignis zuordnen | + | IPS_SetParent($eid, $scriptId); //Ereignis zuordnen |
IPS_SetEventActive($eid, true); //Ereignis aktivieren | IPS_SetEventActive($eid, true); //Ereignis aktivieren | ||
} | } | ||
} | } | ||
?></code> | ?></code> | ||
- | ==== Mosquitto (MQTT Broker) ==== | + | |
+ | ==== MQTT Broker (Mosquitto) ==== | ||
aptitude install mosquitto mosquitto-clients python-pip git -y | aptitude install mosquitto mosquitto-clients python-pip git -y | ||
| |