# Get value from MQTT and set a variable

**URL:** <https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090>\
**Category:** Questions & Help\
**Created:** [December 30, 2018, 12:01pm UTC](https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090 "2018-12-30T12:01:31Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![dewenni](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/dewenni/32/7912_2.png) [@dewenni](https://community.homey.app/u/dewenni)\
**Post date:** [December 30, 2018, 12:01pm UTC](https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090/1 "2018-12-30T12:01:31Z")

</div>

Hello,

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?

thanks!

---

<div class="post-metadata">

**Author:** ![Robin\_van\_Kekem](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robin_van_kekem/32/8187_2.png) [@Robin\_van\_Kekem](https://community.homey.app/u/Robin_van_Kekem)\
**Post date:** [December 30, 2018, 7:23pm UTC](https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090/2 "2018-12-30T19:23:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dewenni](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/dewenni/32/7912_2.png) [@dewenni](https://community.homey.app/u/dewenni)\
**Post date:** [December 31, 2018, 9:21am UTC](https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090/3 "2018-12-31T09:21:37Z")

</div>

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)

 ![HomeyScript](https://us1.discourse-cdn.com/flex025/uploads/athom/original/2X/d/ded7acf043e77601ef8688304836249f214ae926.jpeg)

thanks!

---

<div class="post-metadata">

**Author:** ![dewenni](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/dewenni/32/7912_2.png) [@dewenni](https://community.homey.app/u/dewenni)\
**Post date:** [December 31, 2018, 1:19pm UTC](https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090/4 "2018-12-31T13:19:54Z")

</div>

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:

 ![HomeyScript](https://us1.discourse-cdn.com/flex025/uploads/athom/original/2X/d/ded7acf043e77601ef8688304836249f214ae926.jpeg)

---

<div class="post-metadata">

**Author:** ![Kalle\_Kronbeck](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/kalle_kronbeck/32/13158_2.png) [@Kalle\_Kronbeck](https://community.homey.app/u/Kalle_Kronbeck)\
**Post date:** [June 4, 2019, 12:59pm UTC](https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090/5 "2019-06-04T12:59:36Z")

</div>

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 }

---

<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:** [June 4, 2019, 1:21pm UTC](https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090/6 "2019-06-04T13:21:39Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![Kalle\_Kronbeck](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/kalle_kronbeck/32/13158_2.png) [@Kalle\_Kronbeck](https://community.homey.app/u/Kalle_Kronbeck)\
**Post date:** [June 4, 2019, 1:25pm UTC](https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090/7 "2019-06-04T13:25:17Z")

</div>

> [@dewenni](#):
>
> var MyNum = parseFloat(args[0])

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

 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/2X/f/f48bb85ad0f45fc267c4736cae4a9cc38e98f2e2.jpeg)

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

---

<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:** [June 4, 2019, 1:45pm UTC](https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090/8 "2019-06-04T13:45:25Z")

</div>

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”

---

<div class="post-metadata">

**Author:** ![Kalle\_Kronbeck](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/kalle_kronbeck/32/13158_2.png) [@Kalle\_Kronbeck](https://community.homey.app/u/Kalle_Kronbeck)\
**Post date:** [June 4, 2019, 4:56pm UTC](https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090/9 "2019-06-04T16:56:39Z")

</div>

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;`

---

<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:** [June 4, 2019, 6:01pm UTC](https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090/10 "2019-06-04T18:01:46Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![Kalle\_Kronbeck](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/kalle_kronbeck/32/13158_2.png) [@Kalle\_Kronbeck](https://community.homey.app/u/Kalle_Kronbeck)\
**Post date:** [June 4, 2019, 6:23pm UTC](https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090/11 "2019-06-04T18:23:26Z")

</div>

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 }

---

<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:** [June 4, 2019, 7:12pm UTC](https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090/12 "2019-06-04T19:12:08Z")

</div>

`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.

---

<div class="post-metadata">

**Author:** ![dewenni](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/dewenni/32/7912_2.png) [@dewenni](https://community.homey.app/u/dewenni)\
**Post date:** [June 4, 2019, 8:31pm UTC](https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090/13 "2019-06-04T20:31:20Z")

</div>

are you aware that you have to use the “technical name” of the virtual device for the capability you want to set?  
see here: [https://developer.athom.com/docs/apps/tutorial-Drivers-Reference.html](https://developer.athom.com/docs/apps/tutorial-Drivers-Reference.html)

for example:

 ![IMG_2394](https://us1.discourse-cdn.com/flex025/uploads/athom/original/2X/5/5b8c6d84852de2cdacd593379cd62f03802613a5.png)

---

<div class="post-metadata">

**Author:** ![Kalle\_Kronbeck](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/kalle_kronbeck/32/13158_2.png) [@Kalle\_Kronbeck](https://community.homey.app/u/Kalle_Kronbeck)\
**Post date:** [June 5, 2019, 7:06am UTC](https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090/14 "2019-06-05T07:06:58Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/2X/6/67147e65f260480b819639df135bc4bf5a972c7f.jpeg)

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

---

<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:** [June 5, 2019, 7:22am UTC](https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090/15 "2019-06-05T07:22:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kalle\_Kronbeck](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/kalle_kronbeck/32/13158_2.png) [@Kalle\_Kronbeck](https://community.homey.app/u/Kalle_Kronbeck)\
**Post date:** [June 5, 2019, 7:29am UTC](https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090/16 "2019-06-05T07:29:03Z")

</div>

I will try change them around.  
I thought them was right, see picture  
 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/2X/1/118457f52d5a57a57246cb7ed4c3bce231026f02.jpeg)

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

 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/2X/1/1aa81898dc1c7328334c582fc5dcc418baff583a.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:** [June 5, 2019, 10:28am UTC](https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090/17 "2019-06-05T10:28:40Z")

</div>

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 [https://developer.athom.com/tools/devices](https://developer.athom.com/tools/devices) to see the capabilities of the device.

---

<div class="post-metadata">

**Author:** ![Kalle\_Kronbeck](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/kalle_kronbeck/32/13158_2.png) [@Kalle\_Kronbeck](https://community.homey.app/u/Kalle_Kronbeck)\
**Post date:** [June 5, 2019, 11:20am UTC](https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090/18 "2019-06-05T11:20:27Z")

</div>

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.

---

<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:** [June 5, 2019, 12:18pm UTC](https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090/19 "2019-06-05T12:18:44Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Kalle\_Kronbeck](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/kalle_kronbeck/32/13158_2.png) [@Kalle\_Kronbeck](https://community.homey.app/u/Kalle_Kronbeck)\
**Post date:** [June 5, 2019, 12:26pm UTC](https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090/20 "2019-06-05T12:26:58Z")

</div>

But even in Athoms Homey app it dont show values  
And if I look at the device in [http://developer.athom.com/](http://developer.athom.com/) it says null  
So I am probaly doing something wrong when I cant even get the test for updating sensor values right.

 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/2X/0/03cc4a3a2c2ef5009d582565aa2f168a5a7e1f64.png)

[Next page](https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090.md?page=2)
