# A function with if is not working

**URL:** <https://discourse.nodered.org/t/a-function-with-if-is-not-working/31394>\
**Category:** General\
**Created:** [10 August 2020 13:58 UTC](https://discourse.nodered.org/t/a-function-with-if-is-not-working/31394 "2020-08-10T13:58:38Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![edstobi](https://avatars.discourse-cdn.com/v4/letter/e/aca169/32.png) [@edstobi](https://discourse.nodered.org/u/edstobi)\
**Post date:** [10 August 2020 13:58 UTC](https://discourse.nodered.org/t/a-function-with-if-is-not-working/31394/1 "2020-08-10T13:58:38Z")

</div>

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")

 ![nodered](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/5/d5ed65776804cf614c1d7550adb7ceed58f72f9d.jpeg)

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?  
.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [10 August 2020 14:27 UTC](https://discourse.nodered.org/t/a-function-with-if-is-not-working/31394/2 "2020-08-10T14:27:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![edstobi](https://avatars.discourse-cdn.com/v4/letter/e/aca169/32.png) [@edstobi](https://discourse.nodered.org/u/edstobi)\
**Post date:** [10 August 2020 19:04 UTC](https://discourse.nodered.org/t/a-function-with-if-is-not-working/31394/3 "2020-08-10T19:04:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [10 August 2020 19:17 UTC](https://discourse.nodered.org/t/a-function-with-if-is-not-working/31394/4 "2020-08-10T19:17:21Z")

</div>

> [@edstobi](#):
>
> Please explain: Change the debug nodes to show the complete message.

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"

---

<div class="post-metadata">

**Author:** ![edstobi](https://avatars.discourse-cdn.com/v4/letter/e/aca169/32.png) [@edstobi](https://discourse.nodered.org/u/edstobi)\
**Post date:** [10 August 2020 20:14 UTC](https://discourse.nodered.org/t/a-function-with-if-is-not-working/31394/5 "2020-08-10T20:14:43Z")

</div>

![nodered](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/5/d5ed65776804cf614c1d7550adb7ceed58f72f9d.jpeg)

every 30 seconds

10.8.2020, 21:56:05[node: msg.test](http://10.11.12.160:1880/#)msg : 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:12[node: msg.topic](http://10.11.12.160:1880/#)clicked : 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:13[node: msg.out](http://10.11.12.160:1880/#)clicked : 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?

```auto
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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [10 August 2020 20:57 UTC](https://discourse.nodered.org/t/a-function-with-if-is-not-working/31394/6 "2020-08-10T20:57:16Z")

</div>

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

> [@edstobi](#):
>
> 10.8.2020, 21:56:12[node: msg.topic](http://10.11.12.160:1880/#)clicked : 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"

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.

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [10 August 2020 21:01 UTC](https://discourse.nodered.org/t/a-function-with-if-is-not-working/31394/7 "2020-08-10T21:01:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [10 August 2020 21:07 UTC](https://discourse.nodered.org/t/a-function-with-if-is-not-working/31394/8 "2020-08-10T21:07:14Z")

</div>

Thanks Paul, I had not noticed that.

---

<div class="post-metadata">

**Author:** ![edstobi](https://avatars.discourse-cdn.com/v4/letter/e/aca169/32.png) [@edstobi](https://discourse.nodered.org/u/edstobi)\
**Post date:** [10 August 2020 21:37 UTC](https://discourse.nodered.org/t/a-function-with-if-is-not-working/31394/9 "2020-08-10T21:37:34Z")

</div>

> [@Paul-Reed](#):
>
> Thread category moved from 'Creating nodes' to 'General', as this thread is not about node creation.

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [11 August 2020 21:05 UTC](https://discourse.nodered.org/t/a-function-with-if-is-not-working/31394/10 "2020-08-11T21:05:37Z")

</div>

If you wish to combine information from multiple messages have a look at the use of the Join node in [this example](https://cookbook.nodered.org/basic/join-streams) from the cookbook.

---

<div class="post-metadata">

**Author:** ![edstobi](https://avatars.discourse-cdn.com/v4/letter/e/aca169/32.png) [@edstobi](https://discourse.nodered.org/u/edstobi)\
**Post date:** [12 August 2020 14:18 UTC](https://discourse.nodered.org/t/a-function-with-if-is-not-working/31394/11 "2020-08-12T14:18:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [26 August 2020 14:18 UTC](https://discourse.nodered.org/t/a-function-with-if-is-not-working/31394/12 "2020-08-26T14:18:05Z")

</div>

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