How to add topic in received payload array


Hello,
I am receiving an array of 16, with values. I wanted to replace 0 ,1... with some topic name with values received and return in the same array format. How could I, please help.

After modification data should look like
ABC:"MAT1253685698"
DEF:"55666"
etc

in a function node...

const newPayload= {};
newPayload.ABC = msg.payload[0];
newPayload.DEF = msg.payload[1];
msg.payload = newPayload;
return result;

You cant name array indexes - your choice is an array with indexes or object with property names.

alternatively, you could return an array of objects & each value can include many properties e.g...

[ 
  { topic: "ABC", payload: "MAT777..." }, 
  { topic: "DEF", payload: "5129..." }, 
]

{
"timestamp":1637117632371,
"values":[
{
"id":"result CT1.CT1_AIR_RESIDUAL",
"v":"",
"q":true,
"t":1637115386660
},
{
"id":"result CT1.CT1_CYCLE _TIME",
"v":"165",
"q":true,
"t":1637117152004
},
{
"id":"FIVE cinetics NEW1.L33ER.Five cinetics print result CT1.CT1_CYCLE_DATE",
"v":"2021/11/17",
"q":true,
"t":1637115386692
},
{
"id":"result CT1.CT1_CYCLE_FAULT_CODE",
"v":0,
"q":true,
"t":1637115386676
},

What kind of data structure is this?

I want my data (payload)
"VAT":"8017M3N26190",
"VC":"320868000R",
"CT_VACCUM_SET":10,
"CT_VACCUM_ACTUAL":91,
"CT_PRESSURE_SET":2200,
"CT_PRESSURE_ACTUAL":2392,
In the above posted structure. Please help.

The required format is to be sent on MQTT.

In @Steve-Mcl 's answer I think the last line should be return msg;

const newPayload= {};
newPayload.ABC = msg.payload[0];
newPayload.DEF = msg.payload[1];
msg.payload = newPayload;
return msg;

(obviously you need to use the right names and array indices)
If you fix that, does it give you the desired result?

By the way, "VACCUM" is an unusual way to spell "VACUUM". This might cause problems for the MQTT subscriber. :grinning:

hI Jbudd,
Already i have changed that but output is not in the format i require. It just look like
{
"VIN": "123456",
"VC": "555996",
"CT_VACCUM_SET": 20,
"CT_VACCUM_ACTUAL": 9,
"CT_PRESSURE_SET": 2200,
"CT_PRESSURE_ACTUAL": 2402,
"CT_RESULT": 10,
"CT_CYCLE_TIME": 72,
"CT_CYCLE_NUMBER": 0,
"CT_FAULT_CODE": 0,
"PROCESS_TYPE": "CT",
"FACTORY_CODE": "",
"SHOP_CODE": "",
"LINE_CODE": "",
"MACHINE_CODE": ""
}

Please post an example of your array and precisely what result you want for that input.
Use the preformatted text button to post actual data, not a screengrab.
Untitled 3

ps post your function node too please.

Sorry for the earlier confusing posts.
My required array format is
"values":[
{
"id":" VAT",
"v":"",
"t":1637115386660
},
{
"id":"VC",
"v":"165",
"t":1637117152004
},
{
"id":"CT_VACUUM _SET",
"v":25,
"t":1637115386676
},
{
"id":"CT_VACUUM_ACTUAL",
"v":"5",
"t":1637115386692
},
{
"id":"CT_PRESSURE_SET",
"v":2200,
},

While My data from the payload is
{
"VAT": "81004M3N26193",
"VC": "50702156100R",
"CT_VACCUM_SET": 20,
"CT_VACCUM_ACTUAL": 9,
"CT_PRESSURE_SET": 2200,
}

My function node is.
const newPayload= {};
newPayload.VAT = msg.payload[0];
newPayload.VC = msg.payload[1];
newPayload.CT_VACCUM_SET = msg.payload[2];
newPayload.CT_VACCUM_ACTUAL = msg.payload[3];
newPayload.CT_PRESSURE_SET = msg.payload[4];
newPayload.CT_PRESSURE_ACTUAL = msg.payload[5];
newPayload.CT_RESULT = msg.payload[6];
newPayload.CT_CYCLE_TIME = msg.payload[8];
newPayload.CT_CYCLE_NUMBER = msg.payload[9];
newPayload.CT_FAULT_CODE = msg.payload[10];
newPayload.PROCESS_TYPE = msg.payload[11];
newPayload.FACTORY_CODE = msg.payload[12];
newPayload.SHOP_CODE = msg.payload[13];
newPayload.LINE_CODE = msg.payload[14];
newPayload.MACHINE_CODE = msg.payload[15];
msg.payload = newPayload;
return msg;

My flow here

[{"id":"329af91.a223406","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"5981e3ba.9ac3ac","type":"s7 in","z":"329af91.a223406","endpoint":"e3ef3f7a.8d6dc","mode":"all","variable":"PROCESS COMPLETE","diff":true,"name":"CINETIC_CT","x":110,"y":160,"wires":[["ed6c2b78.d227b8"]]},{"id":"b84c4b64.2bb5c8","type":"mqtt out","z":"329af91.a223406","name":"Line3_CINETIC","topic":"L3_CINETIC_CT","qos":"1","retain":"true","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"ab7e477.fbe3cb8","x":900,"y":160,"wires":[]},{"id":"1b88dd4.9486e23","type":"mqtt in","z":"329af91.a223406","name":"Line3_Cinetic_In","topic":"L3_CINETIC_CT","qos":"2","datatype":"auto","broker":"ab7e477.fbe3cb8","nl":false,"rap":true,"rh":0,"x":400,"y":360,"wires":[["c88d2ef1.ee3fb"]]},{"id":"ed6c2b78.d227b8","type":"function","z":"329af91.a223406","name":"","func":"var a = msg.payload;\nvar K =a.PROCESS_COMPLETE;\nvar L\n\n//if (J == 10){\n    //L = \"NO CYCLE\";\n//}\n//else if (J == 20) {\n    //L= \"PASS\";\n//}\n//else {\n   // L=\"FAIL\";\n//}\n\nif (K == true){\n//msg.payload =\"'\"+H+\"'\"+','+\"'\"+I+\"'\"+','+B+','+C+','+D+','+E+','+F+','+G+','+\"'\"+L+\"'\";\n    return msg;\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":280,"y":160,"wires":[["e5e85ec2.07b9d"]]},{"id":"c88d2ef1.ee3fb","type":"debug","z":"329af91.a223406","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":630,"y":360,"wires":[]},{"id":"4d93e3b0.fbe4ec","type":"function","z":"329af91.a223406","name":"","func":"const newPayload= {};\nnewPayload.VIN = msg.payload[0];\nnewPayload.VC = msg.payload[1];\nnewPayload.CT_VACCUM_SET = msg.payload[2];\nnewPayload.CT_VACCUM_ACTUAL = msg.payload[3];\nnewPayload.CT_PRESSURE_SET = msg.payload[4];\nnewPayload.CT_PRESSURE_ACTUAL = msg.payload[5];\nnewPayload.CT_RESULT = msg.payload[6];\nnewPayload.CT_CYCLE_TIME = msg.payload[8];\nnewPayload.CT_CYCLE_NUMBER = msg.payload[9];\nnewPayload.CT_FAULT_CODE = msg.payload[10];\nnewPayload.PROCESS_TYPE = msg.payload[11];\nnewPayload.FACTORY_CODE = msg.payload[12];\nnewPayload.SHOP_CODE = msg.payload[13];\nnewPayload.LINE_CODE = msg.payload[14];\nnewPayload.MACHINE_CODE = msg.payload[15];\nmsg.payload = newPayload;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":700,"y":160,"wires":[["b84c4b64.2bb5c8"]]},{"id":"e5e85ec2.07b9d","type":"split","z":"329af91.a223406","name":"","splt":",","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":true,"addname":"","x":430,"y":160,"wires":[["e142d292.9134f"]]},{"id":"e142d292.9134f","type":"join","z":"329af91.a223406","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"16","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":570,"y":160,"wires":[["4d93e3b0.fbe4ec"]]},{"id":"e3ef3f7a.8d6dc","type":"s7 endpoint","transport":"iso-on-tcp","address":"192.168.0.9","port":"102","rack":"0","slot":"2","localtsaphi":"01","localtsaplo":"00","remotetsaphi":"01","remotetsaplo":"00","connmode":"rack-slot","adapter":"","busaddr":"2","cycletime":"1000","timeout":"2000","name":"CINETIC_CT","vartable":[{"addr":"DB51,S4.17","name":"VIN"},{"addr":"DB51,S24.12","name":"VC"},{"addr":"DB550,INT66","name":"CT_VACCUM_SET"},{"addr":"DB550,INT64","name":"CT_VACCUM_ACTUAL"},{"addr":"DB550,INT70","name":"CT_PRESSURE_SET"},{"addr":"DB550,INT68","name":"CT_PRESSURE_ACTUAL"},{"addr":"DB550,INT56","name":"CT_RESULT"},{"addr":"M135.0","name":"PROCESS_COMPLETE"},{"addr":"DB550,INT144","name":"CT_CYCLE_TIME"},{"addr":"DB550,INT74","name":"CT_CYCLE_NUMBER"},{"addr":"DB550,INT152","name":"CT_FAULT_CODE"},{"addr":"DB550,S130.2","name":"PROCESS_TYPE"},{"addr":"DB550,S156.6","name":"FACTORY_CODE"},{"addr":"DB550,S164.6","name":"SHOP_CODE"},{"addr":"DB550,S172.8","name":"LINE_CODE"},{"addr":"DB550,S182.8","name":"MACHINE_CODE"}]},{"id":"ab7e477.fbe3cb8","type":"mqtt-broker","name":"Central Broker","broker":"172.27.138.122","port":"1883","clientid":"","usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""}]

@TROJAN053 please use the </> button in the toolbar when posting code/flows to the forum. That ensures they are properly formatted as code and avoids the forum treating them as markdown content and mangling them.

Thanks for the info.

I have had a look and there are one or two things which are confusing me...
In your flow: what are your first function, the split and join nodes supposed to achieve?
In your "Sorry for earlier confusing posts" above: what do you mean by "my required array format is"? Is this the data you are injecting into the flow or the data you want to get out?
"While my data from the payload is..." Is this what you want to get, or what you get now?

If you post, using the </> button, the input data and the desired output data, we will be more able to help.

Because I don't have your s7 input device or your MQTT broker, I would use an inject node to inject your sample data and a debug node to show the output like this:

Forget the split and join as after joining payload is giving data same as S7 input device.
You can inject

{
    "VAT": "MAT781004M3N26193",
    "VC": "50702156100R",
    "CT_VACCUM_SET": 20,
    "CT_VACCUM_ACTUAL": 9,
    "CT_PRESSURE_SET": 2200,
}

The payload data I want is

{
"id":" VAT",
"v":"",
"t":1637115386660
},
{
"id":"VC",
"v":"165",
"t":1637117152004
},
{
"id":"CT_VACUUM _SET",
"v":25,
"t":1637115386676
},
{
"id":"CT_VACUUM_ACTUAL",
"v":"5",
"t":1637115386692
},
{
"id":"CT_PRESSURE_SET",
"v":2200,
"t":1637115386692,
},

Where t is timestamp of injection (that can be common to complete payload)

I cannot guess the algorithm that transforms "MAT781004M3N26193" to "" (VAT), "50702156100R" to "165" (VC), 20 to 25(CT_VACUUM_SET) or 9 to 5 (CT_VACUUM_ACTUAL). Maybe you just didn't bother to give valid data.
However CT_PRESSURE_SET looks simple enough so you can extrapolate from that.

[
    {
        "id": "0aac398f3ab525b9",
        "type": "function",
        "z": "e3329002a7b5a054",
        "name": "",
        "func": "let t = new Date().getTime();\nlet vat = \"\";\nlet vc = \"Foo\";\nlet ct_vacuum_set = \"Bar\";\nlet ct_vacuum_actual = \"Wibble\";\nlet ct_pressure_set = msg.payload.CT_PRESSURE_SET;\nlet newmsg = [{\n    \"id\": \" VAT\",\n    \"v\": vat,\n    \"t\": t\n},\n    {\n        \"id\": \"VC\",\n        \"v\": vc,\n        \"t\": t\n    },\n    {\n        \"id\": \"CT_VACUUM _SET\",\n        \"v\": ct_vacuum_set,\n        \"t\": t\n    },\n    {\n        \"id\": \"CT_VACUUM_ACTUAL\",\n        \"v\": ct_vacuum_actual,\n        \"t\": t\n    },\n    {\n        \"id\": \"CT_PRESSURE_SET\",\n        \"v\": ct_pressure_set,\n        \"t\": t\n    },]\nmsg.payload = newmsg;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 430,
        "y": 120,
        "wires": [
            [
                "c22cc7b5e8da34ef"
            ]
        ]
    },
    {
        "id": "acac0814dc8acc5f",
        "type": "inject",
        "z": "e3329002a7b5a054",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "{\"VAT\":\"MAT781004M3N26193\",\"VC\":\"50702156100R\",\"CT_VACCUM_SET\":20,\"CT_VACCUM_ACTUAL\":9,\"CT_PRESSURE_SET\":2200}",
        "payloadType": "json",
        "x": 290,
        "y": 120,
        "wires": [
            [
                "0aac398f3ab525b9"
            ]
        ]
    },
    {
        "id": "c22cc7b5e8da34ef",
        "type": "debug",
        "z": "e3329002a7b5a054",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 590,
        "y": 120,
        "wires": []
    }
]

This is the input data I used

{"VAT":"MAT781004M3N26193","VC":"50702156100R","CT_VACCUM_SET":20,"CT_VACCUM_ACTUAL":9,"CT_PRESSURE_SET":2200}

And this is the output

[{"id":" VAT","v":"","t":1637283441167},{"id":"VC","v":"Foo","t":1637283441167},{"id":"CT_VACUUM _SET","v":"Bar","t":1637283441167},{"id":"CT_VACUUM_ACTUAL","v":"Wibble","t":1637283441167},{"id":"CT_PRESSURE_SET","v":2200,"t":1637283441167}]

If that is not what you want to achieve, I can be of no further assistance.

1 Like

That worked for me.

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