Homey Community Forum

MQTT flow parse json

I am using the mqtt client in homey to recieve an mqtt message containing a json object. Now I need a way to parse the json object into betterlogic variables? Anyone done it before?

{"battery.charge": "100", "battery.charge.low": "10", "battery.charge.warning": "50", "battery.date": "2001/09/25", "battery.mfr.date": "2015/02/28", "battery.runtime": "900", "battery.runtime.low": "120", "battery.type": "PbAc", "battery.voltage": "27.3", "battery.voltage.nominal": "24.0", "device.mfr": "American Power Conversion", "device.model": "Back-UPS RS 900G", "device.serial": "3B1509X29698 ", "device.type": "ups", "driver.name": "usbhid-ups", "driver.parameter.pollfreq": "30", "driver.parameter.pollinterval": "5", "driver.parameter.port": "auto", "driver.version": "DSM6-2-2-24922-broadwell-fmp-repack-24922-190507", "driver.version.data": "APC HID 0.95", "driver.version.internal": "0.38", "input.sensitivity": "medium", "input.transfer.high": "294", "input.transfer.low": "176", "input.voltage": "233.0", "input.voltage.nominal": "230", "ups.beeper.status": "enabled", "ups.delay.shutdown": "20", "ups.firmware": "879.L4 .I", "ups.firmware.aux": "L4 ", "ups.load": "28", "ups.mfr": "American Power Conversion", "ups.mfr.date": "2015/02/28", "ups.model": "Back-UPS RS 900G", "ups.productid": "0002", "ups.realpower.nominal": "540", "ups.serial": "3B1509X29698 ", "ups.status": "OL", "ups.test.result": "No test initiated", "ups.timer.reboot": "0", "ups.timer.shutdown": "-1", "ups.vendorid": "051d"}

I have not done it before… although I’ve worked with all the puzzle pieces.
I think it’s definitely doable.
Use MQTT client to receive the JSON in the “When” of a flow
Send the tag (=json data) to a Homeyscript in the “Then” of the flow
In homeyscript parse the json (you need to create this script first)
And update the Better logic variable inside the script:


I was able to do this using HomeyScript, thanks for your help!

Thought I’d share the code in case anyone is looking to do the same:

if (args[0] == undefined) {
    console.log ('Missing argument');
    return false;
}

let bl = await Homey.apps.getApp({ id: 'net.i-dev.betterlogic' } );
if (!bl) {
    console.error('Better Logic not installed!');
    return false;
}

obj = JSON.parse(args[0]);

_.forEach(obj, async function(value, key) {
    try {
        let blObj = await bl.apiGet(key);
        if (blObj) {
            console.log('Updating variable: ' + key + '/' + value)
            bl.apiPut(key + '/' + value)
        }
    }
    catch(err) {
        console.log("Couldn't find variable")
    }
});

return true
3 Likes

Hey.
I think this is what i need. But, my mqtt json what i receive is a littlebit diffrent to yours.

I receive a json string like this:

    {"Time":"2020-01-13T17:31:14","DS18B20":{"Temperature":5.6},"TempUnit":"C"}

Now i search a way to get only the Temperature (5.6) Value from the JSON string.

Ok i find it by my self. For all with the same question:

  if (args[0] == undefined) {
    console.log ('Missing argument');
    return false;
}

let bl = await Homey.apps.getApp({ id: 'net.i-dev.betterlogic' } );
if (!bl) {
    console.error('Better Logic not installed!');
    return false;
}

obj = JSON.parse(args[0]);

bl.apiPut('Temperature' + '/' + obj.DS18B20.Temperature)
console.log('Updating variable: ' + 'Temperature' + '/' + obj.DS18B20.Temperature); 

Works for me.

1 Like