# IF- ELSE in a function node

**URL:** https://discourse.nodered.org/t/if-else-in-a-function-node/58988
**Category:** General
**Created:** [25 February 2022 17:25 UTC](https://discourse.nodered.org/t/if-else-in-a-function-node/58988 "2022-02-25T17:25:51Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![BigHitRider93](https://avatars.discourse-cdn.com/v4/letter/b/9fc348/32.png) [@BigHitRider93](https://discourse.nodered.org/u/BigHitRider93)
#### Post date: [25 February 2022 17:25 UTC](https://discourse.nodered.org/t/if-else-in-a-function-node/58988/1 "2022-02-25T17:25:51Z")

</div>

I recieve a string from another Node and want to check and convert it in the following function node.

When I inject a string in the function node, only the input values will be shown in the debug node.

Maybe someone will see the mistake at first sight and can help me with this problem.

```auto
var Feuchte = msg.payload;
var Zähler1 = 0;
var Zähler2 = 0;

if (Feuchte >= 69)
{
    Zähler1++;
    
    if (Zähler1 >= 3)
    {
        msg.payload = 1;
        Zähler1 = 0;
    }
}
else if (Feuchte <= 58)
{
    Zähler2++;
    if (Zähler2 >= 3)
    {
        msg.payload = 2;
        Zähler2 = 0;
    }
}
return msg;

```

Here's my complete flow:

```auto
[
    {
        "id": "faff91a2a3f67c7b",
        "type": "tab",
        "label": "Flow 2",
        "disabled": false,
        "info": ""
    },
    {
        "id": "8173dc7f896278bf",
        "type": "inject",
        "z": "faff91a2a3f67c7b",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "71",
        "payloadType": "str",
        "x": 270,
        "y": 140,
        "wires": [
            [
                "1f22d8b398c05b56"
            ]
        ]
    },
    {
        "id": "6f39213c12a643c6",
        "type": "inject",
        "z": "faff91a2a3f67c7b",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "28",
        "payloadType": "str",
        "x": 270,
        "y": 180,
        "wires": [
            [
                "1f22d8b398c05b56"
            ]
        ]
    },
    {
        "id": "1f22d8b398c05b56",
        "type": "function",
        "z": "faff91a2a3f67c7b",
        "name": "IF - ELSE Verzweigung",
        "func": "var Feuchte = msg.payload;\nvar Zähler1 = 0;\nvar Zähler2 = 0;\n\nif (Feuchte >= 69)\n{\n Zähler1++;\n \n if (Zähler1 >= 3)\n {\n msg.payload = 1;\n Zähler1 = 0;\n }\n}\nelse if (Feuchte <= 58)\n{\n Zähler2++;\n if (Zähler2 >= 3)\n {\n msg.payload = 2;\n Zähler2 = 0;\n }\n}\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 530,
        "y": 160,
        "wires": [
            [
                "72595f921dd14257"
            ]
        ]
    },
    {
        "id": "72595f921dd14257",
        "type": "debug",
        "z": "faff91a2a3f67c7b",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 770,
        "y": 160,
        "wires": []
    }
]

```

---

<div class="post-metadata">

### Author: ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)
#### Post date: [25 February 2022 17:42 UTC](https://discourse.nodered.org/t/if-else-in-a-function-node/58988/2 "2022-02-25T17:42:23Z")

</div>

Hi @BigHitRider93

Welcome to the forums.  
There are a few things I would change here.

```auto
/* 'I recieve a string from another Node' - Then we should parse to an integer */
const Feuchte = parseInt(msg.payload);

/* A function node is just that, its variables don't continue to live after its execution - well there are some excpetions */
/* So we will store them 'outside' the function */

let Zähler1 = flow.get("Zähler1") || 0 /* if it's not yet created, initialise it to 0 */
let Zähler2 = flow.get("Zähler2") || 0 /* if it's not yet created, initialise it to 0 */

if (Feuchte >= 69)
{
    Zähler1++;
    
    if (Zähler1 >= 3)
    {
        msg.payload = 1;
        Zähler1 = 0;
    }
}
else if (Feuchte <= 58)
{
    Zähler2++;
    if (Zähler2 >= 3)
    {
        msg.payload = 2;
        Zähler2 = 0;
    }
}
/* Update our flow vars so the next message can make use */
flow.set("Zähler1", Zähler1 )
flow.set("Zähler2", Zähler2 )
return msg;

```

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [25 February 2022 17:46 UTC](https://discourse.nodered.org/t/if-else-in-a-function-node/58988/3 "2022-02-25T17:46:55Z")

</div>

Zähler1 will never be greater than 1 and Zähler2 will never be greater than 2.

```auto
var Feuchte = msg.payload;
var Zähler1 = 0; <=== this sets Zähler1 to 0
var Zähler2 = 0; <=== this sets Zähler2 to 0

if (Feuchte >= 69)
{
    Zähler1++; <=== this adds 1 to Zähler1 so it is now 1
    
    if (Zähler1 >= 3) <=== this will never be true since Zähler1 now is 1 
    {
        msg.payload = 1;
        Zähler1 = 0;
    }
}
else if (Feuchte <= 58)
{
    Zähler2++; <=== this adds to Zähler2 so it is now 2
    if (Zähler2 >= 3) <=== this will never be true since Zähler2 now is 2
    {
        msg.payload = 2;
        Zähler2 = 0;
    }
}
return msg;

```

---

<div class="post-metadata">

### Author: ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)
#### Post date: [25 February 2022 17:50 UTC](https://discourse.nodered.org/t/if-else-in-a-function-node/58988/4 "2022-02-25T17:50:37Z")

</div>

As also detailed by @zenofmud.  
your are essentially creating a function scope variable - i.e it only lives during execution.

my post above _should_ address it

---

<div class="post-metadata">

### Author: ![BigHitRider93](https://avatars.discourse-cdn.com/v4/letter/b/9fc348/32.png) [@BigHitRider93](https://discourse.nodered.org/u/BigHitRider93)
#### Post date: [25 February 2022 18:41 UTC](https://discourse.nodered.org/t/if-else-in-a-function-node/58988/5 "2022-02-25T18:41:05Z")

</div>

Thank you very much both of you. I didn't know or forgett about the fact, that with every new message the function node starts at the beginning.

I will try the suggested solution or i will try it with a switch and counter node.

---

<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 April 2022 18:41 UTC](https://discourse.nodered.org/t/if-else-in-a-function-node/58988/6 "2022-04-26T18:41:48Z")

</div>

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