# I am new in node red, i am facing problem when addition two values . how to use function block for addition?

**URL:** <https://discourse.nodered.org/t/i-am-new-in-node-red-i-am-facing-problem-when-addition-two-values-how-to-use-function-block-for-addition/62348>\
**Category:** General\
**Created:** [7 May 2022 20:47 UTC](https://discourse.nodered.org/t/i-am-new-in-node-red-i-am-facing-problem-when-addition-two-values-how-to-use-function-block-for-addition/62348 "2022-05-07T20:47:26Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![jitesh](https://avatars.discourse-cdn.com/v4/letter/j/ee59a6/32.png) [@jitesh](https://discourse.nodered.org/u/jitesh)\
**Post date:** [7 May 2022 20:47 UTC](https://discourse.nodered.org/t/i-am-new-in-node-red-i-am-facing-problem-when-addition-two-values-how-to-use-function-block-for-addition/62348/1 "2022-05-07T20:47:26Z")

</div>

```auto
[
    {
        "id": "ccc1360c184b413a",
        "type": "tab",
        "label": "Flow 5",
        "disabled": false,
        "info": ""
    },
    {
        "id": "27e7326f70e90168",
        "type": "modbus-read",
        "z": "ccc1360c184b413a",
        "name": "CT_5",
        "topic": "",
        "showStatusActivities": false,
        "logIOActivities": false,
        "showErrors": false,
        "unitid": "1",
        "dataType": "HoldingRegister",
        "adr": "100",
        "quantity": "60",
        "rate": "1",
        "rateUnit": "s",
        "delayOnStart": false,
        "startDelayTime": "",
        "server": "dc095a4c1ca3d0be",
        "useIOFile": false,
        "ioFile": "",
        "useIOForPayload": false,
        "emptyMsgOnFail": false,
        "x": 210,
        "y": 160,
        "wires": [
            [
                "d3c328a720ac9183"
            ],
            []
        ]
    },
    {
        "id": "d3c328a720ac9183",
        "type": "function",
        "z": "ccc1360c184b413a",
        "name": "",
        "func": "msg.payload = [msg.payload[4],msg.payload[3]];\nlet pay = msg.payload;\n\nconst buf = Buffer.allocUnsafe(4);\nbuf.writeUInt16BE(pay[0],2);\nbuf.writeUInt16BE(pay[1],0);\n\nmsg.payload = buf.readFloatBE(0).toFixed(2);\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 440,
        "y": 160,
        "wires": [
            [
                "b8a8b938b055144f",
                "4b59511b9bd61ba9"
            ]
        ]
    },
    {
        "id": "c3b18d970f8f2680",
        "type": "modbus-read",
        "z": "ccc1360c184b413a",
        "name": "CT_4",
        "topic": "",
        "showStatusActivities": false,
        "logIOActivities": false,
        "showErrors": false,
        "unitid": "1",
        "dataType": "HoldingRegister",
        "adr": "100",
        "quantity": "60",
        "rate": "1",
        "rateUnit": "s",
        "delayOnStart": false,
        "startDelayTime": "",
        "server": "2692a71fc2f3a35f",
        "useIOFile": false,
        "ioFile": "",
        "useIOForPayload": false,
        "emptyMsgOnFail": false,
        "x": 210,
        "y": 320,
        "wires": [
            [
                "2ada03208d088f9f"
            ],
            []
        ]
    },
    {
        "id": "2ada03208d088f9f",
        "type": "function",
        "z": "ccc1360c184b413a",
        "name": "",
        "func": "msg.payload = [msg.payload[4],msg.payload[3]];\nlet pay = msg.payload;\n\nconst buf = Buffer.allocUnsafe(4);\nbuf.writeUInt16BE(pay[0],2);\nbuf.writeUInt16BE(pay[1],0);\n\nmsg.payload = buf.readFloatBE(0).toFixed(2);\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 400,
        "y": 320,
        "wires": [
            [
                "6f58c147eaff54f5",
                "4b59511b9bd61ba9"
            ]
        ]
    },
    {
        "id": "b8a8b938b055144f",
        "type": "debug",
        "z": "ccc1360c184b413a",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 780,
        "y": 120,
        "wires": []
    },
    {
        "id": "6f58c147eaff54f5",
        "type": "debug",
        "z": "ccc1360c184b413a",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 750,
        "y": 320,
        "wires": []
    },
    {
        "id": "4b59511b9bd61ba9",
        "type": "join",
        "z": "ccc1360c184b413a",
        "name": "",
        "mode": "custom",
        "build": "array",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": "\\n",
        "joinerType": "str",
        "accumulate": false,
        "timeout": "",
        "count": "2",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "",
        "reduceFixup": "",
        "x": 480,
        "y": 520,
        "wires": [
            [
                "c141588faab3021b",
                "250b15f56d4ccbbd"
            ]
        ]
    },
    {
        "id": "c141588faab3021b",
        "type": "debug",
        "z": "ccc1360c184b413a",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 730,
        "y": 400,
        "wires": []
    },
    {
        "id": "8e9d7ef5c3fcdf2a",
        "type": "debug",
        "z": "ccc1360c184b413a",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": true,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 940,
        "y": 520,
        "wires": []
    },
    {
        "id": "250b15f56d4ccbbd",
        "type": "change",
        "z": "ccc1360c184b413a",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "$sum(array)\t",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 700,
        "y": 520,
        "wires": [
            [
                "8e9d7ef5c3fcdf2a"
            ]
        ]
    },
    {
        "id": "dc095a4c1ca3d0be",
        "type": "modbus-client",
        "name": "LOCATION_5",
        "clienttype": "tcp",
        "bufferCommands": true,
        "stateLogEnabled": false,
        "queueLogEnabled": false,
        "tcpHost": "103.62.95.123",
        "tcpPort": "502",
        "tcpType": "DEFAULT",
        "serialPort": "/dev/ttyUSB",
        "serialType": "RTU-BUFFERD",
        "serialBaudrate": "9600",
        "serialDatabits": "8",
        "serialStopbits": "1",
        "serialParity": "none",
        "serialConnectionDelay": "100",
        "serialAsciiResponseStartDelimiter": "",
        "unit_id": "1",
        "commandDelay": "1",
        "clientTimeout": "1000",
        "reconnectOnTimeout": true,
        "reconnectTimeout": "2000",
        "parallelUnitIdsAllowed": false
    },
    {
        "id": "2692a71fc2f3a35f",
        "type": "modbus-client",
        "name": "LOCATION_4",
        "clienttype": "tcp",
        "bufferCommands": true,
        "stateLogEnabled": false,
        "queueLogEnabled": false,
        "tcpHost": "103.62.95.145",
        "tcpPort": "502",
        "tcpType": "DEFAULT",
        "serialPort": "/dev/ttyUSB",
        "serialType": "RTU-BUFFERD",
        "serialBaudrate": "9600",
        "serialDatabits": "8",
        "serialStopbits": "1",
        "serialParity": "none",
        "serialConnectionDelay": "100",
        "serialAsciiResponseStartDelimiter": "",
        "unit_id": 1,
        "commandDelay": 1,
        "clientTimeout": 1000,
        "reconnectOnTimeout": true,
        "reconnectTimeout": 2000,
        "parallelUnitIdsAllowed": false
    }
]

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [7 May 2022 21:04 UTC](https://discourse.nodered.org/t/i-am-new-in-node-red-i-am-facing-problem-when-addition-two-values-how-to-use-function-block-for-addition/62348/2 "2022-05-07T21:04:13Z")

</div>

You may need to include just a bit more information on what you are wanting to do.

~~All I see is a message contents displayed. (Guessing)~~  
~~But not what you are trying to add.~~

Ok sorry.

But maybe a _walk through_ to help people understand what you are doing.

What are you seeing in the `debug` nodes?  
Using _foreign_ nodes in your examples is not helpful as others may not have those nodes.

Rather it would be better to make _dummy_ nodes (`inject` and `change`) to construct the message/s you are receiving and then others can see what it is you are getting.

P.S.

It would seem you are adding the values in the `change` node: it won't work.  
All variables are valid only at the time of them being sent.  
If you want to add the value of one input to the value of another (which you haven't really explained) the second set of values won't exist.

But again:  
You haven't explained enough what it is and what you are seeing.

Please put in the nodes as suggested above with the `inject` and `change` to make the messages and repost the code.

---

<div class="post-metadata">

**Author:** ![jitesh](https://avatars.discourse-cdn.com/v4/letter/j/ee59a6/32.png) [@jitesh](https://discourse.nodered.org/u/jitesh)\
**Post date:** [8 May 2022 06:29 UTC](https://discourse.nodered.org/t/i-am-new-in-node-red-i-am-facing-problem-when-addition-two-values-how-to-use-function-block-for-addition/62348/3 "2022-05-08T06:29:48Z")

</div>

```auto
[
    {
        "id": "ccc1360c184b413a",
        "type": "tab",
        "label": "Flow 5",
        "disabled": false,
        "info": ""
    },
    {
        "id": "27e7326f70e90168",
        "type": "modbus-read",
        "z": "ccc1360c184b413a",
        "name": "CT_5",
        "topic": "",
        "showStatusActivities": false,
        "logIOActivities": false,
        "showErrors": false,
        "unitid": "1",
        "dataType": "HoldingRegister",
        "adr": "100",
        "quantity": "60",
        "rate": "1",
        "rateUnit": "s",
        "delayOnStart": false,
        "startDelayTime": "",
        "server": "dc095a4c1ca3d0be",
        "useIOFile": false,
        "ioFile": "",
        "useIOForPayload": false,
        "emptyMsgOnFail": false,
        "x": 210,
        "y": 160,
        "wires": [
            [
                "d3c328a720ac9183"
            ],
            []
        ]
    },
    {
        "id": "d3c328a720ac9183",
        "type": "function",
        "z": "ccc1360c184b413a",
        "name": "",
        "func": "msg.payload = [msg.payload[4],msg.payload[3]];\nlet pay = msg.payload;\n\nconst buf = Buffer.allocUnsafe(4);\nbuf.writeUInt16BE(pay[0],2);\nbuf.writeUInt16BE(pay[1],0);\n\nmsg.payload = buf.readFloatBE(0).toFixed(2);\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 440,
        "y": 160,
        "wires": [
            [
                "812cfffb8fad5391"
            ]
        ]
    },
    {
        "id": "c3b18d970f8f2680",
        "type": "modbus-read",
        "z": "ccc1360c184b413a",
        "name": "CT_4",
        "topic": "",
        "showStatusActivities": false,
        "logIOActivities": false,
        "showErrors": false,
        "unitid": "1",
        "dataType": "HoldingRegister",
        "adr": "100",
        "quantity": "60",
        "rate": "1",
        "rateUnit": "s",
        "delayOnStart": false,
        "startDelayTime": "",
        "server": "2692a71fc2f3a35f",
        "useIOFile": false,
        "ioFile": "",
        "useIOForPayload": false,
        "emptyMsgOnFail": false,
        "x": 210,
        "y": 320,
        "wires": [
            [
                "2ada03208d088f9f"
            ],
            []
        ]
    },
    {
        "id": "2ada03208d088f9f",
        "type": "function",
        "z": "ccc1360c184b413a",
        "name": "",
        "func": "msg.payload = [msg.payload[4],msg.payload[3]];\nlet pay = msg.payload;\n\nconst buf = Buffer.allocUnsafe(4);\nbuf.writeUInt16BE(pay[0],2);\nbuf.writeUInt16BE(pay[1],0);\n\nmsg.payload = buf.readFloatBE(0).toFixed(2);\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 400,
        "y": 320,
        "wires": [
            [
                "de59d139359235a0"
            ]
        ]
    },
    {
        "id": "812cfffb8fad5391",
        "type": "change",
        "z": "ccc1360c184b413a",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "v1",
                "pt": "flow",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 575,
        "y": 77,
        "wires": [
            [
                "0ea204ed95afb028"
            ]
        ]
    },
    {
        "id": "de59d139359235a0",
        "type": "change",
        "z": "ccc1360c184b413a",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "v2",
                "pt": "flow",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 510,
        "y": 240,
        "wires": [
            [
                "0ea204ed95afb028"
            ]
        ]
    },
    {
        "id": "0ea204ed95afb028",
        "type": "change",
        "z": "ccc1360c184b413a",
        "name": "sum",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "$number($flowContext('v1')) + $number($flowContext('v2')) + $number($flowContext('v3'))",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 728,
        "y": 180,
        "wires": [
            [
                "aa3809a6d2a20046"
            ]
        ]
    },
    {
        "id": "9377623d8ccca450",
        "type": "modbus-read",
        "z": "ccc1360c184b413a",
        "name": "AIRPORT",
        "topic": "",
        "showStatusActivities": false,
        "logIOActivities": false,
        "showErrors": false,
        "unitid": "1",
        "dataType": "HoldingRegister",
        "adr": "100",
        "quantity": "60",
        "rate": "1",
        "rateUnit": "s",
        "delayOnStart": false,
        "startDelayTime": "",
        "server": "6b455b808110be31",
        "useIOFile": false,
        "ioFile": "",
        "useIOForPayload": false,
        "emptyMsgOnFail": false,
        "x": 160,
        "y": 460,
        "wires": [
            [
                "25681a0bc899f589"
            ],
            []
        ]
    },
    {
        "id": "25681a0bc899f589",
        "type": "function",
        "z": "ccc1360c184b413a",
        "name": "",
        "func": "msg.payload = [msg.payload[4],msg.payload[3]];\nlet pay = msg.payload;\n\nconst buf = Buffer.allocUnsafe(4);\nbuf.writeUInt16BE(pay[0],2);\nbuf.writeUInt16BE(pay[1],0);\n\nmsg.payload = buf.readFloatBE(0).toFixed(2);\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 380,
        "y": 460,
        "wires": [
            [
                "17a6979a6d639802"
            ]
        ]
    },
    {
        "id": "17a6979a6d639802",
        "type": "change",
        "z": "ccc1360c184b413a",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "v3",
                "pt": "flow",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 597,
        "y": 378,
        "wires": [
            [
                "0ea204ed95afb028",
                "51aff0b66a93d412"
            ]
        ]
    },
    {
        "id": "51aff0b66a93d412",
        "type": "debug",
        "z": "ccc1360c184b413a",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 831,
        "y": 400,
        "wires": []
    },
    {
        "id": "aa3809a6d2a20046",
        "type": "debug",
        "z": "ccc1360c184b413a",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 897,
        "y": 281,
        "wires": []
    },
    {
        "id": "dc095a4c1ca3d0be",
        "type": "modbus-client",
        "name": "LOCATION_5",
        "clienttype": "tcp",
        "bufferCommands": true,
        "stateLogEnabled": false,
        "queueLogEnabled": false,
        "tcpHost": "103.62.95.123",
        "tcpPort": "502",
        "tcpType": "DEFAULT",
        "serialPort": "/dev/ttyUSB",
        "serialType": "RTU-BUFFERD",
        "serialBaudrate": "9600",
        "serialDatabits": "8",
        "serialStopbits": "1",
        "serialParity": "none",
        "serialConnectionDelay": "100",
        "serialAsciiResponseStartDelimiter": "",
        "unit_id": "1",
        "commandDelay": "1",
        "clientTimeout": "1000",
        "reconnectOnTimeout": true,
        "reconnectTimeout": "2000",
        "parallelUnitIdsAllowed": false
    },
    {
        "id": "2692a71fc2f3a35f",
        "type": "modbus-client",
        "name": "LOCATION_4",
        "clienttype": "tcp",
        "bufferCommands": true,
        "stateLogEnabled": false,
        "queueLogEnabled": false,
        "tcpHost": "103.62.95.145",
        "tcpPort": "502",
        "tcpType": "DEFAULT",
        "serialPort": "/dev/ttyUSB",
        "serialType": "RTU-BUFFERD",
        "serialBaudrate": "9600",
        "serialDatabits": "8",
        "serialStopbits": "1",
        "serialParity": "none",
        "serialConnectionDelay": "100",
        "serialAsciiResponseStartDelimiter": "",
        "unit_id": 1,
        "commandDelay": 1,
        "clientTimeout": 1000,
        "reconnectOnTimeout": true,
        "reconnectTimeout": 2000,
        "parallelUnitIdsAllowed": false
    },
    {
        "id": "6b455b808110be31",
        "type": "modbus-client",
        "name": "AIRPORT",
        "clienttype": "tcp",
        "bufferCommands": true,
        "stateLogEnabled": false,
        "queueLogEnabled": false,
        "tcpHost": "103.62.95.133",
        "tcpPort": "502",
        "tcpType": "DEFAULT",
        "serialPort": "/dev/ttyUSB",
        "serialType": "RTU-BUFFERD",
        "serialBaudrate": "9600",
        "serialDatabits": "8",
        "serialStopbits": "1",
        "serialParity": "none",
        "serialConnectionDelay": "100",
        "serialAsciiResponseStartDelimiter": "",
        "unit_id": "1",
        "commandDelay": "1",
        "clientTimeout": "1000",
        "reconnectOnTimeout": true,
        "reconnectTimeout": "2000",
        "parallelUnitIdsAllowed": false
    }
][quote="Trying_to_learn, post:2, topic:62348, full:true"]
You may need to include just a bit more information on what you are wanting to do.

~~All I see is a message contents displayed. (Guessing)~~
 ~~But not what you are trying to add.~~

Ok sorry.

But maybe a *walk through* to help people understand what you are doing.

What are you seeing in the `debug` nodes?
Using *foreign* nodes in your examples is not helpful as others may not have those nodes.

Rather it would be better to make *dummy* nodes (`inject` and `change`) to construct the message/s you are receiving and then others can see what it is you are getting.

P.S.

It would seem you are adding the values in the `change` node: it won't work.
All variables are valid only at the time of them being sent.
If you want to add the value of one input to the value of another (which you haven't really explained) the second set of values won't exist.

But again:
You haven't explained enough what it is and what you are seeing.

Please put in the nodes as suggested above with the `inject` and `change` to make the messages and repost the code.
[/quote]

actually i am using three different IP Modbus values addition but unfortunately when any one data is not coming then its showing error message. 

```

---

<div class="post-metadata">

**Author:** ![jitesh](https://avatars.discourse-cdn.com/v4/letter/j/ee59a6/32.png) [@jitesh](https://discourse.nodered.org/u/jitesh)\
**Post date:** [8 May 2022 06:33 UTC](https://discourse.nodered.org/t/i-am-new-in-node-red-i-am-facing-problem-when-addition-two-values-how-to-use-function-block-for-addition/62348/4 "2022-05-08T06:33:45Z")

</div>

![value_addition](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/f/5f1d782beec7ecadc8fe027637bbfdcd7eecdcca.png)

[quote="jitesh, post:3, topic:62348, full:true"]

```auto
[
    {
        "id": "ccc1360c184b413a",
        "type": "tab",
        "label": "Flow 5",
        "disabled": false,
        "info": ""
    },
    {
        "id": "27e7326f70e90168",
        "type": "modbus-read",
        "z": "ccc1360c184b413a",
        "name": "CT_5",
        "topic": "",
        "showStatusActivities": false,
        "logIOActivities": false,
        "showErrors": false,
        "unitid": "1",
        "dataType": "HoldingRegister",
        "adr": "100",
        "quantity": "60",
        "rate": "1",
        "rateUnit": "s",
        "delayOnStart": false,
        "startDelayTime": "",
        "server": "dc095a4c1ca3d0be",
        "useIOFile": false,
        "ioFile": "",
        "useIOForPayload": false,
        "emptyMsgOnFail": false,
        "x": 210,
        "y": 160,
        "wires": [
            [
                "d3c328a720ac9183"
            ],
            []
        ]
    },
    {
        "id": "d3c328a720ac9183",
        "type": "function",
        "z": "ccc1360c184b413a",
        "name": "",
        "func": "msg.payload = [msg.payload[4],msg.payload[3]];\nlet pay = msg.payload;\n\nconst buf = Buffer.allocUnsafe(4);\nbuf.writeUInt16BE(pay[0],2);\nbuf.writeUInt16BE(pay[1],0);\n\nmsg.payload = buf.readFloatBE(0).toFixed(2);\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 440,
        "y": 160,
        "wires": [
            [
                "812cfffb8fad5391"
            ]
        ]
    },
    {
        "id": "c3b18d970f8f2680",
        "type": "modbus-read",
        "z": "ccc1360c184b413a",
        "name": "CT_4",
        "topic": "",
        "showStatusActivities": false,
        "logIOActivities": false,
        "showErrors": false,
        "unitid": "1",
        "dataType": "HoldingRegister",
        "adr": "100",
        "quantity": "60",
        "rate": "1",
        "rateUnit": "s",
        "delayOnStart": false,
        "startDelayTime": "",
        "server": "2692a71fc2f3a35f",
        "useIOFile": false,
        "ioFile": "",
        "useIOForPayload": false,
        "emptyMsgOnFail": false,
        "x": 210,
        "y": 320,
        "wires": [
            [
                "2ada03208d088f9f"
            ],
            []
        ]
    },
    {
        "id": "2ada03208d088f9f",
        "type": "function",
        "z": "ccc1360c184b413a",
        "name": "",
        "func": "msg.payload = [msg.payload[4],msg.payload[3]];\nlet pay = msg.payload;\n\nconst buf = Buffer.allocUnsafe(4);\nbuf.writeUInt16BE(pay[0],2);\nbuf.writeUInt16BE(pay[1],0);\n\nmsg.payload = buf.readFloatBE(0).toFixed(2);\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 400,
        "y": 320,
        "wires": [
            [
                "de59d139359235a0"
            ]
        ]
    },
    {
        "id": "812cfffb8fad5391",
        "type": "change",
        "z": "ccc1360c184b413a",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "v1",
                "pt": "flow",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 575,
        "y": 77,
        "wires": [
            [
                "0ea204ed95afb028"
            ]
        ]
    },
    {
        "id": "de59d139359235a0",
        "type": "change",
        "z": "ccc1360c184b413a",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "v2",
                "pt": "flow",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 510,
        "y": 240,
        "wires": [
            [
                "0ea204ed95afb028"
            ]
        ]
    },
    {
        "id": "0ea204ed95afb028",
        "type": "change",
        "z": "ccc1360c184b413a",
        "name": "sum",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "$number($flowContext('v1')) + $number($flowContext('v2')) + $number($flowContext('v3'))",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 728,
        "y": 180,
        "wires": [
            [
                "aa3809a6d2a20046"
            ]
        ]
    },
    {
        "id": "9377623d8ccca450",
        "type": "modbus-read",
        "z": "ccc1360c184b413a",
        "name": "AIRPORT",
        "topic": "",
        "showStatusActivities": false,
        "logIOActivities": false,
        "showErrors": false,
        "unitid": "1",
        "dataType": "HoldingRegister",
        "adr": "100",
        "quantity": "60",
        "rate": "1",
        "rateUnit": "s",
        "delayOnStart": false,
        "startDelayTime": "",
        "server": "6b455b808110be31",
        "useIOFile": false,
        "ioFile": "",
        "useIOForPayload": false,
        "emptyMsgOnFail": false,
        "x": 160,
        "y": 460,
        "wires": [
            [
                "25681a0bc899f589"
            ],
            []
        ]
    },
    {
        "id": "25681a0bc899f589",
        "type": "function",
        "z": "ccc1360c184b413a",
        "name": "",
        "func": "msg.payload = [msg.payload[4],msg.payload[3]];\nlet pay = msg.payload;\n\nconst buf = Buffer.allocUnsafe(4);\nbuf.writeUInt16BE(pay[0],2);\nbuf.writeUInt16BE(pay[1],0);\n\nmsg.payload = buf.readFloatBE(0).toFixed(2);\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 380,
        "y": 460,
        "wires": [
            [
                "17a6979a6d639802"
            ]
        ]
    },
    {
        "id": "17a6979a6d639802",
        "type": "change",
        "z": "ccc1360c184b413a",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "v3",
                "pt": "flow",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 597,
        "y": 378,
        "wires": [
            [
                "0ea204ed95afb028",
                "51aff0b66a93d412"
            ]
        ]
    },
    {
        "id": "51aff0b66a93d412",
        "type": "debug",
        "z": "ccc1360c184b413a",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 831,
        "y": 400,
        "wires": []
    },
    {
        "id": "aa3809a6d2a20046",
        "type": "debug",
        "z": "ccc1360c184b413a",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 897,
        "y": 281,
        "wires": []
    },
    {
        "id": "dc095a4c1ca3d0be",
        "type": "modbus-client",
        "name": "LOCATION_5",
        "clienttype": "tcp",
        "bufferCommands": true,
        "stateLogEnabled": false,
        "queueLogEnabled": false,
        "tcpHost": "103.62.95.123",
        "tcpPort": "502",
        "tcpType": "DEFAULT",
        "serialPort": "/dev/ttyUSB",
        "serialType": "RTU-BUFFERD",
        "serialBaudrate": "9600",
        "serialDatabits": "8",
        "serialStopbits": "1",
        "serialParity": "none",
        "serialConnectionDelay": "100",
        "serialAsciiResponseStartDelimiter": "",
        "unit_id": "1",
        "commandDelay": "1",
        "clientTimeout": "1000",
        "reconnectOnTimeout": true,
        "reconnectTimeout": "2000",
        "parallelUnitIdsAllowed": false
    },
    {
        "id": "2692a71fc2f3a35f",
        "type": "modbus-client",
        "name": "LOCATION_4",
        "clienttype": "tcp",
        "bufferCommands": true,
        "stateLogEnabled": false,
        "queueLogEnabled": false,
        "tcpHost": "103.62.95.145",
        "tcpPort": "502",
        "tcpType": "DEFAULT",
        "serialPort": "/dev/ttyUSB",
        "serialType": "RTU-BUFFERD",
        "serialBaudrate": "9600",
        "serialDatabits": "8",
        "serialStopbits": "1",
        "serialParity": "none",
        "serialConnectionDelay": "100",
        "serialAsciiResponseStartDelimiter": "",
        "unit_id": 1,
        "commandDelay": 1,
        "clientTimeout": 1000,
        "reconnectOnTimeout": true,
        "reconnectTimeout": 2000,
        "parallelUnitIdsAllowed": false
    },
    {
        "id": "6b455b808110be31",
        "type": "modbus-client",
        "name": "AIRPORT",
        "clienttype": "tcp",
        "bufferCommands": true,
        "stateLogEnabled": false,
        "queueLogEnabled": false,
        "tcpHost": "103.62.95.133",
        "tcpPort": "502",
        "tcpType": "DEFAULT",
        "serialPort": "/dev/ttyUSB",
        "serialType": "RTU-BUFFERD",
        "serialBaudrate": "9600",
        "serialDatabits": "8",
        "serialStopbits": "1",
        "serialParity": "none",
        "serialConnectionDelay": "100",
        "serialAsciiResponseStartDelimiter": "",
        "unit_id": "1",
        "commandDelay": "1",
        "clientTimeout": "1000",
        "reconnectOnTimeout": true,
        "reconnectTimeout": "2000",
        "parallelUnitIdsAllowed": false
    }
]

```

> [@Trying\_to\_learn](#):
>
> You may need to include just a bit more information on what you are wanting to do.
> 
> ~~All I see is a message contents displayed. (Guessing)~~  
> ~~But not what you are trying to add.~~
> 
> Ok sorry.
> 
> But maybe a _walk through_ to help people understand what you are doing.
> 
> What are you seeing in the `debug` nodes?  
> Using _foreign_ nodes in your examples is not helpful as others may not have those nodes.
> 
> Rather it would be better to make _dummy_ nodes (`inject` and `change`) to construct the message/s you are receiving and then others can see what it is you are getting.
> 
> P.S.
> 
> It would seem you are adding the values in the `change` node: it won't work.  
> All variables are valid only at the time of them being sent.  
> If you want to add the value of one input to the value of another (which you haven't really explained) the second set of values won't exist.
> 
> But again:  
> You haven't explained enough what it is and what you are seeing.
> 
> Please put in the nodes as suggested above with the `inject` and `change` to make the messages and repost the code.

actually i am using three different IP Modbus values addition but unfortunately when any one data is not coming then its showing error message.

please check the image also

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [8 May 2022 06:42 UTC](https://discourse.nodered.org/t/i-am-new-in-node-red-i-am-facing-problem-when-addition-two-values-how-to-use-function-block-for-addition/62348/5 "2022-05-08T06:42:12Z")

</div>

What is this? I can't import it.

And what are those orange nodes at the left of the flow?

`CT-5``CT-4``AIRPORT`

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [8 May 2022 06:48 UTC](https://discourse.nodered.org/t/i-am-new-in-node-red-i-am-facing-problem-when-addition-two-values-how-to-use-function-block-for-addition/62348/6 "2022-05-08T06:48:28Z")

</div>

It looks like they are Modbus nodes.  
[Modbus](https://flows.nodered.org/node/node-red-contrib-modbus)  
 ![Screen Shot 05-08-22 at 07.49 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/b/8bbebb094666c8d54489bf2220367d97c15f5e83.png)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [8 May 2022 06:57 UTC](https://discourse.nodered.org/t/i-am-new-in-node-red-i-am-facing-problem-when-addition-two-values-how-to-use-function-block-for-addition/62348/7 "2022-05-08T06:57:00Z")

</div>

Your third modbus does not have any value - that's why you get undefined.

If you use a function node you can check before adding.

Alternatively, you can avoid using context altogether and use a join node set for a count of 3.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [8 May 2022 07:18 UTC](https://discourse.nodered.org/t/i-am-new-in-node-red-i-am-facing-problem-when-addition-two-values-how-to-use-function-block-for-addition/62348/8 "2022-05-08T07:18:35Z")

</div>

But.... (Playing _devil's advocate_)

If all three (different) messages get out of sync: it will play havoc on the output value.

Sorry... But it could happen.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [8 May 2022 07:22 UTC](https://discourse.nodered.org/t/i-am-new-in-node-red-i-am-facing-problem-when-addition-two-values-how-to-use-function-block-for-addition/62348/9 "2022-05-08T07:22:58Z")

</div>

> [@Trying\_to\_learn](#):
>
> If all three (different) messages get out of sync: it will play havoc on the output value

Since the op is simply adding, it doesn't matter what order the values come in. However, to avoid that never use array mode. Instead, use the key value mode and the variables no matter what order they arrive will always be in the same key.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [8 May 2022 07:24 UTC](https://discourse.nodered.org/t/i-am-new-in-node-red-i-am-facing-problem-when-addition-two-values-how-to-use-function-block-for-addition/62348/10 "2022-05-08T07:24:40Z")

</div>

Sorry.. Really. Sorry.

---

<div class="post-metadata">

**Author:** ![jitesh](https://avatars.discourse-cdn.com/v4/letter/j/ee59a6/32.png) [@jitesh](https://discourse.nodered.org/u/jitesh)\
**Post date:** [8 May 2022 08:56 UTC](https://discourse.nodered.org/t/i-am-new-in-node-red-i-am-facing-problem-when-addition-two-values-how-to-use-function-block-for-addition/62348/11 "2022-05-08T08:56:27Z")

</div>

> [@Steve-Mcl](#):
>
> Your third modbus does not have any value - that's why you get undefined.
> 
> If you use a function node you can check before adding.
> 
> Alternatively, you can avoid using context altogether and use a join node set for a count of 3.

Sir please share any example when 3 data will add and one of them showing undefined value.  
please sir........thanks in advance

---

<div class="post-metadata">

**Author:** ![jitesh](https://avatars.discourse-cdn.com/v4/letter/j/ee59a6/32.png) [@jitesh](https://discourse.nodered.org/u/jitesh)\
**Post date:** [8 May 2022 17:38 UTC](https://discourse.nodered.org/t/i-am-new-in-node-red-i-am-facing-problem-when-addition-two-values-how-to-use-function-block-for-addition/62348/12 "2022-05-08T17:38:59Z")

</div>

Sir please share any example when 3 data will add and one of them showing undefined value.  
please sir........thanks in advance[quote="jitesh, post:1, topic:62348, full:true"]

```auto
[
    {
        "id": "ccc1360c184b413a",
        "type": "tab",
        "label": "Flow 5",
        "disabled": false,
        "info": ""
    },
    {
        "id": "27e7326f70e90168",
        "type": "modbus-read",
        "z": "ccc1360c184b413a",
        "name": "CT_5",
        "topic": "",
        "showStatusActivities": false,
        "logIOActivities": false,
        "showErrors": false,
        "unitid": "1",
        "dataType": "HoldingRegister",
        "adr": "100",
        "quantity": "60",
        "rate": "1",
        "rateUnit": "s",
        "delayOnStart": false,
        "startDelayTime": "",
        "server": "dc095a4c1ca3d0be",
        "useIOFile": false,
        "ioFile": "",
        "useIOForPayload": false,
        "emptyMsgOnFail": false,
        "x": 210,
        "y": 160,
        "wires": [
            [
                "d3c328a720ac9183"
            ],
            []
        ]
    },
    {
        "id": "d3c328a720ac9183",
        "type": "function",
        "z": "ccc1360c184b413a",
        "name": "",
        "func": "msg.payload = [msg.payload[4],msg.payload[3]];\nlet pay = msg.payload;\n\nconst buf = Buffer.allocUnsafe(4);\nbuf.writeUInt16BE(pay[0],2);\nbuf.writeUInt16BE(pay[1],0);\n\nmsg.payload = buf.readFloatBE(0).toFixed(2);\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 440,
        "y": 160,
        "wires": [
            [
                "b8a8b938b055144f",
                "4b59511b9bd61ba9"
            ]
        ]
    },
    {
        "id": "c3b18d970f8f2680",
        "type": "modbus-read",
        "z": "ccc1360c184b413a",
        "name": "CT_4",
        "topic": "",
        "showStatusActivities": false,
        "logIOActivities": false,
        "showErrors": false,
        "unitid": "1",
        "dataType": "HoldingRegister",
        "adr": "100",
        "quantity": "60",
        "rate": "1",
        "rateUnit": "s",
        "delayOnStart": false,
        "startDelayTime": "",
        "server": "2692a71fc2f3a35f",
        "useIOFile": false,
        "ioFile": "",
        "useIOForPayload": false,
        "emptyMsgOnFail": false,
        "x": 210,
        "y": 320,
        "wires": [
            [
                "2ada03208d088f9f"
            ],
            []
        ]
    },
    {
        "id": "2ada03208d088f9f",
        "type": "function",
        "z": "ccc1360c184b413a",
        "name": "",
        "func": "msg.payload = [msg.payload[4],msg.payload[3]];\nlet pay = msg.payload;\n\nconst buf = Buffer.allocUnsafe(4);\nbuf.writeUInt16BE(pay[0],2);\nbuf.writeUInt16BE(pay[1],0);\n\nmsg.payload = buf.readFloatBE(0).toFixed(2);\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 400,
        "y": 320,
        "wires": [
            [
                "6f58c147eaff54f5",
                "4b59511b9bd61ba9"
            ]
        ]
    },
    {
        "id": "b8a8b938b055144f",
        "type": "debug",
        "z": "ccc1360c184b413a",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 780,
        "y": 120,
        "wires": []
    },
    {
        "id": "6f58c147eaff54f5",
        "type": "debug",
        "z": "ccc1360c184b413a",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 750,
        "y": 320,
        "wires": []
    },
    {
        "id": "4b59511b9bd61ba9",
        "type": "join",
        "z": "ccc1360c184b413a",
        "name": "",
        "mode": "custom",
        "build": "array",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": "\\n",
        "joinerType": "str",
        "accumulate": false,
        "timeout": "",
        "count": "2",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "",
        "reduceFixup": "",
        "x": 480,
        "y": 520,
        "wires": [
            [
                "c141588faab3021b",
                "250b15f56d4ccbbd"
            ]
        ]
    },
    {
        "id": "c141588faab3021b",
        "type": "debug",
        "z": "ccc1360c184b413a",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 730,
        "y": 400,
        "wires": []
    },
    {
        "id": "8e9d7ef5c3fcdf2a",
        "type": "debug",
        "z": "ccc1360c184b413a",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": true,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 940,
        "y": 520,
        "wires": []
    },
    {
        "id": "250b15f56d4ccbbd",
        "type": "change",
        "z": "ccc1360c184b413a",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "$sum(array)\t",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 700,
        "y": 520,
        "wires": [
            [
                "8e9d7ef5c3fcdf2a"
            ]
        ]
    },
    {
        "id": "dc095a4c1ca3d0be",
        "type": "modbus-client",
        "name": "LOCATION_5",
        "clienttype": "tcp",
        "bufferCommands": true,
        "stateLogEnabled": false,
        "queueLogEnabled": false,
        "tcpHost": "103.62.95.123",
        "tcpPort": "502",
        "tcpType": "DEFAULT",
        "serialPort": "/dev/ttyUSB",
        "serialType": "RTU-BUFFERD",
        "serialBaudrate": "9600",
        "serialDatabits": "8",
        "serialStopbits": "1",
        "serialParity": "none",
        "serialConnectionDelay": "100",
        "serialAsciiResponseStartDelimiter": "",
        "unit_id": "1",
        "commandDelay": "1",
        "clientTimeout": "1000",
        "reconnectOnTimeout": true,
        "reconnectTimeout": "2000",
        "parallelUnitIdsAllowed": false
    },
    {
        "id": "2692a71fc2f3a35f",
        "type": "modbus-client",
        "name": "LOCATION_4",
        "clienttype": "tcp",
        "bufferCommands": true,
        "stateLogEnabled": false,
        "queueLogEnabled": false,
        "tcpHost": "103.62.95.145",
        "tcpPort": "502",
        "tcpType": "DEFAULT",
        "serialPort": "/dev/ttyUSB",
        "serialType": "RTU-BUFFERD",
        "serialBaudrate": "9600",
        "serialDatabits": "8",
        "serialStopbits": "1",
        "serialParity": "none",
        "serialConnectionDelay": "100",
        "serialAsciiResponseStartDelimiter": "",
        "unit_id": 1,
        "commandDelay": 1,
        "clientTimeout": 1000,
        "reconnectOnTimeout": true,
        "reconnectTimeout": 2000,
        "parallelUnitIdsAllowed": false
    }
]

```

[/quote]

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [7 July 2022 17:39 UTC](https://discourse.nodered.org/t/i-am-new-in-node-red-i-am-facing-problem-when-addition-two-values-how-to-use-function-block-for-addition/62348/13 "2022-07-07T17:39:02Z")

</div>

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