0

I just made the jump from IDE to Node-RED where JavaScript comes into play. I am eager to learn but my knowledge is limited knowledge.

What I am trying to do, is simple to check the msg.payload, if the value is larger than 25 then I want to return 1 else I want to return 0.

I have tried different codes, but it does not work.

m = msg.payload;
if (m < 25)
{
  return 1;
}
else if (m > 25)
{
  return 0;
}

and

m = msg.payload;
if (m < 25)
{
  return 1;
}
  else ()
{
  return 0;
}
2
  • You simply have to change < to >, no? Commented Nov 12, 2017 at 16:26
  • You must return a msg object from a Node-RED function node, not just a value. Convention says you return the value as msg.payload Commented Nov 12, 2017 at 16:32

8 Answers 8

1

For Node-RED function nodes you must return a msg object not just a value.

So the correct form for this test is as follows.

if (msg.payload > 25) {
  msg.payload = 1
} else {
  msg.payload = 0;
}

return msg;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! this was what i was looking for.
0

You can do it in one line:

return msg.payload > 25 ? 1 : 0;

or if you just need booleans (true/false):

return msg.payload > 25;

Comments

0

Try this:

if (Number(m) > 25){
    return 1;
}
else{
    return 0;
}

Comments

0

You can simplify this a good bit, the else isn't even needed in this case:

if (m > 25) {
  return 1;
}
return 0;

When the return inside the if statement is executed, the code that follows won't be run... making the else statement unnecessary.

Comments

0

The else is redundant when you're just returning like that, and you've got your comparison operators backwards. Right now it'll return 1 if it's less than 25, not greater than.

if (msg.payload > 25) {
return 1;
}
return 0;

Comments

0

You should learn comparision operator where > denotes greater than and < denotes lesser than , you can simplify using ternary operator as

return msg.payload > 25 ? 1 : 0; 

DEMO WITH IF ELSE

function check(m){
if (m > 25){
    return 1;
  }
    else 
    {
      return 0;
    }
};

console.log(check(50));

DEMO WITH TERNARY

function check(m){
  return m > 25 ? 1 : 0; 
};

console.log(check(50));

Comments

0

As you have mentioned that you want to return 1 if value is greater than 25. Please check that you have written wrong condition. Your condition must be this:

if ( m > 25 ){ 
    return 1; 
} else { 
    return 0; 
} 

Comments

0

You are making a very silly mistake of less than and greater than sign. Hope it helps you.

m = msg.payload;
if (m > 25)
{
msg.payload=1;
}
else if (m < 25)
{
msg.payload=0;
}
return msg;

5 Comments

Your two examples are not equivalent, so they may be confusing to a beginner.
What sort of confusion ? and what is not equivalent ? They both are exactly same and works in exactly same fashion. I don't think a beginner will get any confusion. It was just a silly mistake.
They're not exactly the same. What happens if m is 25.
Read the question. The question doesn't care for m=25. Following strictly the question.
First, irrespective of the question, they're not equivalent as you claim. Second, maybe you should read the question "if the value is larger than 25 then I want to return 1 else I want to return 0.". So if it's not greater than 25, it may be equal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.