# Compare one variable between two values in function

**URL:** https://discourse.nodered.org/t/compare-one-variable-between-two-values-in-function/88863
**Category:** General
**Created:** [22 June 2024 15:42 UTC](https://discourse.nodered.org/t/compare-one-variable-between-two-values-in-function/88863 "2024-06-22T15:42:39Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![devifast](https://avatars.discourse-cdn.com/v4/letter/d/43a26b/32.png) [@devifast](https://discourse.nodered.org/u/devifast)
#### Post date: [22 June 2024 15:42 UTC](https://discourse.nodered.org/t/compare-one-variable-between-two-values-in-function/88863/1 "2024-06-22T15:42:39Z")

</div>

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:

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

```

Is this correct?

---

<div class="post-metadata">

### Author: ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)
#### Post date: [22 June 2024 15:52 UTC](https://discourse.nodered.org/t/compare-one-variable-between-two-values-in-function/88863/2 "2024-06-22T15:52:23Z")

</div>

Hi @devifast

Use the already provided Nodes 😄

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

```

 ![Screenshot 2024-06-22 at 16.51.53](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/5/85ce2168d34d282c9f2409f016499cacf196b164.png)

 ![Screenshot 2024-06-22 at 16.52.31](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/f/8f71f555d96628becd2d69f0babe81e5934b1233.png)

---

<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: [22 June 2024 15:52 UTC](https://discourse.nodered.org/t/compare-one-variable-between-two-values-in-function/88863/3 "2024-06-22T15:52:41Z")

</div>

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

---

<div class="post-metadata">

### Author: ![devifast](https://avatars.discourse-cdn.com/v4/letter/d/43a26b/32.png) [@devifast](https://discourse.nodered.org/u/devifast)
#### Post date: [22 June 2024 15:57 UTC](https://discourse.nodered.org/t/compare-one-variable-between-two-values-in-function/88863/4 "2024-06-22T15:57:22Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)
#### Post date: [22 June 2024 15:59 UTC](https://discourse.nodered.org/t/compare-one-variable-between-two-values-in-function/88863/5 "2024-06-22T15:59:44Z")

</div>

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

```

OR

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

---

<div class="post-metadata">

### Author: ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)
#### Post date: [22 June 2024 16:05 UTC](https://discourse.nodered.org/t/compare-one-variable-between-two-values-in-function/88863/6 "2024-06-22T16:05:06Z")

</div>

> [@devifast](#):
>
> it's neat and I want to learn.

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

---

<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: [22 June 2024 16:18 UTC](https://discourse.nodered.org/t/compare-one-variable-between-two-values-in-function/88863/7 "2024-06-22T16:18:16Z")

</div>

> [@marcus-j-davies](#):
>
> ```auto
> if(msg.payload.current_value >= 5 && msg.payload.current_value <= 10){
> msg.payload = 'high'
> } else {
> msg.payload = 'low'
> }
> 
> return msg
> 
> ```

@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.

---

<div class="post-metadata">

### Author: ![devifast](https://avatars.discourse-cdn.com/v4/letter/d/43a26b/32.png) [@devifast](https://discourse.nodered.org/u/devifast)
#### Post date: [22 June 2024 16:19 UTC](https://discourse.nodered.org/t/compare-one-variable-between-two-values-in-function/88863/8 "2024-06-22T16:19:53Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)
#### Post date: [22 June 2024 16:23 UTC](https://discourse.nodered.org/t/compare-one-variable-between-two-values-in-function/88863/9 "2024-06-22T16:23:27Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![devifast](https://avatars.discourse-cdn.com/v4/letter/d/43a26b/32.png) [@devifast](https://discourse.nodered.org/u/devifast)
#### Post date: [22 June 2024 16:36 UTC](https://discourse.nodered.org/t/compare-one-variable-between-two-values-in-function/88863/10 "2024-06-22T16:36:35Z")

</div>

How can I solve this miracle?

---

<div class="post-metadata">

### Author: ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)
#### Post date: [22 June 2024 16:41 UTC](https://discourse.nodered.org/t/compare-one-variable-between-two-values-in-function/88863/11 "2024-06-22T16:41:51Z")

</div>

> [@marcus-j-davies](#):
>
> ```auto
> if(msg.payload.current_value >= 5 && msg.payload.current_value <= 10){
> msg.payload = 'high'
> } else {
> msg.payload = 'low'
> }
> 
> return msg
> 
> ```

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?

---

<div class="post-metadata">

### Author: ![devifast](https://avatars.discourse-cdn.com/v4/letter/d/43a26b/32.png) [@devifast](https://discourse.nodered.org/u/devifast)
#### Post date: [22 June 2024 16:46 UTC](https://discourse.nodered.org/t/compare-one-variable-between-two-values-in-function/88863/12 "2024-06-22T16:46:36Z")

</div>

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

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

```

---

<div class="post-metadata">

### Author: ![devifast](https://avatars.discourse-cdn.com/v4/letter/d/43a26b/32.png) [@devifast](https://discourse.nodered.org/u/devifast)
#### Post date: [22 June 2024 16:53 UTC](https://discourse.nodered.org/t/compare-one-variable-between-two-values-in-function/88863/13 "2024-06-22T16:53:39Z")

</div>

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

```

---

<div class="post-metadata">

### Author: ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)
#### Post date: [22 June 2024 16:54 UTC](https://discourse.nodered.org/t/compare-one-variable-between-two-values-in-function/88863/14 "2024-06-22T16:54:12Z")

</div>

still not sure I get 🤪

Ignoring the logic for a minute

you want

```auto
line 
line 
line 

```

to

```auto
line

```

---

<div class="post-metadata">

### Author: ![devifast](https://avatars.discourse-cdn.com/v4/letter/d/43a26b/32.png) [@devifast](https://discourse.nodered.org/u/devifast)
#### Post date: [22 June 2024 16:59 UTC](https://discourse.nodered.org/t/compare-one-variable-between-two-values-in-function/88863/15 "2024-06-22T16:59:37Z")

</div>

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

---

<div class="post-metadata">

### Author: ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)
#### Post date: [22 June 2024 17:04 UTC](https://discourse.nodered.org/t/compare-one-variable-between-two-values-in-function/88863/16 "2024-06-22T17:04:15Z")

</div>

> [@devifast](#):
>
> If the value is outside the limits between 5 and 10 false, if it is within these values ​​true.

Lets try..

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

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [22 June 2024 17:33 UTC](https://discourse.nodered.org/t/compare-one-variable-between-two-values-in-function/88863/17 "2024-06-22T17:33:01Z")

</div>

> [@devifast](#):
>
> If the value is outside the limits between 5 and 10 false, if it is within these values ​​true.

On one line, something like

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

```

Or

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

```

---

<div class="post-metadata">

### Author: ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)
#### Post date: [22 June 2024 17:37 UTC](https://discourse.nodered.org/t/compare-one-variable-between-two-values-in-function/88863/18 "2024-06-22T17:37:17Z")

</div>

it was there the whole time 😂

---

<div class="post-metadata">

### Author: ![devifast](https://avatars.discourse-cdn.com/v4/letter/d/43a26b/32.png) [@devifast](https://discourse.nodered.org/u/devifast)
#### Post date: [22 June 2024 17:51 UTC](https://discourse.nodered.org/t/compare-one-variable-between-two-values-in-function/88863/19 "2024-06-22T17:51:33Z")

</div>

Thanks for the elegant solution. That was what it was 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: [6 July 2024 17:51 UTC](https://discourse.nodered.org/t/compare-one-variable-between-two-values-in-function/88863/20 "2024-07-06T17:51:54Z")

</div>

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