Problems with && in IF-Statement

Hello

I'm originally from the Arduino area, but I'm currently trying my hand at Node-Red because of a semester project.

I have already spent a few hours trying to solve the problem.

My problem is: I get the status of 2 limit switches via two MQTT nodes. "1.00" = open, "0.00" = closed.
When both limit switches are open, I want to send a message, but no message is ever sent.

So I think that && is causing problems or the values from the MQTT are not stored in the two var.

What am I doing wrong?

[
    {
        "id": "85bcebf1bf312ab1",
        "type": "debug",
        "z": "6ce6992560c8505f",
        "name": "debug 5",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 3480,
        "y": 500,
        "wires": []
    },
    {
        "id": "b7c62345c708a650",
        "type": "mqtt in",
        "z": "6ce6992560c8505f",
        "name": "",
        "topic": "esp32/endschstorenoben",
        "qos": "1",
        "datatype": "auto",
        "broker": "8db3fac0.99dd48",
        "nl": false,
        "rap": false,
        "inputs": 0,
        "x": 3430,
        "y": 440,
        "wires": [
            [
                "4b1c77f1eeec5e1e",
                "5735ead9f7d73b21"
            ]
        ]
    },
    {
        "id": "2bd28c74db63ddba",
        "type": "debug",
        "z": "6ce6992560c8505f",
        "name": "debug 8",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 4200,
        "y": 420,
        "wires": []
    },
    {
        "id": "256543966d597159",
        "type": "mqtt in",
        "z": "6ce6992560c8505f",
        "name": "",
        "topic": "esp32/endschstorenunten",
        "qos": "1",
        "datatype": "auto",
        "broker": "8db3fac0.99dd48",
        "nl": false,
        "rap": false,
        "inputs": 0,
        "x": 3430,
        "y": 560,
        "wires": [
            [
                "85bcebf1bf312ab1",
                "4b1c77f1eeec5e1e"
            ]
        ]
    },
    {
        "id": "dfbc9a962399c40c",
        "type": "ui_text",
        "z": "6ce6992560c8505f",
        "group": "868a5dd7b27e20fd",
        "order": 5,
        "width": 0,
        "height": 0,
        "name": "",
        "label": "",
        "format": "{{msg.payload}}",
        "layout": "row-left",
        "className": "",
        "x": 4190,
        "y": 540,
        "wires": []
    },
    {
        "id": "4b1c77f1eeec5e1e",
        "type": "function",
        "z": "6ce6992560c8505f",
        "name": "return a message",
        "func": "if (msg.topic == \"esp32/endschstorenoben\") {\n    var dataoben = msg.payload;\n} else if (msg.topic == \"esp32/endschstorenunten\") {\n    var dataunten = msg.payload;\n}\n\nmsg.payload = {};\n\nif (dataunten === \"1.00\" && dataoben === \"1.00\") {\n    msg.payload = \"Achtung Storen sind unten\";\n    return msg;\n    }\n/*\nif (dataoben === \"0.00\") {\n    msg.payload = \"Achtung Storen sind Oben\";\n    return msg;\n   }\n\nif ( dataunten === \"0.00\" ) {\n   \n   msg.payload = \"Achtung Storen sind unten\";\n    return msg;\n}\n*/\n\n\n\nif (dataoben === \"1.00\") {\n    msg.payload = \"dataoben works\";\n    return msg;\n}\n\nif (dataunten === \"1.00\") {\n\n    msg.payload = \"dataunten works\";\n    return msg;\n}\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 3930,
        "y": 520,
        "wires": [
            [
                "2bd28c74db63ddba",
                "dfbc9a962399c40c"
            ]
        ]
    },
    {
        "id": "5735ead9f7d73b21",
        "type": "debug",
        "z": "6ce6992560c8505f",
        "name": "debug 17",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 3720,
        "y": 380,
        "wires": []
    },
    {
        "id": "8db3fac0.99dd48",
        "type": "mqtt-broker",
        "name": "",
        "broker": "localhost",
        "port": "1883",
        "clientid": "",
        "autoConnect": true,
        "usetls": false,
        "compatmode": false,
        "protocolVersion": "4",
        "keepalive": "60",
        "cleansession": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthPayload": "",
        "birthMsg": {},
        "closeTopic": "",
        "closeQos": "0",
        "closePayload": "",
        "closeMsg": {},
        "willTopic": "",
        "willQos": "0",
        "willPayload": "",
        "willMsg": {},
        "userProps": "",
        "sessionExpiry": ""
    },
    {
        "id": "868a5dd7b27e20fd",
        "type": "ui_group",
        "name": "Storen",
        "tab": "36a54d802e1ee2d2",
        "order": 2,
        "disp": true,
        "width": "6",
        "collapse": false,
        "className": ""
    },
    {
        "id": "36a54d802e1ee2d2",
        "type": "ui_tab",
        "name": "Controlle",
        "icon": "dashboard",
        "order": 2,
        "disabled": false,
        "hidden": false
    }
]

I assume, you misunderstood the nature of Node-RED: Despite you have 2 incoming wires, you'll always process only a single message, either from oben or from unten.

If you like to compare the two values, you need to cache them in a context, like:

context.set("oben", msg.payload)

Later - when the next message is incoming, you may do a

const datoben = context.get("oben")

to retrieve the cached value from the run before...

1 Like

Node-red processes messages 1 by 1, it does not keep track. So your if statement will always be false.
Look at the join node to combine messages into 1 or use context to save/keep track of previous messages.
There is a lot of documentation available in the links in the top.

Thank you very much, with your instructions and the help of the documentation I got it to work.

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