# How can I compare MQTT node value to a normal input number value?

**URL:** https://discourse.nodered.org/t/how-can-i-compare-mqtt-node-value-to-a-normal-input-number-value/60841
**Category:** Share Your Nodes
**Tags:** mqtt
**Created:** [5 April 2022 17:12 UTC](https://discourse.nodered.org/t/how-can-i-compare-mqtt-node-value-to-a-normal-input-number-value/60841 "2022-04-05T17:12:15Z")
**Posts on this page:** 12
**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: [5 April 2022 17:12 UTC](https://discourse.nodered.org/t/how-can-i-compare-mqtt-node-value-to-a-normal-input-number-value/60841/1 "2022-04-05T17:12:15Z")

</div>

Hello all, How am I supposed to compare these two values, is it possible to use the switch node like that?

 ![Screenshot 2022-04-05 180331](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/c/3cec603add211e54e3396caf92688f13c79f5401.png)  
 ![Screenshot 2022-04-05 180555](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/7/87ffc2e2822691a7f3f0e7f5437eaa2e9b562510.png)

---

<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: [5 April 2022 17:29 UTC](https://discourse.nodered.org/t/how-can-i-compare-mqtt-node-value-to-a-normal-input-number-value/60841/2 "2022-04-05T17:29:30Z")

</div>

messages arrive a different times, so to compare you would need to join the messages with a join node, or save the values to context storage.  
there is an example of joining here [Create a single message from separate streams of messages : Node-RED](https://cookbook.nodered.org/basic/join-streams) .  
You would join manually with count of 2 and every subsequent message.

---

<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: [5 April 2022 17:58 UTC](https://discourse.nodered.org/t/how-can-i-compare-mqtt-node-value-to-a-normal-input-number-value/60841/3 "2022-04-05T17:58:27Z")

</div>

So after I join the two nodes the switch node is going to automatically compare them? is this correct?

 ![Screenshot 2022-04-05 185728](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/7/674429b5dd5b94410a70c739bd94cfd879cce50c.png)

---

<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: [5 April 2022 18:34 UTC](https://discourse.nodered.org/t/how-can-i-compare-mqtt-node-value-to-a-normal-input-number-value/60841/4 "2022-04-05T18:34:34Z")

</div>

When posting flows it is best to export and paste code here, so it is then possible to see the configs of the nodes.

I have no idea what topics you have used, lets guess and say number input you gave topic `number`, and mqtt you give topic `mqtt`.

Then in switch you would set property to msg.payload.number, and the rule you would use msg.payload.mqtt.

Again it is hard to help from images.

---

<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: [5 April 2022 18:56 UTC](https://discourse.nodered.org/t/how-can-i-compare-mqtt-node-value-to-a-normal-input-number-value/60841/5 "2022-04-05T18:56:26Z")

</div>

Ok here are the configurations of the 3 nodes mqtt in , input number and switch,  
and that is only a little part of the whole project, i put them separately in another flow so they appear clearer, I never used the switch-node to compare with a value of an MQTT node that's why I got a little confused.

 ![Screenshot 2022-04-05 195324](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/1/3175fbc4c1fa77d21ce63f54a6c1520350a5a4a0.png)  
 ![Screenshot 2022-04-05 195021](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/d/3d595b7748e69bb7aec06b508b6a6204bd554a07.png)

But I think It's kinda clear to me rightnow, thanks.

---

<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: [5 April 2022 18:58 UTC](https://discourse.nodered.org/t/how-can-i-compare-mqtt-node-value-to-a-normal-input-number-value/60841/6 "2022-04-05T18:58:10Z")

</div>

Not quite, again please export your flow so i can edit and show you.

The number input topic needs to be a string not a message. unless you know the msg value. You would then use the string or string value of the number input topic in the switch node property.  
Also you have set the rule to string but it needs to be msg

---

<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: [5 April 2022 19:10 UTC](https://discourse.nodered.org/t/how-can-i-compare-mqtt-node-value-to-a-normal-input-number-value/60841/7 "2022-04-05T19:10:42Z")

</div>

Ok i was gonna send you the flow but he said new users can't send attachments so here's the flow written in jason:  
[  
{  
"id": "5a927e6f6d2d0bde",  
"type": "ui\_text\_input",  
"z": "aa7081bf2afb70c2",  
"name": "",  
"label": "",  
"tooltip": "",  
"group": "8aa84a9188f7dbd5",  
"order": 1,  
"width": 0,  
"height": 0,  
"passthru": true,  
"mode": "number",  
"delay": "900",  
"topic": "topic",  
"sendOnBlur": true,  
"topicType": "msg",  
"x": 230,  
"y": 60,  
"wires": [  
[  
"3607bc9bb9a12003"  
]  
]  
},  
{  
"id": "8aa84a9188f7dbd5",  
"type": "ui\_group",  
"name": "Group 1",  
"tab": "759f4ab73c8d5f48",  
"order": 1,  
"disp": true,  
"width": "6",  
"collapse": false,  
"className": ""  
},  
{  
"id": "759f4ab73c8d5f48",  
"type": "ui\_tab",  
"name": "Tab 5",  
"icon": "dashboard",  
"order": 5  
}  
]

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [5 April 2022 19:11 UTC](https://discourse.nodered.org/t/how-can-i-compare-mqtt-node-value-to-a-normal-input-number-value/60841/8 "2022-04-05T19:11:31Z")

</div>

Messages from your mqtt-in node already have a msg.topic: "esp32/waterflow/volume"  
You have to **make** your number input node explicitly set msg.topic - for example "myinput"  
 ![Untitled 5](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/5/05a76714439575ad9dedfe91850404563630cb10.jpeg)

Then msg.payload that comes from the join node will contain something like this

```auto
{
"esp32/waterflow/volume":75,
"myinput":75
}

```

and your switch node can compare them like this  
 ![Untitled 4](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/3/831b226a8ddfc8a66afc9c0fc41808d9b1025dd4.jpeg)

---

<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: [5 April 2022 19:13 UTC](https://discourse.nodered.org/t/how-can-i-compare-mqtt-node-value-to-a-normal-input-number-value/60841/9 "2022-04-05T19:13:29Z")

</div>

That is just 1 node. Highlight all nodes you wish to export by holding ctrl key and clicking  
The when posting us the \</\> button to paste code in post. No need for attachments just post as said above.

---

<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: [5 April 2022 19:17 UTC](https://discourse.nodered.org/t/how-can-i-compare-mqtt-node-value-to-a-normal-input-number-value/60841/10 "2022-04-05T19:17:29Z")

</div>

```auto
[
    {
        "id": "5a927e6f6d2d0bde",
        "type": "ui_text_input",
        "z": "aa7081bf2afb70c2",
        "name": "",
        "label": "",
        "tooltip": "",
        "group": "8aa84a9188f7dbd5",
        "order": 1,
        "width": 0,
        "height": 0,
        "passthru": true,
        "mode": "number",
        "delay": "900",
        "topic": "topic",
        "sendOnBlur": true,
        "topicType": "msg",
        "x": 230,
        "y": 60,
        "wires": [
            [
                "3607bc9bb9a12003"
            ]
        ]
    },
    {
        "id": "e0f27c79400ac30f",
        "type": "mqtt in",
        "z": "aa7081bf2afb70c2",
        "name": "Volume",
        "topic": "esp32/waterflow/volume",
        "qos": "1",
        "datatype": "auto",
        "broker": "4d4355ab26e27db7",
        "nl": false,
        "rap": true,
        "rh": 0,
        "x": 230,
        "y": 220,
        "wires": [
            [
                "c70dcd7da4e8c961",
                "3607bc9bb9a12003"
            ]
        ]
    },
    {
        "id": "c952372d63af2407",
        "type": "switch",
        "z": "aa7081bf2afb70c2",
        "name": "",
        "property": "payload.topic",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "msg.payload.",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 1,
        "x": 610,
        "y": 160,
        "wires": [
            [
                "d802b6b7c76c2935"
            ]
        ]
    },
    {
        "id": "d802b6b7c76c2935",
        "type": "mqtt out",
        "z": "aa7081bf2afb70c2",
        "name": "",
        "topic": "esp32/valve",
        "qos": "1",
        "retain": "",
        "respTopic": "",
        "contentType": "",
        "userProps": "",
        "correl": "",
        "expiry": "",
        "broker": "4d4355ab26e27db7",
        "x": 830,
        "y": 80,
        "wires": []
    },
    {
        "id": "c70dcd7da4e8c961",
        "type": "ui_gauge",
        "z": "aa7081bf2afb70c2",
        "name": "",
        "group": "8aa84a9188f7dbd5",
        "order": 1,
        "width": 0,
        "height": 0,
        "gtype": "gage",
        "title": "gauge",
        "label": "units",
        "format": "{{value}}",
        "min": 0,
        "max": 10,
        "colors": [
            "#00b500",
            "#e6e600",
            "#ca3838"
        ],
        "seg1": "",
        "seg2": "",
        "className": "",
        "x": 670,
        "y": 240,
        "wires": []
    },
    {
        "id": "3607bc9bb9a12003",
        "type": "join",
        "z": "aa7081bf2afb70c2",
        "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": 390,
        "y": 120,
        "wires": [
            [
                "c952372d63af2407"
            ]
        ]
    },
    {
        "id": "8aa84a9188f7dbd5",
        "type": "ui_group",
        "name": "Group 1",
        "tab": "759f4ab73c8d5f48",
        "order": 1,
        "disp": true,
        "width": "6",
        "collapse": false,
        "className": ""
    },
    {
        "id": "4d4355ab26e27db7",
        "type": "mqtt-broker",
        "name": "",
        "broker": " 192.168.1.16",
        "port": "1883",
        "clientid": "",
        "usetls": false,
        "protocolVersion": "4",
        "keepalive": "60",
        "cleansession": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthPayload": "",
        "birthMsg": {},
        "closeTopic": "",
        "closeQos": "0",
        "closePayload": "",
        "closeMsg": {},
        "willTopic": "",
        "willQos": "0",
        "willPayload": "",
        "willMsg": {},
        "sessionExpiry": ""
    },
    {
        "id": "759f4ab73c8d5f48",
        "type": "ui_tab",
        "name": "Tab 5",
        "icon": "dashboard",
        "order": 5
    }
]

```

---

<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: [5 April 2022 19:19 UTC](https://discourse.nodered.org/t/how-can-i-compare-mqtt-node-value-to-a-normal-input-number-value/60841/11 "2022-04-05T19:19:40Z")

</div>

This should be correct

```auto
[{"id":"d3e0ee50.10c0f","type":"ui_text_input","z":"bf9e1e33.030598","name":"","label":"","tooltip":"","group":"8aa84a9188f7dbd5","order":1,"width":0,"height":0,"passthru":true,"mode":"number","delay":"900","topic":"number","sendOnBlur":true,"className":"","topicType":"str","x":90,"y":2800,"wires":[["aa79dca8.c34ee8"]]},{"id":"aa79dca8.c34ee8","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":250,"y":2860,"wires":[["325554f0.c8fba4"]]},{"id":"df41cd21.735e48","type":"mqtt in","z":"bf9e1e33.030598","name":"Volume","topic":"esp32/waterflow/volume","qos":"1","datatype":"auto","broker":"21cdba4a.be7146","x":90,"y":2960,"wires":[["6f24b59e.6d919c","aa79dca8.c34ee8"]]},{"id":"325554f0.c8fba4","type":"switch","z":"bf9e1e33.030598","name":"","property":"payload.esp32/waterflow/volume","propertyType":"msg","rules":[{"t":"eq","v":"payload.number","vt":"msg"}],"checkall":"true","repair":false,"outputs":1,"x":470,"y":2900,"wires":[["26283b3e.8683fc"]]},{"id":"6f24b59e.6d919c","type":"ui_gauge","z":"bf9e1e33.030598","name":"","group":"8aa84a9188f7dbd5","order":1,"width":0,"height":0,"gtype":"gage","title":"gauge","label":"units","format":"{{value}}","min":0,"max":10,"colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","className":"","x":530,"y":2980,"wires":[]},{"id":"26283b3e.8683fc","type":"mqtt out","z":"bf9e1e33.030598","name":"","topic":"esp32/valve","qos":"1","retain":"","broker":"21cdba4a.be7146","x":690,"y":2820,"wires":[]},{"id":"8aa84a9188f7dbd5","type":"ui_group","name":"Group 1","tab":"759f4ab73c8d5f48","order":1,"disp":true,"width":"6","collapse":false,"className":""},{"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":""}]

```

---

<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: [19 April 2022 19:19 UTC](https://discourse.nodered.org/t/how-can-i-compare-mqtt-node-value-to-a-normal-input-number-value/60841/12 "2022-04-19T19:19:46Z")

</div>

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