# Parsing MQTT responses

**URL:** <https://discourse.nodered.org/t/parsing-mqtt-responses/18562>\
**Category:** General\
**Created:** [28 November 2019 16:36 UTC](https://discourse.nodered.org/t/parsing-mqtt-responses/18562 "2019-11-28T16:36:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![StrongTown](https://avatars.discourse-cdn.com/v4/letter/s/4bbf92/32.png) [@StrongTown](https://discourse.nodered.org/u/StrongTown)\
**Post date:** [28 November 2019 16:36 UTC](https://discourse.nodered.org/t/parsing-mqtt-responses/18562/1 "2019-11-28T16:36:31Z")

</div>

I have what I suppose may be a terribly naive question. I have a bunch of Sonoff switches running just fine, have their status on Dashboard, etc....

What I cannot figure out is how to parse data received from a Sonoff TH10 (tasmota) device to extract variable data (like temp and humidity). I have looked around and if I've seen it explained, I didn't know it. Here is an example response:

```auto
Beechnut/Sauna-TH10/stat/STATUS10 : msg : Object
object
topic: "Beechnut/Sauna-TH10/stat/STATUS10"
payload: "{"StatusSNS":{"Time":"2019-11-28T16:11:11","SI7021":{"Temperature":22.3,"Humidity":45.5},"TempUnit":"C"}}"
qos: 0
retain: false
_msgid: "57bab8b7.eff8a8"

```

I actually can get it to work (if the MQTT respons ehas the proper data) by using

```auto
    temperature = msg.payload.StatusSNS.SI7021.Temperature;
    humidity = msg.payload.StatusSNS.SI7021.Humidity;

```

however there are a bunch of MQTT messages that cause the error msg in debug of:

```auto
function : (error)
"TypeError: Cannot read property 'SI7021' of undefined"

```

that I can't seem to stop occurring. There has to be a better method, I just don't know what it is.  
st

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [28 November 2019 16:45 UTC](https://discourse.nodered.org/t/parsing-mqtt-responses/18562/2 "2019-11-28T16:45:31Z")

</div>

You need to figure out what kind of data is coming from your sonoff device, as your function receives data that is undefined in the function node. ie. the payload that you test for is not always the same.

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [28 November 2019 16:48 UTC](https://discourse.nodered.org/t/parsing-mqtt-responses/18562/3 "2019-11-28T16:48:06Z")

</div>

Filter out messages where required property exists

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/4/4c654227d3e494bc2efed1ae05ad514e38734d4c.png)

---

<div class="post-metadata">

**Author:** ![StrongTown](https://avatars.discourse-cdn.com/v4/letter/s/4bbf92/32.png) [@StrongTown](https://discourse.nodered.org/u/StrongTown)\
**Post date:** [28 November 2019 20:32 UTC](https://discourse.nodered.org/t/parsing-mqtt-responses/18562/4 "2019-11-28T20:32:58Z")

</div>

Thanks, this is working for me. I like that I can separate "cmnd", "stat" and "tele" to individual outputs using this.  
st

---

<div class="post-metadata">

**Author:** ![pcb1962](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pcb1962/32/10113_2.png) [@pcb1962](https://discourse.nodered.org/u/pcb1962)\
**Post date:** [29 November 2019 15:28 UTC](https://discourse.nodered.org/t/parsing-mqtt-responses/18562/5 "2019-11-29T15:28:51Z")

</div>

Inside the function node you can use hasOwnProperty() to check that you are dealing with the expected object type:

```auto
if ( msg.payload.hasOwnProperty("StatusSNS") ) {
  let temperature = msg.payload.StatusSNS.SI7021.Temperature;
  let humidity = msg.payload.StatusSNS.SI7021.Humidity;
  ;
  ;
}

```

---

<div class="post-metadata">

**Author:** ![StrongTown](https://avatars.discourse-cdn.com/v4/letter/s/4bbf92/32.png) [@StrongTown](https://discourse.nodered.org/u/StrongTown)\
**Post date:** [29 November 2019 15:44 UTC](https://discourse.nodered.org/t/parsing-mqtt-responses/18562/6 "2019-11-29T15:44:22Z")

</div>

Very helpful feedback, thank you.  
st
