Function if condition

please i am new in node red and i want to make a function node which have 2 inputs and 1 output,
the function is to check if the 1st input =0 and 2nd input = 0 then the output is not sent (null), else then the output = the same as 1st input.

i tried the below code but i get in the debug = undefined which is not working.
any help please ?

var relay4message = flow.get('relay4message');
var checker = flow.get('checker');

if (relay4message === 0 && checker === 0) {
msg.payload = null;
}
else {
msg.payload = relay4message;
}
return msg;

Are you sure your variables are numeric and not strings? Have you looked ar their values in the sidebar and verified that?

actually the both input variables are strings (1st input mqtt in node which has value msg.payload:string either 0 or 1 ) and (2nd input boolean has also msg.payload:string with value 0 or 1 ), so do i need to change something in my function node to comply with string ?

You are comparing numeric values so you can change your "if" to
If ( something == "0" notice the parentheses,
Probably easiest or change your string to a number

i update the code as show below and i still get this message in the debug:
input_boolean.bedroom_light : msg.payload : undefined (which is the second input)

var relay4message = flow.get('relay4message');
var checker = flow.get('checker');


if (relay4message == "0" && checker == "0") {
msg.payload = null;
}
else {
msg.payload = relay4message;
}
return msg;

It appears you aren't getting data on the one input. I would put an inject node on each input that gives you exactly what you are looking for and see if it works then.

In addition, if you look at the node-red docs page Writing Functions it will show you how to use node.warn() to display data in the debug pane. Add warn() statements to the function to display the values of checker and relay4message to see what they are.

On a side issue, if you set the mqtt node to output the result of parsing the value as json then it will give you a number out rather than a string.

Thank you so much for your replies.
but actually it does not work also.
i tried the inject node and i got undefined also, in addition i put the warn node and changed the function node as shown below but all giving me undefined value

Just to highlight something: my request is actually when i receive any message from the 1st input (relay4message) then go check the status of the 2nd input (checker) and after that to proceed the output according to the if condition statment in the function node

i know i am making something wrong but i have been 2 days trying to figure it out but without any sucess :frowning:

just a question:
in the function code, i put the name of the node which i have provided for each input,
i should write the node id instead or what ?

i put the debug directly to my inputs as show below in case i put their names wrongly in the function node

hint: the checker node is just changing the message value from off to 1 and from on to 0 (i know i it should be off to 0 & on to 1 but i have some configuration that i need to reverse it)

I don't understand what you mean. In order to use flow.get() you have to have written a value somewhere using flow.set(). See the node red docs page on Using Context.

If the values you want are in messages being passed to this node in messages then instead of context you can use a Join node to combine the two values into one message. See this article in the cookbook for an example of how to join messages into one object.

1 Like

may be i didn't phrase my request clearly.
what i actually need is:

if a message received from "relay4message" has value 0, it has to check first if the status of "checker" is 0 or not,
if the checker status is 0, then the message of "relay4message" will not be sent to the next node (lets say the debug node) ,
but if the above condition is not fulfilled (condition = relay4message = 0 & checker = 0), then the message of "relay4message" will be delivered as it is with no change to the next node (debug node).

so may be i have constructed the function node in a wrong way from the begining by using flow.get, and it can be done in another easier way (i do not know).

i hope i have explained it clearly
simply, i want to send a message to flow, or stop it entirely if its value =0 & the current value of another variable=0.

Hi @aymanokail and welcome to NodeRED community!
First as already mentioned, to use context, flow or global variables, you need use:
flow.set() and flow.get(). in a function node.

I try to understand your logic, and developed a simple example, where i simulated the MQTT in node, with two inject nodes and the CHECK condition, also with two inject nodes, but i use change nodes and switch nodes, i hope help you!!

[{"id":"a5a4adb58554b6e2","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"8bf2702708aca886","type":"inject","z":"a5a4adb58554b6e2","name":"relay -> \"0\"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"str","x":130,"y":140,"wires":[["5d368d1216d31c6e"]]},{"id":"973d433ca296448b","type":"inject","z":"a5a4adb58554b6e2","name":"check -> \"0\"","props":[{"p":"check","v":"0","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":130,"y":300,"wires":[["4fda245f89d15c4b"]]},{"id":"add483cca942f5dd","type":"debug","z":"a5a4adb58554b6e2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":815,"y":240,"wires":[],"l":false},{"id":"7a8f51bb04a6d7c0","type":"inject","z":"a5a4adb58554b6e2","name":"relay -> \"1\"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"str","x":130,"y":180,"wires":[["5d368d1216d31c6e"]]},{"id":"54cd0eea641a8a3e","type":"inject","z":"a5a4adb58554b6e2","name":"check -> \"1\"","props":[{"p":"check","v":"1","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":130,"y":340,"wires":[["4fda245f89d15c4b"]]},{"id":"c074f239dbfb6613","type":"switch","z":"a5a4adb58554b6e2","name":"if (check == 0)","property":"check","propertyType":"flow","rules":[{"t":"eq","v":"1","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":360,"y":240,"wires":[["cf77a466092cae31"]]},{"id":"cf77a466092cae31","type":"switch","z":"a5a4adb58554b6e2","name":"if (relay == 0)","property":"relay","propertyType":"flow","rules":[{"t":"eq","v":"0","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":530,"y":240,"wires":[["439a776f970ad43d"]]},{"id":"5d368d1216d31c6e","type":"change","z":"a5a4adb58554b6e2","name":"payload to flow","rules":[{"t":"set","p":"relay","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":160,"wires":[["c074f239dbfb6613"]]},{"id":"4fda245f89d15c4b","type":"change","z":"a5a4adb58554b6e2","name":"check to flow","rules":[{"t":"set","p":"check","pt":"flow","to":"check","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":320,"wires":[["c074f239dbfb6613"]]},{"id":"439a776f970ad43d","type":"change","z":"a5a4adb58554b6e2","name":"flow to payload ","rules":[{"t":"set","p":"payload","pt":"msg","to":"relay","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":700,"y":240,"wires":[["add483cca942f5dd"]]}]

The logic is:

1.- if (check == 1) { then evaluates if (relay == 0) { then send to debug node } }
2.- if (check == 1) { then evaluates if (relay == 1) { then nothing } }
3.- if (check == 0) { then nothing }

thank yo so much, it is too close to what i need, i will try to understand the nodes you mention and modify to what exactly i need.
actually the logic i want is a little bit different as follows:

1.- if (relay == 0) { then evaluates if (check == 0) { then nothing "do not send anything" }
2.- if (relay == 0) { then evaluates if (check == 1) { then send to debug node relay status "0" } }
3.- if (relay == 1) { then evaluates if (check == 0) { then send to debug node relay status "1" } }
4.- if (relay == 1) { then evaluates if (check == 1) { then send to debug node relay status "1" } }

it means that it will send always the new relay status to the debug except in case relay has new received value=0 & status of check was already=0 then send nothing

Okay @aymanokail, in this case i understand much better.
Try to change of position; the switch node "if(relay == 0)" to the switch node "if(check == 0)", this make it will first evaluated the value of relay and then evaluated the value of check, as you need it.

:+1:

Thank you so much :smiley: am so happy it now works

i have modified the workflow as follows and now it do what i want finally after 2 days.
thanks to you again brother :+1: :+1:.

[{"id":"8bf2702708aca886","type":"inject","z":"a5a4adb58554b6e2","name":"relay -> \"0\"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"str","x":130,"y":140,"wires":[["5d368d1216d31c6e"]]},{"id":"973d433ca296448b","type":"inject","z":"a5a4adb58554b6e2","name":"check -> \"0\"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"str","x":110,"y":520,"wires":[["4fda245f89d15c4b"]]},{"id":"7a8f51bb04a6d7c0","type":"inject","z":"a5a4adb58554b6e2","name":"relay -> \"1\"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"str","x":130,"y":180,"wires":[["5d368d1216d31c6e"]]},{"id":"54cd0eea641a8a3e","type":"inject","z":"a5a4adb58554b6e2","name":"check -> \"1\"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"str","x":110,"y":560,"wires":[["4fda245f89d15c4b"]]},{"id":"5d368d1216d31c6e","type":"change","z":"a5a4adb58554b6e2","name":"payload to flow","rules":[{"t":"set","p":"relay","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":160,"wires":[["142d4614.f818aa","c0b966c.6cdca98","305cfdf2.9e8af2"]]},{"id":"4fda245f89d15c4b","type":"change","z":"a5a4adb58554b6e2","name":"check to flow","rules":[{"t":"set","p":"check","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":290,"y":540,"wires":[[]]},{"id":"c0b966c.6cdca98","type":"switch","z":"a5a4adb58554b6e2","name":"if (check == 1)","property":"check","propertyType":"flow","rules":[{"t":"eq","v":"1","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":580,"y":300,"wires":[["a04dcc7.c72123"]]},{"id":"a04dcc7.c72123","type":"switch","z":"a5a4adb58554b6e2","name":"if (relay == 1)","property":"relay","propertyType":"flow","rules":[{"t":"eq","v":"1","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":750,"y":300,"wires":[["9e1e9f30.858e9"]]},{"id":"9e1e9f30.858e9","type":"change","z":"a5a4adb58554b6e2","name":"flow to payload ","rules":[{"t":"set","p":"payload","pt":"msg","to":"relay","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":920,"y":300,"wires":[["cb4c28be.e0f698"]]},{"id":"305cfdf2.9e8af2","type":"switch","z":"a5a4adb58554b6e2","name":"if (check == 1)","property":"check","propertyType":"flow","rules":[{"t":"eq","v":"1","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":580,"y":380,"wires":[["5c95c53f.3773ec"]]},{"id":"5c95c53f.3773ec","type":"switch","z":"a5a4adb58554b6e2","name":"if (relay == 0)","property":"relay","propertyType":"flow","rules":[{"t":"eq","v":"0","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":750,"y":380,"wires":[["6e17ee06.4fa98"]]},{"id":"6e17ee06.4fa98","type":"change","z":"a5a4adb58554b6e2","name":"flow to payload ","rules":[{"t":"set","p":"payload","pt":"msg","to":"relay","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":920,"y":380,"wires":[["e48e73ac.33f52"]]},{"id":"618c6bda.b481b4","type":"debug","z":"a5a4adb58554b6e2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1170,"y":220,"wires":[]},{"id":"cb4c28be.e0f698","type":"debug","z":"a5a4adb58554b6e2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1170,"y":300,"wires":[]},{"id":"e48e73ac.33f52","type":"debug","z":"a5a4adb58554b6e2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1170,"y":380,"wires":[]},{"id":"142d4614.f818aa","type":"switch","z":"a5a4adb58554b6e2","name":"if (check == 0)","property":"check","propertyType":"flow","rules":[{"t":"eq","v":"0","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":580,"y":220,"wires":[["d451c0c0.03235"]]},{"id":"d451c0c0.03235","type":"switch","z":"a5a4adb58554b6e2","name":"if (relay == 1)","property":"relay","propertyType":"flow","rules":[{"t":"eq","v":"1","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":750,"y":220,"wires":[["b7a1eef0.026ea"]]},{"id":"b7a1eef0.026ea","type":"change","z":"a5a4adb58554b6e2","name":"flow to payload ","rules":[{"t":"set","p":"payload","pt":"msg","to":"relay","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":920,"y":220,"wires":[["618c6bda.b481b4"]]}]
1 Like

actually this was part of the whole flow to control a light in my bedroom which have 2 modes of light which can be activated by turn on the physical switch (go mode 1) and then if turn off then on again (go mode 2) etc..
so i wanted to automate it to use only mode 2 (i do not need mode 1) by just pressing once the physical switch on wall or by trigerring the button switch in home assistant on then it automatically turn on then off then on again to reach mode 2
in addition to keep the actual feed back of the light to be displayed on my home assistant switch button.

so much complicated flow for me but finally it is completed :smiley:

The most node red way is to use a Join node as I suggested.

1 Like

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