Splitting msg payload fields

I have a incoming message of


Now I need two messages out and they need to look like this

First one

msg.payload.app_id: "s****1"
msg.payload.payload_fields.temperature: "27.9"
msg.payload.metadata.time: "2021-01-30T11:18:12.547860192Z"
msg.payload.sensor: "temperature"

Second one

msg.payload.app_id: "s****1"
msg.payload.payload.payload_fields.humidity: "77.5"
msg.payload.metadata.time: "2021-01-30T11:18:12.547860192Z"
msg.payload.sensor: "humidity"

Real appreciate your guys help always.

You could probably do it a few ways.

In a function node (deliberately long hand so its easier to read)...

var pl1 = {}; //make a new empty object for payload 1
pl1.app_id =  msg.payload.app_id;
pl1.payload_fields = { temperature: msg.payload.payload_fields.temperature };
pl1.metadata = { time:  msg.payload.metadata.time };
pl1.sensor =  "temperature";
var topic1 = msg.topic + "/" + pl1.sensor;

var pl2 = {}; //make a new empty object for payload 2
pl2.app_id =  msg.payload.app_id;
pl2.payload_fields = { humidity: msg.payload.payload_fields.humidity };
pl2.metadata = { time:  msg.payload.metadata.time };
pl2.sensor =  "humidity";
var topic2 = msg.topic + "/" + pl2.sensor;

node.send( { topic: topic1, payload: pl1 }); //send 1st msg
node.send( { topic: topic2, payload: pl2 }); //send 2nd msg

NOTE: this will send the 2 messages out of the same output pin.

1 Like

Thank you exactly what I were looking for. Appreciate you help

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