# Losant MQTT Broker and nodered

**URL:** <https://forums.losant.com/t/losant-mqtt-broker-and-nodered/4257>\
**Category:** Help\
**Tags:** mqtt\
**Created:** [December 7, 2021, 12:51pm UTC](https://forums.losant.com/t/losant-mqtt-broker-and-nodered/4257 "2021-12-07T12:51:21Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Felice\_Massaro](https://avatars.discourse-cdn.com/v4/letter/f/c2a13f/32.png) [@Felice\_Massaro](https://forums.losant.com/u/Felice_Massaro)\
**Post date:** [December 7, 2021, 12:51pm UTC](https://forums.losant.com/t/losant-mqtt-broker-and-nodered/4257/1 "2021-12-07T12:51:21Z")

</div>

Hi There,  
I am trying to publish an MQTT containing a simple string and subscribe the same from a nodered instance on my network.  
On the Losant side I have created the credentials keys which I am using to subscribe the topic on the nodered side and in the MQTT Node I have only written the topic.  
On the nodered side I am using the above credentials and I see that the MQTT node is connected. However I am not receiving anything on the nodered side. I am not sure where the issue is.

 ![Schermata 2021-12-07 alle 14.42.48](https://us1.discourse-cdn.com/flex015/uploads/getstructure/original/2X/a/a9d114622d99bf1d0df112116047cb6becc25e75.png)

 ![Schermata 2021-12-07 alle 14.48.57](https://us1.discourse-cdn.com/flex015/uploads/getstructure/original/2X/c/cf15efd3c201b0413072ec67912f55bc407bf9a9.jpeg)

Any help will be appreciated. Thanks

---

<div class="post-metadata">

**Author:** ![Heath](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/heath/32/3036_2.png) [@Heath](https://forums.losant.com/u/Heath)\
**Post date:** [December 7, 2021, 2:46pm UTC](https://forums.losant.com/t/losant-mqtt-broker-and-nodered/4257/2 "2021-12-07T14:46:25Z")

</div>

@Felice_Massaro,

I’d like to get a better idea of your use case. Could you tell me what your overall goal is with this project? Having this context will help me give you better answers to your questions.

First, I’d like to point out that QoS 2 is not supported for publishing or subscribing on the Losant Broker per [our documentation](https://docs.losant.com/mqtt/overview/#mqtt-version-and-limitations).

Would you be able to share a screenshot of the MQTT output node you have in the workflow you shared?

Can you confirm on the Node-Red side that the subscription is successful?

As I understand it, you are attempting to trigger a workflow with a [webhook](https://docs.losant.com/applications/webhooks/), then publish to a [custom MQTT topic](https://docs.losant.com/mqtt/overview/#custom-topics), is that correct?

[Access Keys & Secrets](https://docs.losant.com/applications/access-keys/#additional-mqtt-topics-access) by default are only scoped to only `losant` topics. You may need to ensure that your access key & secret pair are set to “Only the following topics…” where you list the specific topic, or “All Topics”.

Please let me know if any of this helps, or if you need more clarification. Any additional information that you can provide will also be a big help.

Thank you,  
Heath

---

<div class="post-metadata">

**Author:** ![Felice\_Massaro](https://avatars.discourse-cdn.com/v4/letter/f/c2a13f/32.png) [@Felice\_Massaro](https://forums.losant.com/u/Felice_Massaro)\
**Post date:** [December 7, 2021, 3:29pm UTC](https://forums.losant.com/t/losant-mqtt-broker-and-nodered/4257/3 "2021-12-07T15:29:44Z")

</div>

Hi @Heath,  
My overall goal:  
I have a RaspberryPi in the field to measure a dam level. The data is encoded and transmitted with Sigfox (it is in a rural area in Africa with no other infrastructures). Thanks to your help, yesterday I finally got the Sigfox data of my interest out of the flow. Now I am attempting to publish the string received from Sigfox to an MQTT Broker which I will subscribe from a node in nodered which is running on my network 1800 km away. From there I will use the data.  
This is the MQTT I have at the end of the flow now:

 ![Schermata 2021-12-07 alle 17.17.10](https://us1.discourse-cdn.com/flex015/uploads/getstructure/original/2X/2/25579d703fb2a157e3b6720cda149a4ec75d3a31.png)  
As mentioned in the previous post, I have generated the access keys for this device and used the same as user/password in the MQTT Node subscribing the topic “Sig1” on nodered.  
On nodered I see the green light on the MQTT Node so I assume is connecting to the broker on Losant. But I do not receive any message. I am not sure how and if I should set the message template in the node. May be that’s the problem. But probably it is not the only one?

---

<div class="post-metadata">

**Author:** ![Heath](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/heath/32/3036_2.png) [@Heath](https://forums.losant.com/u/Heath)\
**Post date:** [December 7, 2021, 3:48pm UTC](https://forums.losant.com/t/losant-mqtt-broker-and-nodered/4257/4 "2021-12-07T15:48:38Z")

</div>

Hey @Felice_Massaro,

Thanks for providing that additional context! It’s a huge help, and I’m really interested in your use case.

I gave you some information and things to try out in my previous message. Would you be able to address those items and get back to me?

> On nodered I see the green light on the MQTT Node so I assume is connecting to the broker on Losant.

Node-Red also has a [Debug Node](https://nodered.org/docs/user-guide/nodes#debug). You can use this node in a similar way to the Losant Debug node. This may give you some information on what is happening in your Node-Red workflow.

Additionally, Losant offers a [Gateway Edge Agent](https://docs.losant.com/edge-compute/gateway-edge-agent/overview/). Have you looked into this to see if it fits your use case?

Thank you,  
Heath

---

<div class="post-metadata">

**Author:** ![Felice\_Massaro](https://avatars.discourse-cdn.com/v4/letter/f/c2a13f/32.png) [@Felice\_Massaro](https://forums.losant.com/u/Felice_Massaro)\
**Post date:** [December 8, 2021, 4:22am UTC](https://forums.losant.com/t/losant-mqtt-broker-and-nodered/4257/5 "2021-12-08T04:22:11Z")

</div>

Hi Heath,  
Yes. I am trying to publish an MQTT with the payload from a function node

```auto
payload = payload.working.decoded.data;
return payload

```

the output value I am trying to publish is something like this: “5af3107a1d47”

I have a debug node on the nodered MQTT subscribing the Losant broker but unless I receive a message there is nothing showing me any detail of the connection rather than the green light “connected”.  
I think my problem is on the Losant side where I have used as topic “Sig1” and I have not defined a message template. How do I format a message template to publish the above payload?  
Is the topic in th correct format or should I format it differently?  
Thanks for helping

---

<div class="post-metadata">

**Author:** ![Heath](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/heath/32/3036_2.png) [@Heath](https://forums.losant.com/u/Heath)\
**Post date:** [December 8, 2021, 4:34pm UTC](https://forums.losant.com/t/losant-mqtt-broker-and-nodered/4257/6 "2021-12-08T16:34:50Z")

</div>

@Felice_Massaro,

I want to take a step back and confirm a few things.

Have you changed the QoS level of your topic subscription from 2 to 1 or 0?

Other than seeing the Green Dot in Node-Red, can you confirm on the Losant side (or some other way) that the connection is successful? How is Node-Red connecting to Losant? As a [Device](https://docs.losant.com/devices/overview/)?

Can you also confirm that the Access Key & Secret are set to All Topics?

The reason I ask these questions is that if the connection from Node-Red to Losant were successful, and the subscription is successful, you should (with your current node configuration) would receive a message with an empty body.

Could you send me the full code from your function node? Could you also include a debug message from your workflow as well as a screenshot of your workflow?

Thank you,  
Heath

---

<div class="post-metadata">

**Author:** ![Felice\_Massaro](https://avatars.discourse-cdn.com/v4/letter/f/c2a13f/32.png) [@Felice\_Massaro](https://forums.losant.com/u/Felice_Massaro)\
**Post date:** [December 9, 2021, 6:02am UTC](https://forums.losant.com/t/losant-mqtt-broker-and-nodered/4257/7 "2021-12-09T06:02:11Z")

</div>

@Heath  
Hi. This is the status

Losant side:  
I have added an MQTT message node just after the function which output the string to be published  
Broker: Losant broker  
Topic: Sig1  
Message template: none  
I generated the keys and used the “access key” in th field user on the nodered “MQTT in” node and the “access key” as password.

On the node red side:  
I have deployed an “MQTT in” node  
Action: Subscribe to single topic (Sig1)  
QoS: 0 (I did also try 2)  
Server: mqtt://broker.losant.com:1883 connect automatically  
Protocol: MQTT V3.1.1  
Clinet ID: autogenerated  
User: the key generated by Losant  
Password: the secret generated by Losant

Stus: connecting… but doesn’t connect.  
Do I need to give a path to the node in the topic field on the nodered? What else?  
I am not sure what you mean with this question " How is Node-Red connecting to Losant? As a [Device](https://docs.losant.com/devices/overview/)?"

 ![Schermata 2021-12-09 alle 08.00.01](https://us1.discourse-cdn.com/flex015/uploads/getstructure/original/2X/b/bbf4a7ef76ba77cd9089b1373a124f81d174b8cb.png)  
 ![Schermata 2021-12-09 alle 07.59.30](https://us1.discourse-cdn.com/flex015/uploads/getstructure/original/2X/1/1af93d5da299ef77a71d1d3408ea4ed1be5f7b53.png)

[Application](https://storage.googleapis.com/losant-exports/61ac43d686ea5501fd13a215/export-61ac43d686ea5501fd13a215-1639029104677-sigfoxCallback.zip?GoogleAccessId=production-data-export%40structure-1104.iam.gserviceaccount.com&Expires=1639633905&Signature=MHUPXvycedqUocmYLzSHWqNhJVy8ODzLWUGPYMWMMqn6e0x5VEB6MC7AoIjB5%2FxjfQuxDcHQYu7OmQmOjKHNf5YSyP2bl5O590rGAdAxU7XzhN%2BBqQRRVvN0%2BczbOdlDNB3vCKiWzis69lDSn3bfuw479H9tCpQaswtmOKfHM52HkKaP4hBuaVJ81hKgbdqfodL4DNpdZ5mqNhn9mcEmbmTUaCIu63tYypY%2FNTW8agzrW0tbM%2FVcEJdYVW2LQQQDUfQxXRxCUQnQAVHsmfuRp8dvbi8yRpT41S00bHqOnNCmUj6MNu%2BqFmpCfR7XdR0U%2BvTunFzJ0q5yrMSghFl5qA%3D%3D&response-content-disposition=attachment%3B%20filename%3D%22export-61ac43d686ea5501fd13a215-1639029104677-sigfoxCallback.zip%22)

Nodered

```auto
[{"id":"7a44c1be03c0f9b8","type":"tab","label":"Flow 3","disabled":false,"info":"","env":[]},{"id":"d863e311fe900eb3","type":"debug","z":"7a44c1be03c0f9b8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":390,"y":320,"wires":[]},{"id":"f8641ad69a52f4a2","type":"mqtt in","z":"7a44c1be03c0f9b8","name":"","topic":"Sig1","qos":"0","datatype":"auto","broker":"96e3ac46313ba4cc","nl":false,"rap":true,"rh":0,"inputs":0,"x":180,"y":380,"wires":[["d863e311fe900eb3"]]},{"id":"96e3ac46313ba4cc","type":"mqtt-broker","name":"","broker":"mqtt://broker.losant.com:1883","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""}]

```

---

<div class="post-metadata">

**Author:** ![Heath](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/heath/32/3036_2.png) [@Heath](https://forums.losant.com/u/Heath)\
**Post date:** [December 9, 2021, 2:15pm UTC](https://forums.losant.com/t/losant-mqtt-broker-and-nodered/4257/8 "2021-12-09T14:15:38Z")

</div>

@Felice_Massaro,

I took a look at your application. And have a few things:

- Your single access key is set to “No Additional Topics”. When your access key is set to this, [your device will only have access to device-specific topics, not custom topics like you have](https://docs.losant.com/applications/access-keys/#additional-mqtt-topics-access). As I mentioned, if you are using the same access key for multiple devices (not a best practice), you will need to set this access key/secret to “All topics”, “Only the following…” and add `Sig1` to the list.
- I’m still unsure how Node-Red is connecting to Losant. You have a single device, which I assume is the device you have in the field. No other device for the node-red connection, and no [MQTT Integrations](https://docs.losant.com/applications/integrations/). I would suggest that you create a **new** device with a **new** access key/secret for the Node-Red connection. If you use the same deviceId for multiple MQTT connections, the devices that connects last will disconnect the other device, and if your devices are set to reconnect, then you will end up in a loop of connects and disconnects.

[Here is an older blog post](https://www.losant.com/blog/how-to-use-node-red-as-losant-gateway) on how to connect Node-Red to Losant as a Gateway that should help. As you can see, we create a new device just for the Node-Red connection.

So, my suggestions would be the following:

1. Create a new device in your application for Node-Red
2. Generate a new Access Key/Secret for this device with “All Topics” access selected.
3. Once this new device has connected, then attempt to run your workflow again.

Let me know if this works for you or if you need any clarification.

Thanks,  
Heath

---

<div class="post-metadata">

**Author:** ![Felice\_Massaro](https://avatars.discourse-cdn.com/v4/letter/f/c2a13f/32.png) [@Felice\_Massaro](https://forums.losant.com/u/Felice_Massaro)\
**Post date:** [December 9, 2021, 2:55pm UTC](https://forums.losant.com/t/losant-mqtt-broker-and-nodered/4257/9 "2021-12-09T14:55:24Z")

</div>

Thank you Heath  
I am progressing. Finally after correcting the access key to all topics", I am receiving an empty string on the nodered side. I assume this is because I have not set a message template in the MQTT node.  
Last question (sorry for that), how should I set the message template in the MQTT Node to receive the output of this function ?

payload = payload.working.decoded.data;  
return payload

It is a simple string, something like this "“5af3107a1d47"”

Thank you

---

<div class="post-metadata">

**Author:** ![Heath](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/heath/32/3036_2.png) [@Heath](https://forums.losant.com/u/Heath)\
**Post date:** [December 9, 2021, 3:01pm UTC](https://forums.losant.com/t/losant-mqtt-broker-and-nodered/4257/10 "2021-12-09T15:01:35Z")

</div>

@Felice_Massaro,

The function node you have is not required. Since the JSON: Decode node is already doing the decoding, you don’t need the function node to be able to reference the value on the payload.

So, I would suggest that you remove the function node, and in your message body for the MQTT output node, your message body is just:

```auto
{
  "message": {{working.decoded.data}}
}

```

What you are doing with that function node is replacing the **entire** payload with just the value of `working.decoded.data`.

Thanks,  
Heath

---

<div class="post-metadata">

**Author:** ![Felice\_Massaro](https://avatars.discourse-cdn.com/v4/letter/f/c2a13f/32.png) [@Felice\_Massaro](https://forums.losant.com/u/Felice_Massaro)\
**Post date:** [December 9, 2021, 3:05pm UTC](https://forums.losant.com/t/losant-mqtt-broker-and-nodered/4257/11 "2021-12-09T15:05:35Z")

</div>

Finally I got it working. Thanks a million.
