Sum 2 nodes in a function

hello all,
I have tried in different ways to add 2 numbers in a function. I can not do it.
I also have very little experience with functions in Nodende.
Who wants to help me get this done.

Thankx,
Peter

[{"id":"960cbfbbef2d69c6","type":"inject","z":"2f5ea886d00cd91e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":110,"y":175,"wires":[["100cb4654a40c9fd","6e78c632028d6c8f"]]},{"id":"100cb4654a40c9fd","type":"api-current-state","z":"2f5ea886d00cd91e","name":"getal1","server":"3c6b2d2d.b63e72","version":3,"outputs":1,"halt_if":"","halt_if_type":"num","halt_if_compare":"is","entity_id":"input_number.getal1","state_type":"num","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"},{"property":"topic","propertyType":"msg","value":"node1","valueType":"str"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":270,"y":150,"wires":[["0f2e0c0827bc1986"]]},{"id":"6e78c632028d6c8f","type":"api-current-state","z":"2f5ea886d00cd91e","name":"getal2","server":"3c6b2d2d.b63e72","version":3,"outputs":1,"halt_if":"","halt_if_type":"num","halt_if_compare":"is","entity_id":"input_number.getal1","state_type":"num","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"},{"property":"topic","propertyType":"msg","value":"node2","valueType":"str"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":270,"y":200,"wires":[["0f2e0c0827bc1986"]]},{"id":"0f2e0c0827bc1986","type":"function","z":"2f5ea886d00cd91e","name":"c = context.node2 + context.node1","func":"context.node1 = context.node1 || 0;\ncontext.node2 = context.node2 || 0;\n\nif(msg.topic == 'node1'){\n    context.node1 = msg.payload;\n} else if (msg.topic == 'node2') {\n    context.node2 = msg.payload; \n}\n\nvar c = context.node2 + context.node1;\n\n\nreturn c;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":540,"y":175,"wires":[["68cd5c749e374caf"]]},{"id":"68cd5c749e374caf","type":"debug","z":"2f5ea886d00cd91e","name":"debug 358","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":795,"y":175,"wires":[]},{"id":"3c6b2d2d.b63e72","type":"server","name":"Home Assistant","version":5,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30,"areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m","enableGlobalContextStore":true}]

Your flow does not work when imported. Export your flow again and follow these steps:

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote ```)

``` 
   code goes here 
```

You can edit and correct your post by clicking the pencil :pencil2: icon.

See this post for more details - How to share code or flow json

[{"id":"960cbfbbef2d69c6","type":"inject","z":"2f5ea886d00cd91e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":110,"y":175,"wires":[["100cb4654a40c9fd","6e78c632028d6c8f"]]},{"id":"100cb4654a40c9fd","type":"api-current-state","z":"2f5ea886d00cd91e","name":"getal1","server":"3c6b2d2d.b63e72","version":3,"outputs":1,"halt_if":"","halt_if_type":"num","halt_if_compare":"is","entity_id":"input_number.getal1","state_type":"num","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"},{"property":"topic","propertyType":"msg","value":"node1","valueType":"str"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":270,"y":150,"wires":[["0f2e0c0827bc1986"]]},{"id":"6e78c632028d6c8f","type":"api-current-state","z":"2f5ea886d00cd91e","name":"getal2","server":"3c6b2d2d.b63e72","version":3,"outputs":1,"halt_if":"","halt_if_type":"num","halt_if_compare":"is","entity_id":"input_number.getal1","state_type":"num","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"},{"property":"topic","propertyType":"msg","value":"node2","valueType":"str"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":270,"y":200,"wires":[["0f2e0c0827bc1986"]]},{"id":"0f2e0c0827bc1986","type":"function","z":"2f5ea886d00cd91e","name":"c = context.node2 + context.node1","func":"context.node1 = context.node1 || 0;\ncontext.node2 = context.node2 || 0;\n\nif(msg.topic == 'node1'){\n    context.node1 = msg.payload;\n} else if (msg.topic == 'node2') {\n    context.node2 = msg.payload; \n}\n\nvar c = context.node2 + context.node1;\n\n\nreturn c;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":540,"y":175,"wires":[["68cd5c749e374caf"]]},{"id":"68cd5c749e374caf","type":"debug","z":"2f5ea886d00cd91e","name":"debug 358","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":795,"y":175,"wires":[]},{"id":"3c6b2d2d.b63e72","type":"server","name":"Home Assistant","version":5,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30,"areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m","enableGlobalContextStore":true}]

This is not how context works. See documentation.

But if you are always expecting 2 messages from 2 different topics, it is probably easier to use a join node instead, which can combine the 2 topics into a single message (note that this is based on the assumption that you actually always receive 2 messages)

I think your function returns wrongly.
Try this function:

context.node1 = context.node1 || 0;
context.node2 = context.node2 || 0;

if(msg.topic == 'node1'){
    context.node1 = msg.payload;
} else if (msg.topic == 'node2') {
    context.node2 = msg.payload; 
}

var c = context.node2 + context.node1;

msg.payload = c;
return msg;

[{"id":"b0063c51cdfd417c","type":"inject","z":"c05fa2617ddabfc3","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"node1","payload":"3","payloadType":"num","x":420,"y":620,"wires":[["468bb01e6757aed8"]]},{"id":"468bb01e6757aed8","type":"function","z":"c05fa2617ddabfc3","name":"c = context.node2 + context.node1","func":"context.node1 = context.node1 || 0;\ncontext.node2 = context.node2 || 0;\n\nif(msg.topic == 'node1'){\n    context.node1 = msg.payload;\n} else if (msg.topic == 'node2') {\n    context.node2 = msg.payload; \n}\n\nvar c = context.node2 + context.node1;\n\nmsg.payload = c;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":750,"y":640,"wires":[["70aacb57b866c708"]]},{"id":"70aacb57b866c708","type":"debug","z":"c05fa2617ddabfc3","name":"debug 359","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1005,"y":640,"wires":[]},{"id":"5448bdfdc7fea366","type":"inject","z":"c05fa2617ddabfc3","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"node2","payload":"5","payloadType":"num","x":420,"y":660,"wires":[["468bb01e6757aed8"]]}]

Caveat: It will return for every incoming messages, regardless if node1 or node2 were sent ever.

Thanx for you reply. Your code is working fine. It helped me a lot.
:+1:

Your option with the join option is also working fine. Thankx
:+1:

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