# Add up all the values

**URL:** <https://discourse.nodered.org/t/add-up-all-the-values/48932>\
**Category:** General\
**Created:** [26 July 2021 15:03 UTC](https://discourse.nodered.org/t/add-up-all-the-values/48932 "2021-07-26T15:03:05Z")\
**Posts on this page:** 7\
**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:** [26 July 2021 15:03 UTC](https://discourse.nodered.org/t/add-up-all-the-values/48932/1 "2021-07-26T15:03:06Z")

</div>

Hello!

I have a CSV file with values.

How can I add up all the values?

Here is an example:

HS-69656; 11  
HS-69657; 14  
HS-69658; 10  
HS-69659; 8  
HS-69660; 8  
HS-69661; 6  
HS-69662; 6  
HS-69663; 5  
HS-69664; 7

The numbers from the second column should add up.  
The result should be 75.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [26 July 2021 15:15 UTC](https://discourse.nodered.org/t/add-up-all-the-values/48932/2 "2021-07-26T15:15:00Z")

</div>

Here is an example using a change node and JSONata. I simulated the csv file with a template node.

```auto
[{"id":"e077981e.bba8b8","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":150,"y":2220,"wires":[["c7c2f8ed.f4ea38"]]},{"id":"c7c2f8ed.f4ea38","type":"template","z":"c74669a0.6a34f8","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"HS-69656; 11\nHS-69657; 14\nHS-69658; 10\nHS-69659; 8\nHS-69660; 8\nHS-69661; 6\nHS-69662; 6\nHS-69663; 5\nHS-69664; 7","output":"str","x":320,"y":2200,"wires":[["da4d628a.6363a8"]]},{"id":"da4d628a.6363a8","type":"csv","z":"c74669a0.6a34f8","name":"","sep":";","hdrin":"","hdrout":"none","multi":"mult","ret":"\\n","temp":"","skip":"0","strings":true,"include_empty_strings":"","include_null_values":"","x":480,"y":2200,"wires":[["89809e5c.05f988"]]},{"id":"89809e5c.05f988","type":"change","z":"c74669a0.6a34f8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$sum(payload[*].$number($trim(col2)))","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":650,"y":2200,"wires":[["9478755d.244bf8"]]},{"id":"9478755d.244bf8","type":"debug","z":"c74669a0.6a34f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":570,"y":2020,"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:** [26 July 2021 15:18 UTC](https://discourse.nodered.org/t/add-up-all-the-values/48932/4 "2021-07-26T15:18:54Z")

</div>

> [@E1cid](#):
>
> Here is an example using a change node and JSONata. I simulated the csv file with a template node.

Hello!

Many thanks for the quick response.

Unfortunately I got an error.  
 ![Bildschirmfoto 2021-07-26 um 17.17.37](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/3/33269546d839a1daa066ad64502ada0a69e9bf23.png)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [26 July 2021 15:29 UTC](https://discourse.nodered.org/t/add-up-all-the-values/48932/5 "2021-07-26T15:29:18Z")

</div>

show us how you used the example, and your cvs file as it is not as you describe in your OG post.  
i suspect there are no spaces after the `;`  
try removing the trim, as when there is no space the csv node will parse the numeric value. It is important to be precise when supplying example data.  
Try replacing the jsonata expression with this  
`$sum(payload[*].col2)`  
If that fails please supply your example data and how you used the example i supplied.

---

<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:** [26 July 2021 16:45 UTC](https://discourse.nodered.org/t/add-up-all-the-values/48932/6 "2021-07-26T16:45:35Z")

</div>

Hello!

Thank you, now it's fine.

---

<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:** [26 July 2021 17:54 UTC](https://discourse.nodered.org/t/add-up-all-the-values/48932/7 "2021-07-26T17:54:50Z")

</div>

> [@E1cid](#):
>
> show us how you used the example, and your cvs file as it is not as you describe in your OG post.  
> i suspect there are no spaces after the `;`  
> try removing the trim, as when there is no space the csv node will parse the numeric value. It is important to be precise when supplying example data.  
> Try replacing the jsonata expression with this  
> `$sum(payload[*].col2)`  
> If that fails please supply your example data and how you used the example i supplied.

Do you know also maybe how the addition works with an XML file?

[https://www.hrcdistribution.com/xml/xml\_feed\_stock.php?c=8407](https://www.hrcdistribution.com/xml/xml_feed_stock.php?c=8407)

The following should be added

payload.HRC\_STOCK.PRODUCT.STOCK [0]

---

<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:** [24 September 2021 17:55 UTC](https://discourse.nodered.org/t/add-up-all-the-values/48932/8 "2021-09-24T17:55:21Z")

</div>

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