IF- ELSE in a function node

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.

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:

[
    {
        "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": []
    }
]

Hi @BigHitRider93

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

/*  '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;
1 Like

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

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;
1 Like

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

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.

2 Likes

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