Extracting data from a TTN payload for insertion into MariadB

Gooday, I have a SenseCAP LoRa barometer connected to The Things Network and trying to extract the payload delivered by a MQTT node. The payload(one big object) is as follows:

22/09/2021, 19:29:38[node: c820ff3f.fc1bf]( : msg.payload : string[1783]


The part I wish to extract and insert into MariadB is the 102241 part of "measurementValue":102241. I have a function node which contains the following:

msg.baro = msg.payload.measurementValue;
msg.topic = "INSERT INTO TTNsensors(barometrics) VALUES('"+msg.baro + "');"
return msg;

Following the mysql node, I get following debug message and nothing is inserted into the database.

22/09/2021, 19:29:40[node: e347f173.4d1de]( INTO TTNsensors(barometrics) VALUES('undefined'); : msg : Object
topic: "INSERT INTO TTNsensors(barometrics) VALUES('undefined');"
payload: object
qos: 0
retain: false
_msgid: "ef678dbd.24a44"
baro: undefined

I am guessing the problem lies in the payload extraction function node. Can I get some guidance in what I need to change to make this work.

It's hard to be sure since you have not supplied the entire message payload

Is your msg.payload a javascript object or is it a string?
If it's a string but contains valid JSON you can run it through a Json node to create an object.

Once it is in the right format I think measurementValue will be msg.payload.uplink_message.messages.measurementValue

Here is an example of debug output.
You see that msg.payload is an Object and you can explore it's properties with the little grey triangles.
The temperature reading is at msg.payload.Readings.temperature

Untitled 7

Do you have the mqtt-in node set to return a string, buffer, json object, base64?
Screen Shot 2021-09-22 at 9.29.08 AM

I think it must be a string as in the first line of the payload it says: msg.payload : string[1783].In my initial post I copied the whole message as output in a debug node after the mqtt node.

I have a couple of other sensors which present the data as an object(like in your temp, hum, illum. example) and I have no problems extracting the values and insertion into MariadB.

So the solution is to run it through a Json parser node.? Will give it a try and see if it looks OK.

Thanks for your input. Yes mqtt node is set to autodetect.

Try changing it to a 'parsed JSON object' then you won’t need to use the json node

Done will see what happens. Node sends data every 30 mins.
Don't know whether this confuses things but I read a post of yours assisting another bloke and to use a split - switch - split series of nodes and was able to extract just the barometric value eg 102241 Not sure of the next step for the database insert though.

Looks good.

Now the copied path is: payload.uplink_message.decoded_payload.messages[0].measurementValue

Don't know where the [0] came from but will see if it works.

You beauty it works. Thanks guys. Here is my decode for completeness.

msg.baro = msg.payload.uplink_message.decoded_payload.messages[0].measurementValue; msg.topic = "INSERT INTO TTNsensors(barometrics) VALUES('"+msg.baro + "');" return msg;

1 Like

I keep forgetting the Mqtt In node's option to output as JSON. Shame the Exec node doesn't have the same options.

FYI you could avoid creating the new message property msg.baro like this:

msg.topic = "INSERT INTO TTNsensors (barometrics) VALUES ( '" 
+  msg.payload.uplink_message.decoded_payload.messages[0].measurementValue + "')"

To be pedantic, the option is to not output as JSON, but convert the JSON it gets in to a javascript object. So outputing "a parsed JSON object" means it parses the JSON and outputs an Object.

Of course that's right. Thanks for the ticking off Colin :smiley:

Thanks, I'll give it a go. Learning lots of good stuff here today, remembering it for next time is the challenge.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.