Get value from MQTT and set a variable

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:

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

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

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:

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.

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

image

It should be (from top to bottom):

  • measure_temperature (a string, not a tag)
  • RetNum (the tag)

Values for Virtual Devices are certainly supported!

The two on the bottom right are Virtual Thermostats

2 Likes

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

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

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

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

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.