MQTT publishing

When posting device data via MQTT, and since the ClientID is the DeviceID, can one device publish data to other devices topics?

Hey @Lars_Andersson,

I think I know what you’re asking, but please correct me if I’m wrong.

Connection to the clientID will happen regardless, but if you try to report state to a different deviceID than the clientID, the connection to the clientID will break, and no state will be published.

Please let me know if I’ve interpreted incorrectly, or if you have any further questions.

Thank so much!
Heath

I think you understood me correctly.

So there is not another way I could do this?
I’m doing a demo and already have dashboards made using several devices.
But now I want to test another device submitting data in place of several devices. Does it help if I create a gate device that will do this?

Hey @Lars_Andersson,

There are two options based on your use case:

  1. You can set up a gateway/peripherals relationship to for your devices. Like you mentioned, the gateway can report state on behalf of your peripherals.
  2. You could set up a custom topic to accept state data for all of the devices. You would trigger the custom topic from the broker and include your device information and values in the JSON. You can then add nodes to “route” your workflow and report state for the devices.

If you have any additional information on your use case, please let me know, just in case neither of the above work for you.

Thank you!
Heath

So if I go with option 1 and create a new deviceID as a gateway type deice, will I then be able to use the gateway’s deviceID as MQTT client ID and publish data for the other device ID’s as part of the /state topic?

@Lars_Andersson,

Unfortunately, no. To do this, you would need to set up a custom topic as I mentioned above in option 2.

Thanks!
Heath

Can you show some screenshots of how I would do that?

Hey @Lars_Andersson!

Below are some screenshots on how you can set a custom MQTT topic and set up a workflow to report state for devices. It is important to note that for this example, the MQTT message included a “devices” array in the message.

First, with an MQTT node, you can set the custom MQTT topic.

Once you’re sending MQTT messages to that topic, you can use a JSON: Decode node to format the string to an Object.

Using a Loop Node, you can loop through all the devices in the “devices” array sent along with the MQTT package.

The Loop Node can then report state to each deviceID listed in the “devices” array.

Here’s the example payload that was used to trigger this workflow.

{
  "devices": [ 
{
"deviceId":"5d35d2ece9bcf1000957fa02",
    "temperature": 72
  },
{
"deviceId":"5dcedf089285680007ea7974",
    "temperature": 40
  } 
] 
}

And, here’s the workflow export:
mqtt-state-ingest-develop.flow (3.2 KB)

Thanks,
Heath

I’m trying to change the “additional mqtt topic access” values for an access key assigned to a device.
I selected “Only the following additional topic” and entered “devices” as the topic name, and “Publish” in topic access.

When trying to publish from this device , should I specify topic as : “losant/{{deviceID}}/devices” or something else?

When I try this I see permission errors in the device debug log.

@Lars_Andersson,

When granting access to additional MQTT topics, they can only be non-device MQTT topics, and not MQTT system or Losant-specific topics. In your above example, including the word Losant in that path will result in permission errors.

The docs can help explain this a bit more: https://docs.losant.com/applications/access-keys/#additional-mqtt-topics-access

If you are creating a custom topic, you can follow Heath’s workflow example above (option 2), and the topic subscription path in the configuration could be for example: myapplication/{{deviceID}}/devices.

Thanks,
@Aidan_Zebertavage

before I received your reply, I tested with just using topic “devices” from the device, and that worked!

What if someone else would use that same topic? Is that possible?
Or is it still tied to the client ID used so that can’t happen?

@Lars_Andersson,

Glad you were able to get it working.

In response to your follow up question, the custom topic is scoped only to your application and therefore the client ID associated with your application. So even if another user defined that custom topic, it would only be referencing values within their application.

Thanks,
@Aidan_Zebertavage