# MQTT topic messages to variable

**URL:** <https://community.homey.app/t/mqtt-topic-messages-to-variable/44689>\
**Category:** Questions & Help\
**Created:** [March 15, 2021, 8:25am UTC](https://community.homey.app/t/mqtt-topic-messages-to-variable/44689 "2021-03-15T08:25:56Z")\
**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:** [March 15, 2021, 8:25am UTC](https://community.homey.app/t/mqtt-topic-messages-to-variable/44689/1 "2021-03-15T08:25:56Z")

</div>

I can’t figure out how to put the result of a MQTT topic into a variable (number, so not a string).

 ![Schermafbeelding 2021-03-14 om 08.53.16](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/5/d/5df8f0593f14f2a7e7f20c9bf85280518141a369.png)

I made a script that parses the temp and hum into a variable for the topic: **itho/sensor**

 ![Schermafbeelding 2021-03-14 om 09.12.47](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/b/b/bbe30018b7e9a42110bcd9b0a9f131e7fb46df3c.png)

This script works fine and the humidity and temperature is put into a variable (number)  
[https://homey.app/f/tFULLw](https://homey.app/f/tFULLw)

The challenge I have with the topic: **itho/state** is that it is overwritten bij the other MQTT topic **itho/sensor**.  
[https://homey.app/f/7yvMLw](https://homey.app/f/7yvMLw)

The first flow is triggered by MQTT: **itho/state/sensor**. The second flow by: **itho/state**.  
What happend is that the variable of topic: **itho/state** is switching between a number 11 and {'temp":20,“hum”:41.9}.  
Does anybody know how to put the topic: **itho/state** into a variable (number, not a string) and/or can I add this to my current scripts?

---

<div class="post-metadata">

**Author:** ![peltsi51](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/peltsi51/32/12564_2.png) [@peltsi51](https://community.homey.app/u/peltsi51)\
**Post date:** [March 15, 2021, 9:45am UTC](https://community.homey.app/t/mqtt-topic-messages-to-variable/44689/2 "2021-03-15T09:45:04Z")

</div>

Will this help?

[https://homey.app/f/pgphRw](https://homey.app/f/pgphRw)

---

<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:** [March 15, 2021, 10:05am UTC](https://community.homey.app/t/mqtt-topic-messages-to-variable/44689/3 "2021-03-15T10:05:55Z")

</div>

I made a test flow and it seems to be that this solution works.

[https://homey.app/f/bXRlRw/](https://homey.app/f/bXRlRw/)

But what I see now is that my script stopt working. I think this is due to the calculation on the field: Message received from topic.

Maybe I have to add something to the script I made and not use a flow for this.

@robertklep, do you have any suggestion for me? I am completely stuck. 😅

---

<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:** [March 17, 2021, 7:26am UTC](https://community.homey.app/t/mqtt-topic-messages-to-variable/44689/4 "2021-03-17T07:26:14Z")

</div>

You say that the script parses the data from the topic `itho/sensor`, but the flow screenshot is showing it uses the topic `itho/state/sensor`.

I also don’t understand this: _" The challenge I have with the topic: **itho/state** is that it is overwritten bij the other MQTT topic **itho/sensor**."_

Those are two different topics, so how can one overwrite the other?

---

<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:** [March 17, 2021, 8:11am UTC](https://community.homey.app/t/mqtt-topic-messages-to-variable/44689/5 "2021-03-17T08:11:02Z")

</div>

Apparently it is not allowed to gave the same name to a Flow as given to a Var.  
I changed the name of the Var and the right information is put into it.

But when I make use of the flow below, my script does not work anymore. If I delete this flow (not disable), the script works again. I have no idea what the relationship is between this flow and the script, accept the label: Message received via topic. The topic used for the scripts is different.

[https://homey.app/f/OI6e-w/](https://homey.app/f/OI6e-w/)

---

<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:** [March 17, 2021, 8:12am UTC](https://community.homey.app/t/mqtt-topic-messages-to-variable/44689/6 "2021-03-17T08:12:47Z")

</div>

What does “my script doesn’t work anymore” actually mean? Does it fail? If so, do you get errors?

Also, I cannot reproduce any issues when I have a flow with the same name as a variable, so I think there’s something else going on.

---

<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:** [March 17, 2021, 8:14am UTC](https://community.homey.app/t/mqtt-topic-messages-to-variable/44689/7 "2021-03-17T08:14:46Z")

</div>

No, no errors. The only thing I can see is that the 2 vars (IthoTemp and IthoHum) which I have created in Better Logic, not updated anymore.

---

<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:** [March 17, 2021, 8:19am UTC](https://community.homey.app/t/mqtt-topic-messages-to-variable/44689/8 "2021-03-17T08:19:26Z")

</div>

Add a notification to see if the other flow actually gets triggered (although I just tested and if I have two flows that trigger on the same topic, they both get triggered).

And just for fun, try renaming the flow.

---

<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:** [March 17, 2021, 8:36am UTC](https://community.homey.app/t/mqtt-topic-messages-to-variable/44689/9 "2021-03-17T08:36:34Z")

</div>

I renamed the flow and have added in the other flow (see below) a notification.  
[https://homey.app/f/84Jj-w/](https://homey.app/f/84Jj-w/)

The result of adding the notification is that the flow which checks the status of itho/state and put this result into the var: Itho Speed State, gets blank.  
It still looks like that something is going wring the"Message received from topic"

You have tested it with 2 flows triggered on the same topic. I have 2 flows triggers on 2 different topics.

---

<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:** [March 17, 2021, 8:40am UTC](https://community.homey.app/t/mqtt-topic-messages-to-variable/44689/10 "2021-03-17T08:40:40Z")

</div>

> [@RobvandeGoor](#):
>
> The result of adding the notification is that the flow which checks the status of itho/state and put this result into the var: Itho Speed State, gets blank.

What does “gets blank” mean?

---

<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:** [March 17, 2021, 8:47am UTC](https://community.homey.app/t/mqtt-topic-messages-to-variable/44689/11 "2021-03-17T08:47:41Z")

</div>

That means that no number is visible anymore.

 ![IMG_0927](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/0/6/06593f33e039d84c98059a1a24cb09cdc7868849.jpeg)

The Var: Itho Speed State will not show a number.

---

<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:** [March 17, 2021, 8:49am UTC](https://community.homey.app/t/mqtt-topic-messages-to-variable/44689/12 "2021-03-17T08:49:24Z")

</div>

~~To be honest, I have no idea what’s going on. These flows shouldn’t be able to influence each other.~~

Something else I don’t understand: in your script, you’re setting Better Logic variables. In the flow that runs the script, you’re also setting Logic(a) variables to “?”:

 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/c/7/c76284e45026ae3ca11613a9a75a6c4bc2933093.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:** [March 17, 2021, 8:57am UTC](https://community.homey.app/t/mqtt-topic-messages-to-variable/44689/13 "2021-03-17T08:57:17Z")

</div>

Sure I did. I think this is a bug in the web app.

I made a screen shot of the flow.

 ![Schermafbeelding 2021-03-17 om 09.54.44](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/1/d/1dea9a38d80cf98b891d7cd491c4ce1ec361abc2.png)

And this flow is working but the vars will not be updated anymore when using the other flow regarding the topic itho/state.

---

<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:** [March 17, 2021, 8:59am UTC](https://community.homey.app/t/mqtt-topic-messages-to-variable/44689/14 "2021-03-17T08:59:56Z")

</div>

> [@RobvandeGoor](#):
>
> And this flow is working but the vars will not be updated anymore when using the other flow regarding the topic itho/state.

Like I said, I don’t understand how one flow can influence the other ☹

---

<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:** [March 17, 2021, 9:13am UTC](https://community.homey.app/t/mqtt-topic-messages-to-variable/44689/15 "2021-03-17T09:13:47Z")

</div>

I did a last test and created a new var as a string for the topic: itho/state.  
The result was visible in the new var.  
But again the 2 vars, IthoTemp and IthoHum, not updated anymore of the other flow with topic: itho/state/sensor.

It looks like that you may not use the label “Message received from topic” for 2 different topics at the same time.  
Maybe the script delays the output a bit or it has something to do with Better Logic.

A possible solution can be that the information via MQTT is provided differently.  
If there is no need to pars the info for temp and hum via a script but can directly retrieved via a topic, like:

itho/state  
itho/temp  
itho/hum  
etc.

I wil contact the developer of this Itho module and ask him what can be done.

Thanks for your support. 😌

---

<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:** [March 17, 2021, 9:29am UTC](https://community.homey.app/t/mqtt-topic-messages-to-variable/44689/16 "2021-03-17T09:29:50Z")

</div>

> [@RobvandeGoor](#):
>
> It looks like that you may not use the label “Message received from topic” for 2 different topics at the same time.

Hmm yes, I think you’re right, I can reproduce the problem. I’ll have to check if it’s an issue with the MQTT Client, or something else.

---

<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:** [March 17, 2021, 9:31am UTC](https://community.homey.app/t/mqtt-topic-messages-to-variable/44689/17 "2021-03-17T09:31:32Z")

</div>

Thanks 😌 I think we are on the same page. 😅

---

<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:** [March 17, 2021, 9:41am UTC](https://community.homey.app/t/mqtt-topic-messages-to-variable/44689/18 "2021-03-17T09:41:27Z")

</div>

Try restarting the MQTT Client app. I initially had the same issue as you did, but when I restarted the app it looks like it’s working as expected.

---

<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:** [March 17, 2021, 9:55am UTC](https://community.homey.app/t/mqtt-topic-messages-to-variable/44689/19 "2021-03-17T09:55:36Z")

</div>

I did and the result is that a restart of the app does not help.  
As a test I have created a new var as a string for the same topic.

[https://homey.app/f/uIlC-w/](https://homey.app/f/uIlC-w/)

And as expected the wrong information is put in this var. Therefore the other var (number) is blank.

 ![IMG_0928](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/f/1/f1ae710fef8990cf7ccf1f2f807c94dd1d02a496.jpeg)

Message received by topic is overwritten every time and as I said earlier is it not allowed to use 2 different MQTT topics at the same time to update a var.

---

<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:** [March 17, 2021, 10:15am UTC](https://community.homey.app/t/mqtt-topic-messages-to-variable/44689/20 "2021-03-17T10:15:12Z")

</div>

I still don’t understand which topics you’re using and what values they contain. I’ve seen three topics being used now:

- `itho/state`
- `itho/sensor`
- `itho/state/sensor`

[Next page](https://community.homey.app/t/mqtt-topic-messages-to-variable/44689.md?page=2)
