MQTT Client Device | JSON data format

I would like to use JSON with a MQTT Client Device, but can’t find out how to publish data to a MQTT topic using a MQTT Client Device configured with JSON data format. Is there a manual, instruction, or someone in the knowledge that can explain this to me? Thanks!

I think I have to execute some PHP script when the MQTT Client Device is triggered, but from that point I’m lost.

Hi,

unfortunatly its not so easy… You need to create a mqtt client device (or copy one)

then you need to bind this device to the correct gateway if you have more than one mqtt instance, type in the topic and choose as type „string“ (yes, string NOT json)

you see on my first picture under the device a new string variable, you need to write to this variable

Create a script like this:

<?php

$ID = 35202; // like in picture one

$a = array("targetTemperature" => 22);
requestaction($ID,json_encode($a));

if xou execute the script, it will send data in json format to the device…

Hope its help.

Best regards

1 „Gefällt mir“

I’m pretty sure I’ve got it up and running now, thanks to your guidance. Much appreciated!

<?php

//Enter your script here
$mqtt_id = 45998;
$mqtt_discovery = array(
    "automation_type" => "trigger",
    "type" => "status",
    "subtype" => "right_side_down",
    "payload" => "true",
    "topic" => "button/enocean_ptm210_00246d5f/status",
    "device" => array(
        "identifiers" => "enocean_ptm210_00246d5f",
        "name" =>  "enocean_ptm210_00246d5f",
        "model" => "PTM210",
        "serial_number" => "00246D5F",
        "manufacturer" => "EnOcean",
        "suggested_area" => "Hal")
        );

RequestAction($mqtt_id,json_encode($mqtt_discovery));
?>
<?php

//Enter your script here
$mqtt_id = 47254;
$mqtt_discovery = array(
    "automation_type" => "trigger",
    "type" => "status",
    "subtype" => "right_side_up",
    "payload" => "false",
    "topic" => "button/enocean_ptm210_00246d5f/status",
    "device" => array(      
        "identifiers" => "enocean_ptm210_00246d5f",
        "name" => "enocean_ptm210_00246d5f",
        "model" => "PTM210",
        "serial_number" => "00246D5F",
        "manufacturer" => "EnOcean",
        "suggested_area" => "Hal")
        );

RequestAction($mqtt_id,json_encode($mqtt_discovery));
?>

I assume it must be possible to get the mqtt_id automatically using variables of the php script that is triggered by the MQTT Birth message. But how? :thinking:

That was the ultimate ‚gotcha!‘.

Hi,

i have written a module that makes it possible to send data without creating an MQTT-Device instance

It is in Beta, if you want to install it, you need to type in the symcon store the name „simple MQTT sending Tool“ exactly.

Now you can create a instance and bind it to your Symcon mqtt-server-instance… If you have more than one MQTT server instance, you must add one for each instance.

After that, you can send json like this:

SMST_sendJson(integer $InstanzID, string $Topic, array $Payload); # sends an array to the topic, the json_encoding is done in the function.

// example: 
SMST_sendJson(12345,'dummy/topic/set/',array("variable" => true));

or string like that

SMST_sendString(integer $InstanzID, string $Topic, string $Payload); # sends a string to the topic

// example: 
SMST_sendString(12345,'dummy/topic/set/','restart');

hope this module is usefull

Best regards

1 „Gefällt mir“

$mqtt_id = IPS_GetParent($_IPS[‚SELF‘]);