If/else for efficiency formula

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

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:

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

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

greetings

so you could simply test for positive value...

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?)

1 Like

thank you my friend.

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

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

[
    {
        "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:


the output is always "NaN". Don´t know why.
I always need a join, even if the formula only contains one input value !

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)

Ok, you mean this:


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:

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

...
...

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

Hi Becker,

.. you still struggling with those Join nodes .. hehe .. it thought you were an expert by now :wink:
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

1 Like

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

but i meant

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)

[{"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}]
1 Like

oh :scream:

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

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

aha .. i see ..that full version of the flow is gonna take a little more time .. but I dont think you have many options :wink: 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

It will take longer as 15min !

Can you tell me please another problem with a formula:

Version1:

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

Version2:

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

But why ?

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

1 Like

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

very similar in :uk: "can't see the wood for the trees"

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