Function: division by 0

Ok, the fault seems to be the variable

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,

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

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

↑ delete the dot from the end of COP.;

As I said before,

And I'' tell you what is wrong.

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.

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

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



Here is proof it can work...

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

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

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:

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..

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 for a solution.

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

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.

1 Like

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

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.

That makes sense. Thanks.

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