How to do a simple sum of 2 msgs

Thank you so much bro....

It is certainly possible to do such things using a Join node, can you explain exactly what you are trying to do please.

as @machadotiago said we can actually do it but there is a small bug in it... If you inject same node twice then it is making sum of that number with itself and the other method it is worrking totally fine.

Can you explain exactly what you are trying to do please?

Hi @Colin,

I believe the problem that @rockstarsmaf89 is facing is that one of the 2 messages is not coming in regularly.

I stumbled upon it as well with two inputs being MQTT messages. In my mind: the two MQTT messages needed to be deducted (electricity delivered from the grid - electricity returned to the grid with solar panels = net grid usage). By use case however one is always 0 (you are either getting energy from the grid or putting energy towards it, never both at the same time).

In the provided "join node" the value that is "0" is not receiving a value (MQTT message is not being send due to "no change") which leads to output being send after 2 message parts (which are basically from the same MQTT value).

In my scenario, i can even just attach the next step in the flow to both values (using a Change -$abs(payload)) to make the eletricity returned to the grid negative. As only one of the two is getting MQTT updates, I don't need to sum (or susbstract).

However the "bug" reported above here is likely in a scenario where MQTT messages order or update speed might not be guaranteed. A solution might be needed where the latest value from both MQTT are "stored and used" until the next update of the MQTT value.

Edit: a second "use case" specific problem is when one of the two inputs is expected (/should be) negative. It looks like the "Join" doesn't handle it providing always a positive value as output.

If you need the sum of the latest values coming from two input sources then this will do it. The join node has no problems with -ve numbers.

image

[{"id":"2ebc82a.553b6fe","type":"inject","z":"bdd7be38.d3b55","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"input_1","payload":"7","payloadType":"num","x":150,"y":680,"wires":[["c801452.6b38cb8"]]},{"id":"2223c6f0.163ae2","type":"inject","z":"bdd7be38.d3b55","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"input_1","payload":"-2","payloadType":"num","x":140,"y":760,"wires":[["c801452.6b38cb8"]]},{"id":"8b7be5f1.fb4b5","type":"inject","z":"bdd7be38.d3b55","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"input_1","payload":"9","payloadType":"num","x":140,"y":720,"wires":[["c801452.6b38cb8"]]},{"id":"48a804ec.73ec9c","type":"inject","z":"bdd7be38.d3b55","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"input_2","payload":"6","payloadType":"num","x":140,"y":860,"wires":[["c801452.6b38cb8"]]},{"id":"150234ef.0c1e23","type":"inject","z":"bdd7be38.d3b55","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"input_2","payload":"10","payloadType":"num","x":140,"y":940,"wires":[["c801452.6b38cb8"]]},{"id":"8c07e018.0f3628","type":"inject","z":"bdd7be38.d3b55","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"input_2","payload":"0","payloadType":"num","x":140,"y":900,"wires":[["c801452.6b38cb8"]]},{"id":"c801452.6b38cb8","type":"join","z":"bdd7be38.d3b55","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":350,"y":780,"wires":[["b7727fd2.189b1"]]},{"id":"4783c79a.80247","type":"debug","z":"bdd7be38.d3b55","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":430,"y":880,"wires":[]},{"id":"b7727fd2.189b1","type":"function","z":"bdd7be38.d3b55","name":"input_1 + input_2","func":"msg.payload = msg.payload.input_1 + msg.payload.input_2\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":500,"y":780,"wires":[["4783c79a.80247"]]}]
2 Likes

I can't thank you enough, this is just what i was looking for :slight_smile:

Thank you very much as well! I was not happy with the individual sum function, therefore I have adapted it to a generic join sum function, which sums all payloads of a join node.

temp

[{"id":"b4c4154097520765","type":"inject","z":"6a053cf7.458084","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"input_1","payload":"7","payloadType":"num","x":240,"y":2480,"wires":[["494f5f61405ee846"]]},{"id":"b5fda5d559b6943e","type":"inject","z":"6a053cf7.458084","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"input_1","payload":"-2","payloadType":"num","x":250,"y":2560,"wires":[["494f5f61405ee846"]]},{"id":"46d5ac44a59fb141","type":"inject","z":"6a053cf7.458084","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"input_1","payload":"9","payloadType":"num","x":250,"y":2520,"wires":[["494f5f61405ee846"]]},{"id":"996bcc2ed5c671da","type":"inject","z":"6a053cf7.458084","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"input_2","payload":"6","payloadType":"num","x":250,"y":2660,"wires":[["494f5f61405ee846"]]},{"id":"a74d043c9e6aec48","type":"inject","z":"6a053cf7.458084","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"input_2","payload":"10","payloadType":"num","x":250,"y":2740,"wires":[["494f5f61405ee846"]]},{"id":"96788023931afdf4","type":"inject","z":"6a053cf7.458084","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"input_2","payload":"0","payloadType":"num","x":250,"y":2700,"wires":[["494f5f61405ee846"]]},{"id":"494f5f61405ee846","type":"join","z":"6a053cf7.458084","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":460,"y":2580,"wires":[["b48f7ec8e5f601cb"]]},{"id":"f91d230317d8dfb1","type":"debug","z":"6a053cf7.458084","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":540,"y":2680,"wires":[]},{"id":"b48f7ec8e5f601cb","type":"function","z":"6a053cf7.458084","name":"join sum","func":"msg.payload = Object.values(msg.payload).reduce((prev, curr) => parseInt(prev) + parseInt(curr ))\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":620,"y":2580,"wires":[["f91d230317d8dfb1"]]}]