Get value from MQTT and set a variable


my idea is to receive a value over MQTT or HTTP request and write this value to a virtual Device or variable.

In case of MQTT there is a tag “message received from topic”, but this tag is only available for comparison/Logic in the AND flow. I see no possibilities to use the value that is in the message Tag to use it in the THEN flow.

I think the main reason will be, that the message is a STRING and it has to be converted to a number before it can be used for a “set value”.

Are there any other possibilities to receive values from outside and use this values in homey flows?


I had the same issue and used Homey script to convert the value to an integer.

to use HomeyScript sounds good. Bu I have no experience with that.
Can anyone help me to program this function to get a start?

My idea is to create a general function with two arguments.

StringToNumber(String, Number)


1 Like

ok, I found a solution:

HomeyScript that converts the argument to a number and create a Tag “RetNum” with the result

// Converts a atring to a number
var MyNum = parseFloat(args[0])
await setTagValue("RetNum", {type: "number", title: "RetNum"}, MyNum);
return true;

The flow looks like this:


I am trying to do the same thing but from my weatherstation.
But I have never used homeyscripts and get a error message trying this script.
The value look like this:
{ 17.9 }
The script I wrote looks like this:
var MyNum = parseFloat(args[0])
await setTagValue(“RetNum”, { “number” }, MyNum);
return true;

Get error Unexpected token }: Unexpected token }

The construct { "number" } is invalid JS code, I think you mean this:

await setTagValue("RetNum", { type : "number" }, MyNum)

That worked.
But I dont get my virtual device (temperature) to update.
Running this flow

How do I know if my tag is a STRING or VALUE ?

I’ll have to defer to people that know more about flows under v2 than I do. But a few potential problems come to mind:

  • THEN cards are not executed one-after-the-other, but all at the same time. So while your script it still running (card #1), the value of the sensor is already been set (card #v2)
  • Instead of using a tag “Temperature”, use the string “Temperature”

Thanks for your answers.
In the code below what does RetNum come from?
var MyNum = parseFloat(args[0])
await setTagValue("RetNum", {type: "number", title: "RetNum"}, MyNum);
return true;

That’s the name of the tag you’re creating, and you can make up that name yourself (so giving it a more descriptive name might be better).

Sorry for more stupid questions.
MyNum is that then the name of the “value” that comes from my MQTT server ?
The topic from MQTT is WeatherDisplay/temp and the “value” looks like this { 18.0 }

MyNum is a JavaScript variable that you are assigning (through args[0]) the value from the MQTT topic. Is that value a string? If so, you first need to parse out the numerical value from it, you can’t use parseFloat on such a string directly.

1 Like

are you aware that you have to use the “technical name” of the virtual device for the capability you want to set?
see here:

for example:

1 Like

Must do something wrong. Cant even get my virtual sensor to update with the test button.

I got the green “arrow” that says the the flow has run without problem, but when I look at the sensor it doens have any value

It looks to me like you have the sensor capability (measure_temperature) and the value (Temperature) the wrong way around.

1 Like

I will try change them around.
I thought them was right, see picture

Still no luck even when changed the sensor and values in the other way.

Perhaps the flow editor shows it in a strange order. The “Sensor” should be measure_temperature and the “Value” should be the tag.

Also, it looks like you’re using the unofficial dashboard. I don’t know it that will show these devices properly. It’s better to check to see the capabilities of the device.

OK, then its correct right now.
The dashboard picks the measure_temperature tag
Even in the Homey app the “Testmqtt” virtual device doesnt show any values.

In that case, there’s a big chance that showing sensor values for virtual devices just isn’t supported. Perhaps @DaneedeKruyff can comment on that with regard to the dashboard?

But even in Athoms Homey app it dont show values
And if I look at the device in it says null
So I am probaly doing something wrong when I cant even get the test for updating sensor values right.