# Mqtt issue setup error

**URL:** <https://discourse.nodered.org/t/mqtt-issue-setup-error/69212>\
**Category:** General\
**Tags:** mqtt\
**Created:** [18 October 2022 18:09 UTC](https://discourse.nodered.org/t/mqtt-issue-setup-error/69212 "2022-10-18T18:09:38Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![ebolisa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ebolisa/32/89203_2.png) [@ebolisa](https://discourse.nodered.org/u/ebolisa)\
**Post date:** [18 October 2022 18:09 UTC](https://discourse.nodered.org/t/mqtt-issue-setup-error/69212/1 "2022-10-18T18:09:38Z")

</div>

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

 ![img](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/2/62c8cf42b752bf1df02c57f621e465a4c0593d6c.png)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [18 October 2022 18:25 UTC](https://discourse.nodered.org/t/mqtt-issue-setup-error/69212/2 "2022-10-18T18:25:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ebolisa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ebolisa/32/89203_2.png) [@ebolisa](https://discourse.nodered.org/u/ebolisa)\
**Post date:** [18 October 2022 18:41 UTC](https://discourse.nodered.org/t/mqtt-issue-setup-error/69212/3 "2022-10-18T18:41:36Z")

</div>

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

```auto
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"

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [18 October 2022 19:09 UTC](https://discourse.nodered.org/t/mqtt-issue-setup-error/69212/4 "2022-10-18T19:09:33Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/d/bd1f333a9e043b061b39917c328b0094d3e52d30.gif)

---

<div class="post-metadata">

**Author:** ![ebolisa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ebolisa/32/89203_2.png) [@ebolisa](https://discourse.nodered.org/u/ebolisa)\
**Post date:** [18 October 2022 19:22 UTC](https://discourse.nodered.org/t/mqtt-issue-setup-error/69212/5 "2022-10-18T19:22:53Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [18 October 2022 19:43 UTC](https://discourse.nodered.org/t/mqtt-issue-setup-error/69212/6 "2022-10-18T19:43:37Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/7/a759f7674003e8ddaeeca96ee137015c05d8cbc8.png)

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![ebolisa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ebolisa/32/89203_2.png) [@ebolisa](https://discourse.nodered.org/u/ebolisa)\
**Post date:** [18 October 2022 19:54 UTC](https://discourse.nodered.org/t/mqtt-issue-setup-error/69212/7 "2022-10-18T19:54:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [18 October 2022 19:58 UTC](https://discourse.nodered.org/t/mqtt-issue-setup-error/69212/8 "2022-10-18T19:58:49Z")

</div>

> [@ebolisa](#):
>
> 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

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

return null;

```

---

<div class="post-metadata">

**Author:** ![ebolisa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ebolisa/32/89203_2.png) [@ebolisa](https://discourse.nodered.org/u/ebolisa)\
**Post date:** [18 October 2022 20:00 UTC](https://discourse.nodered.org/t/mqtt-issue-setup-error/69212/9 "2022-10-18T20:00:01Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [18 October 2022 20:02 UTC](https://discourse.nodered.org/t/mqtt-issue-setup-error/69212/10 "2022-10-18T20:02:42Z")

</div>

> [@ebolisa](#):
>
> 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

> [@Steve-Mcl](#):
>
> ```auto
> if (msg.payload == "1")
> {
> msg.payload = "p2_on";
> msg.topic = "casa/rp2/soil/pumps";
> return msg;
> }
> 
> return null;
> 
> ```

---

<div class="post-metadata">

**Author:** ![ebolisa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ebolisa/32/89203_2.png) [@ebolisa](https://discourse.nodered.org/u/ebolisa)\
**Post date:** [18 October 2022 20:06 UTC](https://discourse.nodered.org/t/mqtt-issue-setup-error/69212/11 "2022-10-18T20:06:07Z")

</div>

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

---

<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:** [1 November 2022 20:06 UTC](https://discourse.nodered.org/t/mqtt-issue-setup-error/69212/12 "2022-11-01T20:06:32Z")

</div>

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