JSON Parse MQTT

Hey there,
Is there a way of combining the payload of multiple object’s into one single JSON (or equivalent) String in order to only have one message sent? I also would need to read that string back in order to assign the values to my object’s after they got modified on another device?

Kind regards
CrunkFX

You’ll probably need to use MQTT in Plugin to accomplish this. I don’t think you can do that with that default stand-alone MQTT Client.

So, i worked myself into the Plugin, got it working also with the automatic Broker switching if one is not reachable. Now i want to pack all attributes of my device into one JSON String. I get it to work with the actual changed values but how can i send all values from the attributes of my device?

This is my actual Code to send the Values:

function onChangeRequest(device, attribute, value) {
switch (device.Id) {
    case "1":
        mqtt.publish("/AstoriaTest/ok", "{" + "\"deviceId\":\"" + device.Id + "\"," + "\"attr\":\"" + attribute + "\"," + "\"value\":\"" + value + "\"}");
        break;
    }
}

In your MQTT Example you used
$HomeRemote.Attribute.Value
But that doesn´t seem to work in the JS Plugin, as it says “$HomeRemote not found”

Thanks
CrunkFX

The device parameter is your actual Device. It has all of your attributes. In that code snippet you shared, you are using device.Id in your payload. Including an additional property/attribute is no different. Say you have a Switch attribute, you can access it like this device.Switch or device.Attributes["Switch"]

1 Like

Wow I could have thought of this myself. Thanks, sometimes you just need a little push into the right direction.

1 Like