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

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


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 .
You would join manually with count of 2 and every subsequent message.

1 Like

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

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.

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.


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

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

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
}
]

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

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

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

and your switch node can compare them like this
Untitled 4

1 Like

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.

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

This should be correct

[{"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":""}]
1 Like

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