Sum up three outputs from mqtt

Hi i think the solution is simple but i get stuck with adding up some values.
I am a total N00b so please be patient.
Situation:

I get data from Domoticz via Mqtt. (works)
I split the messages to get only the data i need (from 3 sensors) (works)
Now the difficoult part. The output is like this:

{"Battery":255,"RSSI":12,"description":"","dtype":"Usage","id":"0000000","idx":25,"name":"Delivery L3","nvalue":0,"stype":"Electric","svalue1":"1710.0","unit":6}
{"Battery":255,"RSSI":12,"description":"","dtype":"Usage","id":"0000000","idx":24,"name":"Delivery L2","nvalue":0,"stype":"Electric","svalue1":"1443.0","unit":5}
{"Battery":255,"RSSI":12,"description":"","dtype":"Usage","id":"0000000","idx":23,"name":"Delivery L1","nvalue":0,"stype":"Electric","svalue1":"1726.0","unit":4}

Now i want to ad the three values from 'svalue1' together. In this case the result would be 1710+1443+1726 = 4879

Fist i tried to create a new function and retrack the svalues.
then is have this result.
{"n1":"1791.0"}
{"n2":"1637.0"}
{"n3":"1826.0"}

But now i am stuck with a nwe function to add up n1+n2+n3

I also tried the join node but that is giving me nothing at all.. :frowning:

This is what is have.
[{"id":"40635ad8.24fde4","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"78374b80.4fda64","type":"debug","z":"40635ad8.24fde4","name":"uitkomst","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":320,"y":40,"wires":},{"id":"52e4c849.3b2da8","type":"mqtt in","z":"40635ad8.24fde4","name":"","topic":"domoticz/out","qos":"2","datatype":"auto","broker":"b008ef44.96838","x":110,"y":60,"wires":[["78374b80.4fda64","136268c3.04fb97"]]},{"id":"c6bf5e01.5ef9e","type":"switch","z":"40635ad8.24fde4","name":"Delivery_P1","property":"payload.idx","propertyType":"msg","rules":[{"t":"eq","v":"25","vt":"str"},{"t":"eq","v":"24","vt":"str"},{"t":"eq","v":"23","vt":"str"}],"checkall":"true","repair":false,"outputs":3,"x":270,"y":200,"wires":[["b5470ea6.8e6db","77d414ed.945cdc","3dc25f8f.4fa3"],["b5470ea6.8e6db","77d414ed.945cdc","a0b0f3c6.09f3f"],["b5470ea6.8e6db","77d414ed.945cdc","1028c61c.0d6c5a"]]},{"id":"1028c61c.0d6c5a","type":"function","z":"40635ad8.24fde4","name":"IDX25","func":"msg.payload = {\n "n3":msg.payload.svalue1,\n};\nreturn msg;","outputs":1,"noerr":0,"x":450,"y":140,"wires":[["19eb6d4.7f9ab93","27b742d8.030f7e"]]},{"id":"27b742d8.030f7e","type":"debug","z":"40635ad8.24fde4","name":"123","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":650,"y":220,"wires":},{"id":"a0b0f3c6.09f3f","type":"function","z":"40635ad8.24fde4","name":"IDX24","func":"msg.payload = {\n "n2":msg.payload.svalue1,\n};\nreturn msg;","outputs":1,"noerr":0,"x":450,"y":100,"wires":[["19eb6d4.7f9ab93","27b742d8.030f7e"]]},{"id":"19eb6d4.7f9ab93","type":"function","z":"40635ad8.24fde4","name":"optellen","func":"msg.payload = \n\nmsg.payload.n1 + msg.payload.n2 + msg.payload.n3;\n\nreturn msg;","outputs":1,"noerr":0,"x":640,"y":80,"wires":[]},{"id":"6c33bb30.de8c24","type":"debug","z":"40635ad8.24fde4","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":770,"y":280,"wires":},{"id":"3dc25f8f.4fa3","type":"function","z":"40635ad8.24fde4","name":"IDX23","func":"msg.payload = {\n "n1":msg.payload.svalue1,\n};\nreturn msg;","outputs":1,"noerr":0,"x":450,"y":60,"wires":[["19eb6d4.7f9ab93","27b742d8.030f7e"]]},{"id":"b5470ea6.8e6db","type":"join","z":"40635ad8.24fde4","name":"opgeteld","mode":"custom","build":"object","property":"","propertyType":"full","key":"payload.svalue1","joiner":"\n","joinerType":"str","accumulate":false,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":520,"y":260,"wires":[["6c33bb30.de8c24"]]},{"id":"77d414ed.945cdc","type":"debug","z":"40635ad8.24fde4","name":"#1","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":500,"y":320,"wires":},{"id":"136268c3.04fb97","type":"json","z":"40635ad8.24fde4","name":"","property":"payload","action":"","pretty":false,"x":120,"y":160,"wires":[["c6bf5e01.5ef9e"]]},{"id":"b008ef44.96838","type":"mqtt-broker","z":"","name":"localhost","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

Do you mean that you are starting with one message from MQTT, then splitting that into three messages and then trying to put it back together to add up the bits you need? If so then don't do the split, just add them up from the original message direct from mqtt. Post what that message contains when viewed in a debug node.

Also see this post on how to share code or flow so that it is importable

No,
From mqtt i get loads of messages from all domoticz devices. From all these messages i filter out three (in this case idx23, idx24 , idx25).
Then i want to add up the svalue1 from these three nodes.

OK, then you can use a Join node. Make sure the three messages have different topics and set the Join node to Manual, key/value pairs, using topic as key, and expecting three messages.

Been asked soooo many times. A simple search would uncover many threads.

This one might give you a head start....

I think there is my problem?
Following the explanation from the other article. using ,"stype":"Electric", as topic. i get the error "message missing key property 'msg.stype' - cannot ad to object"
Sorry cannot post image couse i'm working on my phone.

I don't understand what you mean by

Do you mean you have specified stype as the key? If so then the message must have a property stype, which it does not, according to the error. The most common way is to specify topic as the key and then put a value in the topic that identifies the message contents. If that doesn't make sense then tell us what it is that identifies which message is which.

This is one output of three: see first post. This is the debug message from the split node.
{"
Battery":255,"
RSSI":12,"
description":"","
dtype":"Usage","
id":"0000000","
idx":25,"
name":"Delivery L3","
nvalue":0,"
stype":"Electric","
svalue1":"1710.0","
unit":6:
}
IDX is the unique value, but name is also unique.
But using any value as key i get the error??

Set the debug node to Show Complete Message and show us what it says.

I wil do that at home. I'm now at work and the office firewall does not allow me to connect to my NodeRed. And at my phone it is not working well.

I suspect that the data you posted is in the payload, in which case you need something like msg.payload.idx or msg.payload.name as the key in the Join node.

If i do that msg.payload.idx i get no debug message at all.

Post an image showing how you have configured the Join node.
Also the debug message with Show Complete as previously discussed.

Here is my proposed solution based on the data in the first post.

Join the three chunks together
26%20PM

In a change node, use jsonata to
12%20PM
1 - change svalue1 from a string to a number
payload ~> | $ | { "svalue1": $number(svalue1) } |
2 - sum up all occurances of svalue1
$sum(payload.svalue1)

Here is the sample solution flow:

[{"id":"61bbc058.4acfa","type":"tab","label":"Flow 6","disabled":false,"info":""},{"id":"22e8ad89.490e9a","type":"comment","z":"61bbc058.4acfa","name":"using 2 jsonata statements: (1) change all svalue1's to number (2) sum(svalue1)","info":"","x":560,"y":260,"wires":[]},{"id":"b625917b.a88948","type":"inject","z":"61bbc058.4acfa","name":"","topic":"","payload":"{\"Battery\":255,\"RSSI\":12,\"description\":\"\",\"dtype\":\"Usage\",\"id\":\"0000000\",\"idx\":25,\"name\":\"Delivery L3\",\"nvalue\":0,\"stype\":\"Electric\",\"svalue1\":\"1710.0\",\"unit\":6}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":100,"wires":[["4ea2092f.458a68","836cc330.865038","daa1d47.dfc32a8"]]},{"id":"daa1d47.dfc32a8","type":"change","z":"61bbc058.4acfa","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"Battery\":255,\"RSSI\":12,\"description\":\"\",\"dtype\":\"Usage\",\"id\":\"0000000\",\"idx\":25,\"name\":\"Delivery L3\",\"nvalue\":0,\"stype\":\"Electric\",\"svalue1\":\"1710.0\",\"unit\":6}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":60,"wires":[["15971af0.23c13d"]]},{"id":"836cc330.865038","type":"change","z":"61bbc058.4acfa","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"Battery\":255,\"RSSI\":12,\"description\":\"\",\"dtype\":\"Usage\",\"id\":\"0000000\",\"idx\":24,\"name\":\"Delivery L2\",\"nvalue\":0,\"stype\":\"Electric\",\"svalue1\":\"1443.0\",\"unit\":5}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":120,"wires":[["15971af0.23c13d"]]},{"id":"4ea2092f.458a68","type":"change","z":"61bbc058.4acfa","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"Battery\":255,\"RSSI\":12,\"description\":\"\",\"dtype\":\"Usage\",\"id\":\"0000000\",\"idx\":23,\"name\":\"Delivery L1\",\"nvalue\":0,\"stype\":\"Electric\",\"svalue1\":\"1726.0\",\"unit\":4}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":180,"wires":[["15971af0.23c13d"]]},{"id":"15971af0.23c13d","type":"join","z":"61bbc058.4acfa","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"3","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":570,"y":120,"wires":[["1e092022.9a923"]]},{"id":"1e092022.9a923","type":"change","z":"61bbc058.4acfa","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload ~> | $ | { \"svalue1\": $number(svalue1) } |\t","tot":"jsonata"},{"t":"set","p":"payload","pt":"msg","to":"$sum(payload.svalue1)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":320,"wires":[["8f328c43.8a085"]]},{"id":"8f328c43.8a085","type":"debug","z":"61bbc058.4acfa","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":550,"y":380,"wires":[]}]
1 Like

This is what i have now. And no messages in the debug window.

Thank you this works.
You use the join node different. Why?
What was wrong with the approach of manual join?
Thanks in advance, i wil reuse this a lot. :smiley:

This is my result. I gues some optimisation from the string to number back to string again? Or is it not possible to add up a string?

[{"id":"b34e1f67.0949d","type":"comment","z":"e0b44d74.cf477","name":"using 2 jsonata statements: (1) change all svalue1's to number (2) sum(svalue1)","info":"","x":780,"y":60,"wires":[]},{"id":"44c403d4.0fbabc","type":"join","z":"e0b44d74.cf477","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"3","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":530,"y":140,"wires":[["b0c152b3.ace7a"]]},{"id":"b0c152b3.ace7a","type":"change","z":"e0b44d74.cf477","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload ~> | $ | { \"svalue1\": $number(svalue1) } |\t","tot":"jsonata"},{"t":"set","p":"payload","pt":"msg","to":"$sum(payload.svalue1)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":700,"y":140,"wires":[["eeaac0cc.e1a51"]]},{"id":"ac6ac279.5615d","type":"mqtt in","z":"e0b44d74.cf477","name":"","topic":"domoticz/out","qos":"2","datatype":"auto","broker":"b008ef44.96838","x":70,"y":140,"wires":[["4cfb6221.8f05dc"]]},{"id":"4cfb6221.8f05dc","type":"json","z":"e0b44d74.cf477","name":"","property":"payload","action":"","pretty":false,"x":210,"y":140,"wires":[["f61d7c2d.233ad"]]},{"id":"f61d7c2d.233ad","type":"switch","z":"e0b44d74.cf477","name":"Delivery_P1","property":"payload.idx","propertyType":"msg","rules":[{"t":"eq","v":"25","vt":"str"},{"t":"eq","v":"24","vt":"str"},{"t":"eq","v":"23","vt":"str"}],"checkall":"true","repair":false,"outputs":3,"x":350,"y":140,"wires":[["44c403d4.0fbabc"],["44c403d4.0fbabc"],["44c403d4.0fbabc"]]},{"id":"eeaac0cc.e1a51","type":"function","z":"e0b44d74.cf477","name":"teruglevering","func":"msg.payload = {\n  \"idx\": 298,\n  \"nvalue\": 0,\n  \"svalue\": msg.payload.toString(),\n};\nreturn msg;","outputs":1,"noerr":0,"x":890,"y":140,"wires":[["211ec0e3.71a5a"]]},{"id":"b0e29ea5.57ba7","type":"debug","z":"e0b44d74.cf477","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1210,"y":100,"wires":[]},{"id":"211ec0e3.71a5a","type":"json","z":"e0b44d74.cf477","name":"","property":"payload","action":"","pretty":false,"x":1050,"y":140,"wires":[["b0b1d0b6.ed5fc","b0e29ea5.57ba7"]]},{"id":"b0b1d0b6.ed5fc","type":"mqtt out","z":"e0b44d74.cf477","name":"Send to domoticz","topic":"domoticz/in","qos":"","retain":"","broker":"b008ef44.96838","x":1230,"y":140,"wires":[]},{"id":"b008ef44.96838","type":"mqtt-broker","z":"","name":"localhost","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

You use the join node different. Why?
What was wrong with the approach of manual join?
I used the join just to put the three objects together into one so I could apply the jsonata to it and do the summing.

is it not possible to add up a string?
using the plus sign on two numbers that are stored as strings will concatinate them not add them...because they are strings not numbers :slightly_smiling_face:

The reason your Join node did not work is because you had not told it when to send the message on. You should have told it to send it after three message parts.
When I said to set the debug node to show the complete message I meant a debug node showing what was going into the Join node. However if you have it all working now then that is ok, there are often multiple ways of solving a problem. The more people you have contributing suggestions the more different solutions you are likely to get.

Thank you. I'm stil going to try my first thoughts.
Now i have at least an outcome. But nothing is joined? after three inputs, te last input is shown.

[{"id":"78374b80.4fda64","type":"debug","z":"40635ad8.24fde4","name":"uitkomst","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":760,"y":440,"wires":[]},{"id":"c6bf5e01.5ef9e","type":"switch","z":"40635ad8.24fde4","name":"Delivery_P1","property":"payload.idx","propertyType":"msg","rules":[{"t":"eq","v":"25","vt":"str"},{"t":"eq","v":"24","vt":"str"},{"t":"eq","v":"23","vt":"str"}],"checkall":"true","repair":false,"outputs":3,"x":350,"y":440,"wires":[["b5470ea6.8e6db","77d414ed.945cdc"],["b5470ea6.8e6db","77d414ed.945cdc"],["b5470ea6.8e6db","77d414ed.945cdc"]]},{"id":"b5470ea6.8e6db","type":"join","z":"40635ad8.24fde4","name":"opgeteld","mode":"custom","build":"object","property":"svalue1","propertyType":"msg","key":"payload.name","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"3","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"num","reduceFixup":"","x":560,"y":440,"wires":[["78374b80.4fda64"]]},{"id":"77d414ed.945cdc","type":"debug","z":"40635ad8.24fde4","name":"#1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":530,"y":340,"wires":[]},{"id":"99c2cc0a.073b4","type":"inject","z":"40635ad8.24fde4","name":"","topic":"","payload":"{\"Battery\":255,\"RSSI\":12,\"description\":\"\",\"dtype\":\"Usage\",\"id\":\"0000000\",\"idx\":25,\"name\":\"Delivery L3\",\"nvalue\":0,\"stype\":\"Electric\",\"svalue1\":\"25.0\",\"unit\":6}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":400,"wires":[["c6bf5e01.5ef9e"]]},{"id":"96775b00.c3d8d8","type":"inject","z":"40635ad8.24fde4","name":"","topic":"","payload":"{\"Battery\":255,\"RSSI\":12,\"description\":\"\",\"dtype\":\"Usage\",\"id\":\"0000000\",\"idx\":23,\"name\":\"Delivery L1\",\"nvalue\":0,\"stype\":\"Electric\",\"svalue1\":\"23.0\",\"unit\":4}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":480,"wires":[["c6bf5e01.5ef9e"]]},{"id":"42bffe0e.ce61c","type":"inject","z":"40635ad8.24fde4","name":"","topic":"","payload":"{\"Battery\":255,\"RSSI\":12,\"description\":\"\",\"dtype\":\"Usage\",\"id\":\"0000000\",\"idx\":24,\"name\":\"Delivery L2\",\"nvalue\":0,\"stype\":\"Electric\",\"svalue1\":\"24.0\",\"unit\":5}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":440,"wires":[["c6bf5e01.5ef9e"]]}]