Compare one variable between two values in function

I want to compare a variable from the message two set values ​​in a function but I can't describe it (I'm bad with the syntax) I imagine it like this:

if (msg.payload.current_value >= 5 && msg.payload.current_value =< 10) {
    msg.payload = "high;}
    else msg.payload = "low";
return msg;

Is this correct?

Hi @devifast

Use the already provided Nodes :smile:

[{"id":"c2c09c2b70fcbd89","type":"inject","z":"2d7bf6e3.84c97a","name":"Hgh","props":[{"p":"payload.current_value","v":"6","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":1250,"y":380,"wires":[["156b5bbf15a56727"]]},{"id":"156b5bbf15a56727","type":"switch","z":"2d7bf6e3.84c97a","name":"","property":"payload.current_value","propertyType":"msg","rules":[{"t":"btwn","v":"5","vt":"num","v2":"10","v2t":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":1410,"y":420,"wires":[["5b09369544891d85"],["5837258fc297d9cb"]]},{"id":"5b09369544891d85","type":"change","z":"2d7bf6e3.84c97a","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"high","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1600,"y":380,"wires":[["e09cf838760d0c3b"]]},{"id":"5837258fc297d9cb","type":"change","z":"2d7bf6e3.84c97a","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"low","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1600,"y":420,"wires":[["e09cf838760d0c3b"]]},{"id":"aafe0b4487fe833a","type":"inject","z":"2d7bf6e3.84c97a","name":"Low","props":[{"p":"payload.current_value","v":"4","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":1250,"y":440,"wires":[["156b5bbf15a56727"]]},{"id":"e09cf838760d0c3b","type":"debug","z":"2d7bf6e3.84c97a","name":"debug 384","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1770,"y":400,"wires":[]}]

For comparisons and setting values, the switch and change nodes are all you need.

1 Like

Awesome! But how to describe it in a function. I know your solution works, but I'm trying to do things in a function. it's neat and I want to learn.

1 Like
if(msg.payload.current_value >= 5 && msg.payload.current_value <= 10){
    return {payload:'high'}
} else {
    return {payload:'low'}
}

OR

if(msg.payload.current_value >= 5 && msg.payload.current_value <= 10){
    msg.payload = 'high'
} else {
    msg.payload = 'low'
}

return msg

everything being sent is wrapped in an object called msg

We don't hear this often enough (wanting to learn instead of just copy and pasting ): :smiling_face:

@devifast This is the preferred method of the 2 proposed as it returns the original message & therefore retains any other properties the message needs for processing in downstream nodes.

1 Like

Did not understand? Maybe you mean that they want ready-made solutions from you? I know how it is with two messages, but here it is about the same message. comparators are something like <=, >=, ==, etc. I don't know them all by heart and that's why I ask people like you. by the way, it doesn't work the way you sent me. the idea is that I just want to describe it in a single line of function. otherwise there is a solution above. Greetings.

You haven't asked for a solution, but instead getting feedback on the attempt/willing to be corrected.

Usually its "Do this please" - so its credit to you.

1 Like

How can I solve this miracle?

The above in a function node will do what you want (if we have your quest right)
but are you simply trying to compress this into 1 line?

Yes that's right, that's exactly what I'm trying to do right now but to no avail. here is the current code.

var value = msg.payload.current_value;
if(value >= 5)
{
msg.payload = "high";} 
else if (value <= 10) 
{
msg.payload = "low";
}
else msg.payload = null;
msg.payload = "test";
return msg
[
    {
        "id": "94220ac1144d4fcb",
        "type": "tab",
        "label": "Flow 2",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "d960b2163cbb35ca",
        "type": "inject",
        "z": "94220ac1144d4fcb",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "current_value",
        "payload": "11",
        "payloadType": "num",
        "x": 170,
        "y": 160,
        "wires": [
            [
                "af617717be62be9e"
            ]
        ]
    },
    {
        "id": "3bbc14546fee0417",
        "type": "inject",
        "z": "94220ac1144d4fcb",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "current_value",
        "payload": "7",
        "payloadType": "num",
        "x": 160,
        "y": 110,
        "wires": [
            [
                "af617717be62be9e"
            ]
        ]
    },
    {
        "id": "3a370cc26a953786",
        "type": "inject",
        "z": "94220ac1144d4fcb",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "current_value",
        "payload": "4",
        "payloadType": "num",
        "x": 160,
        "y": 220,
        "wires": [
            [
                "af617717be62be9e"
            ]
        ]
    },
    {
        "id": "eb423a07871d86f8",
        "type": "function",
        "z": "94220ac1144d4fcb",
        "name": "function 1",
        "func": "if(msg.payload.current_value >= 5 && msg.payload.current_value <= 10){\n    msg.payload = \"high\"\n} else {\n    msg.payload = \"low\"\n}\n\nreturn msg",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 390,
        "y": 110,
        "wires": [
            [
                "c6465614c70fb548"
            ]
        ]
    },
    {
        "id": "c6465614c70fb548",
        "type": "debug",
        "z": "94220ac1144d4fcb",
        "name": "debug 15",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 500,
        "y": 170,
        "wires": []
    },
    {
        "id": "85879627632084ef",
        "type": "function",
        "z": "94220ac1144d4fcb",
        "name": "function 2",
        "func": "var value = msg.payload.current_value;\nif(value >= 5)\n{\nmsg.payload = \"high\";} \nelse if (value <= 10) \n{\nmsg.payload = \"low\";\n}\nelse msg.payload = null;\nmsg.payload = \"test\";\nreturn msg",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 370,
        "y": 250,
        "wires": [
            [
                "a67704c543b711db"
            ]
        ]
    },
    {
        "id": "a67704c543b711db",
        "type": "debug",
        "z": "94220ac1144d4fcb",
        "name": "debug 16",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 500,
        "y": 260,
        "wires": []
    },
    {
        "id": "3701aa908755686b",
        "type": "debug",
        "z": "94220ac1144d4fcb",
        "name": "debug 17",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 490,
        "y": 380,
        "wires": []
    },
    {
        "id": "af617717be62be9e",
        "type": "function",
        "z": "94220ac1144d4fcb",
        "name": "function 3",
        "func": "if(msg.payload.current_value >= 5 && msg.payload.current_value <= 10){\n    msg.payload = 'high'\n} else {\n    msg.payload = 'low'\n}\n\nreturn msg",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 350,
        "y": 380,
        "wires": [
            [
                "3701aa908755686b"
            ]
        ]
    }
]

still not sure I get :crazy_face:

Ignoring the logic for a minute

you want

line 
line 
line 

to

line

If the value is outside the limits between 5 and 10 false, if it is within these values ​​true.

Lets try..

/* Set Initial value */
let result = false

/* Alter it based on condition */
if(msg.payload.current_value >= 5 && msg.payload.current_value <= 10){
   result = true
} 

msg.payload = result

return msg

i.e set to true IF ONLY in the range

On one line, something like

msg.payload = (msg.payload >= 5 && msg.payload <= 10);
return msg;

Or

return {payload: (msg.payload >= 5 && msg.payload <= 10)};
2 Likes

it was there the whole time :joy:

1 Like

Thanks for the elegant solution. That was what it was about.

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