# If/else for efficiency formula

**URL:** <https://discourse.nodered.org/t/if-else-for-efficiency-formula/36323>\
**Category:** General\
**Created:** [22 November 2020 11:11 UTC](https://discourse.nodered.org/t/if-else-for-efficiency-formula/36323 "2020-11-22T11:11:58Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Becker](https://avatars.discourse-cdn.com/v4/letter/b/c77e96/32.png) [@Becker](https://discourse.nodered.org/u/Becker)\
**Post date:** [22 November 2020 11:11 UTC](https://discourse.nodered.org/t/if-else-for-efficiency-formula/36323/1 "2020-11-22T11:11:58Z")

</div>

Hi,  
I need to switch the values BattP \<-\> PAC3 for (dis)charging my battery:

```auto
var temp = ((msg.payload["BattP"] / msg.payload["PAC3"]) * 100 );
var temp2 = parseFloat(temp.toFixed(0));
msg.payload = temp2; 
msg.topic = "W3";
return msg;

```

the formula is correct if both are negative (charging),  
if both (or one of them) is positive, it has to be:

```auto
 ((msg.payload["PAC3"] / msg.payload["BattP"]) * 100 );

```

I don´t know how to create such a if/else formula 🙄

greetings

---

<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:** [22 November 2020 11:22 UTC](https://discourse.nodered.org/t/if-else-for-efficiency-formula/36323/2 "2020-11-22T11:22:20Z")

</div>

> [@Becker](#):
>
> if both (or one of them) is positive, it has to be:
> 
> ```auto
> ((msg.payload["PAC3"] / msg.payload["BattP"]) * 100 );
> 
> ```

so you could simply test for positive value...

```auto
var BattP = msg.payload["BattP"];
var PAC3 = msg.payload["PAC3"];
var temp; 

if(BattP > 0 || PAC3 > 0) {
    temp = ((PAC3 / BattP) * 100);
} else {
    temp = ((BattP / PAC3) * 100);
}

msg.payload = parseFloat(temp.toFixed(0)); 
msg.topic = "W3";
return msg;

```

NOTE: divide by zero is not handled. You should decide what you want to happen if the divisor is zero (halt the flow? return zero? throw error?)

---

<div class="post-metadata">

**Author:** ![Becker](https://avatars.discourse-cdn.com/v4/letter/b/c77e96/32.png) [@Becker](https://discourse.nodered.org/u/Becker)\
**Post date:** [22 November 2020 11:30 UTC](https://discourse.nodered.org/t/if-else-for-efficiency-formula/36323/3 "2020-11-22T11:30:51Z")

</div>

thank you my friend.

both values are always at the same time positiv or negative 🤪 I expressed myself badly

---

<div class="post-metadata">

**Author:** ![Becker](https://avatars.discourse-cdn.com/v4/letter/b/c77e96/32.png) [@Becker](https://discourse.nodered.org/u/Becker)\
**Post date:** [22 November 2020 12:07 UTC](https://discourse.nodered.org/t/if-else-for-efficiency-formula/36323/4 "2020-11-22T12:07:53Z")

</div>

maybe you can tell me why I can not make a formula without "join" node, here is the flow:

```auto
[
    {
        "id": "be7272e9.9bf7b8",
        "type": "tab",
        "label": "Batterie",
        "disabled": false,
        "info": ""
    },
    {
        "id": "3890d21.274caae",
        "type": "inject",
        "z": "be7272e9.9bf7b8",
        "name": "5s Abfrage",
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "repeat": "5",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 130,
        "y": 80,
        "wires": [
            [
                "55af0f5b.836618"
            ]
        ]
    },
    {
        "id": "55af0f5b.836618",
        "type": "modbus-getter",
        "z": "be7272e9.9bf7b8",
        "name": "30843 - 30852",
        "showStatusActivities": false,
        "showErrors": false,
        "logIOActivities": false,
        "unitid": "3",
        "dataType": "InputRegister",
        "adr": "30843",
        "quantity": "10",
        "server": "8d503b14.f14ac8",
        "useIOFile": false,
        "ioFile": "",
        "useIOForPayload": false,
        "emptyMsgOnFail": false,
        "keepMsgProperties": false,
        "x": 340,
        "y": 80,
        "wires": [
            [
                "24b3a002.d81888"
            ],
            []
        ]
    },
    {
        "id": "24b3a002.d81888",
        "type": "buffer-parser",
        "z": "be7272e9.9bf7b8",
        "name": "",
        "data": "responseBuffer.buffer",
        "dataType": "msg",
        "specification": "spec",
        "specificationType": "ui",
        "items": [
            {
                "type": "int32be",
                "name": "BattA",
                "offset": 0,
                "length": 1,
                "offsetbit": 0,
                "scale": 0.001,
                "mask": ""
            },
            {
                "type": "uint32be",
                "name": "SoC",
                "offset": 4,
                "length": 1,
                "offsetbit": 0,
                "scale": 1,
                "mask": ""
            },
            {
                "type": "uint32be",
                "name": "SoH",
                "offset": 8,
                "length": 1,
                "offsetbit": 0,
                "scale": 1,
                "mask": ""
            },
            {
                "type": "int32be",
                "name": "BattT",
                "offset": 12,
                "length": 1,
                "offsetbit": 0,
                "scale": 0.1,
                "mask": ""
            },
            {
                "type": "uint32be",
                "name": "BattV",
                "offset": 16,
                "length": 1,
                "offsetbit": 0,
                "scale": 0.01,
                "mask": ""
            }
        ],
        "swap1": "",
        "swap2": "",
        "swap3": "",
        "swap1Type": "swap",
        "swap2Type": "swap",
        "swap3Type": "swap",
        "msgProperty": "payload",
        "msgPropertyType": "str",
        "resultType": "keyvalue",
        "resultTypeType": "output",
        "multipleResult": false,
        "setTopic": true,
        "x": 570,
        "y": 80,
        "wires": [
            [
                "3f876e04.e9cbf2",
                "a7642778.21ef4",
                "c73fb64f.fea6a8",
                "1157fdc7.cccc4a",
                "24ab137d.291274",
                "f7f34f80.fe991"
            ]
        ]
    },
    {
        "id": "fa7304ff.0f313",
        "type": "ui_text",
        "z": "be7272e9.9bf7b8",
        "group": "a4ae5bd.929ca28",
        "order": 2,
        "width": 0,
        "height": 0,
        "name": "BYD Spannung",
        "label": "Spannung",
        "format": "{{msg.payload}} V",
        "layout": "row-spread",
        "x": 1120,
        "y": 120,
        "wires": []
    },
    {
        "id": "469f11a7.915cc8",
        "type": "ui_text",
        "z": "be7272e9.9bf7b8",
        "group": "a4ae5bd.929ca28",
        "order": 3,
        "width": 0,
        "height": 0,
        "name": "BYD Strom",
        "label": "Strom ",
        "format": "{{msg.payload}} A",
        "layout": "row-spread",
        "x": 1110,
        "y": 160,
        "wires": []
    },
    {
        "id": "cc5afbaf.b0cfb8",
        "type": "ui_text",
        "z": "be7272e9.9bf7b8",
        "group": "a4ae5bd.929ca28",
        "order": 6,
        "width": 0,
        "height": 0,
        "name": "SoH",
        "label": "Kapazität",
        "format": "{{msg.payload}} %",
        "layout": "row-spread",
        "x": 1090,
        "y": 240,
        "wires": []
    },
    {
        "id": "3ec936b3.624aa2",
        "type": "ui_text",
        "z": "be7272e9.9bf7b8",
        "group": "a4ae5bd.929ca28",
        "order": 5,
        "width": 0,
        "height": 0,
        "name": "BYD Temperatur",
        "label": "Temperatur",
        "format": "{{msg.payload}} °C",
        "layout": "row-spread",
        "x": 1130,
        "y": 200,
        "wires": []
    },
    {
        "id": "3f876e04.e9cbf2",
        "type": "modbus-getter",
        "z": "be7272e9.9bf7b8",
        "name": "30775 - 30776",
        "showStatusActivities": false,
        "showErrors": false,
        "logIOActivities": false,
        "unitid": "3",
        "dataType": "InputRegister",
        "adr": "30775",
        "quantity": "2",
        "server": "8d503b14.f14ac8",
        "useIOFile": false,
        "ioFile": "",
        "useIOForPayload": false,
        "emptyMsgOnFail": false,
        "keepMsgProperties": false,
        "x": 340,
        "y": 320,
        "wires": [
            [
                "786a5e09.e964d8"
            ],
            []
        ]
    },
    {
        "id": "786a5e09.e964d8",
        "type": "buffer-parser",
        "z": "be7272e9.9bf7b8",
        "name": "",
        "data": "responseBuffer.buffer",
        "dataType": "msg",
        "specification": "spec",
        "specificationType": "ui",
        "items": [
            {
                "type": "int32be",
                "name": "PAC3",
                "offset": 0,
                "length": 1,
                "offsetbit": 0,
                "scale": 1,
                "mask": ""
            }
        ],
        "swap1": "",
        "swap2": "",
        "swap3": "",
        "swap1Type": "swap",
        "swap2Type": "swap",
        "swap3Type": "swap",
        "msgProperty": "payload",
        "msgPropertyType": "str",
        "resultType": "keyvalue",
        "resultTypeType": "output",
        "multipleResult": false,
        "setTopic": true,
        "x": 570,
        "y": 320,
        "wires": [
            [
                "c534e4d0.2a2868",
                "6b2fe76f.cde508"
            ]
        ]
    },
    {
        "id": "398184ff.974bfc",
        "type": "ui_text",
        "z": "be7272e9.9bf7b8",
        "group": "e035cd18.8c0fd8",
        "order": 1,
        "width": 0,
        "height": 0,
        "name": "SBS Leistung AC",
        "label": "Leistung AC",
        "format": "{{msg.payload}} W",
        "layout": "row-spread",
        "x": 1090,
        "y": 320,
        "wires": []
    },
    {
        "id": "a7642778.21ef4",
        "type": "function",
        "z": "be7272e9.9bf7b8",
        "name": "SoC",
        "func": "msg.payload = msg.payload.SoC\nmsg.topic = \"SoC\"\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 790,
        "y": 80,
        "wires": [
            [
                "c0ca0117.42f7"
            ]
        ]
    },
    {
        "id": "c73fb64f.fea6a8",
        "type": "function",
        "z": "be7272e9.9bf7b8",
        "name": "Batt V",
        "func": "msg.payload = msg.payload.BattV\nmsg.topic = \"BattV\"\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 790,
        "y": 120,
        "wires": [
            [
                "96c7fa66.435148",
                "ee79b693.bcc4e8"
            ]
        ]
    },
    {
        "id": "1157fdc7.cccc4a",
        "type": "function",
        "z": "be7272e9.9bf7b8",
        "name": "Batt A",
        "func": "msg.payload = msg.payload.BattA\nmsg.topic = \"BattA\"\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 790,
        "y": 160,
        "wires": [
            [
                "96c7fa66.435148",
                "ebdb2054.bc52e8"
            ]
        ]
    },
    {
        "id": "24ab137d.291274",
        "type": "function",
        "z": "be7272e9.9bf7b8",
        "name": "Batt T",
        "func": "msg.payload = msg.payload.BattT\nmsg.topic = \"BattT\"\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 790,
        "y": 200,
        "wires": [
            [
                "3ec936b3.624aa2"
            ]
        ]
    },
    {
        "id": "f7f34f80.fe991",
        "type": "function",
        "z": "be7272e9.9bf7b8",
        "name": "SoH",
        "func": "msg.payload = msg.payload.SoH\nmsg.topic = \"SoH\"\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 790,
        "y": 240,
        "wires": [
            [
                "cc5afbaf.b0cfb8"
            ]
        ]
    },
    {
        "id": "c534e4d0.2a2868",
        "type": "function",
        "z": "be7272e9.9bf7b8",
        "name": "PAC SBS2.5",
        "func": "msg.payload = msg.payload.PAC3\nmsg.topic = \"PAC3\"\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 810,
        "y": 320,
        "wires": [
            [
                "398184ff.974bfc",
                "6cdf85eb.70da6c"
            ]
        ]
    },
    {
        "id": "96c7fa66.435148",
        "type": "join",
        "z": "be7272e9.9bf7b8",
        "name": "",
        "mode": "custom",
        "build": "object",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": "\\n",
        "joinerType": "str",
        "accumulate": false,
        "timeout": "",
        "count": "2",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "",
        "reduceFixup": "",
        "x": 1350,
        "y": 140,
        "wires": [
            [
                "e3ceb624.b07098",
                "8dfa6082.02ded"
            ]
        ]
    },
    {
        "id": "e3ceb624.b07098",
        "type": "function",
        "z": "be7272e9.9bf7b8",
        "name": "Leistung DC",
        "func": "var temp = (msg.payload[\"BattV\"] * msg.payload[\"BattA\"]);\nvar temp2 = parseFloat(temp.toFixed(0));\nmsg.payload = temp2; \nmsg.topic = \"BattP\";\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 1530,
        "y": 140,
        "wires": [
            [
                "bc0ec570.681ff",
                "6cdf85eb.70da6c"
            ]
        ]
    },
    {
        "id": "bc0ec570.681ff",
        "type": "ui_text",
        "z": "be7272e9.9bf7b8",
        "group": "a4ae5bd.929ca28",
        "order": 4,
        "width": 0,
        "height": 0,
        "name": "Leistung DC",
        "label": "Leistung DC",
        "format": "{{msg.payload}} W",
        "layout": "row-spread",
        "x": 1770,
        "y": 140,
        "wires": []
    },
    {
        "id": "6cdf85eb.70da6c",
        "type": "join",
        "z": "be7272e9.9bf7b8",
        "name": "",
        "mode": "custom",
        "build": "object",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": "\\n",
        "joinerType": "str",
        "accumulate": false,
        "timeout": "",
        "count": "2",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "num",
        "reduceFixup": "",
        "x": 1350,
        "y": 200,
        "wires": [
            [
                "bee87067.f715d"
            ]
        ]
    },
    {
        "id": "ed1d5c0e.a95a6",
        "type": "ui_text",
        "z": "be7272e9.9bf7b8",
        "group": "e035cd18.8c0fd8",
        "order": 1,
        "width": 0,
        "height": 0,
        "name": "Wirkungsgrad",
        "label": "Wirkungsgrad",
        "format": "{{msg.payload}} %",
        "layout": "row-spread",
        "x": 1780,
        "y": 200,
        "wires": []
    },
    {
        "id": "6b2fe76f.cde508",
        "type": "modbus-getter",
        "z": "be7272e9.9bf7b8",
        "name": "30953 - 30954",
        "showStatusActivities": false,
        "showErrors": false,
        "logIOActivities": false,
        "unitid": "3",
        "dataType": "InputRegister",
        "adr": "30953",
        "quantity": "2",
        "server": "8d503b14.f14ac8",
        "useIOFile": false,
        "ioFile": "",
        "useIOForPayload": false,
        "emptyMsgOnFail": false,
        "keepMsgProperties": false,
        "x": 340,
        "y": 400,
        "wires": [
            [
                "372c08ce.4b36b"
            ],
            []
        ]
    },
    {
        "id": "372c08ce.4b36b",
        "type": "buffer-parser",
        "z": "be7272e9.9bf7b8",
        "name": "",
        "data": "responseBuffer.buffer",
        "dataType": "msg",
        "specification": "spec",
        "specificationType": "ui",
        "items": [
            {
                "type": "int32be",
                "name": "T3",
                "offset": 0,
                "length": 1,
                "offsetbit": 0,
                "scale": 0.1,
                "mask": ""
            }
        ],
        "swap1": "",
        "swap2": "",
        "swap3": "",
        "swap1Type": "swap",
        "swap2Type": "swap",
        "swap3Type": "swap",
        "msgProperty": "payload",
        "msgPropertyType": "str",
        "resultType": "keyvalue",
        "resultTypeType": "output",
        "multipleResult": false,
        "setTopic": true,
        "x": 570,
        "y": 400,
        "wires": [
            [
                "a409e237.96c3a"
            ]
        ]
    },
    {
        "id": "a409e237.96c3a",
        "type": "function",
        "z": "be7272e9.9bf7b8",
        "name": "T3",
        "func": "msg.payload = msg.payload.T3\nmsg.payload = msg.payload.toFixed(1).replace('.', ',');\nmsg.topic = \"T3\"\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 790,
        "y": 400,
        "wires": [
            [
                "e78414e2.08ed"
            ]
        ]
    },
    {
        "id": "e78414e2.08ed",
        "type": "ui_text",
        "z": "be7272e9.9bf7b8",
        "group": "e035cd18.8c0fd8",
        "order": 1,
        "width": 0,
        "height": 0,
        "name": "SBS Temperatur",
        "label": "Temperatur",
        "format": "{{msg.payload}} °C",
        "layout": "row-spread",
        "x": 1080,
        "y": 400,
        "wires": []
    },
    {
        "id": "ee79b693.bcc4e8",
        "type": "function",
        "z": "be7272e9.9bf7b8",
        "name": ". zu ,",
        "func": "msg.payload = msg.payload.toFixed(1).replace('.', ',')\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 930,
        "y": 120,
        "wires": [
            [
                "fa7304ff.0f313"
            ]
        ]
    },
    {
        "id": "ebdb2054.bc52e8",
        "type": "function",
        "z": "be7272e9.9bf7b8",
        "name": ". zu ,",
        "func": "msg.payload = msg.payload.toFixed(2).replace('.', ',')\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 930,
        "y": 160,
        "wires": [
            [
                "469f11a7.915cc8"
            ]
        ]
    },
    {
        "id": "c0ca0117.42f7",
        "type": "ui_gauge",
        "z": "be7272e9.9bf7b8",
        "name": "SoC",
        "group": "a4ae5bd.929ca28",
        "order": 1,
        "width": 0,
        "height": 0,
        "gtype": "gage",
        "title": "Ladezustand",
        "label": "",
        "format": "{{value}} %",
        "min": 0,
        "max": "100",
        "colors": [
            "#b30206",
            "#f9f900",
            "#00f400"
        ],
        "seg1": "",
        "seg2": "",
        "x": 1090,
        "y": 80,
        "wires": []
    },
    {
        "id": "bee87067.f715d",
        "type": "function",
        "z": "be7272e9.9bf7b8",
        "name": "Wirkungsgrad AC/DC",
        "func": "var BattP = msg.payload[\"BattP\"];\nvar PAC3 = msg.payload[\"PAC3\"];\nvar temp; \n\nif(BattP > 0 || PAC3 > 0) {\n temp = ((PAC3 / BattP) * 100);\n} else {\n temp = ((BattP / PAC3) * 100);\n}\n\nmsg.payload = parseFloat(temp.toFixed(0)); \nmsg.topic = \"W3\";\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 1560,
        "y": 200,
        "wires": [
            [
                "ed1d5c0e.a95a6"
            ]
        ]
    },
    {
        "id": "8dfa6082.02ded",
        "type": "function",
        "z": "be7272e9.9bf7b8",
        "name": "Zellspannung",
        "func": "var temp = (msg.payload[\"BattV\"] / 96);\nmsg.payload = parseFloat(temp.toFixed(2)); \nmsg.topic = \"BattZV\";\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 1440,
        "y": 80,
        "wires": [
            [
                "e0993c71.eef048"
            ]
        ]
    },
    {
        "id": "d034f9e4.5d602",
        "type": "ui_text",
        "z": "be7272e9.9bf7b8",
        "group": "a4ae5bd.929ca28",
        "order": 2,
        "width": 0,
        "height": 0,
        "name": "BYD Zellspannung",
        "label": "Zellspannung",
        "format": "{{msg.payload}} V",
        "layout": "row-spread",
        "x": 1790,
        "y": 80,
        "wires": []
    },
    {
        "id": "e0993c71.eef048",
        "type": "function",
        "z": "be7272e9.9bf7b8",
        "name": ". zu ,",
        "func": "msg.payload = msg.payload.toFixed(2).replace('.', ',')\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 1610,
        "y": 80,
        "wires": [
            [
                "d034f9e4.5d602"
            ]
        ]
    },
    {
        "id": "8d503b14.f14ac8",
        "type": "modbus-client",
        "z": "",
        "name": "SBS2.5",
        "clienttype": "tcp",
        "bufferCommands": true,
        "stateLogEnabled": false,
        "queueLogEnabled": false,
        "tcpHost": "192.168.168.55",
        "tcpPort": "502",
        "tcpType": "TPC-RTU-BUFFERED",
        "serialPort": "/dev/ttyUSB",
        "serialType": "RTU-BUFFERD",
        "serialBaudrate": "9600",
        "serialDatabits": "8",
        "serialStopbits": "1",
        "serialParity": "none",
        "serialConnectionDelay": "100",
        "unit_id": 3,
        "commandDelay": 1,
        "clientTimeout": 1000,
        "reconnectOnTimeout": true,
        "reconnectTimeout": 2000,
        "parallelUnitIdsAllowed": true
    },
    {
        "id": "a4ae5bd.929ca28",
        "type": "ui_group",
        "z": "",
        "name": "BYD",
        "tab": "8d2f0b42.f2aa68",
        "order": 1,
        "disp": true,
        "width": "5",
        "collapse": false
    },
    {
        "id": "e035cd18.8c0fd8",
        "type": "ui_group",
        "z": "",
        "name": "SBS 2.5",
        "tab": "8d2f0b42.f2aa68",
        "order": 2,
        "disp": true,
        "width": "5",
        "collapse": false
    },
    {
        "id": "8d2f0b42.f2aa68",
        "type": "ui_tab",
        "z": "",
        "name": "Batterie",
        "icon": "fa-battery-half",
        "order": 2,
        "disabled": false,
        "hidden": false
    }
]

```

If I connect here:

 ![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/b/9b75904abd69ad81950e07d69e96e1fc47fc8049.png)  
the output is always "NaN". Don´t know why.  
I always need a join, even if the formula only contains one input value !

---

<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:** [22 November 2020 12:11 UTC](https://discourse.nodered.org/t/if-else-for-efficiency-formula/36323/5 "2020-11-22T12:11:49Z")

</div>

Because you have wired the functions (after the buffer parser) in parallel (so that generates parallel message flows). This is quite a fundamental requirement to understanding node-red.

If you wired them functions in series this would not be necessary.

I dont have time to explain further right now but give it a go. (i.e. only have one wire out of buffer parser and connect each function in series (not parallel)

---

<div class="post-metadata">

**Author:** ![Becker](https://avatars.discourse-cdn.com/v4/letter/b/c77e96/32.png) [@Becker](https://discourse.nodered.org/u/Becker)\
**Post date:** [22 November 2020 14:27 UTC](https://discourse.nodered.org/t/if-else-for-efficiency-formula/36323/6 "2020-11-22T14:27:55Z")

</div>

Ok, you mean this:

 ![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/0/f0f9d9f46a85d4db449eedc88263ec980e1ab679.png)  
should I do in only 1 function per buffer parser ?

the function extract each value from the buffer parser and give each value a topic, so that I can use them for calculations:

```auto
msg.payload = msg.payload.SoC
msg.topic = "SoC"
return msg;

```

...  
...

how Can I do this in one function (in series) ??

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [22 November 2020 14:57 UTC](https://discourse.nodered.org/t/if-else-for-efficiency-formula/36323/7 "2020-11-22T14:57:15Z")

</div>

Hi Becker,

.. you still struggling with those Join nodes .. hehe .. it thought you were an expert by now 😉  
i recommenced a couple of times in your previous threads that instead of having separate Join nodes further down your flow, you can have ONE after the Buffer parsers that does the merge of the values so all your values will be **simultaneously** available to your Functions for calculations.

Something like this :

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/f/4f81f489f35fd1245f70cee2b425de0b49c3267b.png)

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/e/be6950ed823ee3d8fe021d4db68b10da5a2191f5.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:** [22 November 2020 14:59 UTC](https://discourse.nodered.org/t/if-else-for-efficiency-formula/36323/8 "2020-11-22T14:59:41Z")

</div>

> [@Becker](#):
>
> should I do in only 1 function per buffer parser ?

You can do it that way yes - that is perfectly fine.

but i meant

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/1/116629fb4da76c672e0c3199b183dc5309ffcb38.png)

> [@Becker](#):
>
> how Can I do this in one function (in series) ??

There are lots of ways to do this but I chose to put the output of the top buffer parser into `msg.data` and pass the msg through the functions (so even though you overwrite msg.payload, msg.data stays untouched with all its original values from the the buffer parser & available for the next node)

demo... (untested)

```auto
[{"id":"3890d21.274caae","type":"inject","z":"be7272e9.9bf7b8","name":"5s Abfrage","repeat":"5","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":130,"y":80,"wires":[["55af0f5b.836618"]]},{"id":"55af0f5b.836618","type":"modbus-getter","z":"be7272e9.9bf7b8","name":"30843 - 30852","showStatusActivities":false,"showErrors":false,"logIOActivities":false,"unitid":"3","dataType":"InputRegister","adr":"30843","quantity":"10","server":"8d503b14.f14ac8","useIOFile":false,"ioFile":"","useIOForPayload":false,"emptyMsgOnFail":false,"keepMsgProperties":false,"x":340,"y":80,"wires":[["24b3a002.d81888"],[]]},{"id":"24b3a002.d81888","type":"buffer-parser","z":"be7272e9.9bf7b8","name":"","data":"responseBuffer.buffer","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"int32be","name":"BattA","offset":0,"length":1,"offsetbit":0,"scale":0.001,"mask":""},{"type":"uint32be","name":"SoC","offset":4,"length":1,"offsetbit":0,"scale":1,"mask":""},{"type":"uint32be","name":"SoH","offset":8,"length":1,"offsetbit":0,"scale":1,"mask":""},{"type":"int32be","name":"BattT","offset":12,"length":1,"offsetbit":0,"scale":0.1,"mask":""},{"type":"uint32be","name":"BattV","offset":16,"length":1,"offsetbit":0,"scale":0.01,"mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"data","msgPropertyType":"str","resultType":"keyvalue","resultTypeType":"output","multipleResult":false,"setTopic":true,"x":570,"y":140,"wires":[["3f876e04.e9cbf2","a7642778.21ef4"]]},{"id":"fa7304ff.0f313","type":"ui_text","z":"be7272e9.9bf7b8","group":"a4ae5bd.929ca28","order":2,"width":0,"height":0,"name":"BYD Spannung","label":"Spannung","format":"{{msg.payload.BattV}} V","layout":"row-spread","x":1240,"y":80,"wires":[]},{"id":"469f11a7.915cc8","type":"ui_text","z":"be7272e9.9bf7b8","group":"a4ae5bd.929ca28","order":3,"width":0,"height":0,"name":"BYD Strom","label":"Strom ","format":"{{msg.payload.BattA}} A","layout":"row-spread","x":1230,"y":120,"wires":[]},{"id":"cc5afbaf.b0cfb8","type":"ui_text","z":"be7272e9.9bf7b8","group":"a4ae5bd.929ca28","order":6,"width":0,"height":0,"name":"SoH","label":"Kapazität","format":"{{msg.data.SoH}} %","layout":"row-spread","x":1210,"y":200,"wires":[]},{"id":"3ec936b3.624aa2","type":"ui_text","z":"be7272e9.9bf7b8","group":"a4ae5bd.929ca28","order":5,"width":0,"height":0,"name":"BYD Temperatur","label":"Temperatur","format":"{{msg.data.BattT}} °C","layout":"row-spread","x":1240,"y":160,"wires":[]},{"id":"3f876e04.e9cbf2","type":"modbus-getter","z":"be7272e9.9bf7b8","name":"30775 - 30776","showStatusActivities":false,"showErrors":false,"logIOActivities":false,"unitid":"3","dataType":"InputRegister","adr":"30775","quantity":"2","server":"8d503b14.f14ac8","useIOFile":false,"ioFile":"","useIOForPayload":false,"emptyMsgOnFail":false,"keepMsgProperties":false,"x":340,"y":380,"wires":[["786a5e09.e964d8"],[]]},{"id":"786a5e09.e964d8","type":"buffer-parser","z":"be7272e9.9bf7b8","name":"","data":"responseBuffer.buffer","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"int32be","name":"PAC3","offset":0,"length":1,"offsetbit":0,"scale":1,"mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"keyvalue","resultTypeType":"output","multipleResult":false,"setTopic":true,"x":570,"y":380,"wires":[["c534e4d0.2a2868","6b2fe76f.cde508"]]},{"id":"398184ff.974bfc","type":"ui_text","z":"be7272e9.9bf7b8","group":"e035cd18.8c0fd8","order":1,"width":0,"height":0,"name":"SBS Leistung AC","label":"Leistung AC","format":"{{msg.payload}} W","layout":"row-spread","x":1230,"y":380,"wires":[]},{"id":"a7642778.21ef4","type":"function","z":"be7272e9.9bf7b8","name":"SoC","func":"\nmsg.payload = msg.data.SoC;\nmsg.topic = \"SoC\";\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":790,"y":40,"wires":[["c0ca0117.42f7","ee79b693.bcc4e8"]]},{"id":"c534e4d0.2a2868","type":"function","z":"be7272e9.9bf7b8","name":"PAC SBS2.5","func":"msg.payload = msg.payload.PAC3\nmsg.topic = \"PAC3\"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":810,"y":380,"wires":[["398184ff.974bfc","6cdf85eb.70da6c"]]},{"id":"e3ceb624.b07098","type":"function","z":"be7272e9.9bf7b8","name":"Leistung DC","func":"var temp = (msg.data.BattV * msg.data.BattA);\nvar temp2 = parseFloat(temp.toFixed(0));\nmsg.payload = temp2; \nmsg.topic = \"BattP\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":810,"y":300,"wires":[["bc0ec570.681ff","6cdf85eb.70da6c"]]},{"id":"bc0ec570.681ff","type":"ui_text","z":"be7272e9.9bf7b8","group":"a4ae5bd.929ca28","order":4,"width":0,"height":0,"name":"Leistung DC","label":"Leistung DC","format":"{{msg.payload}} W","layout":"row-spread","x":1390,"y":280,"wires":[]},{"id":"6cdf85eb.70da6c","type":"join","z":"be7272e9.9bf7b8","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"num","reduceFixup":"","x":990,"y":320,"wires":[["bee87067.f715d"]]},{"id":"ed1d5c0e.a95a6","type":"ui_text","z":"be7272e9.9bf7b8","group":"e035cd18.8c0fd8","order":1,"width":0,"height":0,"name":"Wirkungsgrad","label":"Wirkungsgrad","format":"{{msg.payload}} %","layout":"row-spread","x":1400,"y":320,"wires":[]},{"id":"6b2fe76f.cde508","type":"modbus-getter","z":"be7272e9.9bf7b8","name":"30953 - 30954","showStatusActivities":false,"showErrors":false,"logIOActivities":false,"unitid":"3","dataType":"InputRegister","adr":"30953","quantity":"2","server":"8d503b14.f14ac8","useIOFile":false,"ioFile":"","useIOForPayload":false,"emptyMsgOnFail":false,"keepMsgProperties":false,"x":340,"y":460,"wires":[["372c08ce.4b36b"],[]]},{"id":"372c08ce.4b36b","type":"buffer-parser","z":"be7272e9.9bf7b8","name":"","data":"responseBuffer.buffer","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"int32be","name":"T3","offset":0,"length":1,"offsetbit":0,"scale":0.1,"mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"keyvalue","resultTypeType":"output","multipleResult":false,"setTopic":true,"x":570,"y":460,"wires":[["a409e237.96c3a"]]},{"id":"a409e237.96c3a","type":"function","z":"be7272e9.9bf7b8","name":"T3","func":"msg.payload = msg.payload.T3\nmsg.payload = msg.payload.toFixed(1).replace('.', ',');\nmsg.topic = \"T3\"\nreturn msg;","outputs":1,"noerr":0,"x":790,"y":460,"wires":[["e78414e2.08ed"]]},{"id":"e78414e2.08ed","type":"ui_text","z":"be7272e9.9bf7b8","group":"e035cd18.8c0fd8","order":1,"width":0,"height":0,"name":"SBS Temperatur","label":"Temperatur","format":"{{msg.payload}} °C","layout":"row-spread","x":1220,"y":460,"wires":[]},{"id":"ee79b693.bcc4e8","type":"function","z":"be7272e9.9bf7b8","name":". zu ,","func":"msg.payload = {\n BattV: msg.data.BattV.toFixed(1).replace('.', ','),\n BattA: msg.data.BattA.toFixed(1).replace('.', ',')\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":790,"y":100,"wires":[["fa7304ff.0f313","469f11a7.915cc8","3ec936b3.624aa2","cc5afbaf.b0cfb8","8dfa6082.02ded"]]},{"id":"c0ca0117.42f7","type":"ui_gauge","z":"be7272e9.9bf7b8","name":"SoC","group":"a4ae5bd.929ca28","order":1,"width":0,"height":0,"gtype":"gage","title":"Ladezustand","label":"","format":"{{value}} %","min":0,"max":"100","colors":["#b30206","#f9f900","#00f400"],"seg1":"","seg2":"","x":1210,"y":40,"wires":[]},{"id":"bee87067.f715d","type":"function","z":"be7272e9.9bf7b8","name":"Wirkungsgrad AC/DC","func":"var BattP = msg.payload[\"BattP\"];\nvar PAC3 = msg.payload[\"PAC3\"];\nvar temp; \n\nif(BattP > 0 || PAC3 > 0) {\n temp = ((PAC3 / BattP) * 100);\n} else {\n temp = ((BattP / PAC3) * 100);\n}\n\nmsg.payload = parseFloat(temp.toFixed(0)); \nmsg.topic = \"W3\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1180,"y":320,"wires":[["ed1d5c0e.a95a6"]]},{"id":"8dfa6082.02ded","type":"function","z":"be7272e9.9bf7b8","name":"Zellspannung","func":"var temp = (msg.data.BattV / 96);\nmsg.payload = parseFloat(temp.toFixed(2)); \nmsg.topic = \"BattZV\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":820,"y":240,"wires":[["e3ceb624.b07098","591b6976.31f638"]]},{"id":"d034f9e4.5d602","type":"ui_text","z":"be7272e9.9bf7b8","group":"a4ae5bd.929ca28","order":2,"width":0,"height":0,"name":"BYD Zellspannung","label":"Zellspannung","format":"{{msg.payload}} V","layout":"row-spread","x":1410,"y":240,"wires":[]},{"id":"591b6976.31f638","type":"function","z":"be7272e9.9bf7b8","name":". zu ,","func":"msg.payload = msg.payload.toFixed(2).replace('.', ',')\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1030,"y":240,"wires":[["d034f9e4.5d602"]]},{"id":"8d503b14.f14ac8","type":"modbus-client","name":"SBS2.5","clienttype":"tcp","bufferCommands":true,"stateLogEnabled":false,"queueLogEnabled":false,"tcpHost":"192.168.168.55","tcpPort":"502","tcpType":"TPC-RTU-BUFFERED","serialPort":"/dev/ttyUSB","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","unit_id":3,"commandDelay":1,"clientTimeout":1000,"reconnectOnTimeout":true,"reconnectTimeout":2000,"parallelUnitIdsAllowed":true},{"id":"a4ae5bd.929ca28","type":"ui_group","name":"BYD","tab":"8d2f0b42.f2aa68","order":1,"disp":true,"width":"5","collapse":false},{"id":"e035cd18.8c0fd8","type":"ui_group","name":"SBS 2.5","tab":"8d2f0b42.f2aa68","order":2,"disp":true,"width":"5","collapse":false},{"id":"8d2f0b42.f2aa68","type":"ui_tab","name":"Batterie","icon":"fa-battery-half","order":2,"disabled":false,"hidden":false}]

```

---

<div class="post-metadata">

**Author:** ![Becker](https://avatars.discourse-cdn.com/v4/letter/b/c77e96/32.png) [@Becker](https://discourse.nodered.org/u/Becker)\
**Post date:** [22 November 2020 15:08 UTC](https://discourse.nodered.org/t/if-else-for-efficiency-formula/36323/9 "2020-11-22T15:08:10Z")

</div>

oh 😱

I think the flow is already to big to change so much

> **[flows9.json](https://drive.google.com/file/d/1dZzM_q7_u1d-gooFl8i1ZnGHCtyH3klP/view?usp=sharing)**
>
> Google Drive file.

is my programming in parallel bad / harmful in any way?

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [22 November 2020 15:37 UTC](https://discourse.nodered.org/t/if-else-for-efficiency-formula/36323/10 "2020-11-22T15:37:06Z")

</div>

> [@Becker](#):
>
> I think the flow is already to big to change so much

aha .. i see ..that full version of the flow is gonna take a little more time .. but I dont think you have many options 😉 it needs to be fixed

besides .. how long will it take to re-wire everything and do some checks .. 10-15min ?  
do a backup of your flow and start changing it

---

<div class="post-metadata">

**Author:** ![Becker](https://avatars.discourse-cdn.com/v4/letter/b/c77e96/32.png) [@Becker](https://discourse.nodered.org/u/Becker)\
**Post date:** [22 November 2020 16:03 UTC](https://discourse.nodered.org/t/if-else-for-efficiency-formula/36323/11 "2020-11-22T16:03:43Z")

</div>

It will take longer as 15min !

Can you tell me please another problem with a formula:

Version1:

```auto
var temp = ((msg.payload[40008] - msg.payload[40012]) * msg.payload[40072] * 69.457);
msg.playload = parseFloat(temp.toFixed(0));
msg.topic = "Leistung nach ZH";
return msg;

```

often (not ever) give me this output:  
 ![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/f/bf45a4df40bcc009046911ab6b1072e0c686228c.png)

Version2:

```auto
var temp = ((msg.payload[40008] - msg.payload[40012]) * msg.payload[40072] * 69.457);
var temp2 = parseFloat(temp.toFixed(0));
msg.payload = temp2;
msg.topic = "Leistung nach ZH";
return msg;

```

always right:  
 ![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/f/ff1ffef1a56955b9ffd1389bb0ec90a891cc5b62.png)

But why ?

---

<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:** [22 November 2020 17:07 UTC](https://discourse.nodered.org/t/if-else-for-efficiency-formula/36323/12 "2020-11-22T17:07:31Z")

</div>

> [@Becker](#):
>
> Can you tell me please another problem with a formula:

Sure - simple spelling - `msg.playload` is NOT the same variable as `msg.payload`

---

<div class="post-metadata">

**Author:** ![Becker](https://avatars.discourse-cdn.com/v4/letter/b/c77e96/32.png) [@Becker](https://discourse.nodered.org/u/Becker)\
**Post date:** [22 November 2020 20:16 UTC](https://discourse.nodered.org/t/if-else-for-efficiency-formula/36323/13 "2020-11-22T20:16:08Z")

</div>

🙈  
in german we say: "he no longer saw the forest for the trees" 🙈

---

<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:** [22 November 2020 20:20 UTC](https://discourse.nodered.org/t/if-else-for-efficiency-formula/36323/14 "2020-11-22T20:20:18Z")

</div>

very similar in 🇬🇧 "can't see the wood for the trees"

---

<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:** [6 December 2020 20:20 UTC](https://discourse.nodered.org/t/if-else-for-efficiency-formula/36323/15 "2020-12-06T20:20:20Z")

</div>

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