Manipulate Json format before sending in MQTT

Hi,
I'm new in the world of API and json format. I start my first project with some IOT device.
I take the value of my Temperature sensor with HTTP Request on Sensecap API.

I curently use TagoIO website for my alarm and dashboard. I try to send my data to Tagoio with MQTT. The problem is they need a specific format of data.
Now I have this:

{"code":"0","data":[{"channel":"1","points":[{"value":"23.45","measurement_id":"4097","created":"1669950770354"},{"value":"28.5","measurement_id":"4098","created":"1669950770354"}]}]}

I need to have something similar to this

[
  {
    "variable": "temperature",
    "value": 23.45,
    "unit": "C",
    "time": "$timestamp$",

  }
]

How I can manipulate the data and format it correctly? Thx for your hints.

use a template to put your structure together like you need. Use mustache and address the data from your message output. More tomorrow.

If you are sending it over MQTT it is the payload that is sent.

So you would have to put all of that information in the payload part of the message.

eg:

msg.payload = 
    {
        "variable": "temperature",
        "value": 71,
        "unit": "F",
        "time": "$timestamp$",
        "group": "1568913302243",

    };

I have this originaly:
{"code":"0","data":[{"channel":"1","points":[{"value":"23.45","measurement_id":"4097","created":"1669950770354"},{"value":"28.5","measurement_id":"4098","created":"1669950770354"}]}]}
On My MQTT service, he dont recognise it.

I know 4097 is temperature sensor and 4098 is humidity.

So'll need something like

"variable": "temperature",
 "value": 23.45,
 "time": 1669950770354,

"variable": "humidity",
 "value": 28.5,
 "time": 1669950770354,

Why not an object that describes the values ?

With a change node, set a new property to a JSONata expression:

{
    "temperature":$number(payload.data[0].points[0].value),
    "humidity": $number(payload.data[0].points[1].value),
    "timestamp": payload.data[0].points[0].created
}

output:
image

1 Like

Seem interesting bit i'm really noob on nodered. I set this code on a function block?

In a change node.

I would highly recommend to go through the extensive node red documentation, as it explains a lot of core concepts.

1 Like

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