Merge elements of objects to payload

Hello

I have signals, I would like merge elements Obiect and Obiect 2...
my function not work,
Important for me is mergle to payload

  • (tagName 1, value 1, timestamp 1, value 2)
  • (tagName 1, value 1, timestamp 1, value 3)

can you help me.

msg.payload : Object
object
deviceName: "cell_3"
**tagName: "HMI_H1_Temperature"**
deviceID: "49D9941E-8915-44E2-BCC9-0A0D0652389F"
success: true
datatype: "real"
timestamp: 1654751958598
**value: 37.457176208496094**
metadata: object
registerId: "E87D5CED-0734-4947-8963-0F111A6BCF1E"
description: "Temperatura H1 cela 3"

msg.payload : Object
object
deviceName: "cell_3"
tagName: "C31_Tracking_Data"
deviceID: "49D9941E-8915-44E2-BCC9-0A0D0652389F"
success: true
datatype: "word"
**timestamp: 1654752905277**
**value: 0**
metadata: object
registerId: "C66C5852-2438-4BBA-B0BD-8C256F0E11A8"
description: ""
const a = new Date();
const top = msg.topic;
const pay = msg.payload;
const temp = msg.payload.success;
const temp1 = msg.payload.tagName;




if(temp  && temp1)
{
 if(temp  && temp1 === "C31_Tracking_Data")
    {
         let datafilter1 = msg.payload;
         let current1 = datafilter1.value;
         let obj1 = datafilter1.tagName;
         let previous1 = context.get(obj1);
        context.set(obj1,current1);  
     }

 if(temp  && temp1 !== "C31_Tracking_Data") // Checks if the message succeeded and if the tag name exists before continuing.
    {
           let datafilter = msg.payload;
           let current = datafilter.value;
            let obj = datafilter.tagName;
            let previous = context.get(obj);
             context.set(obj,current);
    }  
          
            msg.topic = "Dom/SC3";
          msg.payload = {"location": obj, "utctimestamp_ms": a.getTime(), "code": current, "taskID": current1 };
   node.status({fill:"blue",shape:"dot",text: (obj, current)});
  return msg;
}

What do you expect the output to be from these 2 objects? Do you want all values avaiable in 1 message OR do you really want values of 2 objects merged into 1 object & if so, then what happens when there are 2 properties with the same name but different values?

Yes, but you will need to correctly export the data - use the "copy value" button that appears under your mouse when you hover over the debug message properties.

I have connect to PLC, , when parts stop on station I would like add to IDparts to fiew parametes.

  • (tagName.paylad 2, value.paylad 2, timestamp.paylad 2, value.paylad 1)
  • (tagName.paylad 3, value.paylad 3, timestamp.paylad 3, value.paylad 1)
  • (tagName.paylad 4, value.paylad 4, timestamp.paylad 4, value.paylad 1)
  • (tagName.paylad 5, value.paylad 5, timestamp.paylad 5, value.paylad 1)
    end next
    part go on next station,

IDparts = payload 1 -> C31_Tracking_Data
value: 10
data = payload 1,2,3,4.. -> HMI_H1_Temperature
tagName: "HMI_H1_Temperature"
timestamp: 1654751958598
value: 37.457176208496094

[{"id":"c55613f7.93395","type":"Datahub Subscribe","z":"58fe519a.9abd2","name":"T_Lutowania C3H1","natc":"d16b4f91.27069","topicname":"devicehub.raw.49D9941E-8915-44E2-BCC9-0A0D0652389F.E87D5CED-0734-4947-8963-0F111A6BCF1E","x":130,"y":540,"wires":[["a4f63409.147c78"]]},{"id":"55148774.36ca88","type":"Datahub Subscribe","z":"58fe519a.9abd2","name":"C31_Tracking_Data","natc":"d16b4f91.27069","topicname":"devicehub.raw.49D9941E-8915-44E2-BCC9-0A0D0652389F.C66C5852-2438-4BBA-B0BD-8C256F0E11A8","x":130,"y":460,"wires":[["a4f63409.147c78","9e447256.be923"]]},{"id":"9e447256.be923","type":"debug","z":"58fe519a.9abd2","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":370,"y":400,"wires":[]},{"id":"aca0a33f.b5b3f","type":"debug","z":"58fe519a.9abd2","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":930,"y":500,"wires":[]},{"id":"a4f63409.147c78","type":"json","z":"58fe519a.9abd2","name":"JSON to Object","property":"payload","action":"obj","pretty":false,"x":360,"y":500,"wires":[["1c67c677.deddda"]]},{"id":"1c67c677.deddda","type":"function","z":"58fe519a.9abd2","name":"NewValue","func":"const a = new Date();\nconst top = msg.topic;\nconst pay = msg.payload;\nconst temp = msg.payload.success;\nconst temp1 = msg.payload.tagName;\nconst temp2 = msg.payload.value;\n\nif(temp1 === \"C31_Tracking_Data\" )\n    {\n     let  trace = pay.value;\n    \n      }\n\n\nif(temp && temp1 !== \"C31_Tracking_Data\") // Checks if the message succeeded and if the tag name exists before continuing.\n{\n   let datafilter = msg.payload;\n   \n    msg.payload.deviceID  = \"trac\";\n   let current = datafilter.value;\n  \n   \n   let obj = datafilter.registerId;\n   let previous = context.get(obj);\n   \n   if (previous !== current)\n   {\n   context.set(obj,current);\n   node.status({fill:\"blue\",shape:\"dot\",text: (obj, current)});\n   return msg;\n   }\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","x":520,"y":500,"wires":[["329ec114.ec3f7e","64dd267f.2203f8"]]},{"id":"329ec114.ec3f7e","type":"function","z":"58fe519a.9abd2","name":"NewName_SV","func":" const myObj = msg.payload;\n const text = myObj.tagName;\n    \n\t\tif(text.startsWith(\"0#\") == true){\n\t\t\n\t\t\t//dla wartości 0#\n\t\t\t    const restText = text.substr(\"0#\".length);\n\t\t    myObj.tagName = restText;\n\t\t\tmyObj.value = myObj.value/1;\n\t\t\tmsg.payload = myObj;\n\t\t    return msg;\n\t\t\t}\n        else if(text.startsWith(\"1#\") == true){\n\t\t\n\t\t\t//dla wartości 1# - dzielenie wartości przez 10\n\t\t    \tconst restText = text.substr(\"1#\".length);\n\t\t    myObj.tagName = restText;\n\t\t\tmyObj.value = myObj.value/10;\n\t\t\tmsg.payload = myObj;\n\t\t    return msg;\n\t\t\t}\n\t\telse if(text.startsWith(\"2#\") == true){\n\t\t\n\t\t\t//dla wartości 2# - dzielenie wartości przez 100\n\t\t    \tconst restText = text.substr(\"2#\".length);\n\t\t    myObj.tagName = restText;\n\t\t\tmyObj.value = myObj.value/100;\n\t\t\tmsg.payload = myObj;\n\t\t    return msg;\n\t\t\t}\n\t\telse if(text.startsWith(\"3#\") == true){\n\t\t\n\t\t\t//dla wartości 3# - dzielenie wartości przez 1000\n\t\t    \tconst restText = text.substr(\"3#\".length);\n\t\t    myObj.tagName = restText;\n\t\t\tmyObj.value = myObj.value/1000;\n\t\t\tmsg.payload = myObj;\n\t\t    return msg;\n\t\t\t}\n\t\telse if(text.startsWith(\"#\", 1 ) == false){\n\t\t\n\t\t\t//brak przedrostka\n\t\t\tmsg.payload = myObj;\n\t\t\treturn msg;\n\t\t    }","outputs":1,"noerr":0,"initialize":"","finalize":"","x":700,"y":500,"wires":[["b86e757f.90d6c8","aca0a33f.b5b3f"]]},{"id":"b86e757f.90d6c8","type":"debug","z":"58fe519a.9abd2","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":910,"y":440,"wires":[]},{"id":"64dd267f.2203f8","type":"debug","z":"58fe519a.9abd2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":790,"y":380,"wires":[]},{"id":"d16b4f91.27069","type":"Datahub Connect","ipadd":"127.0.0.1","port":"4222"}]

I can't download the data and add it.
msg.payload = {"location": tagName, "utctimestamp_ms": a.getTime() ,"code": payload2.value restCode, "taskID": " payload1. value "};

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