Add up all the values

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.

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

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

Hello!

Many thanks for the quick response.

Unfortunately I got an error.
Bildschirmfoto 2021-07-26 um 17.17.37

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.

Hello!

Thank you, now it's fine.

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

https://www.hrcdistribution.com/xml/xml_feed_stock.php?c=8407

The following should be added

payload.HRC_STOCK.PRODUCT.STOCK [0]

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