# Less than does not work but greater than works. Any assistance will be appreciated

**URL:** <https://discourse.nodered.org/t/less-than-does-not-work-but-greater-than-works-any-assistance-will-be-appreciated/68731>\
**Category:** General\
**Created:** [8 October 2022 19:11 UTC](https://discourse.nodered.org/t/less-than-does-not-work-but-greater-than-works-any-assistance-will-be-appreciated/68731 "2022-10-08T19:11:34Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andries](https://avatars.discourse-cdn.com/v4/letter/a/e56c9b/32.png) [@Andries](https://discourse.nodered.org/u/Andries)\
**Post date:** [8 October 2022 19:11 UTC](https://discourse.nodered.org/t/less-than-does-not-work-but-greater-than-works-any-assistance-will-be-appreciated/68731/1 "2022-10-08T19:11:34Z")

</div>

```auto
[
    {
        "id": "e23c99f9e5c74834",
        "type": "function",
        "z": "c3e2344d2de8acfd",
        "name": "if Fan Machine",
        "func": "node.status({fill:\"blue\",shape:\"dot\",text:msg.payload});\n\n var day = msg.payload.day;\n var mains = msg.payload.mains;\n var voltage = msg.payload.voltage;\n var manual = msg.payload.manual;\n var charge = msg.payload.charge;\n\n if (day == true && manual == false && charge >= 7){msg = {payload: \"FANON\"}\n} \n if (day == true && manual == false && charge <= 6){msg = {payload: \"FANOFF\"}\n} \n if (day == true && manual == true && charge >= 7){msg = {payload: \"FANON\"}\n} \n if (day == true && manual == true && charge <= 6){msg = {payload: \"FANOFF\"}\n}\nif (day == false && manual == true && charge >= 7){msg = {payload: \"FANON\"}\n} \nif (day == false && manual == true && charge <= 6){msg = {payload: \"FANOFF\"}\n} \nif (day == false && manual == false){msg = {payload: \"FANOFF\"}\n} \n\n\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 320,
        "y": 320,
        "wires": [
            [
                "49ab83b71a1a7818",
                "3503d94bb48cf6d4"
            ]
        ]
    }
]

```

---

<div class="post-metadata">

**Author:** ![Andries](https://avatars.discourse-cdn.com/v4/letter/a/e56c9b/32.png) [@Andries](https://discourse.nodered.org/u/Andries)\
**Post date:** [8 October 2022 19:25 UTC](https://discourse.nodered.org/t/less-than-does-not-work-but-greater-than-works-any-assistance-will-be-appreciated/68731/2 "2022-10-08T19:25:06Z")

</div>

This is my first post. I am typing this for clearer understanding. I am getting a string from MQTT which I changed to a number. If this value is higher than 7 it switches the fan on. Less than 6 it switches the fan off. The higher than part works but as soon as the value goes below 7 but still higher than 6 the fan switches off.  
Thanks.

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [8 October 2022 19:45 UTC](https://discourse.nodered.org/t/less-than-does-not-work-but-greater-than-works-any-assistance-will-be-appreciated/68731/3 "2022-10-08T19:45:12Z")

</div>

I would have used if/else statements;

```auto
if (day == true && manual == false && charge >= 7){msg = {payload: "FANON"}
}  
else if (day == true && manual == false && charge <= 6){msg = {payload: "FANOFF"}
} 
else if (day == true && manual == true && charge >= 7){msg = {payload: "FANON"}
}
...

```

and finished with;

```auto
else if (day == false && manual == true && charge <= 6){msg = {payload: "FANOFF"}
}  
else if (day == false && manual == false){msg = {payload: "FANOFF"}
}
else {return}

return msg

```

So in the event of none of the conditions being matched (such as a value of 6.5), the `else {return}` will stop the flow, instead of the `return msg` sending the original msg which was inputted into the function node.

**[EDIT -]** Corrected typos

---

<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:** [9 October 2022 02:06 UTC](https://discourse.nodered.org/t/less-than-does-not-work-but-greater-than-works-any-assistance-will-be-appreciated/68731/4 "2022-10-09T02:06:12Z")

</div>

> [@Paul-Reed](#):
>
> ```auto
> if (day == true && manual == false && charge >= 7){msg = {payload: "FANON"}
> }  
> else if (day == true && manual == false && charge <= 6){msg = {payload: "FANOFF"}
> } 
> else if (day == true && manual == true && charge >= 7){msg = {payload: "FANON"}
> }
> 
> ```

I would have thought that this is easier to read?

```auto
if (day === true) {
    if (manual === false) {
        if (charge >= 7) {
            msg.payload = "FANON"
        } else if (charge <= 6) {
            msg.payload = "FANOFF"
        }
    } else {
        if (charge >= 7) {
            msg.payload = "FANON"
        }
    }
}

```

Always remembering that you need to write code in a way that you will understand in 12m time.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [9 October 2022 04:51 UTC](https://discourse.nodered.org/t/less-than-does-not-work-but-greater-than-works-any-assistance-will-be-appreciated/68731/5 "2022-10-09T04:51:09Z")

</div>

Correct me if I am wrong, but I think this can be simplified with guard clauses, meaning, return early when condition has been met, no need to evaluate further;

```auto
if (!day && !manual) return { payload: "FANOFF" }
if (charge >= 7) return { payload: "FANON" }
if (charge <= 6) return { payload: "FANOFF" }

```

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [9 October 2022 08:25 UTC](https://discourse.nodered.org/t/less-than-does-not-work-but-greater-than-works-any-assistance-will-be-appreciated/68731/6 "2022-10-09T08:25:55Z")

</div>

> [@TotallyInformation](#):
>
> I would have thought that this is easier to read?

The OP obviously felt comfortable writing the code that way, so I just tweaked what was already written, so that it would work without making wholesale changes.  
But yes agree, there are numerous & better ways to structure it.

---

<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:** [9 October 2022 08:44 UTC](https://discourse.nodered.org/t/less-than-does-not-work-but-greater-than-works-any-assistance-will-be-appreciated/68731/7 "2022-10-09T08:44:48Z")

</div>

-- ignore --

---

<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:** [9 October 2022 16:01 UTC](https://discourse.nodered.org/t/less-than-does-not-work-but-greater-than-works-any-assistance-will-be-appreciated/68731/8 "2022-10-09T16:01:10Z")

</div>

> [@bakman2](#):
>
> Correct me if I am wrong, but I think this can be simplified with guard clauses, meaning, return early when condition has been met, no need to evaluate further;

That took more than the 1 minute I gave the problem 🙂

Better still would be to set a default if possible - if the default was FANOFF, only 1 if statement would be needed.

---

<div class="post-metadata">

**Author:** ![Andries](https://avatars.discourse-cdn.com/v4/letter/a/e56c9b/32.png) [@Andries](https://discourse.nodered.org/u/Andries)\
**Post date:** [11 October 2022 20:24 UTC](https://discourse.nodered.org/t/less-than-does-not-work-but-greater-than-works-any-assistance-will-be-appreciated/68731/9 "2022-10-11T20:24:22Z")

</div>

Good Day All,  
Thanks for the input !  
I will when I have time, try all the suggestions but at the moment I am a quite busy.

Thanks to everyone.

Andries.
