# 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:** 2

<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:28pm UTC](https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090/21 "2019-06-05T12:28:50Z")

</div>

Ah okay, I misunderstood. In that case, try `log(MyNum)` in your script to see what the actual value of that variable is, perhaps it’s not being parsed correctly.

And/or try setting a fixed value to try and see if it appears:

```auto
await setTagValue("RetNum", {type: "number", title: "RetNum"}, 12.3);

```

---

<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:41pm UTC](https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090/22 "2019-06-05T12:41:36Z")

</div>

> [@Kalle\_Kronbeck](#):
>
> var MyNum = parseFloat(args[0])

Thanks for your patience 🙂  
Still no luck, can I test it very simple first so I can see that the virtual sensor is updating

 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/2X/1/1742c5ab83ae249eb8a006f8a2b852fbe63a1920.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, 1:15pm UTC](https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090/23 "2019-06-05T13:15:31Z")

</div>

Can you make a screenshot of the opened “Set a virtual sensor value” card? And I see now that you’re using the `Temperature` tag there, but the tag you are setting in your code is called `RetNum`. _That’s_ the tag whose value you want to assign to `measure_temperature`.

Also, logging the value can be done like this:

```auto
log(args);
var MyNum = parseFloat(args[0]);
...the rest of your script...

```

Perhaps it’s easier if you visit the Homey Slack channel, more people are active there and you can probably get answers much quicker.

---

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

</div>

Be aware that variables made by HomyScript are shown under HomeyScript:

![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/2X/3/3bfb9d3ae5778872064599d441caa4e642479a92.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, 2:33pm UTC](https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090/25 "2019-06-05T14:33:33Z")

</div>

![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/2X/e/edbff893b5faa265a8190b7b0e10783f6cd3e350.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, 2:40pm UTC](https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090/26 "2019-06-05T14:40:34Z")

</div>

It should be (from top to bottom):

- `measure_temperature` (a string, not a tag)
- `RetNum` (the tag)

---

<div class="post-metadata">

**Author:** ![DaneedeKruyff](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/daneedekruyff/32/964_2.png) [@DaneedeKruyff](https://community.homey.app/u/DaneedeKruyff)\
**Post date:** [June 5, 2019, 3:28pm UTC](https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090/27 "2019-06-05T15:28:46Z")

</div>

Values for Virtual Devices are certainly supported!

> [@Homeydash.com, a Homey dashboard](https://community.homey.app/t/homeydash-com-a-homey-dashboard/13509/40):
>
> Find the custom made (credits to @DaneedeKruyff) icons!!

The two on the bottom right are Virtual Thermostats

---

<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, 3:36pm UTC](https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090/28 "2019-06-05T15:36:43Z")

</div>

You are right. Now I can get it to change values when I press Test flow.  
Problem now is that when the message come from MQTT server it look like this:  
{ 18.4 }

And when I push test flow and enter the message as { 18.4 } it dont update virtual sensor.  
I assume its now the script comes in ?!

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 5, 2019, 5:35pm UTC](https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090/29 "2019-06-05T17:35:11Z")

</div>

If `{ 18.4 }` is _literally_ the value, you need to parse out the numerical value. Something like this:

```auto
var MyNum = parseFloat(args[0].match(/[\d\.-]+/)[0]);

```

---

<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, 6:59pm UTC](https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090/30 "2019-06-05T18:59:27Z")

</div>

Created this script:  
var MyNum = parseFloat(args[0].match(/[\d.-]+/)[0]);  
await setTagValue(“RetNum”, {type: “number”, title: “RetNum”}, MyNum);  
return true

But get this error message:  
Script Error:  
Cannot read property ‘match’ of undefined

Using this flow

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

---

<div class="post-metadata">

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

</div>

Did you try adding an MQTT Device, which is available from the MQTT Hub (beta)? It has the sole purpose of linking capabilities to mqtt topics. Also adds some flow triggers.

---

<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, 9:02pm UTC](https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090/32 "2019-06-05T21:02:16Z")

</div>

I have read so much bad about the MQTT Hub so I want to stick with mosquitto.  
What I want to do is getting data from my weatherstation into Homey. And the software I use can send MQTT messages.

---

<div class="post-metadata">

**Author:** ![HarriedeGroot](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/harriedegroot/32/56160_2.png) [@HarriedeGroot](https://community.homey.app/u/HarriedeGroot)\
**Post date:** [June 5, 2019, 9:03pm UTC](https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090/33 "2019-06-05T21:03:55Z")

</div>

Just disable the hub service (app settings, so it will take 0 resources). Try using the MQTT device. Implementation is based on the Virtual Devices.  
Maybe it works for ya, maybe not…

---

<div class="post-metadata">

**Author:** ![HarriedeGroot](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/harriedegroot/32/56160_2.png) [@HarriedeGroot](https://community.homey.app/u/HarriedeGroot)\
**Post date:** [June 5, 2019, 9:05pm UTC](https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090/34 "2019-06-05T21:05:46Z")

</div>

And “read so much bad about the hub”?  
Note: The hub is not a broker like mosquito, it serves as an interface between homey devices & mqtt.

---

<div class="post-metadata">

**Author:** ![HarriedeGroot](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/harriedegroot/32/56160_2.png) [@HarriedeGroot](https://community.homey.app/u/HarriedeGroot)\
**Post date:** [June 5, 2019, 9:34pm UTC](https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090/35 "2019-06-05T21:34:39Z")

</div>

@Kalle_Kronbeck solved 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:** [June 6, 2019, 6:16am UTC](https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090/36 "2019-06-06T06:16:37Z")

</div>

You’re not passing any arguments (in the form of a tag) to the script, so `args` is an empty array. You did pass an argument (`Message received from topic`) [earlier on in this thread](https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090/7).

---

<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 6, 2019, 7:27am UTC](https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090/37 "2019-06-06T07:27:51Z")

</div>

@robertklep your the MAN !!  
Now it works perfect. Now I can try doing the same for other values like rain, wind and hum

Here is the solution:

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

And this script:  
var MyNum = parseFloat(args[0].match(/[\d.-]+/)[0]);

await setTagValue(“RetNum”, {type: “number”, title: “RetNum”}, MyNum);

return true

---

<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 25, 2019, 3:09pm UTC](https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090/38 "2019-06-25T15:09:21Z")

</div>

I am back 🙂

I now have built a Arudino unit that send MQTT with temp and hum  
Problem now is that parseFloat(args[0].match(/[\d.-]+/)[0]); cant get my values beacuse the messages dont look the same. So i need something that can get me values fom this message:

forrad/data {“TSTAMP”:69341650,“TEMP”:28.5,“HUM”:37.8}

---

<div class="post-metadata">

**Author:** ![Klim](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/klim/32/26121_2.png) [@Klim](https://community.homey.app/u/Klim)\
**Post date:** [June 6, 2020, 2:17am UTC](https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090/39 "2020-06-06T02:17:49Z")

</div>

Are your devices (El, Multisensor and Motion sensor) on screenshot virtual or real?  
If they are virtual, how did you manage to show value on badget?  
I’m very interested to do the same, especially with Lux and Watts. I have to go inside each badget to read value.

P.S  
Okay, i found answer. You are using homeydash.

---

<div class="post-metadata">

**Author:** ![sebyldino](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/sebyldino/32/20067_2.png) [@sebyldino](https://community.homey.app/u/sebyldino)\
**Post date:** [February 22, 2022, 9:17pm UTC](https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090/40 "2022-02-22T21:17:08Z")

</div>

Hello,

I created a Virtual Device, a noise sensor. So the capability is `measure_noise`.

I create too an [OpenMQTTGateway](https://docs.openmqttgateway.com/) with an esp-32.

How extract the “rssi” value of this MQTT message to put it into the Virtual Sensor?

```auto
home/esp32/BTtoMQTT/DA9A435CDCB9
{
"id":"D6:9A:43:5C:DC:B9",
"mac_type":1,
"rssi":-54,
"servicedata":"02008c411805b8dc7ea9"
}

```

i tried some scripts in this topic but does not work.

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

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