# MQTT flow parse json

**URL:** <https://community.homey.app/t/mqtt-flow-parse-json/20531>\
**Category:** Flows\
**Created:** [November 10, 2019, 9:38pm UTC](https://community.homey.app/t/mqtt-flow-parse-json/20531 "2019-11-10T21:38:52Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![christian.ek](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/christian.ek/32/64171_2.png) [@christian.ek](https://community.homey.app/u/christian.ek)\
**Post date:** [November 10, 2019, 9:38pm UTC](https://community.homey.app/t/mqtt-flow-parse-json/20531/1 "2019-11-10T21:38:52Z")

</div>

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"}`

---

<div class="post-metadata">

**Author:** ![Satoer](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/satoer/32/38113_2.png) [@Satoer](https://community.homey.app/u/Satoer)\
**Post date:** [November 12, 2019, 8:05pm UTC](https://community.homey.app/t/mqtt-flow-parse-json/20531/2 "2019-11-12T20:05:07Z")

</div>

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:

> [@Setting Logic Variable by HomeyScript](https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819/20):
>
> You can also create the variable in BetterLogic and update it like: let BLApp = await Homey.apps.getApp({id:“net.i-dev.betterlogic” }); BLApp.apiPut(‘Klok12h/’ + theHour);

---

<div class="post-metadata">

**Author:** ![JPe4619](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jpe4619/32/9017_2.png) [@JPe4619](https://community.homey.app/u/JPe4619)\
**Post date:** [November 13, 2019, 3:56pm UTC](https://community.homey.app/t/mqtt-flow-parse-json/20531/3 "2019-11-13T15:56:23Z")

</div>

> **[HomeyScript - share your scripts - main discussion topic](https://forum.athom.com/discussion/3666/homeyscript-share-your-scripts-main-discussion-topic)**
>
> HomeyScript Write scripts for Homey, for when the Flow Editor might not be enough. App store link: HomeyScript App 

  

> **[zytric/homeyscripts](https://github.com/zytric/homeyscripts)**
>
> homeyscripts. Contribute to zytric/homeyscripts development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![christian.ek](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/christian.ek/32/64171_2.png) [@christian.ek](https://community.homey.app/u/christian.ek)\
**Post date:** [November 13, 2019, 5:35pm UTC](https://community.homey.app/t/mqtt-flow-parse-json/20531/4 "2019-11-13T17:35:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![christian.ek](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/christian.ek/32/64171_2.png) [@christian.ek](https://community.homey.app/u/christian.ek)\
**Post date:** [November 16, 2019, 8:22am UTC](https://community.homey.app/t/mqtt-flow-parse-json/20531/5 "2019-11-16T08:22:33Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![schmucke](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/schmucke/32/79913_2.png) [@schmucke](https://community.homey.app/u/schmucke)\
**Post date:** [January 13, 2020, 4:35pm UTC](https://community.homey.app/t/mqtt-flow-parse-json/20531/6 "2020-01-13T16:35:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![schmucke](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/schmucke/32/79913_2.png) [@schmucke](https://community.homey.app/u/schmucke)\
**Post date:** [January 14, 2020, 4:45pm UTC](https://community.homey.app/t/mqtt-flow-parse-json/20531/7 "2020-01-14T16:45:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Pieterv123](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/pieterv123/32/13353_2.png) [@Pieterv123](https://community.homey.app/u/Pieterv123)\
**Post date:** [January 21, 2021, 12:39pm UTC](https://community.homey.app/t/mqtt-flow-parse-json/20531/8 "2021-01-21T12:39:27Z")

</div>

Hi I am trying to get the data from my energy monitor throught MQTT to homey.  
The totalpower I can extract with the above homeyscript to Betterlogic.  
But how to get the Power data from ‘ctInput’ 1 and 2? (I need the power data)

this is the data send every second:

> `{“totalPower”:2834,“totalReactivePower”:292,“totalExportEnergy”:0,“totalImportEnergy”:4361340,“monitorStatus”:0,“utcTimeStamp”:1611230106090,“channelPowers”:[  
> {“ctInput”:1,“power”:1302,“exportEnergy”:0,“importEnergy”:3825651,“phaseId”:0,“current”:55},  
> {“ctInput”:2,“power”:2834,“exportEnergy”:0,“importEnergy”:4361340,“phaseId”:0,“current”:120}],  
> “voltages”:[{“voltage”:236,“phaseId”:0}]}

---

<div class="post-metadata">

**Author:** ![MBGek](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/mbgek/32/17249_2.png) [@MBGek](https://community.homey.app/u/MBGek)\
**Post date:** [September 4, 2022, 6:51pm UTC](https://community.homey.app/t/mqtt-flow-parse-json/20531/9 "2022-09-04T18:51:25Z")

</div>

I’m trying do do the same, did you manage to get it to work?
