# Insert a mqtt tree ( directories? )

**URL:** https://discourse.nodered.org/t/insert-a-mqtt-tree-directories/80385
**Category:** General
**Tags:** home-assistant
**Created:** [7 August 2023 17:47 UTC](https://discourse.nodered.org/t/insert-a-mqtt-tree-directories/80385 "2023-08-07T17:47:03Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![plekke](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/plekke/32/81640_2.png) [@plekke](https://discourse.nodered.org/u/plekke)
#### Post date: [7 August 2023 17:47 UTC](https://discourse.nodered.org/t/insert-a-mqtt-tree-directories/80385/1 "2023-08-07T17:47:03Z")

</div>

hello ,

i'm learning nodered and mqtt.  
i use also homeassistant , and when i create an esp32node with mqtt enabled , then i got a very nice tree-structure in mqtt-explorer

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

now i want to create some data to send to my mqtt broker , but as test i want to inject some hardcoded values to my broker.  
but it need to have a nice tree-strucure like in this image above.

my first test is complete differtent result in mqtt-explorer :

![](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/e/9ec8e2064e6321806dc1eedbdd31eb42d0e9fc6a.png)  
 ![](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/f/0f16e03d549a621b35504d9fb66a7f95dbd8e577.png)  
 ![](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/f/6f1ca1ba0612fe1e298e13aa9b99744ca9b5c829.png)  
how can i create a kind of directory and subdirectorries ?

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [7 August 2023 17:52 UTC](https://discourse.nodered.org/t/insert-a-mqtt-tree-directories/80385/2 "2023-08-07T17:52:39Z")

</div>

The 'tree' structure is just the parts of the topic in the msg being published. The topic. is broken into levels with a slash `/`.

You should spend some time and read thru this tutorial [MQTT Essentials - All Core Concepts explained](https://www.hivemq.com/mqtt-essentials/)

---

<div class="post-metadata">

### Author: ![plekke](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/plekke/32/81640_2.png) [@plekke](https://discourse.nodered.org/u/plekke)
#### Post date: [7 August 2023 18:07 UTC](https://discourse.nodered.org/t/insert-a-mqtt-tree-directories/80385/3 "2023-08-07T18:07:50Z")

</div>

is there no easy flow to generate a mqtt wit a 'directory structure with values ' with 1inject node ?

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [7 August 2023 18:20 UTC](https://discourse.nodered.org/t/insert-a-mqtt-tree-directories/80385/4 "2023-08-07T18:20:32Z")

</div>

It is very easy to create a topic in a msg with various levels for example setting msg.topic to:

`home/firstFloor/livingroom/northwall/switch`

but really you should go read the tutorial, it examines a lot and has a complete section on topics and hoe to subscribe to them. It will be worth your time to learn the terminology and get an understanding of how the pub/sub process works.

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [7 August 2023 20:17 UTC](https://discourse.nodered.org/t/insert-a-mqtt-tree-directories/80385/5 "2023-08-07T20:17:41Z")

</div>

I like to reproduce internal JSON structures to equivalent trees in MQTT. The best way is to create a function node that walks through the JSON input and outputs the topic/value pairs. Done right, you only need to do it once and then you should be able to reuse it.

I guess I should probably write a node for that at some point unless someone has beaten me to it.

---

<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: [7 August 2023 20:35 UTC](https://discourse.nodered.org/t/insert-a-mqtt-tree-directories/80385/6 "2023-08-07T20:35:46Z")

</div>

> [@plekke](#):
>
> is there no easy flow to generate a mqtt wit a 'directory structure with values ' with 1inject node ?

As a matter of interest, why do you want to do this? Under what circumstances?

---

<div class="post-metadata">

### Author: ![plekke](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/plekke/32/81640_2.png) [@plekke](https://discourse.nodered.org/u/plekke)
#### Post date: [7 August 2023 20:48 UTC](https://discourse.nodered.org/t/insert-a-mqtt-tree-directories/80385/7 "2023-08-07T20:48:47Z")

</div>

i want to send a simple hardcode big mqtt structure , so i can test my recievers and other flows.

just a matter to inject something to "debug/unit tes"t my recieving flows

---

<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: [7 August 2023 20:57 UTC](https://discourse.nodered.org/t/insert-a-mqtt-tree-directories/80385/8 "2023-08-07T20:57:00Z")

</div>

OK, good reason. In a function node you can do something like

```auto
node.send({topic: "a/b/c1", payload: "somevalue"})
node.send({topic: "a/b/c2", payload: "someothervalue"})
etc.

```

---

<div class="post-metadata">

### Author: ![plekke](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/plekke/32/81640_2.png) [@plekke](https://discourse.nodered.org/u/plekke)
#### Post date: [8 August 2023 15:34 UTC](https://discourse.nodered.org/t/insert-a-mqtt-tree-directories/80385/9 "2023-08-08T15:34:02Z")

</div>

thank u Colin

---

<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: [22 August 2023 15:34 UTC](https://discourse.nodered.org/t/insert-a-mqtt-tree-directories/80385/10 "2023-08-22T15:34:21Z")

</div>

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