If condition with a function

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

in a function it would be something like this and you would need to attach it to the output of the home/away presence node:

const isNight = global.get(“isNight”);
if(msg.payload === “home” && isNight === true){
    msg.payload = “got home and its night”;
    return msg;
} else {
    return null;
}

not tested

Perfect!

I can manage do that with the switch node instead.

1 Like

respected sir, i request to help me out..
i am making pulse read sensor so for that i run python code and send data to node red. in that i had used (function node) with code

msg.payload=msg.payload.Pulse
return msg;

reading is showing no problem but sir now i want to make another 2nd function node such that whenever the value exceeded 100 then condition is matched and give true message to (switch node) which is connected to 2nd function node.so sir what code should i wright in 2nd function code.
path name of sanser value coming is payload.
thankyou

1 - how are you sending the data to NR?
2 - is

msg.payload=msg.payload.Pulse
return msg;

the only code in the 'function' node? if so, you could use a change node instead:
Screen Shot 2020-06-13 at 4.36.25 AM
3 - you say

now i want to make another 2nd function node such that whenever the value exceeded 100 then condition is matched
why not use a switch node for this?

1- sir through ibm cloud.
2- yes sir
msg.payload=msg.payload.Pulse
return msg;
these same code i am running in my 1st function node named (pulse rate)
3- but sir before using switch node i thought comparison of pulse values must be there with the help of 1st and 2nd function node.Then 2nd function node will be connected with switch node which will connect the further node only when condition is fulfilled. for this i use 1:- value rules as (is true) in switch node..
however i want what you think sir
thankyou

Ok, now you have confused me. Are you saying you have two (2) inputs and you need to compare their values?

Please provide a flow diagram explaining what you need to do.

Hi sir, its true that i'm very late to reply. i'm sorry for that. it's about the project that i had to do and due to less time i had submitted with another way.
thank you 'Zenofmud' for helping us out!

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