# If function two values

**URL:** https://discourse.nodered.org/t/if-function-two-values/34085
**Category:** General
**Created:** [11 October 2020 07:43 UTC](https://discourse.nodered.org/t/if-function-two-values/34085 "2020-10-11T07:43:44Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![dowermyr](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dowermyr/32/31547_2.png) [@dowermyr](https://discourse.nodered.org/u/dowermyr)
#### Post date: [11 October 2020 07:43 UTC](https://discourse.nodered.org/t/if-function-two-values/34085/1 "2020-10-11T07:43:45Z")

</div>

Hi, I cant get my if fuction node to work  
I have a msg.payload.medel and a msg.payload.aktuellt from two different nodes.  
If msg.payload.aktuellt is higher than msg.payload.medel i want to send "false", else true

---

<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: [11 October 2020 07:46 UTC](https://discourse.nodered.org/t/if-function-two-values/34085/2 "2020-10-11T07:46:15Z")

</div>

You will need to join the 2 before you can compare

See [this article in the cookbook](https://cookbook.nodered.org/basic/join-streams) for an example of how to join messages into one object.

---

<div class="post-metadata">

### Author: ![dowermyr](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dowermyr/32/31547_2.png) [@dowermyr](https://discourse.nodered.org/u/dowermyr)
#### Post date: [11 October 2020 07:56 UTC](https://discourse.nodered.org/t/if-function-two-values/34085/3 "2020-10-11T07:56:11Z")

</div>

I have joind them into an array, output when I have 2 inputs.  
Then made this function node:

```auto
const a = msg.payload.medel;
const b = msg.payload.aktuellt;
if(b => a){
 msg.payload === true ;}
else if(b < a){
 msg.payload === false ;}
return msg;

```

---

<div class="post-metadata">

### Author: ![dowermyr](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dowermyr/32/31547_2.png) [@dowermyr](https://discourse.nodered.org/u/dowermyr)
#### Post date: [11 October 2020 08:08 UTC](https://discourse.nodered.org/t/if-function-two-values/34085/4 "2020-10-11T08:08:34Z")

</div>

Changed it, but doesn't get any output :

```auto
const a = msg.payload.medel;
const b = msg.payload.aktuellt;
if(b => a) msg.payload === "true" ;
else if(b < a) msg.payload === "false" ;
return msg;

```

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [11 October 2020 08:19 UTC](https://discourse.nodered.org/t/if-function-two-values/34085/5 "2020-10-11T08:19:14Z")

</div>

First look at what you are getting out of the join node. If you followed the example linked then it should not be an array.

---

<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: [11 October 2020 08:32 UTC](https://discourse.nodered.org/t/if-function-two-values/34085/6 "2020-10-11T08:32:47Z")

</div>

> `if(b => a) msg.payload === "true" ;`

you are using === (a comparison)  
you should be using = .

---

<div class="post-metadata">

### Author: ![dowermyr](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dowermyr/32/31547_2.png) [@dowermyr](https://discourse.nodered.org/u/dowermyr)
#### Post date: [11 October 2020 09:21 UTC](https://discourse.nodered.org/t/if-function-two-values/34085/7 "2020-10-11T09:21:37Z")

</div>

I cant´t make it work with topic. Not to good on the function node

---

<div class="post-metadata">

### Author: ![dowermyr](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dowermyr/32/31547_2.png) [@dowermyr](https://discourse.nodered.org/u/dowermyr)
#### Post date: [11 October 2020 09:33 UTC](https://discourse.nodered.org/t/if-function-two-values/34085/8 "2020-10-11T09:33:32Z")

</div>

Could someone help me with the function node?

---

<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: [11 October 2020 09:35 UTC](https://discourse.nodered.org/t/if-function-two-values/34085/9 "2020-10-11T09:35:13Z")

</div>

Show us the value going into the function node. (Use a debug node before the function)

---

<div class="post-metadata">

### Author: ![dowermyr](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dowermyr/32/31547_2.png) [@dowermyr](https://discourse.nodered.org/u/dowermyr)
#### Post date: [11 October 2020 10:10 UTC](https://discourse.nodered.org/t/if-function-two-values/34085/10 "2020-10-11T10:10:53Z")

</div>

It's 2 different values  
I want to send true if the msg.payload.aktuellt is the same or greater than the msg.payload.medel

2020-10-11 12:07:57[node: 732ef24.f9e090c](http://192.168.1.195:8123/api/hassio_ingress/3xDDwSsIsE4iGmU83IrQkx9hBaIwh2Sq45S1Ic-tfpM/#)msg.payload : Object

object

medel: 0.266

2020-10-11 12:07:57[node: ae9c7c89.7bfef](http://192.168.1.195:8123/api/hassio_ingress/3xDDwSsIsE4iGmU83IrQkx9hBaIwh2Sq45S1Ic-tfpM/#)msg.payload : Object

object

aktuellt:

---

<div class="post-metadata">

### Author: ![dowermyr](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dowermyr/32/31547_2.png) [@dowermyr](https://discourse.nodered.org/u/dowermyr)
#### Post date: [11 October 2020 10:14 UTC](https://discourse.nodered.org/t/if-function-two-values/34085/11 "2020-10-11T10:14:16Z")

</div>

I set a topic on each node to "medel" and "aktuellt"  
Joined them in a Join node, but them I'm lost 🙂

---

<div class="post-metadata">

### Author: ![dowermyr](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dowermyr/32/31547_2.png) [@dowermyr](https://discourse.nodered.org/u/dowermyr)
#### Post date: [11 October 2020 10:15 UTC](https://discourse.nodered.org/t/if-function-two-values/34085/12 "2020-10-11T10:15:29Z")

</div>

I want the output from the function to be true or false

---

<div class="post-metadata">

### Author: ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)
#### Post date: [11 October 2020 10:57 UTC](https://discourse.nodered.org/t/if-function-two-values/34085/13 "2020-10-11T10:57:37Z")

</div>

> [@dowermyr](#):
>
> `if(b => a) `

This should probably be:

```auto
if(b >= a)

```

Node.js console tells you why:

```auto
> b = 2
2
> b >= 2
true
> b => 2
[Function (anonymous)]
> if (b => 10) console.log('oops');
oops

```

You can do similar quick experiments in a JavaScript console (REPL) by opening your browser development tools window and typing in.

I suggest doing that to get a better hang of the syntax to avoid having to bang your head on the wall trying to figure out why your function isn't working.

---

<div class="post-metadata">

### Author: ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)
#### Post date: [11 October 2020 12:00 UTC](https://discourse.nodered.org/t/if-function-two-values/34085/14 "2020-10-11T12:00:08Z")

</div>

> [@dowermyr](#):
>
> I want the output from the function to be true or false

Also if you want it to be an actual `true` or `false` (a boolean value), you should set

```javascript
msg.payload = true; // or false

// instead of a string value
msg.payload = "true" // or "false"

```

This can become an issue as:

```javascript
msg.payload = "false";
if (msg.payload) {
  // this code will execute as any non-empty 
  // string value is "truthy"
}

```

---

<div class="post-metadata">

### Author: ![dowermyr](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dowermyr/32/31547_2.png) [@dowermyr](https://discourse.nodered.org/u/dowermyr)
#### Post date: [11 October 2020 12:04 UTC](https://discourse.nodered.org/t/if-function-two-values/34085/15 "2020-10-11T12:04:16Z")

</div>

Yeah, I did that. Everything seems to be working now, thanks !!

---

<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: [10 December 2020 12:04 UTC](https://discourse.nodered.org/t/if-function-two-values/34085/16 "2020-12-10T12:04:22Z")

</div>

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