Homey Community Forum

MQTT topic messages to variable

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

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

This script works fine and the humidity and temperature is put into a variable (number)

The challenge I have with the topic: itho/state is that it is overwritten bij the other MQTT topic itho/sensor.

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?

Will this help?


1 Like

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

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. :sweat_smile:

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?

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.

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.

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.

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.

I renamed the flow and have added in the other flow (see below) a notification.

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.

What does “gets blank” mean?

That means that no number is visible anymore.

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

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 “?”:

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

I made a screen shot of the flow.

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 :frowning:

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:


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

Thanks for your support. :relaxed:

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.

1 Like

Thanks :relaxed: I think we are on the same page. :sweat_smile:

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.

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.

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

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.

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