Function extract value help

#1

hi guys,

I have simple question.
working on some rough data and want to extract just the value in function node:

{"topic":"xiaomi/lounge/th","payload":{"cmd":"report","model":"weather.v1","sid":"158d000201b16a","short_id":33470,"data":"{"temperature":"2803"}"},"qos":0,"retain":false,"_msgid":"cc82776e.9b0708"}

just need numeric value of temperature out of it. Possibly with proper formatting 28.0 instead of 2803.
can anyone help?

thanks

0 Likes

#2

divide by 1000?

0 Likes

#3

Cough :wink:

0 Likes

#4

oops! I’m blaming auto correct!

1 Like

#5

@cismarine - divide by 100

0 Likes

#6

Thanks Paul,
That is the easy part, but how do I get just the numeric value out of the object?

0 Likes

#7

If you attach a debug node to the incoming message, what do you see?

0 Likes

#8

that's output from my mqtt:

{"topic":"xiaomi/lounge/th","payload":{"cmd":"report","model":"weather.v1","sid":"158d000201b16a","short_id":33470,"data":"{"humidity":"9334"}"},"qos":0,"retain":false,"_msgid":"53f2dd35.0db3d4"}

0 Likes

#9

Wire the mqtt-in node to a function-node:

var xm_msg = JSON.parse(msg.payload);

if (xm_msg.data) {
    var data = JSON.parse(xm_msg.data);

    if (data.temperature) {
        msg.payload = {"temperature": Math.round(data.temperature / 100)};
    } else if (data.humidity) {
        msg.payload = {"humidity": Math.round(data.humidity / 100)};
    }
}

return msg;
0 Likes

#10

Thank you very much.

0 Likes

#11

I have tested it and added couple more values.
Now I have follow up question...
How can I send each individual value on different function output?

return [msg1,msg2,msg3,msg4];

TIA

0 Likes

#12

Have you read the docs?

https://nodered.org/docs/writing-functions#multiple-outputs

0 Likes

#13

I suppose you are using several xiaomi devices. If so, you will have to select several parameters (cmd, sid, model). I would use switch nodes to do the job.
Which xiaomi models are you using?

0 Likes

#14

quite a few. Mainly aquara sensors.
I inject their data into mqtt already sorted by device (with separate topics)

0 Likes