# CSV files and to add the values

**URL:** <https://discourse.nodered.org/t/csv-files-and-to-add-the-values/47480>\
**Category:** General\
**Created:** [22 June 2021 12:49 UTC](https://discourse.nodered.org/t/csv-files-and-to-add-the-values/47480 "2021-06-22T12:49:46Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![madmax](https://avatars.discourse-cdn.com/v4/letter/m/8baadc/32.png) [@madmax](https://discourse.nodered.org/u/madmax)\
**Post date:** [22 June 2021 12:49 UTC](https://discourse.nodered.org/t/csv-files-and-to-add-the-values/47480/1 "2021-06-22T12:49:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [22 June 2021 13:05 UTC](https://discourse.nodered.org/t/csv-files-and-to-add-the-values/47480/2 "2021-06-22T13:05:33Z")

</div>

Here is one way...

```auto
[{"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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/3/c39da48d2503b54adc13d9733bb41848987fe3ad.png)

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [22 June 2021 13:05 UTC](https://discourse.nodered.org/t/csv-files-and-to-add-the-values/47480/3 "2021-06-22T13:05:37Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![madmax](https://avatars.discourse-cdn.com/v4/letter/m/8baadc/32.png) [@madmax](https://discourse.nodered.org/u/madmax)\
**Post date:** [22 June 2021 13:45 UTC](https://discourse.nodered.org/t/csv-files-and-to-add-the-values/47480/4 "2021-06-22T13:45:56Z")

</div>

Hello!

Many thanks for the answer.

Unfortunately, he doesn't add them up for me.  
 ![Bildschirmfoto 2021-06-22 um 15.44.37](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/0/c0fcfc58b0b2618b84b7388ff161e448ee4c415f.png)

---

<div class="post-metadata">

**Author:** ![madmax](https://avatars.discourse-cdn.com/v4/letter/m/8baadc/32.png) [@madmax](https://discourse.nodered.org/u/madmax)\
**Post date:** [22 June 2021 13:46 UTC](https://discourse.nodered.org/t/csv-files-and-to-add-the-values/47480/5 "2021-06-22T13:46:46Z")

</div>

They are not the same.

I made a mistake.

---

<div class="post-metadata">

**Author:** ![madmax](https://avatars.discourse-cdn.com/v4/letter/m/8baadc/32.png) [@madmax](https://discourse.nodered.org/u/madmax)\
**Post date:** [22 June 2021 13:57 UTC](https://discourse.nodered.org/t/csv-files-and-to-add-the-values/47480/6 "2021-06-22T13:57:38Z")

</div>

Hello!

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

---

<div class="post-metadata">

**Author:** ![madmax](https://avatars.discourse-cdn.com/v4/letter/m/8baadc/32.png) [@madmax](https://discourse.nodered.org/u/madmax)\
**Post date:** [22 June 2021 14:23 UTC](https://discourse.nodered.org/t/csv-files-and-to-add-the-values/47480/7 "2021-06-22T14:23:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [22 June 2021 20:17 UTC](https://discourse.nodered.org/t/csv-files-and-to-add-the-values/47480/8 "2021-06-22T20:17:24Z")

</div>

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

```auto
[{"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":[]}]

```

---

<div class="post-metadata">

**Author:** ![madmax](https://avatars.discourse-cdn.com/v4/letter/m/8baadc/32.png) [@madmax](https://discourse.nodered.org/u/madmax)\
**Post date:** [23 June 2021 15:46 UTC](https://discourse.nodered.org/t/csv-files-and-to-add-the-values/47480/9 "2021-06-23T15:46:22Z")

</div>

Hello!

Thanks a lot for this. Goes great.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [22 August 2021 15:46 UTC](https://discourse.nodered.org/t/csv-files-and-to-add-the-values/47480/10 "2021-08-22T15:46:45Z")

</div>

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