Compare of two values (temperatures)

Hello,

I´m trying to compare two temperature values via function.
Unfortunately the result is wrong and I have no clue which mistake I did

The result is always 0. I would be glad if you can point at my mistake.

Thanks in advance

[
    {
        "id": "1f3aaf64d6d89107",
        "type": "ioBroker in",
        "z": "e7ed6021e07b8f49",
        "name": "Outdoor air °C",
        "topic": "valloxmv.0.A_CYC_TEMP_OUTDOOR_AIR",
        "payloadType": "value",
        "onlyack": "",
        "func": "all",
        "gap": "",
        "fireOnStart": "true",
        "outFormat": "MQTT",
        "x": 960,
        "y": 520,
        "wires": [
            [
                "4d84e2a6b189976e"
            ]
        ]
    },
    {
        "id": "207a224b46be658d",
        "type": "ioBroker in",
        "z": "e7ed6021e07b8f49",
        "name": "Extract air °C",
        "topic": "valloxmv.0.A_CYC_TEMP_EXTRACT_AIR",
        "payloadType": "value",
        "onlyack": "",
        "func": "all",
        "gap": "",
        "fireOnStart": "true",
        "outFormat": "MQTT",
        "x": 970,
        "y": 440,
        "wires": [
            [
                "4d84e2a6b189976e"
            ]
        ]
    },
    {
        "id": "4d84e2a6b189976e",
        "type": "join",
        "z": "e7ed6021e07b8f49",
        "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": 1210,
        "y": 460,
        "wires": [
            [
                "0085ff6df7522a24",
                "62da71443895c02b"
            ]
        ]
    },
    {
        "id": "0085ff6df7522a24",
        "type": "function",
        "z": "e7ed6021e07b8f49",
        "name": "function 3",
        "func": "var outdoor = msg.payload[\"valloxmv/0/A_CYC_TEMP_OUTDOOR_AIR\"] + 3;\nvar extract = msg.payload[\"valloxmv/0/A_CYC_TEMP_EXTRACT_AIR\"];\n\nif (outdoor <= extract) {\n    msg.payload = 1;\n}\nif (outdoor > extract) {\n    msg.payload = 2;\n}\nelse {\n    msg.payload = 0;\n}\nreturn msg",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1480,
        "y": 460,
        "wires": [
            [
                "236315c5ea9435ed"
            ]
        ]
    },
    {
        "id": "62da71443895c02b",
        "type": "debug",
        "z": "e7ed6021e07b8f49",
        "name": "debug 2",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1300,
        "y": 300,
        "wires": []
    },
    {
        "id": "236315c5ea9435ed",
        "type": "debug",
        "z": "e7ed6021e07b8f49",
        "name": "debug 4",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1720,
        "y": 460,
        "wires": []
    }
]

Function code:

var outdoor = msg.payload["valloxmv/0/A_CYC_TEMP_OUTDOOR_AIR"] + 3;
var extract = msg.payload["valloxmv/0/A_CYC_TEMP_EXTRACT_AIR"];

if (outdoor <= extract) {
    msg.payload = 1;
}
else if (outdoor > extract) {
    msg.payload = 2;
}
else {
    msg.payload = 0;
}
return msg

Please post function node code separately so that people don't have to load the flow just to see the function code. It should mean that you get more people helping. Thanks.

Lets look at your function code:

var outdoor = msg.payload["valloxmv/0/A_CYC_TEMP_OUTDOOR_AIR"] + 3;
var extract = msg.payload["valloxmv/0/A_CYC_TEMP_EXTRACT_AIR"];

if (outdoor <= extract) {
    msg.payload = 1;
}
if (outdoor > extract) {
    msg.payload = 2;
}
else {
    msg.payload = 0;
}
return msg

So you first test if outdoor <= extract. If outdoor is 5, and extract is 10, the statement would be true so msg.payload would be 1. Then you have an if/else and you check to see if if outdoor > extract and (in this case) it is not, so the else is executed and msg.payload is set to 0

If outdoor is 10 and extract is 5 the first if would be false so nothing is done. Then comes the if/else
since outdoor (10) is greater than extract (5) msg.payload is set to 2 and that is the end

You probably want the second if to be an `else if'.

But you should look at the output of the debug 2 node to see what the actual data is.

Thank you for the feedback.
I already had only the first condition (if) and finally the else - but the result was the same - always 0. Meanwhile I changed the second if to "else if"

Result of debug 2 & 4:
grafik

Could it be about the temperature data format?

This is where you start debugging your function node. First thing to do is see if the incoming data is what you expect. The way you can do that is to use node.warn() in the function node. What I would do is add several statements in with a string 'debugN' were N is a number so you can tell which node.ward you are using.For example:

var outdoor = msg.payload["valloxmv/0/A_CYC_TEMP_OUTDOOR_AIR"] + 3;
var extract = msg.payload["valloxmv/0/A_CYC_TEMP_EXTRACT_AIR"];
node.warn('debug1= ' + outdoor)
node.warn('debug2= ' + extract)
if (outdoor <= extract) {
    msg.payload = 1;
}
node.warn('debug3= ' + msg.payload)
if (outdoor > extract) {
    msg.payload = 2;
    node.warn('debug4= ' + msg.payload)
}
else {
    msg.payload = 0;
    node.warn('debug5= '+ msg.payload)
}
return msg
3 Likes

I wasn't really aware of how to achieve this. So thanks a lot :slight_smile:

For future readers: Writing Functions : Node-RED

1 Like

I was in particular referring to "node.warn" ... :slight_smile:

1 Like

& why I linked to that paragraph :slight_smile:

Additional info for future readers.

node.warn can be far more effective if you send arrays or objects.

Additionally, setting extra properties in the msg object will help you understand what is going on

e.g.

const outdoor = msg.payload["valloxmv/0/A_CYC_TEMP_OUTDOOR_AIR"] + 3;
const extract = msg.payload["valloxmv/0/A_CYC_TEMP_EXTRACT_AIR"];

msg.input_payload = msg.payload  // put a copy in msg - for later viewing
msg.outdoor = outdoor  // put a copy in msg - for later viewing
msg.extract = extract  // put a copy in msg - for later viewing

if (outdoor <= extract) {
    msg.payload = 1;
    node.warn ({
      outdoor: outdoor,
      extract: extract,
      desc: " outdoor is <= extract so setting msg.payload = 1"
    })
}

if (outdoor > extract) {
    msg.payload = 2;
    node.warn ({
      outdoor: outdoor,
      extract: extract,
      desc: " outdoor is > extract so setting msg.payload = 2"
    })
}
else {
    msg.payload = 0;
    node.warn ({
      outdoor: outdoor,
      extract: extract,
      desc: "outdoor is NOT > extract is, so setting msg.payload = 0"
    })
}

return msg

This will give you much clearer picture of what is happening:

3 Likes

Thank you very much.
Something strange happend:
I needed to update node-red, after doing so the function been working as expected :crazy_face:

Nevertheless I´ve learned the topic about node.warn - which I was not aware about

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