Mqtt issue setup error

Hi,
I get "Error: Invalid topic specified" with my mqtt server but the topic is injected from the nodes.
I know it was working before so, not sure what I changed to cause the error.
I appreciate some help.
TIA

Have you put a debug node before to ensure the msg going into the node actually has a topic?

If the issue is intermittent, use a catch node (pointed at the problematic mqtt node) and send that to a debug node (set to show the complete message AND to output to console).

Show us what you get when it re-occurs.

It's not intermittent, can be reproduced every time.

18/10/2022, 20:38:02[node: mqtt server](http://192.168.1.220:1880/#)
msg : error
"Error: Invalid topic specified"
18/10/2022, 20:38:03[node: debug 3](http://192.168.1.220:1880/#)casa/rp2/soil/moist : msg : Object
object
payload: "p2_on"
event: object
clientX: 759
clientY: 144
bbox: array[4]
socketid: "dfK_P9KVRJq-iX5kAAAD"
_msgid: "83f74b1eef85b140"
label: "Pump 2"
background: "#4b7930"
topic: "casa/rp2/soil/moist"

I can see that msg has a topic from "node: debug 3" - but I have no idea where in your flows that was generated.

please show your flow and indicate where the error is generated and caught.


Also...
Please use the "Copy Value" button when grabbing the msg so we can see valid JSON/complete info
BX00Cy7yHi

Ok, here it is. Thanks.

{"payload":"p2_on","event":{"clientX":726,"clientY":257,"bbox":[644,281,800,233]},"socketid":"dfK_P9KVRJq-iX5kAAAD","_msgid":"2ff65355b81eac5a","label":"Pump 2","background":"#4b7930","topic":"casa/rp2/soil/pumps"}

{"clientX":726,"clientY":257,"bbox":[644,281,800,233]}

[644,281,800,233]

[
    {
        "id": "451ef45263984c42",
        "type": "function",
        "z": "af3ff860e238ed8f",
        "name": "With icon on press",
        "func": "if (msg.payload == \"X\")\n{\n    msg.payload = \"0\";\n    msg.label  = \"Activated\";\n    msg.background = \"#da3232\";\n}\nelse\n{\n    msg.payload = \"1\";\n    msg.label  = \"Pump 2\";\n    msg.background = \"#4b7930\";\n}\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "\n",
        "finalize": "",
        "libs": [],
        "x": 150,
        "y": 640,
        "wires": [
            [
                "01190d4244adf4d6",
                "9ebd569304b8e7ec",
                "46acd6e80b4dbce2"
            ]
        ]
    },
    {
        "id": "01190d4244adf4d6",
        "type": "function",
        "z": "af3ff860e238ed8f",
        "name": "Translate",
        "func": "if (msg.payload == \"1\")\n{\n    msg.payload = \"p2_on\";\n    msg.topic = \"casa/rp2/soil/pumps\";\n}\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 420,
        "y": 640,
        "wires": [
            [
                "3caf5e983a00d017",
                "077f063d542393a5"
            ]
        ]
    },
    {
        "id": "9ebd569304b8e7ec",
        "type": "switch",
        "z": "af3ff860e238ed8f",
        "name": "",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "0",
                "vt": "num"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 1,
        "x": 150,
        "y": 720,
        "wires": [
            [
                "5367e1119788e871"
            ]
        ]
    },
    {
        "id": "5367e1119788e871",
        "type": "trigger",
        "z": "af3ff860e238ed8f",
        "name": "",
        "op1": "",
        "op2": "Z",
        "op1type": "nul",
        "op2type": "str",
        "duration": "1",
        "extend": false,
        "overrideDelay": false,
        "units": "s",
        "reset": "",
        "bytopic": "all",
        "topic": "topic",
        "outputs": 1,
        "x": 160,
        "y": 780,
        "wires": [
            [
                "451ef45263984c42"
            ]
        ]
    },
    {
        "id": "46acd6e80b4dbce2",
        "type": "ui_button",
        "z": "af3ff860e238ed8f",
        "name": "btnPump2",
        "group": "87f00ff5ca79e605",
        "order": 6,
        "width": "3",
        "height": "1",
        "passthru": false,
        "label": "{{msg.label}}",
        "tooltip": "",
        "color": "white",
        "bgcolor": "{{msg.background}}",
        "className": "",
        "icon": "https",
        "payload": "X",
        "payloadType": "str",
        "topic": "topic",
        "topicType": "msg",
        "x": 150,
        "y": 560,
        "wires": [
            [
                "451ef45263984c42"
            ]
        ]
    },
    {
        "id": "1dd84fca24fee9a8",
        "type": "inject",
        "z": "af3ff860e238ed8f",
        "name": "Set at start",
        "props": [
            {
                "p": "label",
                "v": "Pump 2",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": true,
        "onceDelay": 0.1,
        "topic": "",
        "x": 150,
        "y": 500,
        "wires": [
            [
                "46acd6e80b4dbce2"
            ]
        ]
    },
    {
        "id": "3caf5e983a00d017",
        "type": "debug",
        "z": "af3ff860e238ed8f",
        "name": "debug 3",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 620,
        "y": 640,
        "wires": []
    },
    {
        "id": "077f063d542393a5",
        "type": "mqtt out",
        "z": "af3ff860e238ed8f",
        "name": "mqtt server",
        "topic": "",
        "qos": "",
        "retain": "",
        "respTopic": "",
        "contentType": "",
        "userProps": "",
        "correl": "",
        "expiry": "",
        "broker": "b4e435ada747d30e",
        "x": 630,
        "y": 580,
        "wires": []
    },
    {
        "id": "87f00ff5ca79e605",
        "type": "ui_group",
        "name": "SOIL",
        "tab": "59f076b601e8190d",
        "order": 8,
        "disp": true,
        "width": "6",
        "collapse": false,
        "className": ""
    },
    {
        "id": "b4e435ada747d30e",
        "type": "mqtt-broker",
        "name": "HIVEMQ",
        "broker": "broker.mqttdashboard.com",
        "port": "1883",
        "clientid": "myClientID",
        "autoConnect": true,
        "usetls": false,
        "compatmode": false,
        "protocolVersion": "5",
        "keepalive": "60",
        "cleansession": true,
        "birthTopic": "",
        "birthQos": "1",
        "birthPayload": "",
        "birthMsg": {},
        "closeTopic": "",
        "closeQos": "0",
        "closePayload": "",
        "closeMsg": {},
        "willTopic": "",
        "willQos": "0",
        "willPayload": "",
        "willMsg": {},
        "userProps": "",
        "sessionExpiry": ""
    },
    {
        "id": "59f076b601e8190d",
        "type": "ui_tab",
        "name": "PICO",
        "icon": "dashboard",
        "order": 2,
        "disabled": false,
        "hidden": false
    }
]

That happens because you do this in the function...

if (msg.payload == "1")
{
    msg.payload = "p2_on";
    msg.topic = "casa/rp2/soil/pumps";
}

return msg;

↑ imagine for a second that msg.payload == '0' - no topic will be set!


It might be better to explain what you actually want to happen.

I want to send out "p2_on" when a button is pressed but, as you can see, I'm flashing the button by changing its color. Maybe I'm complacating things by doing that. However, I'm using the "if" statement to filter any other possibilities.

No, you are "inhibiting msg.payload = "p2_on"; and msg.topic = "casa/rp2/soil/pumps"; but you ALWAYS return the msg.

If you want to prevent the msg getting to the next node, return null

if (msg.payload == "1")
{
    msg.payload = "p2_on";
    msg.topic = "casa/rp2/soil/pumps";
    return msg;
}

return null;

Ok, got rid of the error by modifying the function to

if (msg.payload == "1")
{
    msg.payload = "p2_on";
    msg.topic = "casa/rp2/soil/pumps";
}
else {
    msg.topic = "none";
}
return msg;

but not sure if it's the way to go.

I wouldnt think so - you will be sending a payload of "0" to topic "none" - for no reason.

Return null like I showed if you dont want a transmission

Yes, that works. Didn't catch it at first. Thank you.

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