Get value from MQTT and set a variable

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.

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.

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…

1 Like

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.

1 Like

@Kalle_Kronbeck solved it?

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.

@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:

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

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

return true

2 Likes

I am back :slight_smile:

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}

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.

Hello,

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

I create too an OpenMQTTGateway with an esp-32.

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

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.

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!