# Add a MQTT return to a var

**URL:** <https://community.homey.app/t/add-a-mqtt-return-to-a-var/33635>\
**Category:** Questions & Help\
**Created:** [August 15, 2020, 11:06am UTC](https://community.homey.app/t/add-a-mqtt-return-to-a-var/33635 "2020-08-15T11:06:49Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![RobvandeGoor](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robvandegoor/32/20328_2.png) [@RobvandeGoor](https://community.homey.app/u/RobvandeGoor)\
**Post date:** [August 15, 2020, 11:06am UTC](https://community.homey.app/t/add-a-mqtt-return-to-a-var/33635/1 "2020-08-15T11:06:49Z")

</div>

I am struggling with the following:  
I am able to receive messages from a MQTT Topic. The message is for example: {“state”:1}  
In no way I can use this result in a logic part to make a decision.

I think that I have to create a script to pars the message. Is there a way and how to get as result after parsing: state:1?

---

<div class="post-metadata">

**Author:** ![Marcel\_Ubels](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcel_ubels/32/18860_2.png) [@Marcel\_Ubels](https://community.homey.app/u/Marcel_Ubels)\
**Post date:** [August 15, 2020, 11:48am UTC](https://community.homey.app/t/add-a-mqtt-return-to-a-var/33635/2 "2020-08-15T11:48:56Z")

</div>

I don’t know anything about MQTT, buth the result looks like a result I get from the Sonos api which i get through my NAS. The following message i receive

 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/b/b/bb5ac4bfc70e6a4aaf079f5749dd0f65c9948e05.jpeg)

This way I parse it in a variable

 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/7/4/74a2d7b7cb901be69fc570e6c6f1f0c9c18562c4.jpeg)

---

<div class="post-metadata">

**Author:** ![RobvandeGoor](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robvandegoor/32/20328_2.png) [@RobvandeGoor](https://community.homey.app/u/RobvandeGoor)\
**Post date:** [August 15, 2020, 12:59pm UTC](https://community.homey.app/t/add-a-mqtt-return-to-a-var/33635/3 "2020-08-15T12:59:13Z")

</div>

I try to get this example working but for now without success.  
What type of var did you make to get the parts data into RadioStation or RadioTrack?

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [August 15, 2020, 1:03pm UTC](https://community.homey.app/t/add-a-mqtt-return-to-a-var/33635/4 "2020-08-15T13:03:27Z")

</div>

MQTT is different from HTTP, those cards aren’t usable. I think the only way to parse data from MQTT message is using Homeyscript.

---

<div class="post-metadata">

**Author:** ![RobvandeGoor](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robvandegoor/32/20328_2.png) [@RobvandeGoor](https://community.homey.app/u/RobvandeGoor)\
**Post date:** [August 15, 2020, 1:13pm UTC](https://community.homey.app/t/add-a-mqtt-return-to-a-var/33635/5 "2020-08-15T13:13:16Z")

</div>

I also thought so… question is… what is a simple way to get the data part form {“state”:0} or {“state”:1} to state 0 or state 1. Or maybe tetter to pars it to: Zone Off or Zone On.

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [August 15, 2020, 1:18pm UTC](https://community.homey.app/t/add-a-mqtt-return-to-a-var/33635/6 "2020-08-15T13:18:08Z")

</div>

You may be able to use the card “Logica — Bevat” and check if the message contain either `"state":1` or `"state":0`. Bit of a hack but might work and easier than using Homeyscript.

---

<div class="post-metadata">

**Author:** ![RobvandeGoor](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robvandegoor/32/20328_2.png) [@RobvandeGoor](https://community.homey.app/u/RobvandeGoor)\
**Post date:** [August 15, 2020, 1:55pm UTC](https://community.homey.app/t/add-a-mqtt-return-to-a-var/33635/7 "2020-08-15T13:55:36Z")

</div>

This was also my first thought but this will not work. I suppose due to the " " , logic can’t read it.

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [August 15, 2020, 2:19pm UTC](https://community.homey.app/t/add-a-mqtt-return-to-a-var/33635/8 "2020-08-15T14:19:13Z")

</div>

You could also try matching against `:1` and `:0`.

---

<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:** [August 15, 2020, 2:26pm UTC](https://community.homey.app/t/add-a-mqtt-return-to-a-var/33635/9 "2020-08-15T14:26:41Z")

</div>

> [@robertklep](#):
>
> `"state":1`

I don’t understand why @robertklep solution won’t work. Have you tried this?

 ![afbeelding](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/2/8/2884828a29ff4bb8d1ea3ac854cc5895bb8e823e.png)

If this does not work, I would pass the received “state” to a variable. This way you can exactly extract the string to compare with. (and try the first flow again with the new string)

 ![afbeelding](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/2/b/2bae6cac5823963a39f1bca075f97bce120ec011.png)

---

<div class="post-metadata">

**Author:** ![RobvandeGoor](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robvandegoor/32/20328_2.png) [@RobvandeGoor](https://community.homey.app/u/RobvandeGoor)\
**Post date:** [August 15, 2020, 2:51pm UTC](https://community.homey.app/t/add-a-mqtt-return-to-a-var/33635/10 "2020-08-15T14:51:10Z")

</div>

> [@robertklep](#):
>
> “state”:1

I have done as you requested. Even with a var included this solution does not work.  
I included also a notification so I was sure that the Topic was received.

This is the received Topic:

 ![IMG_0410](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/2/2/22dcfdf02555e4fede4c715ea7370f596d54a573.jpeg)

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [August 15, 2020, 2:55pm UTC](https://community.homey.app/t/add-a-mqtt-return-to-a-var/33635/11 "2020-08-15T14:55:18Z")

</div>

“is precies” won’t work because the value is `{"state":1}` (brackets around it).

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [August 15, 2020, 2:55pm UTC](https://community.homey.app/t/add-a-mqtt-return-to-a-var/33635/12 "2020-08-15T14:55:39Z")

</div>

Can you show the flow you’re using?

---

<div class="post-metadata">

**Author:** ![RobvandeGoor](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robvandegoor/32/20328_2.png) [@RobvandeGoor](https://community.homey.app/u/RobvandeGoor)\
**Post date:** [August 15, 2020, 3:00pm UTC](https://community.homey.app/t/add-a-mqtt-return-to-a-var/33635/13 "2020-08-15T15:00:26Z")

</div>

Sure, no problem.

Flow:

 ![Schermafbeelding 2020-08-15 om 16.57.43](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/e/5/e5043be443d0f9d8b248b4b85cfc2ded06f5fe08.png)

The var part is like this:

 ![IMG_0411](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/d/3/d330d302202040e070775f7e1ed9626a7b84b37a.jpeg)

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [August 15, 2020, 5:17pm UTC](https://community.homey.app/t/add-a-mqtt-return-to-a-var/33635/14 "2020-08-15T17:17:21Z")

</div>

I don’t understand why you’re using a variable (`ZoneAan`). Try matching against normal text content, and make sure that you’re using the right quote characters (not `“state”` but `"state"`, i.e. regular quotes instead of “smart” quotes).

---

<div class="post-metadata">

**Author:** ![RobvandeGoor](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robvandegoor/32/20328_2.png) [@RobvandeGoor](https://community.homey.app/u/RobvandeGoor)\
**Post date:** [August 15, 2020, 11:30pm UTC](https://community.homey.app/t/add-a-mqtt-return-to-a-var/33635/15 "2020-08-15T23:30:14Z")

</div>

> [@robertklep](#):
>
> “state”

Using a var wat a proposal of Satoer. I have changed is back as you can see in the flow below.

 ![Schermafbeelding 2020-08-16 om 01.25.47](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/b/2/b2c7b1171d2166f21d870ba69e248cba40f0687f.png)

I have checked the quotes but it doesn’t; work. For me it is a big question mark why this simple test flow does not work.

---

<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:** [August 15, 2020, 11:41pm UTC](https://community.homey.app/t/add-a-mqtt-return-to-a-var/33635/16 "2020-08-15T23:41:29Z")

</div>

You can send the received message to a log to see how the message exactly looks.

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [August 16, 2020, 6:26am UTC](https://community.homey.app/t/add-a-mqtt-return-to-a-var/33635/17 "2020-08-16T06:26:46Z")

</div>

The “Logica bevat” card apparently is broken, it can’t deal with quotes. So instead of matching against `"state":1`, match against `:1`. I confirmed that this works for me.

---

<div class="post-metadata">

**Author:** ![RobvandeGoor](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robvandegoor/32/20328_2.png) [@RobvandeGoor](https://community.homey.app/u/RobvandeGoor)\
**Post date:** [August 16, 2020, 6:50am UTC](https://community.homey.app/t/add-a-mqtt-return-to-a-var/33635/18 "2020-08-16T06:50:37Z")

</div>

Robert,

I have tried your proposal and it worked. Strange thing is when I got the topic message for switching it off within the 20 second, this flow wil also be activated.  
When I looked to the other status (off) the topic will be like this: {“state”:0,“duration”:60}  
This is a combined topic State and Time.  
Maybe it will at the end easer using a different solution, like scripting. No clue yet how to make a script but that is also a challenging part.  
What I do not completely understand is, MQTT is not new. Why is there still so little information how to deal with topic information in Homey?"

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [August 16, 2020, 6:58am UTC](https://community.homey.app/t/add-a-mqtt-return-to-a-var/33635/19 "2020-08-16T06:58:17Z")

</div>

> [@RobvandeGoor](#):
>
> What I do not completely understand is, MQTT is not new. Why is there still so little information how to deal with topic information in Homey?

Topic information in MQTT can be _anything_: JSON (like in this case), plain strings, XML, whatever. So I understand that the MQTT Client app doesn’t try to parse payloads and passes them as-is.

Ideally, someone would make a specific parser app to deal with different kinds of payloads, like a JSON parser app. But such an app doesn’t exist, so for now we have to use workarounds.

---

<div class="post-metadata">

**Author:** ![RobvandeGoor](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robvandegoor/32/20328_2.png) [@RobvandeGoor](https://community.homey.app/u/RobvandeGoor)\
**Post date:** [August 16, 2020, 7:02am UTC](https://community.homey.app/t/add-a-mqtt-return-to-a-var/33635/20 "2020-08-16T07:02:24Z")

</div>

Thanks for the support.  
I wil check if other MQTT client apps behave the same as the one which is currently running.

It is clear for me that there is no solution yet for Homey to simply deal with MQTT topics and have to use a workaround.

[Next page](https://community.homey.app/t/add-a-mqtt-return-to-a-var/33635.md?page=2)
