# Problems with && in IF-Statement

**URL:** <https://discourse.nodered.org/t/problems-with-in-if-statement/72602>\
**Category:** General\
**Created:** [22 December 2022 19:56 UTC](https://discourse.nodered.org/t/problems-with-in-if-statement/72602 "2022-12-22T19:56:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dolewan](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dolewan/32/73049_2.png) [@Dolewan](https://discourse.nodered.org/u/Dolewan)\
**Post date:** [22 December 2022 19:56 UTC](https://discourse.nodered.org/t/problems-with-in-if-statement/72602/1 "2022-12-22T19:56:22Z")

</div>

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?

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/6/06a1e3a603489d44e036e1fd33e5e700472c3528.png)

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

```

---

<div class="post-metadata">

**Author:** ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)\
**Post date:** [22 December 2022 20:06 UTC](https://discourse.nodered.org/t/problems-with-in-if-statement/72602/2 "2022-12-22T20:06:50Z")

</div>

> [@Dolewan](#):
>
> What am I doing wrong?

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:

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

```

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

```auto
const datoben = context.get("oben")

```

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

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [22 December 2022 20:07 UTC](https://discourse.nodered.org/t/problems-with-in-if-statement/72602/3 "2022-12-22T20:07:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Dolewan](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dolewan/32/73049_2.png) [@Dolewan](https://discourse.nodered.org/u/Dolewan)\
**Post date:** [22 December 2022 21:08 UTC](https://discourse.nodered.org/t/problems-with-in-if-statement/72602/4 "2022-12-22T21:08:33Z")

</div>

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

---

<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:** [20 February 2023 21:08 UTC](https://discourse.nodered.org/t/problems-with-in-if-statement/72602/5 "2023-02-20T21:08:51Z")

</div>

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