# Function: division by 0

**URL:** <https://discourse.nodered.org/t/function-division-by-0/47365>\
**Category:** General\
**Created:** [19 June 2021 12:20 UTC](https://discourse.nodered.org/t/function-division-by-0/47365 "2021-06-19T12:20:16Z")\
**Posts on this page:** 12\
**Page:** 2

<div class="post-metadata">

**Author:** ![Crashy77](https://avatars.discourse-cdn.com/v4/letter/c/bcef8e/32.png) [@Crashy77](https://discourse.nodered.org/u/Crashy77)\
**Post date:** [20 June 2021 10:37 UTC](https://discourse.nodered.org/t/function-division-by-0/47365/21 "2021-06-20T10:37:05Z")

</div>

Ok, the fault seems to be the variable

```auto
var HP = msg.payload.HP;
var HC = msg.payload.HC;
var COP = 0;
if(CC !=0) { COP = (HP / HC); } 
msg.payload = COP.;
msg.topic = "Heat_COP";
return msg;

```

Because if I´m chanigng them to numbers,

```auto
var HP = 2;
var HC = 4;
var COP = 0;
if(HC !=0) { COP = (HP / HC); } 
msg.payload = COP;
msg.topic = "Heat_COP";
return msg;

```

then it works. What´s wrong with the declaration ?  
The function before is

```auto
msg.payload = Number(msg.payload);
msg.topic = "HP";
return msg;

```

---

<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:** [20 June 2021 10:39 UTC](https://discourse.nodered.org/t/function-division-by-0/47365/22 "2021-06-20T10:39:48Z")

</div>

> [@Crashy77](#):
>
> `msg.payload = COP.;`

↑ delete the dot from the end of `COP.;`

As I said before,

> [@Steve-Mcl](#):
>
> paste the updated flow (just the relevant parts)
> 
> AND
> 
> capture the MQTT message that feeds into it by doing this...
> 
> 1. add a debug to the output of the MQTT IN
> 2. set the debug to show complete message
> 3. press the `copy value` button that appears under your mouse when you hover on thew debug message object  
> ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/d/edaf159c628b3374eb979f10147db6571982c1d3.png)
> 4. paste the copied data into a reply with your flow

And I'' tell you what is wrong.

---

<div class="post-metadata">

**Author:** ![Crashy77](https://avatars.discourse-cdn.com/v4/letter/c/bcef8e/32.png) [@Crashy77](https://discourse.nodered.org/u/Crashy77)\
**Post date:** [20 June 2021 10:49 UTC](https://discourse.nodered.org/t/function-division-by-0/47365/23 "2021-06-20T10:49:30Z")

</div>

The dot was just a fault of copying. In my function is no dot..  
Can´t debug the output, because there´re hundreds of strings/payload listed seperately.

```auto
[
    {
        "id": "4f2c76dd.27c688",
        "type": "switch",
        "z": "b9af3883.9c8698",
        "name": "Heat_Energy_Production",
        "property": "topic",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "jeisha/main/Heat_Energy_Production",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 1,
        "x": 490,
        "y": 180,
        "wires": [
            [
                "271d52fe.9431de"
            ]
        ]
    },
    {
        "id": "32bb0252.2f1c1e",
        "type": "switch",
        "z": "b9af3883.9c8698",
        "name": "Heat_Energy_Consumption",
        "property": "topic",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "jeisha/main/Heat_Energy_Consumption",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 1,
        "x": 500,
        "y": 220,
        "wires": [
            [
                "2358c94b.8214b6"
            ]
        ]
    },
    {
        "id": "271d52fe.9431de",
        "type": "function",
        "z": "b9af3883.9c8698",
        "name": "HP",
        "func": "msg.payload = Number(msg.payload);\nmsg.topic = \"HP\";\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 710,
        "y": 180,
        "wires": [
            [
                "b75e6986.331248",
                "cdc8af37.342c6"
            ]
        ]
    },
    {
        "id": "2358c94b.8214b6",
        "type": "function",
        "z": "b9af3883.9c8698",
        "name": "HC",
        "func": "msg.payload = Number(msg.payload);\nmsg.topic = \"HC\";\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 710,
        "y": 220,
        "wires": [
            [
                "cdc8af37.342c6"
            ]
        ]
    },
    {
        "id": "cdc8af37.342c6",
        "type": "function",
        "z": "b9af3883.9c8698",
        "name": "Heat_COP",
        "func": "var HP = msg.payload.HP;\nvar HC = msg.payload.HC;\nvar COP = 0;\nif(HC !=0) { COP = (HP / HC); } \nmsg.payload = COP;\nmsg.topic = \"Heat_COP\";\nreturn msg;\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 930,
        "y": 200,
        "wires": [
            [
                "baaa9fa1.f7d5e"
            ]
        ]
    },
    {
        "id": "baaa9fa1.f7d5e",
        "type": "debug",
        "z": "b9af3883.9c8698",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 1150,
        "y": 140,
        "wires": []
    },
    {
        "id": "b75e6986.331248",
        "type": "debug",
        "z": "b9af3883.9c8698",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 880,
        "y": 80,
        "wires": []
    }
]

```

Result of Heat\_COP is NaN.

---

<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:** [20 June 2021 11:01 UTC](https://discourse.nodered.org/t/function-division-by-0/47365/24 "2021-06-20T11:01:52Z")

</div>

> [@Crashy77](#):
>
> Result of Heat\_COP is NaN.

because you have deleted the JOIN node. Now only individual values arrive in the Heat\_COP function one at a time (not together in the same message).

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

  

* * *

Here is proof it can work...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/a/5afc74e0b6c44698110f20b392fee4e780b55732.png)

```auto
[{"id":"a86237e4da497c79","type":"inject","z":"553814a2.1248ec","name":"fake MQTT - eat_Energy_Production \"123\"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"jeisha/main/Heat_Energy_Production","payload":"123","payloadType":"str","x":1700,"y":640,"wires":[["4f2c76dd.27c688"]]},{"id":"4f2c76dd.27c688","type":"switch","z":"553814a2.1248ec","name":"Heat_Energy_Production","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"jeisha/main/Heat_Energy_Production","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":2010,"y":640,"wires":[["271d52fe.9431de"]]},{"id":"271d52fe.9431de","type":"function","z":"553814a2.1248ec","name":"HP","func":"msg.payload = Number(msg.payload);\nmsg.topic = \"HP\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2230,"y":640,"wires":[["b75e6986.331248","18087c189f7be93b"]]},{"id":"b75e6986.331248","type":"debug","z":"553814a2.1248ec","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":2390,"y":580,"wires":[]},{"id":"18087c189f7be93b","type":"join","z":"553814a2.1248ec","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":2390,"y":660,"wires":[["cdc8af37.342c6"]]},{"id":"2358c94b.8214b6","type":"function","z":"553814a2.1248ec","name":"HC","func":"msg.payload = Number(msg.payload);\nmsg.topic = \"HC\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2230,"y":680,"wires":[["18087c189f7be93b","f9509429a14d6fb8"]]},{"id":"cdc8af37.342c6","type":"function","z":"553814a2.1248ec","name":"Heat_COP","func":"var HP = msg.payload.HP;\nvar HC = msg.payload.HC;\nvar COP = 0;\nif(HC !=0) { COP = (HP / HC); } \nmsg.payload = COP;\nmsg.topic = \"Heat_COP\";\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2550,"y":660,"wires":[["baaa9fa1.f7d5e"]]},{"id":"32bb0252.2f1c1e","type":"switch","z":"553814a2.1248ec","name":"Heat_Energy_Consumption","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"jeisha/main/Heat_Energy_Consumption","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":2020,"y":680,"wires":[["2358c94b.8214b6"]]},{"id":"f9509429a14d6fb8","type":"debug","z":"553814a2.1248ec","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":2390,"y":720,"wires":[]},{"id":"baaa9fa1.f7d5e","type":"debug","z":"553814a2.1248ec","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":2670,"y":600,"wires":[]},{"id":"791e49c2b8009bf7","type":"inject","z":"553814a2.1248ec","name":"fake MQTT - Heat_Energy_Consumption \"456\"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"jeisha/main/Heat_Energy_Consumption","payload":"456","payloadType":"str","x":1700,"y":680,"wires":[["32bb0252.2f1c1e"]]}]

```

and here is a slightly condensed version...

```auto
[{"id":"271d52fe.9431de","type":"function","z":"553814a2.1248ec","name":"HP","func":"const expectedTopic = \"jeisha/main/Heat_Energy_Production\"\n\nif (msg.topic === expectedTopic) {\n msg.payload = Number(msg.payload);\n node.status(msg.payload);\n msg.topic = \"HP\";\n return msg;\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1970,"y":620,"wires":[["18087c189f7be93b"]]},{"id":"2358c94b.8214b6","type":"function","z":"553814a2.1248ec","name":"HC","func":"const expectedTopic = \"jeisha/main/Heat_Energy_Consumption\"\nconst newTopic = \"HC\"\n\nif (msg.topic === expectedTopic) {\n msg.payload = Number(msg.payload);\n node.status(msg.payload);\n msg.topic = newTopic;\n return msg;\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1970,"y":680,"wires":[["18087c189f7be93b"]]},{"id":"cdc8af37.342c6","type":"function","z":"553814a2.1248ec","name":"Heat_COP","func":"var HP = msg.payload.HP;\nvar HC = msg.payload.HC;\nvar COP = 0;\nif(HC !=0) { COP = (HP / HC); } \nmsg.payload = COP;\nnode.status(msg.payload);\nmsg.topic = \"Heat_COP\";\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2270,"y":620,"wires":[["baaa9fa1.f7d5e"]]},{"id":"baaa9fa1.f7d5e","type":"debug","z":"553814a2.1248ec","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":2430,"y":620,"wires":[]},{"id":"a86237e4da497c79","type":"inject","z":"553814a2.1248ec","name":"fake MQTT - eat_Energy_Production \"123\"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"jeisha/main/Heat_Energy_Production","payload":"123","payloadType":"str","x":1700,"y":620,"wires":[["271d52fe.9431de"]]},{"id":"791e49c2b8009bf7","type":"inject","z":"553814a2.1248ec","name":"fake MQTT - Heat_Energy_Consumption \"456\"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"jeisha/main/Heat_Energy_Consumption","payload":"456","payloadType":"str","x":1720,"y":680,"wires":[["2358c94b.8214b6"]]},{"id":"18087c189f7be93b","type":"join","z":"553814a2.1248ec","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":2130,"y":620,"wires":[["cdc8af37.342c6"]]}]

```

---

<div class="post-metadata">

**Author:** ![Crashy77](https://avatars.discourse-cdn.com/v4/letter/c/bcef8e/32.png) [@Crashy77](https://discourse.nodered.org/u/Crashy77)\
**Post date:** [20 June 2021 11:20 UTC](https://discourse.nodered.org/t/function-division-by-0/47365/25 "2021-06-20T11:20:11Z")

</div>

the join was gone, because Colin questioned that.  
I thought it would be possible without join.  
Now all values are numbers, but I get this fault:

```auto
HttpError: 400 Bad Request : {"error":"partial write: field type conflict: input field \"CC\" on measurement \"Aquarea\" is type float, already exists as type string dropped=1"}

```

I´ve delete the measurement, but it´s still appearing after show measurements..

---

<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:** [20 June 2021 11:25 UTC](https://discourse.nodered.org/t/function-division-by-0/47365/26 "2021-06-20T11:25:41Z")

</div>

It is quite self explanatory "measurement "Aquarea" is type float, already exists as type string"

In essence, you were writing as a string - now you are writing as float.

If you dont need the old data, delete it & it will be fixed.

Otherwise, search [here](https://www.google.com/search?q=influxdb+already+exists+as+type+string) for a solution.

---

<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:** [20 June 2021 11:28 UTC](https://discourse.nodered.org/t/function-division-by-0/47365/27 "2021-06-20T11:28:05Z")

</div>

> [@Crashy77](#):
>
> the join was gone, because Colin questioned that.

Yes, if you were sending individual measured (the CC and CP values) - you did NOT need to join them.

But since you are still trying to calculate COP up front, you will need them joind together so you can divide one with the other,

As I said before, it depends on what you want / need.

If you MUST have the computed value stored along side the raw values - then do it. Colins suggestion (and the link i posted) say it is better to store the raw data measures (and therefore you would not need to JOIN or compute the COP value).

---

<div class="post-metadata">

**Author:** ![Crashy77](https://avatars.discourse-cdn.com/v4/letter/c/bcef8e/32.png) [@Crashy77](https://discourse.nodered.org/u/Crashy77)\
**Post date:** [20 June 2021 11:59 UTC](https://discourse.nodered.org/t/function-division-by-0/47365/28 "2021-06-20T11:59:12Z")

</div>

Ok, I´ve fixed it. The fault was, that the measurement was older than 48hours.  
Now everything is working and I´m having my lines.  
Thanks you very much for your help and patience.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [20 June 2021 13:06 UTC](https://discourse.nodered.org/t/function-division-by-0/47365/29 "2021-06-20T13:06:59Z")

</div>

As a matter of interest, what are HC, HP, CC and CP in the real world?

---

<div class="post-metadata">

**Author:** ![Crashy77](https://avatars.discourse-cdn.com/v4/letter/c/bcef8e/32.png) [@Crashy77](https://discourse.nodered.org/u/Crashy77)\
**Post date:** [20 June 2021 13:53 UTC](https://discourse.nodered.org/t/function-division-by-0/47365/30 "2021-06-20T13:53:52Z")

</div>

HC / HP ... heat energy consumption / production  
CC / CP ... cool energy consumption / production  
DHWC / DHWP ... water energy consumption / production  
COP is the "coefficient of performance"

I´m controlling and analyzing my heat pump.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [20 June 2021 14:42 UTC](https://discourse.nodered.org/t/function-division-by-0/47365/31 "2021-06-20T14:42:09Z")

</div>

That makes sense. Thanks.

---

<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:** [19 August 2021 14:43 UTC](https://discourse.nodered.org/t/function-division-by-0/47365/32 "2021-08-19T14:43:02Z")

</div>

Closed on 19 August 2021 14:43 UTC

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

[Previous page](https://discourse.nodered.org/t/function-division-by-0/47365.md?page=1)
