If condition with a function

Hi all node red maniacs!!

I'm working on a simple function condition. I'm a javascript beginner so please patience jejeje.

I have to make several if conditions so far i have.

let night = msg.payload
let day = msg.payload
let home = msg.payload
let away = msg.payload

if (night === true) {
    msg.payload = 'noche'
} else {
    msg.payload = 'dia'
}

return msg;

That works ok and i can retrieve the correct payload depending the day or night.

This is the output:
image

NOW, i have to also make the condition for the others two variables (home and away) but no luck with that because the conditions make a little mess.

How can i achieve that to perform correctly?

Thanks a lot!

Hello,
There is two problems here.
Firstly you are assigning your message payload value to all variables. So var night and day and so on will all have the same value. That’s the first thing as it doesn’t make sense to have four variables that have exactly the same value.
Secondly by doing if(somevar === true) you are only checking if somevar is a boolean type with a value of true. So you can only ever have two states true or false but you want to check for four different states.
So you need to put something like a string or number into the function that can have more states and than you can compare the value of msg.payload directly.
For example for a string something like: if(msg.payload = "afternoon") and than use if, else if etc.
An alternative would be to have a look at switch cases which are made for exactly this purpose:


They compare one input value to a bunch of other values.
Johannes

Ok, let me see if i get it.

First i do something like:

let night, day, home, away = msg.payload is that ok ?

Any example on how to manage the conditions?

I also will check the switch. Maybe i can do that with a node directly?.

Thanks a lot!

What is in the msg.payload you put in the function? Can you please tell us?
Why am i asking? If the msg.payload you receive just includes one piece of information there is no point in assigning it to for different variables. The only thing you achieve is that you now have the same piece of information 4 times. So testing any of the for vars would do exactly the same so there is no point.

Edit and yes if you just want to test the msg.payload for a certain content mostly the switch node is the easier option.

Edit 2 that syntax wont work

The function is declared as is above with the declared variables and so on.
Also there is the payload info that i receive and that condition is well delivered.

Maybe i don't get it something.

Thanks !

You receive msg.payload.
Check conditions on the content of the payload.

Like @JGKK stated:

if(msg.payload == "afternoon"){

}

What is your input ?

Hi bakman2!.

This is my output, as the image that i posted above.
image

what do you put into the function not what comes out

Sorry maybe i dont get it correctly.

At the beginning of the post there is my whole function and the output. That works ok. The complex part that im doing is use more conditions inside the function.

I wrote in the first answer if than else if, so:

if(){
    some code;
} else if (){
    some code;
}else if (){
    some code;
} else {
    some code;
}

or a switch case.
But you don’t need this part:

as this means that day, night, home and away will all be a copy of msg.payload. So there all the same thing.
You can just use msg.payload === true or msg.payload === false directly in an if or else if statement. As i also wrote above.
Why do you want 4 variables that all have the same value?

Yes, but what are you feeding into the function node?
Instead of a function node, add a debug node, and show us what data you are working with.

This is my node.

[{"id":"fbac250c.314aa8","type":"nighttime","z":"3c9968a7.da8038","name":"Day/night","lon":"-58.381557","lat":"-34.603683","start":"sunrise","end":"sunsetStart","x":120,"y":625,"wires":[["b5963aeb.561fe8"],["b5963aeb.561fe8"]]},{"id":"b5963aeb.561fe8","type":"function","z":"3c9968a7.da8038","name":"day/night & home/away","func":"let night = msg.payload\nlet day = msg.payload\nlet home = msg.payload\nlet away = msg.payload\n\nif (night === true) {\n msg.payload = 'noche'\n} else {\n msg.payload = 'dia'\n}\n\nreturn msg;\n\n","outputs":1,"noerr":0,"x":450,"y":660,"wires":[["4d84070d.1ed408"]]},{"id":"4d84070d.1ed408","type":"debug","z":"3c9968a7.da8038","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":570,"y":760,"wires":}]

Marty, you are not answering our questions!
Both JGKK and myself have asked you the same question....

Im feeding the day or night node into the function node.

I think you might have a basic miss understanding of concepts.
Lets do this step by step. Are night, day etc the values you receive?
Please attach a debug node before! the function node and show us a picture of an msg that you see in the debug tab that is send to the function node.
This will help everybody trying to help you as its easier if we can understand the exact structure of the msg.payload that the function node handles.
Unfortunately i cant import your code as im on my phone right now.

This is send before the function.

So just to make clear that I understand this correctly. You want to write a function that combines a check for two things:
1st is it day or night?
2nd is somebody home or not?
Both informations come from different sources.
If I understood it right my next question is do you want to save this state somewhere or do you want to root other messages base on the properties above and is home/away also a boolean?

1 Like

Right, the thing is like this.

First I want to check if is day or night. That will have an output true or false because is boolean definded by the node by default. So, i "transcribed" that for day or night depending the boolean output. The function node (as described in top of the topic) do that correctly.

Second, i want to check if im home or not and depending on that the lights will turn on or off. It's useless turn on the lights if it's daylight right?

Maybe i'm doing the thing more complex than it is.

Thanks again!

Did you read the nightime nodes description?
It sets a global var isNight to a boolean. You can just use this in a simple switch node. So use two switch nodes in series behind your home/away output one to test the msg.payload if you just arrived or left, the second one to check if global.isNight is true.
simple as that and no function needed.

Perfect! i was thinking in switches too but maybe i can do that with a function, i made the thing more complicated i think.

Thanks a lot!!