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

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

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.

I would have used if/else statements;

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;

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

I would have thought that this is easier to read?

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.

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;

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

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.

-- ignore --

That took more than the 1 minute I gave the problem :slight_smile:

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

1 Like

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.