# Calculate unbalanced load

**URL:** <https://discourse.nodered.org/t/calculate-unbalanced-load/50110>\
**Category:** General\
**Created:** [24 August 2021 11:48 UTC](https://discourse.nodered.org/t/calculate-unbalanced-load/50110 "2021-08-24T11:48:45Z")\
**Posts on this page:** 3\
**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:** [24 August 2021 11:48 UTC](https://discourse.nodered.org/t/calculate-unbalanced-load/50110/1 "2021-08-24T11:48:45Z")

</div>

Hi,

I´ve made a function which is calculating the "unbalanced load" from grid ("Schieflast").  
but in the case, when 2 values are the same I get "0".  
example:  
I1 = 12A  
I2 = -15A  
I3 = -15A

```auto
[
    {
        "id": "bc97dbc59e7fba8b",
        "type": "inject",
        "z": "9f6752b0.b55988",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "{\"I1\":12,\"I2\":-15,\"I3\":-15}",
        "payloadType": "json",
        "x": 450,
        "y": 1500,
        "wires": [
            [
                "b1cf5dccc16d85e3",
                "88d1c3acdc0c6f43"
            ]
        ]
    },
    {
        "id": "b1cf5dccc16d85e3",
        "type": "function",
        "z": "9f6752b0.b55988",
        "name": "Schieflast",
        "func": "var I1 = msg.payload.I1;\nvar I2 = msg.payload.I2;\nvar I3 = msg.payload.I3;\nvar Ia = I1-I2;\nvar Ib = I1-I3;\nvar Ic = I2-I3;\nvar Iaa = Math.abs(Ia);\nvar Ibb = Math.abs(Ib);\nvar Icc = Math.abs(Ic);\nif (Iaa > Ibb && Icc ) {\n I = Iaa;\n} \nelse if (Ibb > Iaa && Icc) {\n I = Ibb;\n}\nelse if (Icc > Iaa && Ibb) {\n I = Icc;\n}\nmsg.payload = I\nmsg.payload = msg.payload.toFixed(1).replace('.', ',');\nmsg.topic = \"Schieflast\";\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 840,
        "y": 1540,
        "wires": [
            [
                "88d1c3acdc0c6f43"
            ]
        ]
    },
    {
        "id": "88d1c3acdc0c6f43",
        "type": "debug",
        "z": "9f6752b0.b55988",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 1380,
        "y": 1560,
        "wires": []
    }
]

```

with "or" ( || ) I get 0

🤨

maybe there is a simple function in JS for this case 🧐

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [24 August 2021 12:59 UTC](https://discourse.nodered.org/t/calculate-unbalanced-load/50110/2 "2021-08-24T12:59:21Z")

</div>

```auto
if (Iaa > Ibb && Icc ) 

```

should be

```auto
if (Iaa > Ibb && Iaa > Icc )  

```

But if you require the max differnce in load use Math.max()

```auto
var I1 = msg.payload.I1;
var I2 = msg.payload.I2;
var I3 = msg.payload.I3;
var max = Math.max(I1,I2,I3);
var Ia = max - I1;
var Ib = max - I2;
var Ic = max - I3;
I = Math.max(Ia,Ib,Ic);
msg.payload = I
msg.payload = msg.payload.toFixed(1).replace('.', ',');
msg.topic = "Schieflast";
return msg;

```

---

<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 September 2021 12:59 UTC](https://discourse.nodered.org/t/calculate-unbalanced-load/50110/3 "2021-09-07T12:59:37Z")

</div>

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