A function with if is not working

Hi
I nead help. I cant finde my error.
the debug (msg.test : string[2] "01" )
the debug ( msg.out : clicked : msg.out : string[20] "10.11.12.144:8080/00")
the debug ( msg.topic: clicked : msg.topic : string[7] "clicked")

my function is not working

var test =msg.test // = now it is 01

if (test === "00")
{status = "01";
}
else
{status = "00";
}

let A2 = status;
let A1 ="10.11.12.144:8080/";

if (msg.topic == 'clicked') {
//return {payload: msg.test}
msg.out ="10.11.12.144:8080/"+A2;
return msg;
}

the status A2 is not changing
wehr is my fault?
.

Change the debug nodes to show the complete message. Does the message from the button include an attribute msg.test? If not then test will be undefined so your else code will be executed.

Can you explain what you are trying to achieve? I suspect you are not doing it the best way.

Hello
Thanks for the answer.

Description
I am reading a web page with an HTTP request.(Siemens LOGO webserver)
The information results in an array consisting of 16 pieces of information.
A1-A8 and E1-E8
return {payloadarray:{"A1:A1,A2:A2,A3:A3,A4:A4,A5:A5,A6:A6,A7:A7,A8:A8,E1:E1,E2:E2,E3:E3,E4:E4,E5:E5,E6:E6,E7:E7,E8:E8}}
shows in a Text Node, "A1":"0","A2":"0","A3":"0","A4":"1","A5":"0","A6":"0","A7":"1","A8":"0","E1":"1","E2":"1","E3":"1","E4":"1","E5":"1","E6":"1","E7":"1","E8":"1"
For example:
A1 = 1 and therefore the valve is activated. A1 = 0 valve is closed
E1 = 1 and the pressure is OK. E1 = 0 pressure is NOK

E1-E8 controls a text field and change, depending on the state the Info Color

<font color={{{msg.color}}} ><i class="fa fa-circle" style="font-size:24px;"></i></font>

Everything works up to here.

A1-A8 should do the same with a button, when I press this button an http request msg.out ="10.11.12.144:8080/"+A2 should be sent.
If I set A2 by hand it works. The button does a toggle of the aktual state of A1

For each valve there is a different variable to control for on and off

Please explain: Change the debug nodes to show the complete message.

Unfortunately I only get what I see in debugging windows.
can I increase the debugging level? if yes please how .
I've only been working with Node Red for a few hours and I'm not a programmer

Look in the config of the debug node, there is an option to show the complete message rather than just a single attribute. To understand what the function will do it is necessary to see the complete message coming from the button.

So change the debug node showing what is coming out of the button to show the complete message and show us what it shows. If that message does not contain msg.test with the value "01" then A2 will be "00"

every 30 seconds

10.8.2020, 21:56:05node: msg.testmsg : Object
object
payloadarray: object
A1: "1"
A2: "0"
A3: "0"
A4: "1"
A5: "0"
A6: "0"
A7: "1"
A8: "0"
E1: "1"
E2: "1"
E3: "1"
E4: "1"
E5: "1"
E6: "1"
E7: "1"
E8: "1"
_msgid: "1d5c7952.65d6c7"
color: "lime"
state: "00"
test: "00"

by changing the state:
color: "red"
state: "01"
test: "01"

on Click on button

10.8.2020, 21:56:12node: msg.topicclicked : msg : Object
object
payload: "6bfac349.df215c"
topic: "clicked"
event: object
clientX: 518
clientY: 839
bbox: array[4]
0: 435
1: 870
2: 753
3: 822
socketid: "36ayniCiobgKgVa6AAAB"
_msgid: "2e4a4f42.c4ce4"

10.8.2020, 21:56:13node: msg.outclicked : msg : Object
object
payload: "6bfac349.df215c"
topic: "clicked"
event: object
clientX: 518
clientY: 839
bbox: array[4]
0: 435
1: 870
2: 753
3: 822
socketid: "36ayniCiobgKgVa6AAAB"
_msgid: "2e4a4f42.c4ce4"
out: "10.11.12.144:8080/00"

actually all points should be there for the function?

var test =msg.test  
 if (test === "00")
   {status = "01";
   }
else
   {status = "00";
   }
let S = status;
if (msg.topic == 'clicked') {
//return {payload:  msg.test}
 msg.out ="10.11.12.144:8080/"+S;
 return msg; 
}

this part does not work
if (test === "00")
it will not be recognised as true

Look at the message that comes out of the button (and into the function). If I interpret your post correctly it is this one.

That goes into the function and the first line
var test = msg.test
will set test to undefined because msg.test is undefined.
Then test is compared to "00" and since is undefined it is not "00" the else condition is run, which sets status to "00".
Then, since msg.topic is 'clicked', msg.out is set to "10.11.12.144:8080/00" and the message is returned.

Thread category moved from 'Creating nodes' to 'General', as this thread is not about node creation.

Thanks Paul, I had not noticed that.

1 Like

Thanks Paul-Reed for the move.

Thanks, Colin, I understand and that is logical.
Unfortunately, I have no idea how to bring them together so that msg.test does not become undefined.
Would you be so kind and give me a little coaching here on how to merge the two information.
My knowledge is not yet sufficient for this.
Thank you very much and good night for today

1 Like

If you wish to combine information from multiple messages have a look at the use of the Join node in this example from the cookbook.

1 Like

Thanks Colin
The manual mode I did not know. I solved it yesterday with flow.set and flow.get.
But I will implement this solution once in parallel.
Thanks
That was some interesting lessons.
Marc

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