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

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:


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 :


Here's the function:

And here's the flow :

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

Your topic in the number input is not set correct.

No need for context storage, here is an example

[{"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.

1 Like

It worked, thanks again.

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