CSV files and to add the values

Hello!

Is it possible to search for the same values from CSV files and to add the values in the second column?

Here is the CSV

TRX1188-RED-LXL; 3
TRX1188-WHT-LXL; 2
TRX1188-WHT-LXL; 2
TRX1252; 2
TRX1252; 2
TRX1362-L; 2

and this is how it should look like.

TRX1188-RED-LXL; 7
TRX1252; 4
TRX1362-L; 2

Here is one way...

[{"id":"b28499ac2344c279","type":"inject","z":"4b3f21a3.ba434","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":2230,"y":2040,"wires":[["1ad20766cb32fbad"]]},{"id":"1ad20766cb32fbad","type":"template","z":"4b3f21a3.ba434","name":"CSV data","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"TRX1188-RED-LXL; 3\nTRX1188-WHT-LXL; 2\nTRX1188-WHT-LXL; 2\nTRX1252; 2\nTRX1252; 2\nTRX1362-L; 2","output":"str","x":2380,"y":2040,"wires":[["9d21bcaaf698a8a1"]]},{"id":"9d21bcaaf698a8a1","type":"csv","z":"4b3f21a3.ba434","name":"","sep":";","hdrin":"","hdrout":"none","multi":"mult","ret":"\\n","temp":"","skip":"0","strings":true,"include_empty_strings":"","include_null_values":"","x":2530,"y":2040,"wires":[["6e7523a05622d43f"]]},{"id":"a7abeae97f4e0ef5","type":"debug","z":"4b3f21a3.ba434","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2390,"y":2140,"wires":[]},{"id":"6e7523a05622d43f","type":"function","z":"4b3f21a3.ba434","name":"reduce","func":"\nvar data = msg.payload;\nvar result = {};\nfor (let index = 0; index < data.length; index++) {\n    const element = data[index];\n    result[element.col1] = result[element.col1] ? result[element.col1] + element.col2 : element.col2\n}\nmsg.payload = result;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2230,"y":2140,"wires":[["a7abeae97f4e0ef5"]]}]

image

Hi madmax ..

just to clarify .. TRX1188-RED-LXL and TRX1188-WHT-LXL have a different id but in your example you count them as one .. is this how it should be ?

Hello!

Many thanks for the answer.

Unfortunately, he doesn't add them up for me.
Bildschirmfoto 2021-06-22 um 15.44.37

They are not the same.

I made a mistake.

Hello!

I found the error in the sample file, there were blanks in the crowd.

Hello! Unfortunately I have to bother again. How do I then write it in a new CSV file?

After you do the count/sum of the different devices you can use the CSV node again to make into a csv and write the file with the File out node.

but the csv node doesnt play nice with the structure of the result in Steve's example.
Here's a similar flow of my attempt including making it into a csv

[{"id":"b864a7220ae9be76","type":"inject","z":"af25975e21b17ff4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":120,"y":540,"wires":[["4613383ac4e3ba67"]]},{"id":"30b5bb508d03c895","type":"function","z":"af25975e21b17ff4","name":"","func":"let result = [];\n\nmsg.ids.forEach(id => {\n    let count = 0;\n    msg.payload.forEach(el => {\n        if (el.id == id) count += el.count;\n    });\n    result.push({ id: id, count: count });\n});\n\nmsg.payload = result;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":800,"y":540,"wires":[["24b9035dd620e9e6","7711ca0ae58004ad"]]},{"id":"9914e33f060b0ba9","type":"debug","z":"af25975e21b17ff4","name":"1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":390,"y":480,"wires":[]},{"id":"b39d4fb3ac96d1a4","type":"csv","z":"af25975e21b17ff4","name":"","sep":";","hdrin":"","hdrout":"none","multi":"mult","ret":"\\n","temp":"id, count","skip":"0","strings":true,"include_empty_strings":"","include_null_values":"","x":470,"y":540,"wires":[["dafdf1ab940ed997","35d154baa0bebe97"]]},{"id":"dafdf1ab940ed997","type":"debug","z":"af25975e21b17ff4","name":"2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":570,"y":480,"wires":[]},{"id":"4613383ac4e3ba67","type":"template","z":"af25975e21b17ff4","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"plain","template":"TRX1188-RED-LXL; 3\nTRX1188-WHT-LXL; 2\nTRX1188-WHT-LXL; 2\nTRX1252; 2\nTRX1252; 2\nTRX1362-L; 2","output":"str","x":280,"y":540,"wires":[["b39d4fb3ac96d1a4","9914e33f060b0ba9"]]},{"id":"35d154baa0bebe97","type":"change","z":"af25975e21b17ff4","name":"distinct ids","rules":[{"t":"set","p":"ids","pt":"msg","to":"$distinct(payload.id)\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":630,"y":540,"wires":[["30b5bb508d03c895"]]},{"id":"6f28bd6123e163ee","type":"debug","z":"af25975e21b17ff4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1150,"y":540,"wires":[]},{"id":"24b9035dd620e9e6","type":"csv","z":"af25975e21b17ff4","name":"","sep":";","hdrin":"","hdrout":"none","multi":"one","ret":"\\n","temp":"id, count","skip":"0","strings":true,"include_empty_strings":"","include_null_values":"","x":990,"y":540,"wires":[["6f28bd6123e163ee"]]},{"id":"7711ca0ae58004ad","type":"debug","z":"af25975e21b17ff4","name":"3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":910,"y":480,"wires":[]},{"id":"6c3ce33983152af6","type":"comment","z":"af25975e21b17ff4","name":".. to file out node","info":"","x":1170,"y":480,"wires":[]}]

Hello!

Thanks a lot for this. Goes great.

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