Adding 2 Msg.payloads numbers


#1

Hi there,

I am currently having trouble with adding 2 numbers in the dashboard. Is there a explained way to do this?

Essentially I am just trying to add 2 msg.payloads values together, to give 1 msg.payload

Ex. 120 First msg 121 second msg = 241 total.

These numbers are being updated every 5 seconds.

The only thing Ive seen similar to what Im trying to do is below:

// store pid/temperature in flow context
if (msg.hasOwnProperty("pid")) {
flow.set('pid', msg.pid);
} else if (msg.hasOwnProperty("temperature")) {
flow.set('temperature', msg.temperature);
}

// get the latest values of pid/temperature from context - default to 0 if not yet set
var pid = flow.get('pid') || 0;
var temperature = flow.get('temperature') || 0;
msg.payload = pid+temperature;
return msg

Can anyone assist me? When I tried to change the variables in the above, it did not work for me.

If you need more of an explanation, I'll try my best to explain it in more detail.

Thanks


#2

Why have you started a new thread for basically the same question? [1]
As was explained there you can use a Join node.

[1] 2 msg.topic in calculation function node


#3

I don't understand why it is not working for you, here's a flow with exactly your function and 2 inputs: 1 where msg.pid = 5 and the other where msg.temperature = 10 and the output is 15
Maybe one of your inputs is a string instead of a number? Put some debug nodes

[
    {
        "id": "86a79d62.a1836",
        "type": "inject",
        "z": "143ac8d4.56de37",
        "name": "",
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 380,
        "y": 540,
        "wires": [
            [
                "e8c36fa1.33cef"
            ]
        ]
    },
    {
        "id": "e8c36fa1.33cef",
        "type": "change",
        "z": "143ac8d4.56de37",
        "name": "msg.pid = 5",
        "rules": [
            {
                "t": "set",
                "p": "pid",
                "pt": "msg",
                "to": "5",
                "tot": "num"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 550,
        "y": 540,
        "wires": [
            [
                "e2c5d677.1a3458"
            ]
        ]
    },
    {
        "id": "e2c5d677.1a3458",
        "type": "function",
        "z": "143ac8d4.56de37",
        "name": "",
        "func": "// store pid/temperature in flow context\nif (msg.hasOwnProperty(\"pid\")) {\nflow.set('pid', msg.pid);\n} else if (msg.hasOwnProperty(\"temperature\")) {\nflow.set('temperature', msg.temperature);\n}\n\n// get the latest values of pid/temperature from context - default to 0 if not yet set\nvar pid = flow.get('pid') || 0;\nvar temperature = flow.get('temperature') || 0;\nmsg.payload = pid+temperature;\nreturn msg",
        "outputs": 1,
        "noerr": 0,
        "x": 730,
        "y": 540,
        "wires": [
            [
                "24c397f9.dd1158"
            ]
        ]
    },
    {
        "id": "1da64086.58464f",
        "type": "inject",
        "z": "143ac8d4.56de37",
        "name": "",
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 380,
        "y": 580,
        "wires": [
            [
                "9e418ce.9689b7"
            ]
        ]
    },
    {
        "id": "9e418ce.9689b7",
        "type": "change",
        "z": "143ac8d4.56de37",
        "name": "msg.temperature = 10",
        "rules": [
            {
                "t": "set",
                "p": "temperature",
                "pt": "msg",
                "to": "10",
                "tot": "num"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 580,
        "y": 580,
        "wires": [
            [
                "e2c5d677.1a3458"
            ]
        ]
    },
    {
        "id": "24c397f9.dd1158",
        "type": "debug",
        "z": "143ac8d4.56de37",
        "name": "5 + 10 = 15",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "x": 870,
        "y": 540,
        "wires": []
    }
]