Changing the message payload outcome without changing the function

Hi,
I have a node function that gives an output with a certain data format as follows

This is the script that produces the TempHumi outcome in the figure above

var TempHumi = [2];
tempEvents = msg.payload.TempHumi.Event
TempHumi[1] = msg.payload.TempHumi.SenVal/1000;
tempRange = msg.payload.TempHumi.Range;

switch (tempEvents).case 0:TempHumi[0] = ("Status normal");
break;
case 1:
TempHumi[0] = ("High alarm!");
break;
default:
TempHumi[0] = ("Reading error!");
}

msg.color = ((TempHumi[0]==="Status normal")? "White" : "Red");

switch (tempRange){ case 0: tempRange = (" 'C");
break;
default:
tempRange = ("\nRange parsing error!");
}

TempHumi[1] = TempHumi[1] + tempRange;
msg.payload = TempHumi;
return msg;

I want to change it to a different data format as follows

Is it possible to do this without changing the script of the function? In other words, is it possible to add a node that only can make this conversion process? should I change the returned message outcome or what?

Does anybody have an idea, is it possible to change the return message?

I cannot see any correlation between the two sets of data, so it isn't clear what mapping you actually want to do.

Do you have an example of the same set of data in the different formats you can share (screenshots aren't very helpful for us to copy and paste from...)?

The answer will likely be either a Change node using a JSONata expression, or another Function node. But it's hard to be any more specific without a complete example of the data

This is the build-up for the nodes. I the script for the function nodes for device, accelerometer and TempHumi node are as given shown here

I mean here the message payload. I want the data type to be converted for this format without changing the original script of each function to as I mentioned in my post

you told me something about Jsonata, would you please declare this to me, or the other options?

this is the original script for TempHumi node function

var TempHumi = [2];
tempEvents = msg.payload.TempHumi.Event
TempHumi[1] = msg.payload.TempHumi.SenVal/1000;
tempRange = msg.payload.TempHumi.Range;

switch (tempEvents){
case 0:
TempHumi[0] = ("Status normal");
break;
case 1:
TempHumi[0] = ("High alarm!");
break;
default:
TempHumi[0] = ("Reading error!");
}

msg.color = ((TempHumi[0]==="Status normal")? "White" : "Red");

switch (tempRange){ case 0: tempRange = (" 'C");
break;
default:
tempRange = ("\nRange parsing error!");
}

TempHumi[1] = TempHumi[1] + tempRange;
msg.payload = TempHumi;
return msg;

This is for accelerometer

var aryAccel=[12];
var aryAccelColor=[3];

//X-axis
aryAccel [0] = msg.payload.Accelerometer['X-Axis'].SenEvent;
switch (aryAccel [0]){
case 0:
aryAccel[0] = ("Status normal");
break;
case 1:
aryAccel[0] = ("High alarm!!!");
break;
default:
aryAccel[0] = ("Reading error!!!");
}

aryAccelColor[0] = ((aryAccel[0]==="Status normal")? "White" : "Red");
aryAccel [1] = msg.payload.Accelerometer['X-Axis'].OAVelocity/100;
aryAccel [2] = msg.payload.Accelerometer['X-Axis'].Peakmg/1000;
aryAccel [3] = msg.payload.Accelerometer['X-Axis'].RMSmg/1000;

//Y-axis
aryAccel [4] = msg.payload.Accelerometer['Y-Axis'].SenEvent;
switch (aryAccel [4]){
case 0:
aryAccel[4] = ("Status normal");
break;
case 1:
aryAccel[4] = ("High alarm!!!");
break;
default:
aryAccel[4] = ("Reading error!!!");
}

aryAccelColor[1] = ((aryAccel[4]==="Status normal")? "White" : "Red");
aryAccel [5] = msg.payload.Accelerometer['Y-Axis'].OAVelocity/100;
aryAccel [6] = msg.payload.Accelerometer['Y-Axis'].Peakmg/1000;
aryAccel [7] = msg.payload.Accelerometer['Y-Axis'].RMSmg/1000;

//Z-axis
aryAccel [8] = msg.payload.Accelerometer['Z-Axis'].SenEvent;
switch (aryAccel [8]){
case 0:
aryAccel[8] = ("Status normal");
break;
case 1:
aryAccel[8] = ("High alarm!!!");
break;
default:
aryAccel[8] = ("Reading error!!!");
}

aryAccelColor[2] = ((aryAccel[8]==="Status normal")? "White" : "Red");
aryAccel [9] = msg.payload.Accelerometer['Z-Axis'].OAVelocity/100;
aryAccel [10] = msg.payload.Accelerometer['Z-Axis'].Peakmg/1000;
aryAccel [11] = msg.payload.Accelerometer['Z-Axis'].RMSmg/1000;

msg.payload = aryAccel;
msg.color = aryAccelColor;
return msg;

and this is for the device

var aryDevice = ;

var tempEvents = msg.payload['Advantech/FF6026A5/data'].Device.Events;
case 0.aryDevice[0] = ("Status normal");
break;
case 1:
aryDevice [0] = ("High alarm!!!");
break;
default:
aryDevice [0] = ("Reading error!!!");
}
aryDevice[1] = msg.payload['uplink/FF6026A5'].devaddr;
var tempPowerSrc = msg.payload['Advantech/FF6026A5/data'].Device.PowerSrc;
switch (tempPowerSrc){
case 0:
aryDevice [2] = ("NA");
break;
case 1:
aryDevice [2] = ("Line power");
break;
case 2:
aryDevice [2] = ("Device battery");
break;
case 3:
aryDevice [2] = ("Line power+Device battery");
break;
default:
aryDevice [2] = ("Reading error!!!");
}
aryDevice[3] = msg.payload['Advantech/FF6026A5/data'].Device.BatteryVolt/1000;
aryDevice[4] = msg.payload['uplink/FF6026A5'].fcnt;
aryDevice[5] = msg.payload['uplink/FF6026A5'].lsnr;
aryDevice[6] = msg.payload['uplink/FF6026A5'].rssi;
aryDevice[7] = msg.payload['Advantech/FF6026A5/data'].Device.Time;
var tempDate = new Date();
aryDevice[8] = tempDate.toGMTString(aryDevice[7]);
aryDevice[7] = aryDevice[7] + " UTC";

msg.payload = aryDevice;
return msg;

I am still not clear what you want to do.

Please provide an example of the data you have and an example of what you want it to look like so we can see exactly what conversion you want to do.

The screenshots in your first post do not appear to contain the same data, so it is not clear what you want.

JSONata is a way of transforming JSON data between different formats. The Change node lets you provide a JSONata Expression that will get applied to each message passing through. See jsonata.org for details. It can be a bit tricky to get started with, which is why it would be helpful if you provided more details on the mapping you want to do.

This data is received from a sensor. I will attach the original script to import it into your node red. The general idea is that the msg payload of the script I am attaching is giving the outcome shown in the first figure.

What I would like to do is to convert the msg payload outcome to be like the second figure as this data should be sent to a private cloud and the cloud receives the data in a certain format.

If the msg payload remain the same as the first figure, the cloud will reject the data and will never receive and store it. therefore, I want the msg payload outcome to like the second example.

Note that: there is no connection between the outcome of the two figures. the two figures are just examples of the data format I am having right now and the data format wanted

here is the full code and you can import it directly to your node-red

[{"id":"26a6bafe.355f36","type":"ui_template","z":"350d6959.0572c6","group":"b91de5f.d4f6018","name":"table for Device","order":2,"width":"9","height":"7","format":"\n th{\n border-bottom: 3px solid white;\n }\n caption, th, td {\n padding: 5px;\n text-align: left; \n }\n\n<table style="width:100%">\n \n \n Device
address\n Power
source\n Battery
voltage (V) \n <th colspan="2">Timestamp\n \n \n \n {{msg.payload[1]}}\n {{msg.payload[2]}}\n {{msg.payload[3]}}\n <td colspan="2">{{msg.payload[7]}}
\n {{msg.payload[8]}} \n \n \n Fcnt\n U/L SNR\n U/L RSSI\n \n {{msg.payload[4]}}\n {{msg.payload[5]}}\n {{msg.payload[6]}} \n \n \n","storeOutMessages":true,"fwdInMessages":true,"x":957,"y":200,"wires":[]},{"id":"d57e518d.d5999","type":"mqtt in","z":"350d6959.0572c6","name":"WISE-2410_UpLink","topic":"uplink/#","qos":"2","broker":"bdb9800e.62a88","x":208,"y":170.00000190734863,"wires":[["4ac8176.067a5e8"]]},{"id":"6373bec.d91b44","type":"mqtt in","z":"350d6959.0572c6","name":"WISE-2410","topic":"Advantech/#","qos":"0","broker":"bdb9800e.62a88","x":186,"y":251.00000190734863,"wires":[["b81e76d6.f6cba8"]]},{"id":"b81e76d6.f6cba8","type":"json","z":"350d6959.0572c6","name":"","x":366,"y":251.00000190734863,"wires":[["1edc6145.beb50f","f795ec88.cfc4f","1192206d.84723","c05c7892.122fe8","92e143d8.0a773","612e7df1.96f314"]]},{"id":"4ac8176.067a5e8","type":"json","z":"350d6959.0572c6","name":"","x":429,"y":171.00000190734863,"wires":[["92e143d8.0a773"]]},{"id":"1edc6145.beb50f","type":"function","z":"350d6959.0572c6","name":"TempHumi","func":"var TempHumi = [2];\ntempEvents = msg.payload.TempHumi.Event\nTempHumi[1] = msg.payload.TempHumi.SenVal/1000;\ntempRange = msg.payload.TempHumi.Range;\n\nswitch (tempEvents){\n case 0:\n TempHumi[0] = ("Status normal");\n break;\n case 1:\n TempHumi[0] = ("High alarm!");\n break;\n default:\n TempHumi[0] = ("Reading error!");\n}\n\nmsg.color = ((TempHumi[0]==="Status normal")? "White" : "Red");\n\nswitch (tempRange){\n case 0:\n tempRange = (" \'C");\n break;\n default:\n tempRange = ("\nRange parsing error!");\n}\n\nTempHumi[1] = TempHumi[1] + tempRange;\nmsg.payload = TempHumi;\nreturn msg;\n","outputs":"1","noerr":0,"x":627,"y":438,"wires":[["d04cdd16.167e4","ec83835f.9c06a"]]},{"id":"ad2a796f.07fe48","type":"ui_template","z":"350d6959.0572c6","group":"ee55d72.8bcad28","name":"table for Accelerometer","order":0,"width":"10","height":"4","format":"<table style="width:100%">\n \n \n Sensor Event\n Velocity RMS (mm/s) \n Acceloratioin RMS (g) \n Acceloratioin Peak (g) \n \n \n X-Axis\n {{msg.payload[0]}}\n {{msg.payload[1]}} \n {{msg.payload[2]}} \n {{msg.payload[3]}} \n \n \n Y-Axis\n {{msg.payload[4]}}\n {{msg.payload[5]}} \n {{msg.payload[6]}} \n {{msg.payload[7]}} \n \n \n Z-Axis\n {{msg.payload[8]}}\n {{msg.payload[9]}} \n {{msg.payload[10]}} \n {{msg.payload[11]}} \n \n","storeOutMessages":true,"fwdInMessages":true,"x":901,"y":255,"wires":[]},{"id":"f795ec88.cfc4f","type":"function","z":"350d6959.0572c6","name":"Accelerometer","func":"var aryAccel=[12];\nvar aryAccelColor=[3];\n\n//X-axis\naryAccel [0] = msg.payload.Accelerometer['X-Axis'].SenEvent;\nswitch (aryAccel [0]){\n case 0:\n aryAccel[0] = ("Status normal");\n break;\n case 1:\n aryAccel[0] = ("High alarm!!!");\n break;\n default:\n aryAccel[0] = ("Reading error!!!");\n}\n\naryAccelColor[0] = ((aryAccel[0]==="Status normal")? "White" : "Red");\naryAccel [1] = msg.payload.Accelerometer['X-Axis'].OAVelocity/100;\naryAccel [2] = msg.payload.Accelerometer['X-Axis'].Peakmg/1000;\naryAccel [3] = msg.payload.Accelerometer['X-Axis'].RMSmg/1000;\n\n//Y-axis\naryAccel [4] = msg.payload.Accelerometer['Y-Axis'].SenEvent;\nswitch (aryAccel [4]){\n case 0:\n aryAccel[4] = ("Status normal");\n break;\n case 1:\n aryAccel[4] = ("High alarm!!!");\n break;\n default:\n aryAccel[4] = ("Reading error!!!");\n}\n\naryAccelColor[1] = ((aryAccel[4]==="Status normal")? "White" : "Red");\naryAccel [5] = msg.payload.Accelerometer['Y-Axis'].OAVelocity/100;\naryAccel [6] = msg.payload.Accelerometer['Y-Axis'].Peakmg/1000;\naryAccel [7] = msg.payload.Accelerometer['Y-Axis'].RMSmg/1000;\n\n//Z-axis\naryAccel [8] = msg.payload.Accelerometer['Z-Axis'].SenEvent;\nswitch (aryAccel [8]){\n case 0:\n aryAccel[8] = ("Status normal");\n break;\n case 1:\n aryAccel[8] = ("High alarm!!!");\n break;\n default:\n aryAccel[8] = ("Reading error!!!");\n}\n\naryAccelColor[2] = ((aryAccel[8]==="Status normal")? "White" : "Red");\naryAccel [9] = msg.payload.Accelerometer['Z-Axis'].OAVelocity/100;\naryAccel [10] = msg.payload.Accelerometer['Z-Axis'].Peakmg/1000;\naryAccel [11] = msg.payload.Accelerometer['Z-Axis'].RMSmg/1000;\n\nmsg.payload = aryAccel;\nmsg.color = aryAccelColor;\nreturn msg;","outputs":1,"noerr":0,"x":635,"y":282.00000190734863,"wires":[["ad2a796f.07fe48","b164d3b8.428dd","c4a0262a.112098","87e7c1e4.be359"]]},{"id":"d04cdd16.167e4","type":"ui_template","z":"350d6959.0572c6","group":"c9d732f5.4d951","name":"table for Temparature","order":0,"width":"10","height":"2","format":"<table style="width:100%">\n \n Event\n Temparature\n \n \n {{msg.payload[0]}}\n {{msg.payload[1]}} <i class="fa fa-thermometer-three-quarters fa-2x" aria-hidden="true">\n \n\n","storeOutMessages":true,"fwdInMessages":true,"x":897,"y":445,"wires":[]},{"id":"1192206d.84723","type":"function","z":"350d6959.0572c6","name":"OAVelocityChart","func":"msg.payload = msg.payload.Accelerometer['X-Axis'].OAVelocity/100;\n\nreturn msg;","outputs":1,"noerr":0,"x":648,"y":536,"wires":[["99275ab2.5a66e8"]]},{"id":"99275ab2.5a66e8","type":"ui_chart","z":"350d6959.0572c6","name":"","group":"1eec8fb4.9e1aa","order":0,"width":"11","height":"4","label":"Velocity RMS (Root-Mean-Squar)","chartType":"line","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","ymin":"","ymax":"","removeOlder":1,"removeOlderPoints":"1000","removeOlderUnit":"3600","cutout":0,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"x":847,"y":539,"wires":[,]},{"id":"c05c7892.122fe8","type":"function","z":"350d6959.0572c6","name":"TempHumiChart","func":"msg.payload = msg.payload.TempHumi.SenVal/1000;\n\nreturn msg;","outputs":1,"noerr":0,"x":648,"y":605,"wires":[["b973abc0.57d728"]]},{"id":"b973abc0.57d728","type":"ui_chart","z":"350d6959.0572c6","name":"","group":"1eec8fb4.9e1aa","order":0,"width":"11","height":"5","label":"Temperature","chartType":"line","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","ymin":"","ymax":"","removeOlder":1,"removeOlderPoints":"1000","removeOlderUnit":"3600","cutout":0,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"x":855,"y":603,"wires":[,]},{"id":"92e143d8.0a773","type":"join","z":"350d6959.0572c6","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\n","timeout":"","count":"2","x":605,"y":210.00000190734863,"wires":[["bb36241.378c0d8"]]},{"id":"bb36241.378c0d8","type":"function","z":"350d6959.0572c6","name":"Device","func":"var aryDevice = ;\n\nvar tempEvents = msg.payload['Advantech/FFFFFFFF/data'].Device.Events;\nswitch (tempEvents){\n case 0:\n aryDevice[0] = ("Status normal");\n break;\n case 1:\n aryDevice [0] = ("High alarm!!!");\n break;\n default:\n aryDevice [0] = ("Reading error!!!");\n}\naryDevice[1] = msg.payload['uplink/FFFFFFFF'].devaddr;\nvar tempPowerSrc = msg.payload['Advantech/FFFFFFFF/data'].Device.PowerSrc;\nswitch (tempPowerSrc){\n case 0:\n aryDevice [2] = ("NA");\n break;\n case 1:\n aryDevice [2] = ("Line power");\n break;\n case 2:\n aryDevice [2] = ("Device battery");\n break;\n case 3:\n aryDevice [2] = ("Line power+Device battery");\n break;\n default:\n aryDevice [2] = ("Reading error!!!");\n}\naryDevice[3] = msg.payload['Advantech/FFFFFFFF/data'].Device.BatteryVolt/1000;\naryDevice[4] = msg.payload['uplink/FFFFFFFF'].fcnt;\naryDevice[5] = msg.payload['uplink/FFFFFFFF'].lsnr;\naryDevice[6] = msg.payload['uplink/FFFFFFFF'].rssi;\naryDevice[7] = msg.payload['Advantech/FFFFFFFF/data'].Device.Time;\nvar tempDate = new Date();\naryDevice[8] = tempDate.toGMTString(aryDevice[7]);\naryDevice[7] = aryDevice[7] + " UTC";\n\nmsg.payload = aryDevice;\nreturn msg;\n\n\n\n","outputs":1,"noerr":0,"x":769,"y":209.00000190734863,"wires":[["26a6bafe.355f36"]]},{"id":"612e7df1.96f314","type":"debug","z":"350d6959.0572c6","name":"","active":true,"console":"false","complete":"false","x":389,"y":417.00000190734863,"wires":},{"id":"b164d3b8.428dd","type":"ui_text","z":"350d6959.0572c6","group":"b305b0.10f3da5","order":0,"width":"4","height":"1","name":"","label":"","format":" <font color=" {{msg.color[0]}}"><i class="fa fa-exclamation fa-3x" aria-hidden="true">","layout":"col-center","x":882,"y":304,"wires":},{"id":"c4a0262a.112098","type":"ui_text","z":"350d6959.0572c6","group":"c284b9b9.f3f458","order":0,"width":"4","height":"1","name":"","label":"","format":" <font color=" {{msg.color[1]}}"><i class="fa fa-exclamation fa-3x" aria-hidden="true">","layout":"col-center","x":880,"y":344,"wires":},{"id":"87e7c1e4.be359","type":"ui_text","z":"350d6959.0572c6","group":"86daea8a.539478","order":0,"width":"4","height":"1","name":"","label":"","format":" <font color=" {{msg.color[2]}}"><i class="fa fa-exclamation fa-3x" aria-hidden="true">","layout":"col-center","x":881,"y":389,"wires":},{"id":"ec83835f.9c06a","type":"ui_text","z":"350d6959.0572c6","group":"3097e8ea.629bb8","order":0,"width":"4","height":"1","name":"","label":"","format":" <font color=" {{msg.color}}"><i class="fa fa-exclamation fa-3x" aria-hidden="true">","layout":"col-center","x":850,"y":495,"wires":},{"id":"41dcc056.2f92","type":"ui_template","z":"350d6959.0572c6","group":"f4159884.bd6df8","name":"","order":0,"width":"23","height":"2","format":"<img src="/title.jpg"/> ","storeOutMessages":false,"fwdInMessages":false,"x":205,"y":116,"wires":[]},{"id":"b91de5f.d4f6018","type":"ui_group","z":"","name":"Device: WISE-2410","tab":"6d05fd53.a2d884","order":2,"disp":true,"width":"9"},{"id":"bdb9800e.62a88","type":"mqtt-broker","z":"","broker":"192.168.1.2","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""},{"id":"ee55d72.8bcad28","type":"ui_group","z":"","name":"Accelerometer","tab":"6d05fd53.a2d884","order":3,"disp":true,"width":"10"},{"id":"c9d732f5.4d951","type":"ui_group","z":"","name":"Temperature of the target","tab":"6d05fd53.a2d884","order":4,"disp":true,"width":"10"},{"id":"1eec8fb4.9e1aa","type":"ui_group","z":"","name":"History Charts","tab":"6d05fd53.a2d884","order":9,"disp":true,"width":"23"},{"id":"b305b0.10f3da5","type":"ui_group","z":"","name":"X-Axis Alarm","tab":"6d05fd53.a2d884","order":5,"disp":true,"width":"4"},{"id":"c284b9b9.f3f458","type":"ui_group","z":"","name":"Y-Axis Alarm","tab":"6d05fd53.a2d884","order":6,"disp":true,"width":"4"},{"id":"86daea8a.539478","type":"ui_group","z":"","name":"Z-Axis Alarm","tab":"6d05fd53.a2d884","order":7,"disp":true,"width":"4"},{"id":"3097e8ea.629bb8","type":"ui_group","z":"","name":"Temperature Alarm","tab":"6d05fd53.a2d884","order":8,"disp":true,"width":"4"},{"id":"f4159884.bd6df8","type":"ui_group","z":"","name":"Title","tab":"6d05fd53.a2d884","order":1,"disp":false,"width":"23"},{"id":"6d05fd53.a2d884","type":"ui_tab","z":"","name":"Advantech LoRaWAN Vibration/Temp sensor Solution ","icon":"dashboard","order":1}]

Please edit your post, remove the flow, click the little <\> button in the editor and include your code in between the backticks so that your post is importable/readable.

Looking at your first function for "tempHumi", I am not sure what you are trying to produce in terms of the output (that is actually more useful, if you can post your input - as text- and the output object/array you are looking for, also in text, not screenshots).

From the surface it looks like you could rewrite the function more efficiently ?

const m = msg.payload
const humi = m.TempHumi.SenVal/1000

msg.payload = (m.TempHumi.Range===0) ? `${humi} 'C` : "\n Range parsing error!"
msg.color = (m.TempHumi.Event === 0 ) ? "White" : "Red" 

return msg

So given the data you have got, give us an example of what that exact data should look like in your desired format.

Providing a screenshot of unrelated data in a different format doesn't tell us how you want to map it.

I have tried to upload the script but because I am a new user, it is not possible to upload a file as an attachment, that's why I uploaded the full script

Please read this post about how to share code or flows on the forum: How to share code or flow json

If you do not format them properly, they cannot be imported.

I am trying to produce the data in a certain format. I am not sure if you saw my first post or not with the two different figures. Those figures are just examples of the data format I can produce right now and the one I want to get.

[{"id":"26a6bafe.355f36","type":"ui_template","z":"350d6959.0572c6","group":"b91de5f.d4f6018","name":"table for Device","order":2,"width":"9","height":"7","format":"<style>\n    th{\n      border-bottom: 3px solid white;\n    }\n    caption, th, td {\n      padding: 5px;\n      text-align: left;    \n    }\n</style>\n<table style=\"width:100%\">\n    <tr>\n    <!--    <th>Events</th> -->\n        <th>Device<br>address</th>\n        <th>Power<br>source</th>\n        <th>Battery<br>voltage (V)</th> \n        <th colspan=\"2\">Timestamp</th>\n    </tr>\n    <tr>\n    <!--    <td>{{msg.payload[0]}}</td>-->\n        <td>{{msg.payload[1]}}</td>\n        <td>{{msg.payload[2]}}</td>\n        <td>{{msg.payload[3]}}</td>\n            <td colspan=\"2\">{{msg.payload[7]}}<br>\n            {{msg.payload[8]}}</td> \n     </tr>\n     <tr>\n        <th>Fcnt</th>\n        <th>U/L SNR</th>\n        <th>U/L RSSI</th>\n        <tr>\n        <td>{{msg.payload[4]}}</td>\n        <td>{{msg.payload[5]}}</td>\n        <td>{{msg.payload[6]}}</td> \n        </tr>\n     </tr>\n</table>","storeOutMessages":true,"fwdInMessages":true,"x":957,"y":200,"wires":[[]]},{"id":"d57e518d.d5999","type":"mqtt in","z":"350d6959.0572c6","name":"WISE-2410_UpLink","topic":"uplink/#","qos":"2","broker":"bdb9800e.62a88","x":208,"y":170.00000190734863,"wires":[["4ac8176.067a5e8"]]},{"id":"6373bec.d91b44","type":"mqtt in","z":"350d6959.0572c6","name":"WISE-2410","topic":"Advantech/#","qos":"0","broker":"bdb9800e.62a88","x":186,"y":251.00000190734863,"wires":[["b81e76d6.f6cba8"]]},{"id":"b81e76d6.f6cba8","type":"json","z":"350d6959.0572c6","name":"","x":366,"y":251.00000190734863,"wires":[["1edc6145.beb50f","f795ec88.cfc4f","1192206d.84723","c05c7892.122fe8","92e143d8.0a773","612e7df1.96f314"]]},{"id":"4ac8176.067a5e8","type":"json","z":"350d6959.0572c6","name":"","x":429,"y":171.00000190734863,"wires":[["92e143d8.0a773"]]},{"id":"1edc6145.beb50f","type":"function","z":"350d6959.0572c6","name":"TempHumi","func":"var TempHumi = [2];\ntempEvents = msg.payload.TempHumi.Event\nTempHumi[1] = msg.payload.TempHumi.SenVal/1000;\ntempRange = msg.payload.TempHumi.Range;\n\nswitch (tempEvents){\n    case 0:\n        TempHumi[0] = (\"Status normal\");\n        break;\n    case 1:\n        TempHumi[0] = (\"High alarm!\");\n        break;\n    default:\n        TempHumi[0] = (\"Reading error!\");\n}\n\nmsg.color = ((TempHumi[0]===\"Status normal\")? \"White\" : \"Red\");\n\nswitch (tempRange){\n    case 0:\n        tempRange = (\" \\'C\");\n        break;\n    default:\n        tempRange = (\"\\nRange parsing error!\");\n}\n\nTempHumi[1] = TempHumi[1] + tempRange;\nmsg.payload = TempHumi;\nreturn msg;\n","outputs":"1","noerr":0,"x":627,"y":438,"wires":[["d04cdd16.167e4","ec83835f.9c06a"]]},{"id":"ad2a796f.07fe48","type":"ui_template","z":"350d6959.0572c6","group":"ee55d72.8bcad28","name":"table for Accelerometer","order":0,"width":"10","height":"4","format":"<table style=\"width:100%\">\n  <tr>\n    <th></th>\n    <th>Sensor Event</th>\n    <th>Velocity RMS (mm/s)</th> \n    <th>Acceloratioin RMS (g)</th> \n    <th>Acceloratioin Peak (g)</th> \n  </tr>\n  <tr>\n    <td>X-Axis</td>\n    <td>{{msg.payload[0]}}</td>\n    <td>{{msg.payload[1]}}</td> \n    <td>{{msg.payload[2]}}</td> \n    <td>{{msg.payload[3]}}</td> \n  </tr>\n  <tr>\n    <td>Y-Axis</td>\n    <td>{{msg.payload[4]}}</td>\n    <td>{{msg.payload[5]}}</td> \n    <td>{{msg.payload[6]}}</td> \n    <td>{{msg.payload[7]}}</td> \n  </tr>\n  <tr>\n    <td>Z-Axis</td>\n    <td>{{msg.payload[8]}}</td>\n    <td>{{msg.payload[9]}}</td> \n    <td>{{msg.payload[10]}}</td> \n    <td>{{msg.payload[11]}}</td> \n  </tr>\n</table>","storeOutMessages":true,"fwdInMessages":true,"x":901,"y":255,"wires":[[]]},{"id":"f795ec88.cfc4f","type":"function","z":"350d6959.0572c6","name":"Accelerometer","func":"var aryAccel=[12];\nvar aryAccelColor=[3];\n\n//X-axis\naryAccel [0] = msg.payload.Accelerometer['X-Axis'].SenEvent;\nswitch (aryAccel [0]){\n    case 0:\n        aryAccel[0] = (\"Status normal\");\n        break;\n    case 1:\n        aryAccel[0] = (\"High alarm!!!\");\n        break;\n    default:\n        aryAccel[0] = (\"Reading error!!!\");\n}\n\naryAccelColor[0] = ((aryAccel[0]===\"Status normal\")? \"White\" : \"Red\");\naryAccel [1] = msg.payload.Accelerometer['X-Axis'].OAVelocity/100;\naryAccel [2] = msg.payload.Accelerometer['X-Axis'].Peakmg/1000;\naryAccel [3] = msg.payload.Accelerometer['X-Axis'].RMSmg/1000;\n\n//Y-axis\naryAccel [4] = msg.payload.Accelerometer['Y-Axis'].SenEvent;\nswitch (aryAccel [4]){\n    case 0:\n        aryAccel[4] = (\"Status normal\");\n        break;\n    case 1:\n        aryAccel[4] = (\"High alarm!!!\");\n        break;\n    default:\n        aryAccel[4] = (\"Reading error!!!\");\n}\n\naryAccelColor[1] = ((aryAccel[4]===\"Status normal\")? \"White\" : \"Red\");\naryAccel [5] = msg.payload.Accelerometer['Y-Axis'].OAVelocity/100;\naryAccel [6] = msg.payload.Accelerometer['Y-Axis'].Peakmg/1000;\naryAccel [7] = msg.payload.Accelerometer['Y-Axis'].RMSmg/1000;\n\n//Z-axis\naryAccel [8] = msg.payload.Accelerometer['Z-Axis'].SenEvent;\nswitch (aryAccel [8]){\n    case 0:\n        aryAccel[8] = (\"Status normal\");\n        break;\n    case 1:\n        aryAccel[8] = (\"High alarm!!!\");\n        break;\n    default:\n        aryAccel[8] = (\"Reading error!!!\");\n}\n\naryAccelColor[2] = ((aryAccel[8]===\"Status normal\")? \"White\" : \"Red\");\naryAccel [9] = msg.payload.Accelerometer['Z-Axis'].OAVelocity/100;\naryAccel [10] = msg.payload.Accelerometer['Z-Axis'].Peakmg/1000;\naryAccel [11] = msg.payload.Accelerometer['Z-Axis'].RMSmg/1000;\n\nmsg.payload = aryAccel;\nmsg.color = aryAccelColor;\nreturn msg;","outputs":1,"noerr":0,"x":635,"y":282.00000190734863,"wires":[["ad2a796f.07fe48","b164d3b8.428dd","c4a0262a.112098","87e7c1e4.be359"]]},{"id":"d04cdd16.167e4","type":"ui_template","z":"350d6959.0572c6","group":"c9d732f5.4d951","name":"table for Temparature","order":0,"width":"10","height":"2","format":"<table style=\"width:100%\">\n    <tr>\n        <th>Event</th>\n        <th>Temparature</th>\n    </tr>\n    <tr>\n        <td>{{msg.payload[0]}}</td>\n        <td>{{msg.payload[1]}}  <i class=\"fa fa-thermometer-three-quarters fa-2x\" aria-hidden=\"true\"></i></td>\n    </tr>\n</table>\n","storeOutMessages":true,"fwdInMessages":true,"x":897,"y":445,"wires":[[]]},{"id":"1192206d.84723","type":"function","z":"350d6959.0572c6","name":"OAVelocityChart","func":"msg.payload = msg.payload.Accelerometer['X-Axis'].OAVelocity/100;\n\nreturn msg;","outputs":1,"noerr":0,"x":648,"y":536,"wires":[["99275ab2.5a66e8"]]},{"id":"99275ab2.5a66e8","type":"ui_chart","z":"350d6959.0572c6","name":"","group":"1eec8fb4.9e1aa","order":0,"width":"11","height":"4","label":"Velocity RMS (Root-Mean-Squar)","chartType":"line","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","ymin":"","ymax":"","removeOlder":1,"removeOlderPoints":"1000","removeOlderUnit":"3600","cutout":0,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"x":847,"y":539,"wires":[[],[]]},{"id":"c05c7892.122fe8","type":"function","z":"350d6959.0572c6","name":"TempHumiChart","func":"msg.payload = msg.payload.TempHumi.SenVal/1000;\n\nreturn msg;","outputs":1,"noerr":0,"x":648,"y":605,"wires":[["b973abc0.57d728"]]},{"id":"b973abc0.57d728","type":"ui_chart","z":"350d6959.0572c6","name":"","group":"1eec8fb4.9e1aa","order":0,"width":"11","height":"5","label":"Temperature","chartType":"line","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","ymin":"","ymax":"","removeOlder":1,"removeOlderPoints":"1000","removeOlderUnit":"3600","cutout":0,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"x":855,"y":603,"wires":[[],[]]},{"id":"92e143d8.0a773","type":"join","z":"350d6959.0572c6","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","timeout":"","count":"2","x":605,"y":210.00000190734863,"wires":[["bb36241.378c0d8"]]},{"id":"bb36241.378c0d8","type":"function","z":"350d6959.0572c6","name":"Device","func":"var aryDevice = [];\n\nvar tempEvents = msg.payload['Advantech/FFFFFFFF/data'].Device.Events;\nswitch (tempEvents){\n    case 0:\n        aryDevice[0] = (\"Status normal\");\n        break;\n    case 1:\n        aryDevice [0] = (\"High alarm!!!\");\n        break;\n    default:\n        aryDevice [0] = (\"Reading error!!!\");\n}\naryDevice[1] = msg.payload['uplink/FFFFFFFF'].devaddr;\nvar tempPowerSrc = msg.payload['Advantech/FFFFFFFF/data'].Device.PowerSrc;\nswitch (tempPowerSrc){\n    case 0:\n        aryDevice [2] = (\"NA\");\n        break;\n    case 1:\n        aryDevice [2] = (\"Line power\");\n        break;\n    case 2:\n        aryDevice [2] = (\"Device battery\");\n        break;\n    case 3:\n        aryDevice [2] = (\"Line power+Device battery\");\n        break;\n    default:\n        aryDevice [2] = (\"Reading error!!!\");\n}\naryDevice[3] = msg.payload['Advantech/FFFFFFFF/data'].Device.BatteryVolt/1000;\naryDevice[4] = msg.payload['uplink/FFFFFFFF'].fcnt;\naryDevice[5] = msg.payload['uplink/FFFFFFFF'].lsnr;\naryDevice[6] = msg.payload['uplink/FFFFFFFF'].rssi;\naryDevice[7] = msg.payload['Advantech/FFFFFFFF/data'].Device.Time;\nvar tempDate = new Date();\naryDevice[8] = tempDate.toGMTString(aryDevice[7]);\naryDevice[7] = aryDevice[7] + \" UTC\";\n\nmsg.payload = aryDevice;\nreturn msg;\n\n\n\n","outputs":1,"noerr":0,"x":769,"y":209.00000190734863,"wires":[["26a6bafe.355f36"]]},{"id":"612e7df1.96f314","type":"debug","z":"350d6959.0572c6","name":"","active":true,"console":"false","complete":"false","x":389,"y":417.00000190734863,"wires":[]},{"id":"b164d3b8.428dd","type":"ui_text","z":"350d6959.0572c6","group":"b305b0.10f3da5","order":0,"width":"4","height":"1","name":"","label":"","format":"  <font color=\" {{msg.color[0]}}\"><i class=\"fa fa-exclamation  fa-3x\" aria-hidden=\"true\"></i></font>","layout":"col-center","x":882,"y":304,"wires":[]},{"id":"c4a0262a.112098","type":"ui_text","z":"350d6959.0572c6","group":"c284b9b9.f3f458","order":0,"width":"4","height":"1","name":"","label":"","format":"  <font color=\" {{msg.color[1]}}\"><i class=\"fa fa-exclamation  fa-3x\" aria-hidden=\"true\"></i></font>","layout":"col-center","x":880,"y":344,"wires":[]},{"id":"87e7c1e4.be359","type":"ui_text","z":"350d6959.0572c6","group":"86daea8a.539478","order":0,"width":"4","height":"1","name":"","label":"","format":"  <font color=\" {{msg.color[2]}}\"><i class=\"fa fa-exclamation  fa-3x\" aria-hidden=\"true\"></i></font>","layout":"col-center","x":881,"y":389,"wires":[]},{"id":"ec83835f.9c06a","type":"ui_text","z":"350d6959.0572c6","group":"3097e8ea.629bb8","order":0,"width":"4","height":"1","name":"","label":"","format":"  <font color=\" {{msg.color}}\"><i class=\"fa fa-exclamation  fa-3x\" aria-hidden=\"true\"></i></font>","layout":"col-center","x":850,"y":495,"wires":[]},{"id":"41dcc056.2f92","type":"ui_template","z":"350d6959.0572c6","group":"f4159884.bd6df8","name":"","order":0,"width":"23","height":"2","format":"<img src=\"/title.jpg\"/> ","storeOutMessages":false,"fwdInMessages":false,"x":205,"y":116,"wires":[[]]},{"id":"b91de5f.d4f6018","type":"ui_group","z":"","name":"Device: WISE-2410","tab":"6d05fd53.a2d884","order":2,"disp":true,"width":"9"},{"id":"bdb9800e.62a88","type":"mqtt-broker","z":"","broker":"192.168.1.2","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""},{"id":"ee55d72.8bcad28","type":"ui_group","z":"","name":"Accelerometer","tab":"6d05fd53.a2d884","order":3,"disp":true,"width":"10"},{"id":"c9d732f5.4d951","type":"ui_group","z":"","name":"Temperature of the target","tab":"6d05fd53.a2d884","order":4,"disp":true,"width":"10"},{"id":"1eec8fb4.9e1aa","type":"ui_group","z":"","name":"History Charts","tab":"6d05fd53.a2d884","order":9,"disp":true,"width":"23"},{"id":"b305b0.10f3da5","type":"ui_group","z":"","name":"X-Axis Alarm","tab":"6d05fd53.a2d884","order":5,"disp":true,"width":"4"},{"id":"c284b9b9.f3f458","type":"ui_group","z":"","name":"Y-Axis Alarm","tab":"6d05fd53.a2d884","order":6,"disp":true,"width":"4"},{"id":"86daea8a.539478","type":"ui_group","z":"","name":"Z-Axis Alarm","tab":"6d05fd53.a2d884","order":7,"disp":true,"width":"4"},{"id":"3097e8ea.629bb8","type":"ui_group","z":"","name":"Temperature Alarm","tab":"6d05fd53.a2d884","order":8,"disp":true,"width":"4"},{"id":"f4159884.bd6df8","type":"ui_group","z":"","name":"Title","tab":"6d05fd53.a2d884","order":1,"disp":false,"width":"23"},{"id":"6d05fd53.a2d884","type":"ui_tab","z":"","name":"Advantech LoRaWAN Vibration/Temp sensor Solution ","icon":"dashboard","order":1}]

Now I am sharing it with a proper format

Please understand that it is not your flow that you need to share at this point.

Provide an example of your original data in the format you want to see it in. Then we can clearly understand what you want to do.

I will tell you something. The original code that gives the outcome is the first figure is this one `

This one for Temp Humi node

var TempHumi = [2];
tempEvents = msg.payload.TempHumi.Event
TempHumi[1] = msg.payload.TempHumi.SenVal/1000;
tempRange = msg.payload.TempHumi.Range;

switch (tempEvents){
    case 0:
        TempHumi[0] = ("Status normal");
        break;
    case 1:
        TempHumi[0] = ("High alarm!");
        break;
    default:
        TempHumi[0] = ("Reading error!");
}

msg.color = ((TempHumi[0]==="Status normal")? "White" : "Red");

switch (tempRange){ case 0: tempRange = (" \'C");
        break;
    default:
        tempRange = ("\nRange parsing error!");
}

TempHumi[1] = TempHumi[1] + tempRange;
msg.payload = TempHumi;
return msg;

and this one Acceleometer funcation
var aryAccel=[12];
var aryAccelColor=[3];

//X-axis
aryAccel [0] = msg.payload.Accelerometer['X-Axis'].SenEvent;
switch (aryAccel [0]){
    case 0:
        aryAccel[0] = ("Status normal");
        break;
    case 1:
        aryAccel[0] = ("High alarm!!!");
        break;
    default:
        aryAccel[0] = ("Reading error!!!");
}

aryAccelColor[0] = ((aryAccel[0]==="Status normal")? "White" : "Red");
aryAccel [1] = msg.payload.Accelerometer['X-Axis'].OAVelocity/100;
aryAccel [2] = msg.payload.Accelerometer['X-Axis'].Peakmg/1000;
aryAccel [3] = msg.payload.Accelerometer['X-Axis'].RMSmg/1000;

//Y-axis
aryAccel [4] = msg.payload.Accelerometer['Y-Axis'].SenEvent;
switch (aryAccel [4]){
    case 0:
        aryAccel[4] = ("Status normal");
        break;
    case 1:
        aryAccel[4] = ("High alarm!!!");
        break;
    default:
        aryAccel[4] = ("Reading error!!!");
}

aryAccelColor[1] = ((aryAccel[4]==="Status normal")? "White" : "Red");
aryAccel [5] = msg.payload.Accelerometer['Y-Axis'].OAVelocity/100;
aryAccel [6] = msg.payload.Accelerometer['Y-Axis'].Peakmg/1000;
aryAccel [7] = msg.payload.Accelerometer['Y-Axis'].RMSmg/1000;

//Z-axis
aryAccel [8] = msg.payload.Accelerometer['Z-Axis'].SenEvent;
switch (aryAccel [8]){
    case 0:
        aryAccel[8] = ("Status normal");
        break;
    case 1:
        aryAccel[8] = ("High alarm!!!");
        break;
    default:
        aryAccel[8] = ("Reading error!!!");
}

aryAccelColor[2] = ((aryAccel[8]==="Status normal")? "White" : "Red");
aryAccel [9] = msg.payload.Accelerometer['Z-Axis'].OAVelocity/100;
aryAccel [10] = msg.payload.Accelerometer['Z-Axis'].Peakmg/1000;
aryAccel [11] = msg.payload.Accelerometer['Z-Axis'].RMSmg/1000;

msg.payload = aryAccel;
msg.color = aryAccelColor;
return msg;

and This one for the device

var aryDevice = [];

var tempEvents = msg.payload['Advantech/FF6026A5/data'].Device.Events;
case 0.aryDevice[0] = ("Status normal");
        break;
    case 1:
        aryDevice [0] = ("High alarm!!!");
        break;
    default:
        aryDevice [0] = ("Reading error!!!");
}
aryDevice[1] = msg.payload['uplink/FF6026A5'].devaddr;
var tempPowerSrc = msg.payload['Advantech/FF6026A5/data'].Device.PowerSrc;
switch (tempPowerSrc){
    case 0:
        aryDevice [2] = ("NA");
        break;
    case 1:
        aryDevice [2] = ("Line power");
        break;
    case 2:
        aryDevice [2] = ("Device battery");
        break;
    case 3:
        aryDevice [2] = ("Line power+Device battery");
        break;
    default:
        aryDevice [2] = ("Reading error!!!");
}
aryDevice[3] = msg.payload['Advantech/FF6026A5/data'].Device.BatteryVolt/1000;
aryDevice[4] = msg.payload['uplink/FF6026A5'].fcnt;
aryDevice[5] = msg.payload['uplink/FF6026A5'].lsnr;
aryDevice[6] = msg.payload['uplink/FF6026A5'].rssi;
aryDevice[7] = msg.payload['Advantech/FF6026A5/data'].Device.Time;
var tempDate = new Date();
aryDevice[8] = tempDate.toGMTString(aryDevice[7]);
aryDevice[7] = aryDevice[7] + " UTC";

msg.payload = aryDevice;
return msg;`

Those are the three main nodes that gives the msg payload as follows
{ "TempHumi": { "Range": 0, "Status": 0, "Event": 0, "SenVal": 30062 }, 
"Accelerometer": { "X-Axis": { "SenEvent": 0, "OAVelocity": 5, "Peakmg": 5, "RMSmg": 4 }, 
"Y-Axis": { "SenEvent": 0, "OAVelocity": 4, "Peakmg": 5, "RMSmg": 4 }, 
"Z-Axis": { "SenEvent": 0, "OAVelocity": 8, "Peakmg": 5, "RMSmg": 4 }, 
"LogIndex": 0, "Timestamp": 1552536330 }, 
"Device": { "Events": 0, "PowerSrc": 1, "BatteryVolt": 0, "Time": 1552536331 } }

I want to add a node that convert the msg payload data format shown above to the other one I shown in the second screen shot as an example. If I have the code for it I would not have asked from the beginning.

I want to modify the code I shared in this reply or add a converter node that gives me the other data format for the message payload, which in the format of

"TempHumi.Accelerometer. X-axis:10" and so on

I hope you got my idea and if it is not clear please let me know

This code is in the function node called Device node.

Your function nodes are written in a very unusual way (I would like to recommend to follow some javascript array/object tutorials) and I don't really see the connection with the input and output.

But if your output data is:

{
    "TempHumi": {
        "Range": 0,
        "Status": 0,
        "Event": 0,
        "SenVal": 30062
    },
    "Accelerometer": {
        "X-Axis": {
            "SenEvent": 0,
            "OAVelocity": 5,
            "Peakmg": 5,
            "RMSmg": 4
        },
        "Y-Axis": {
            "SenEvent": 0,
            "OAVelocity": 4,
            "Peakmg": 5,
            "RMSmg": 4
        },
        "Z-Axis": {
            "SenEvent": 0,
            "OAVelocity": 8,
            "Peakmg": 5,
            "RMSmg": 4
        },
        "LogIndex": 0,
        "Timestamp": 1552536330
    },
    "Device": {
        "Events": 0,
        "PowerSrc": 1,
        "BatteryVolt": 0,
        "Time": 1552536331
    }
}

and you want to converted it in a "flattened" output, you can use the following function:

const object = msg.payload

function flattenObject(ob) {
    var toReturn = {};

    for (var i in ob) {
        if (!ob.hasOwnProperty(i)) continue;

        if ((typeof ob[i]) == 'object' && ob[i] !== null) {
            var flatObject = flattenObject(ob[i]);
            for (var x in flatObject) {
                if (!flatObject.hasOwnProperty(x)) continue;

                toReturn[i + '.' + x] = flatObject[x];
            }
        } else {
            toReturn[i] = ob[i];
        }
    }
    return toReturn;
}


msg.payload = flattenObject(object)
return msg

Result:

{
    "TempHumi.Range": 0,
    "TempHumi.Status": 0,
    "TempHumi.Event": 0,
    "TempHumi.SenVal": 30062,
    "Accelerometer.X-Axis.SenEvent": 0,
    "Accelerometer.X-Axis.OAVelocity": 5,
    "Accelerometer.X-Axis.Peakmg": 5,
    "Accelerometer.X-Axis.RMSmg": 4,
    "Accelerometer.Y-Axis.SenEvent": 0,
    "Accelerometer.Y-Axis.OAVelocity": 4,
    "Accelerometer.Y-Axis.Peakmg": 5,
    "Accelerometer.Y-Axis.RMSmg": 4,
    "Accelerometer.Z-Axis.SenEvent": 0,
    "Accelerometer.Z-Axis.OAVelocity": 8,
    "Accelerometer.Z-Axis.Peakmg": 5,
    "Accelerometer.Z-Axis.RMSmg": 4,
    "Accelerometer.LogIndex": 0,
    "Accelerometer.Timestamp": 1552536330,
    "Device.Events": 0,
    "Device.PowerSrc": 1,
    "Device.BatteryVolt": 0,
    "Device.Time": 1552536331
}
3 Likes

Many thanks for your answer, I will try it and give you feedback. but I have one more question, the script you added here can be added as a separate node function and get the data from the main node, without changing the original script, am I right?

Yes you can create the new function created bybakman2 in the flow that is separate from the original function you have.

1 Like

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