Get value from MQTT and set a variable

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

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.

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:

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 …

1 Like

With this scipt:

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

I get this error:

———————————————————
❌ 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…

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

This is working:

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

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

But not in the flow.
I have tried to log something:

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 …

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

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

And I get message in the log in MQTT Client app


I don’t unsterstand what’s happen

This is the solution:

So this Flow works:

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! :smiley:

2 Likes

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!