# Compare of two values (temperatures)

**URL:** <https://discourse.nodered.org/t/compare-of-two-values-temperatures/79597>\
**Category:** General\
**Created:** [5 July 2023 07:40 UTC](https://discourse.nodered.org/t/compare-of-two-values-temperatures/79597 "2023-07-05T07:40:33Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![schmarra](https://avatars.discourse-cdn.com/v4/letter/s/bb73d2/32.png) [@schmarra](https://discourse.nodered.org/u/schmarra)\
**Post date:** [5 July 2023 07:40 UTC](https://discourse.nodered.org/t/compare-of-two-values-temperatures/79597/1 "2023-07-05T07:40:34Z")

</div>

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

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

```auto
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

```

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [5 July 2023 10:15 UTC](https://discourse.nodered.org/t/compare-of-two-values-temperatures/79597/2 "2023-07-05T10:15:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [5 July 2023 10:24 UTC](https://discourse.nodered.org/t/compare-of-two-values-temperatures/79597/3 "2023-07-05T10:24:17Z")

</div>

Lets look at your `function` code:

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

---

<div class="post-metadata">

**Author:** ![schmarra](https://avatars.discourse-cdn.com/v4/letter/s/bb73d2/32.png) [@schmarra](https://discourse.nodered.org/u/schmarra)\
**Post date:** [5 July 2023 11:11 UTC](https://discourse.nodered.org/t/compare-of-two-values-temperatures/79597/4 "2023-07-05T11:11:22Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/5/8573b5aad4ec5cfffe8b15fd335559c1efaa36c6.png)

Could it be about the temperature data format?

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [5 July 2023 12:24 UTC](https://discourse.nodered.org/t/compare-of-two-values-temperatures/79597/5 "2023-07-05T12:24:25Z")

</div>

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:

```auto
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

```

---

<div class="post-metadata">

**Author:** ![jodelkoenig](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jodelkoenig/32/44645_2.png) [@jodelkoenig](https://discourse.nodered.org/u/jodelkoenig)\
**Post date:** [5 July 2023 12:37 UTC](https://discourse.nodered.org/t/compare-of-two-values-temperatures/79597/6 "2023-07-05T12:37:10Z")

</div>

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

---

<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:** [6 July 2023 06:22 UTC](https://discourse.nodered.org/t/compare-of-two-values-temperatures/79597/7 "2023-07-06T06:22:45Z")

</div>

> [@jodelkoenig](#):
>
> I wasn't really aware of how to achieve this

For future readers: [Writing Functions : Node-RED](https://nodered.org/docs/user-guide/writing-functions#logging-events)

---

<div class="post-metadata">

**Author:** ![jodelkoenig](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jodelkoenig/32/44645_2.png) [@jodelkoenig](https://discourse.nodered.org/u/jodelkoenig)\
**Post date:** [6 July 2023 07:23 UTC](https://discourse.nodered.org/t/compare-of-two-values-temperatures/79597/8 "2023-07-06T07:23:57Z")

</div>

I was in particular referring to "node.warn" ... 🙂

---

<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:** [6 July 2023 07:26 UTC](https://discourse.nodered.org/t/compare-of-two-values-temperatures/79597/9 "2023-07-06T07:26:44Z")

</div>

& why I linked to that paragraph 🙂

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.

```auto
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:

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

---

<div class="post-metadata">

**Author:** ![schmarra](https://avatars.discourse-cdn.com/v4/letter/s/bb73d2/32.png) [@schmarra](https://discourse.nodered.org/u/schmarra)\
**Post date:** [6 July 2023 12:53 UTC](https://discourse.nodered.org/t/compare-of-two-values-temperatures/79597/10 "2023-07-06T12:53:08Z")

</div>

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

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

---

<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:** [20 July 2023 12:53 UTC](https://discourse.nodered.org/t/compare-of-two-values-temperatures/79597/11 "2023-07-20T12:53:48Z")

</div>

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