# How to compare two values (number input) and(mqtt node) and return true if they're equal

**URL:** https://discourse.nodered.org/t/how-to-compare-two-values-number-input-and-mqtt-node-and-return-true-if-theyre-equal/60945
**Category:** Share Your Nodes
**Tags:** mqtt
**Created:** [7 April 2022 15:05 UTC](https://discourse.nodered.org/t/how-to-compare-two-values-number-input-and-mqtt-node-and-return-true-if-theyre-equal/60945 "2022-04-07T15:05:02Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![emnabk\_12](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/emnabk_12/32/59752_2.png) [@emnabk\_12](https://discourse.nodered.org/u/emnabk_12)
#### Post date: [7 April 2022 15:05 UTC](https://discourse.nodered.org/t/how-to-compare-two-values-number-input-and-mqtt-node-and-return-true-if-theyre-equal/60945/1 "2022-04-07T15:05:02Z")

</div>

Hey, I want to compare an entering value of a sensor in MQTT node with a number I choose from dashboard and return "True" if they're equal, this didn't seem to work with the Switch node, and even with the function node I couldn't obtain a correct result.  
First things first, even the join node didn't work as shown below:

 ![Error1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/7/27f9bbfa40beffcb96770c3784e251cccc29f8de.png)  
Second, when I try to work without the join node and without mqtt node, with only two number inputs to see how the function works, it gives me false results as well :  
 ![Error3](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/9/19fc93ef04b1307fe4275af761cf60035b1a4f43.png)  
 ![Error4](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/1/b1bf121e9d56bd4a9e3225f30cc6e390df1bcdfd.png)  
Here's the function:  
 ![Error5](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/d/9dfe14e81e62155341cdd55c52c0566f5b4493c5.png)  
And here's the flow :

````auto
    {
        "id": "fcd2461e8006fcee",
        "type": "debug",
        "z": "5827c9d4d1c8df58",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 670,
        "y": 240,
        "wires": []
    },
    {
        "id": "f412802581850e86",
        "type": "ui_text_input",
        "z": "5827c9d4d1c8df58",
        "name": "",
        "label": "",
        "tooltip": "",
        "group": "461e2ece4e14a1f0",
        "order": 0,
        "width": 0,
        "height": 0,
        "passthru": true,
        "mode": "number",
        "delay": 300,
        "topic": "input",
        "sendOnBlur": true,
        "topicType": "msg",
        "x": 110,
        "y": 160,
        "wires": [
            [
                "ec8a10572ea4a3f6"
            ]
        ]
    },
    {
        "id": "562da58c24b1338e",
        "type": "function",
        "z": "5827c9d4d1c8df58",
        "name": "",
        "func": "context.node = context.node || 0;\ncontext.node1 = context.node1 || 0;\nif(msg.topic == 'input') {\n context.node = msg.payload;\n}\nelse if(msg.topic == 'esp32') {\n context.node1 = msg.payload;\n}\nif ( context.node == context.node1 ) {\n return {topic: 'result_true' , payload: 'True'}\n}\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 480,
        "y": 160,
        "wires": [
            [
                "fcd2461e8006fcee"
            ]
        ]
    },
    {
        "id": "a984d08bac04a505",
        "type": "mqtt in",
        "z": "5827c9d4d1c8df58",
        "name": "",
        "topic": "esp32",
        "qos": "1",
        "datatype": "auto",
        "broker": "21cdba4a.be7146",
        "nl": false,
        "rap": true,
        "rh": 0,
        "x": 140,
        "y": 220,
        "wires": [
            [
                "ec8a10572ea4a3f6"
            ]
        ]
    },
    {
        "id": "ec8a10572ea4a3f6",
        "type": "join",
        "z": "5827c9d4d1c8df58",
        "name": "",
        "mode": "custom",
        "build": "object",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": "\\n",
        "joinerType": "str",
        "accumulate": true,
        "timeout": "",
        "count": "2",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "",
        "reduceFixup": "",
        "x": 310,
        "y": 160,
        "wires": [
            [
                "562da58c24b1338e"
            ]
        ]
    },
    {
        "id": "461e2ece4e14a1f0",
        "type": "ui_group",
        "name": "Group 1",
        "tab": "ea5e7575d0f9638f",
        "order": 1,
        "disp": true,
        "width": 6
    },
    {
        "id": "21cdba4a.be7146",
        "type": "mqtt-broker",
        "name": "",
        "broker": " 192.168.1.16",
        "port": "1883",
        "clientid": "",
        "usetls": false,
        "keepalive": "60",
        "cleansession": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthPayload": "",
        "closeTopic": "",
        "closeQos": "0",
        "closePayload": "",
        "willTopic": "",
        "willQos": "0",
        "willPayload": ""
    },
    {
        "id": "ea5e7575d0f9638f",
        "type": "ui_tab",
        "name": "Tab 6",
        "icon": "dashboard",
        "order": 6
    }
] ```
````

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [7 April 2022 15:15 UTC](https://discourse.nodered.org/t/how-to-compare-two-values-number-input-and-mqtt-node-and-return-true-if-theyre-equal/60945/2 "2022-04-07T15:15:22Z")

</div>

Your topic in the number input is not set correct.

No need for context storage, here is an example

```auto
[{"id":"f412802581850e86","type":"ui_text_input","z":"bf9e1e33.030598","name":"","label":"","tooltip":"","group":"461e2ece4e14a1f0","order":0,"width":0,"height":0,"passthru":true,"mode":"number","delay":300,"topic":"input","sendOnBlur":true,"className":"","topicType":"str","x":150,"y":2680,"wires":[["ec8a10572ea4a3f6"]]},{"id":"ec8a10572ea4a3f6","type":"join","z":"bf9e1e33.030598","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":350,"y":2680,"wires":[["b525f638.c84768"]]},{"id":"a984d08bac04a505","type":"mqtt in","z":"bf9e1e33.030598","name":"","topic":"esp32","qos":"1","datatype":"auto","broker":"21cdba4a.be7146","x":180,"y":2740,"wires":[["ec8a10572ea4a3f6"]]},{"id":"b525f638.c84768","type":"function","z":"bf9e1e33.030598","name":"","func":"msg.payload = msg.payload.esp32 === msg.payload.input\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":520,"y":2680,"wires":[["fcd2461e8006fcee"]]},{"id":"fcd2461e8006fcee","type":"debug","z":"bf9e1e33.030598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":710,"y":2760,"wires":[]},{"id":"461e2ece4e14a1f0","type":"ui_group","name":"Group 1","tab":"ea5e7575d0f9638f","order":1,"disp":true,"width":6},{"id":"21cdba4a.be7146","type":"mqtt-broker","name":"","broker":" 192.168.1.16","port":"1883","clientid":"","usetls":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"ea5e7575d0f9638f","type":"ui_tab","name":"Tab 6","icon":"dashboard","order":6}]

```

Assuming that both inputs are numbers.

---

<div class="post-metadata">

### Author: ![emnabk\_12](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/emnabk_12/32/59752_2.png) [@emnabk\_12](https://discourse.nodered.org/u/emnabk_12)
#### Post date: [7 April 2022 15:31 UTC](https://discourse.nodered.org/t/how-to-compare-two-values-number-input-and-mqtt-node-and-return-true-if-theyre-equal/60945/3 "2022-04-07T15:31:24Z")

</div>

It worked, thanks again.

---

<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: [21 April 2022 15:32 UTC](https://discourse.nodered.org/t/how-to-compare-two-values-number-input-and-mqtt-node-and-return-true-if-theyre-equal/60945/4 "2022-04-21T15:32:04Z")

</div>

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