# Help for NR to MQTT by variable msg

**URL:** <https://discourse.nodered.org/t/help-for-nr-to-mqtt-by-variable-msg/93480>\
**Category:** General\
**Created:** [26 November 2024 14:15 UTC](https://discourse.nodered.org/t/help-for-nr-to-mqtt-by-variable-msg/93480 "2024-11-26T14:15:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![stephane.olivier](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@stephane.olivier](https://discourse.nodered.org/u/stephane.olivier)\
**Post date:** [26 November 2024 14:15 UTC](https://discourse.nodered.org/t/help-for-nr-to-mqtt-by-variable-msg/93480/1 "2024-11-26T14:15:25Z")

</div>

Hello,  
Thanks for reading me.  
I can send a message on a Mqtt topic, it works!  
But NR leaves me an error and I don't like that.

Sending code:

> msg.topic = "EffiCompteV2\_485/Action";  
> msg.payload = "getModBus=010600110001:1:19200;";  
> console.log('MANO mqtt--\>\> ' + msg.topic + ' :=: ' + msg.payload);  
> return msg;

Error code:

> MANO mqtt--\>\> EffiCompteV2\_485/Action :=: getModBus=010600110001:1:19200;  
> 26 Nov 15:06:08 - [error] [json:d97050094f96a565] Unexpected token 'g', "getModBus="... is not valid JSON  
> 26 Nov 15:06:08 - [error] [json:d97050094f96a565] Unexpected number in JSON at position 1

Thanks for any help you can give me.

All code:

```auto
[
    {
        "id": "5e310c666522d99f",
        "type": "mqtt out",
        "z": "7b3ba7f3b90d2298",
        "name": "Envoie Mosquitto",
        "topic": "",
        "qos": "0",
        "retain": "",
        "respTopic": "",
        "contentType": "",
        "userProps": "",
        "correl": "",
        "expiry": "",
        "broker": "0d3d3e77a1e17f01",
        "x": 670,
        "y": 220,
        "wires": []
    },
    {
        "id": "ef5f825532812ac7",
        "type": "inject",
        "z": "7b3ba7f3b90d2298",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "str",
        "x": 150,
        "y": 220,
        "wires": [
            [
                "e2ebc0074c7f0543"
            ]
        ]
    },
    {
        "id": "e2ebc0074c7f0543",
        "type": "function",
        "z": "7b3ba7f3b90d2298",
        "name": "Boucle Tab_Action",
        "func": "msg.topic = \"EffiCompteV2_485/Action\";\nmsg.payload = \"getModBus=010600110001:1:19200;\";\nconsole.log('MANO mqtt-->> ' + msg.topic + ' :=: ' + msg.payload);\nreturn msg;\n\n\n\n",
        "outputs": 1,
        "timeout": "",
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 390,
        "y": 220,
        "wires": [
            [
                "5e310c666522d99f"
            ]
        ]
    },
    {
        "id": "0d3d3e77a1e17f01",
        "type": "mqtt-broker",
        "name": "Local",
        "broker": "192.168.1.60",
        "port": "1884",
        "clientid": "",
        "autoConnect": true,
        "usetls": false,
        "protocolVersion": "4",
        "keepalive": "60",
        "cleansession": true,
        "autoUnsubscribe": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthPayload": "",
        "birthMsg": {},
        "closeTopic": "",
        "closeQos": "0",
        "closePayload": "",
        "closeMsg": {},
        "willTopic": "",
        "willQos": "0",
        "willPayload": "",
        "willMsg": {},
        "userProps": "",
        "sessionExpiry": ""
    }
]

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [26 November 2024 14:34 UTC](https://discourse.nodered.org/t/help-for-nr-to-mqtt-by-variable-msg/93480/2 "2024-11-26T14:34:44Z")

</div>

> [@stephane.olivier](#):
>
> d97050094f96a565

Search for that (use CTRL+F) to find which node is giving that error. If it is a JSON node then the error is because that string is not valid JSON.

---

<div class="post-metadata">

**Author:** ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)\
**Post date:** [26 November 2024 14:56 UTC](https://discourse.nodered.org/t/help-for-nr-to-mqtt-by-variable-msg/93480/3 "2024-11-26T14:56:39Z")

</div>

Works for me using Mosquitto MQTT

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [26 November 2024 15:12 UTC](https://discourse.nodered.org/t/help-for-nr-to-mqtt-by-variable-msg/93480/4 "2024-11-26T15:12:37Z")

</div>

I suspect that it is whatever is receiving that data that is complaining.

---

<div class="post-metadata">

**Author:** ![stephane.olivier](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@stephane.olivier](https://discourse.nodered.org/u/stephane.olivier)\
**Post date:** [26 November 2024 16:06 UTC](https://discourse.nodered.org/t/help-for-nr-to-mqtt-by-variable-msg/93480/5 "2024-11-26T16:06:17Z")

</div>

> [@Colin](#):
>
> Search for that (use CTRL+F) to find which node is giving that error. If it is a JSON node then the error is because that string is not valid JSON.

Thanks to all and especially Colin, this helped me a lot.  
The problem actually came from the return of the MQTT message that I also retrieve in NR, but I didn't remember it anymore.

It's solved for me.  
Have a nice day

---

<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:** [10 December 2024 16:06 UTC](https://discourse.nodered.org/t/help-for-nr-to-mqtt-by-variable-msg/93480/6 "2024-12-10T16:06:44Z")

</div>

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