# 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:** 10\
**Page:** 3

<div class="post-metadata">

**Author:** ![Peter\_Kawa](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/peter_kawa/32/173848_2.png) [@Peter\_Kawa](https://community.homey.app/u/Peter_Kawa)\
**Post date:** [February 23, 2022, 12:26am UTC](https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090/41 "2022-02-23T00:26:07Z")

</div>

Here’s a way to do this (hit the `v` icon on the right to view the flow)

> [@MQTT NodeRed values](https://community.homey.app/t/mqtt-nodered-values/60459/6):
>
> You’re welcome. Yea, I left it for you to figure out grimacing It is possible, but is it better… There’s a disadvantage: As you can see, there’s a delay needed. While the shortest delay possible is ONE whole second, using 2 flows makes it respond quicker. In the previous 2 flow example, the 2nd flow fires as soon as the numeric var has changed…

---

<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 23, 2022, 5:48am UTC](https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090/42 "2022-02-23T05:48:29Z")

</div>

Hello,  
Thank you for your answer. But it does not work.  
I edit my post below with add the MQTT topic.

I have “home/esp32/BTtoMQTT/DA9A435CDCB9” not “home/esp32/BTtoMQTT/DA9A435CDCB9/rssi”

I think MQTT message is string value, the Virtual Device use string too.

---

<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:** [February 23, 2022, 6:21am UTC](https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090/43 "2022-02-23T06:21:31Z")

</div>

> [@sebyldino](#):
>
> i tried some script in this topic but does not work.

Using a script is the only way, though (I don’t think there are apps that can extract properties from JSON data).

Something like this:

```auto
const message = JSON.parse(args[0]);
await tag('rssi', Number(message.rssi));

```

And pass the received message from the MQTT broker, as a token, to the script:  
WHEN trigger when message is received on topic `home/esp32/BTtoMQTT/DA9A435CDCB9`  
AND execute `script` with argument `[message]`  
THEN …

---

<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 23, 2022, 6:50am UTC](https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090/44 "2022-02-23T06:50:30Z")

</div>

With this scipt:

```auto
const message = JSON.parse(args[0]);
await tag('rssi', Number(message.rssi));

```

I get this error:

```auto
———————————————————
❌ Script Error
⚠️ SyntaxError: Unexpected token u in JSON at position 0
    at JSON.parse (<anonymous>)
    at StringToNumber.js:1:22
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

```

“message” must be “home/esp32/BTtoMQTT/DA9A435CDCB9”?

Edit: I think i found thomething. I get an error because u is undefined (args is undefined in the script, but it will be in the flow). I wil try…

---

<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:** [February 23, 2022, 7:02am UTC](https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090/45 "2022-02-23T07:02:29Z")

</div>

> [@sebyldino](#):
>
> “message” must be “home/esp32/BTtoMQTT/DA9A435CDCB9”?

No, it must be a flow token representing the message.

---

<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 23, 2022, 8:01am UTC](https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090/46 "2022-02-23T08:01:44Z")

</div>

This is working:

```auto
const message = JSON.parse('{"id":"D6:9A:43:5C:DC:B9","mac_type":1,"rssi":-54,"servicedata":"02008c411805b8dc7ea9"}');
console.log(message.rssi)
return true;

```

```auto
-84

———————————————————
✅ Script Success
↩️ Returned: true

```

But not in the flow.  
I have tried to log something:  
[https://homey.app/f/QTOCBY/](https://homey.app/f/QTOCBY/)  
But nothing appear in the log. it looks like i’m not getting the messages.  
i have tried too with “home/#” in topic case of the first card but not working …

---

<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:** [February 23, 2022, 8:08am UTC](https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090/47 "2022-02-23T08:08:30Z")

</div>

> [@sebyldino](#):
>
> it looks like i’m not getting the messages.

That seems to be the case, yes, so you should start by fixing that 😉

---

<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 23, 2022, 8:10am UTC](https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090/48 "2022-02-23T08:10:17Z")

</div>

However, Home Assistant and Homey have been communicating perfectly for 2 years… 🙄

And I get message in the log in MQTT Client app

 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/b/6/b6da699cc6846d9dde12601879b995fb581129a3.png)  
I don’t unsterstand what’s happen

---

<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 23, 2022, 1:21pm UTC](https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090/49 "2022-02-23T13:21:54Z")

</div>

This is the solution:

> [@robertklep](#):
>
> Using a script is the only way, though (I don’t think there are apps that can extract properties from JSON data).
> 
> Something like this:
> 
> ```auto
> const message = JSON.parse(args[0]);
> await tag('rssi', Number(message.rssi));
> 
> ```
> 
> And pass the received message from the MQTT broker, as a token, to the script:  
> WHEN trigger when message is received on topic `home/esp32/BTtoMQTT/DA9A435CDCB9`  
> AND execute `script` with argument `[message]`  
> THEN …

So this Flow works:

> **[Rssi | Homey](https://homey.app/en-us/flow/u93LCY/)**

The “?” tag in the last card is the homeyscript’s tag. I dont know why it does not appear.

The error what i did, was add tag “Message received from topic” in the first card.

Thank you for helping! 😃

---

<div class="post-metadata">

**Author:** ![Martijn\_Hoogenbosch](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/martijn_hoogenbosch/32/10647_2.png) [@Martijn\_Hoogenbosch](https://community.homey.app/u/Martijn_Hoogenbosch)\
**Post date:** [April 6, 2022, 7:40am UTC](https://community.homey.app/t/get-value-from-mqtt-and-set-a-variable/6090/50 "2022-04-06T07:40:27Z")

</div>

Based on the scripts above I managed to automate my blinds, with the removal of support for my solar power I had to switch to MQTT and now I can lower and rise the blinds based on the MQTT value.

Thanks guys!

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