If function two values

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

You will need to join the 2 before you can compare

See this article in the cookbook for an example of how to join messages into one object.

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

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;

Changed it, but doesn't get any output :

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;

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.

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

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

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

Could someone help me with the function node?

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

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:57node: 732ef24.f9e090cmsg.payload : Object

object

medel: 0.266

2020-10-11 12:07:57node: ae9c7c89.7bfefmsg.payload : Object

object

aktuellt:

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

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

This should probably be:

if(b >= a)

Node.js console tells you why:

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

1 Like

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

msg.payload = true; // or false

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

This can become an issue as:

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

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

1 Like