# Solved: Formatting message with subtopics for mqtt

**URL:** <https://discourse.nodered.org/t/solved-formatting-message-with-subtopics-for-mqtt/41573>\
**Category:** General\
**Created:** [24 February 2021 16:04 UTC](https://discourse.nodered.org/t/solved-formatting-message-with-subtopics-for-mqtt/41573 "2021-02-24T16:04:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ArcherThompson](https://avatars.discourse-cdn.com/v4/letter/a/8e8cbc/32.png) [@ArcherThompson](https://discourse.nodered.org/u/ArcherThompson)\
**Post date:** [24 February 2021 16:04 UTC](https://discourse.nodered.org/t/solved-formatting-message-with-subtopics-for-mqtt/41573/1 "2021-02-24T16:04:53Z")

</div>

I am trying to send a mqtt message where I have a subtopic but I am having no luck getting my message formatted correctly. The main topic contains identifiers and the subtopic will be an array with the name/values. My exact code has lots of private info but this is some sudo code of my message structures

```auto
payload = {
    Id1: "value"
    Id2: "value"
    Id3: "value"
    dimensions: {
        array[]
    }
}

```

I would like to be able access the values only on the mqtt message with main/dimensions. How do I need to reformat this so I can get that?

---

<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:** [24 February 2021 16:35 UTC](https://discourse.nodered.org/t/solved-formatting-message-with-subtopics-for-mqtt/41573/2 "2021-02-24T16:35:33Z")

</div>

Hopefully you mean `msg.payload = {`, which is not what you have written.

> [@ArcherThompson](#):
>
> I would like to be able access the values only on the mqtt message with main/dimensions

I don't understand what you mean by that, can you explain in a bit more detail what you mean? In particular what does the word 'access' mean here. Do you mean subscribe? If so then you need to use the topic to identify that, not something in the payload.

---

<div class="post-metadata">

**Author:** ![ArcherThompson](https://avatars.discourse-cdn.com/v4/letter/a/8e8cbc/32.png) [@ArcherThompson](https://discourse.nodered.org/u/ArcherThompson)\
**Post date:** [24 February 2021 17:39 UTC](https://discourse.nodered.org/t/solved-formatting-message-with-subtopics-for-mqtt/41573/3 "2021-02-24T17:39:22Z")

</div>

Yes, sorry I meat subscribe. The mqtt publisher node sets the topic and that part is fine and I can subscribe to that. The problem is right now all my data has to go to that topic with no hierarchy...which is obviously not a very clean method of doing things.

---

<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:** [24 February 2021 17:55 UTC](https://discourse.nodered.org/t/solved-formatting-message-with-subtopics-for-mqtt/41573/4 "2021-02-24T17:55:44Z")

</div>

If you different types of data then publish them to different topics.

---

<div class="post-metadata">

**Author:** ![ArcherThompson](https://avatars.discourse-cdn.com/v4/letter/a/8e8cbc/32.png) [@ArcherThompson](https://discourse.nodered.org/u/ArcherThompson)\
**Post date:** [24 February 2021 18:11 UTC](https://discourse.nodered.org/t/solved-formatting-message-with-subtopics-for-mqtt/41573/5 "2021-02-24T18:11:50Z")

</div>

That is not particularly useful because I need to know what that data belongs too. Across, for instance, 40 devices the concept or trying to organize that data into 40 different to level topics sounds rather terrible. I have another way I can do this but this seemed to be the easiest way to deploy at scale. However, maybe I misunderstanding what this means:

"Topics are treated as a hierarchy, using a slash (/) as a separator. This allows sensible arrangement of common themes to be created, much in the same way as a filesystem."

> **[MQTT man page](https://mosquitto.org/man/mqtt-7.html)**
>
> Name
> mqtt — MQ Telemetry Transport
> 
> 
> Synopsis
> MQTT 
> 
> 
> Description
> MQTT is a lightweight publish/subscribe
> messaging protocol. It is useful for use with low power sensors, but
> is applicable to man

---

<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:** [24 February 2021 18:27 UTC](https://discourse.nodered.org/t/solved-formatting-message-with-subtopics-for-mqtt/41573/6 "2021-02-24T18:27:00Z")

</div>

If you have multiple similar devices you can publish to `main/dev1`, `main/dev2` for example. Then you can get them all by subscribing to `main/#` or you can subscribe to them individually if you prefer.  
If you not already done so work through the hive mqtt essentials guides. Your search engine will find it.

---

<div class="post-metadata">

**Author:** ![ArcherThompson](https://avatars.discourse-cdn.com/v4/letter/a/8e8cbc/32.png) [@ArcherThompson](https://discourse.nodered.org/u/ArcherThompson)\
**Post date:** [24 February 2021 18:47 UTC](https://discourse.nodered.org/t/solved-formatting-message-with-subtopics-for-mqtt/41573/7 "2021-02-24T18:47:03Z")

</div>

Ok I tried that and subscribing to main/# returns nothing but subscribing to main/dev1 or main/dev2 will return the expected data that was published to them. It is almost like it is just treating main/dev1 as a name. Might just go for a different addressing approach.  
edit: I forgot the"#" silly mistake, no wildcard, no result Thanks, that got it!

---

<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 March 2021 18:48 UTC](https://discourse.nodered.org/t/solved-formatting-message-with-subtopics-for-mqtt/41573/8 "2021-03-10T18:48:02Z")

</div>

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