# Splitting Data and Publishing to MQTT Topic & Message

**URL:** <https://discourse.nodered.org/t/splitting-data-and-publishing-to-mqtt-topic-message/80293>\
**Category:** General\
**Tags:** mqtt\
**Created:** [3 August 2023 13:27 UTC](https://discourse.nodered.org/t/splitting-data-and-publishing-to-mqtt-topic-message/80293 "2023-08-03T13:27:33Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![gettmad](https://avatars.discourse-cdn.com/v4/letter/g/48db29/32.png) [@gettmad](https://discourse.nodered.org/u/gettmad)\
**Post date:** [3 August 2023 13:27 UTC](https://discourse.nodered.org/t/splitting-data-and-publishing-to-mqtt-topic-message/80293/1 "2023-08-03T13:27:33Z")

</div>

Hello,

I feel familiar with Publishing Data to MQTT using an iterative method where an index number is the 'Topic' and the data I am looking to present is the 'Message'.

Ex/  
0= Version 1F34/22  
1= Nextdata 1212/44

This being said, I am looking to take my data and publish it as follows:  
**Version** 1F34/22  
**Nextdata** 1212/44

I have successfully published the data in the desired form as listed above however the '/' is creating a new message underneath the topic and splitting the message into multiple messages underneath the topic. For example,

**Version** 1F34  
22  
**Nextdata** 1212  
44

My function node used to publish the data contains the following code:

```auto
var ioStatusInfo = msg.payload;
flow.set('ioStatusInfo', msg.payload);

for (var i = 0; i < ioStatusInfo.length; i++) {
    var topic = ioStatusInfo[i]
    var payload = ioStatusInfo[i+1]
    node.send({ topic: topic, payload: payload });
}

return msg

```

The msg entering the topic node is a string value within an array. For example,  
UI[16] OFF RSR8/PNS8/STYLE8

Further, to note, I am splitting the string over ']' where 'UI[16]' is the topic and 'OFF RSR8/PNS8/STYLE8' would be the message

Thanks for any help in advance.

---

<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:** [3 August 2023 14:29 UTC](https://discourse.nodered.org/t/splitting-data-and-publishing-to-mqtt-topic-message/80293/2 "2023-08-03T14:29:20Z")

</div>

Can you show us an example message that you are sending to MQTT please. Set the debug node to Output Complete Message.

---

<div class="post-metadata">

**Author:** ![gettmad](https://avatars.discourse-cdn.com/v4/letter/g/48db29/32.png) [@gettmad](https://discourse.nodered.org/u/gettmad)\
**Post date:** [3 August 2023 15:28 UTC](https://discourse.nodered.org/t/splitting-data-and-publishing-to-mqtt-topic-message/80293/3 "2023-08-03T15:28:30Z")

</div>

Thanks for the reply Colin.

This is what enters into my 'Publish' Function Node:

```auto
['F Number', 'F205804 ']
3 Aug 11:22:57 - [info] [debug:debug 364]
['VERSION ', 'LR HandlingTool ']
3 Aug 11:22:57 - [info] [debug:debug 364]
['$VERSION', 'V8.30226 9/27/2017']
3 Aug 11:22:57 - [info] [debug:debug 364]
['DATE', ' 07-JUL-23 03:52 ']

```

This is what leaves the Node:

```auto
{
  topic: 'FANUC Robot/Initial Information/F Number',
  payload: '"F205804 "',
  _msgid: 'f3dcaaa027c1cb42'
}
3 Aug 11:25:36 - [info] [debug:debug 366]
{
  topic: 'FANUC Robot/Initial Information/F205804 ',
  payload: undefined,
  _msgid: 'f3dcaaa027c1cb42'
}
3 Aug 11:25:36 - [info] [debug:debug 366]
{
  payload: ['F Number', 'F205804 '],
  topic: '',
  filename: 'C:\\Users\\PC\\Documents\\FANUC _ FANUC Robotics.html',
  parts: { id: '424a0f96cb3665df', type: 'array', count: 4, len: 1, index: 0 },
  _msgid: 'f3dcaaa027c1cb42'
}
3 Aug 11:25:36 - [info] [debug:debug 366]
{
  topic: 'FANUC Robot/Initial Information/VERSION ',
  payload: '"LR HandlingTool "',
  _msgid: '8e5b7ad3704bbc19'
}
3 Aug 11:25:36 - [info] [debug:debug 366]
{
  topic: 'FANUC Robot/Initial Information/LR HandlingTool ',
  payload: undefined,
  _msgid: '8e5b7ad3704bbc19'
}
3 Aug 11:25:36 - [info] [debug:debug 366]
{
  payload: ['VERSION ', 'LR HandlingTool '],
  topic: '',
  filename: 'C:\\Users\\PC\\Documents\\FANUC _ FANUC Robotics.html',
  parts: { id: '424a0f96cb3665df', type: 'array', count: 4, len: 1, index: 1 },
  _msgid: '8e5b7ad3704bbc19'
}
3 Aug 11:25:36 - [info] [debug:debug 366]
{
  topic: 'FANUC Robot/Initial Information/$VERSION',
  payload: '"V8.30226 9/27/2017"',
  _msgid: 'c73dfa4091d6eae8'
}
3 Aug 11:25:36 - [info] [debug:debug 366]
{
  topic: 'FANUC Robot/Initial Information/V8.30226 9/27/2017',
  payload: undefined,
  _msgid: 'c73dfa4091d6eae8'
}
3 Aug 11:25:36 - [info] [debug:debug 366]
{
  payload: ['$VERSION', 'V8.30226 9/27/2017'],
  topic: '',
  filename: 'C:\\Users\\PC\\Documents\\FANUC _ FANUC Robotics.html',
  parts: { id: '424a0f96cb3665df', type: 'array', count: 4, len: 1, index: 2 },
  _msgid: 'c73dfa4091d6eae8'
}
3 Aug 11:25:36 - [info] [debug:debug 366]
{
  topic: 'FANUC Robot/Initial Information/DATE',
  payload: '" 07-JUL-23 03:52 "',
  _msgid: '7587543be12b3167'
}
3 Aug 11:25:36 - [info] [debug:debug 366]
{
  topic: 'FANUC Robot/Initial Information/ 07-JUL-23 03:52 ',
  payload: undefined,
  _msgid: '7587543be12b3167'
}
3 Aug 11:25:36 - [info] [debug:debug 366]
{
  payload: ['DATE', ' 07-JUL-23 03:52 '],
  topic: '',
  filename: 'C:\\Users\\PC\\Documents\\FANUC _ FANUC Robotics.html',
  parts: { id: '424a0f96cb3665df', type: 'array', count: 4, len: 1, index: 3 },
  _msgid: '7587543be12b3167'
}

```

This is an example of my issue here where only the first message is correct but it is creating duplicate malformed messages for the following 2.

```auto
{
  topic: 'FANUC Robot/Initial Information/$VERSION',
  payload: '"V8.30226 9/27/2017"',
  _msgid: 'c73dfa4091d6eae8'
}
3 Aug 11:25:36 - [info] [debug:debug 366]
{
  topic: 'FANUC Robot/Initial Information/V8.30226 9/27/2017',
  payload: undefined,
  _msgid: 'c73dfa4091d6eae8'
}
3 Aug 11:25:36 - [info] [debug:debug 366]
{
  payload: ['$VERSION', 'V8.30226 9/27/2017'],
  topic: '',
  filename: 'C:\\Users\\PC\\Documents\\FANUC _ FANUC Robotics.html',
  parts: { id: '424a0f96cb3665df', type: 'array', count: 4, len: 1, index: 2 },
  _msgid: 'c73dfa4091d6eae8'
}

```

---

<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:** [3 August 2023 15:44 UTC](https://discourse.nodered.org/t/splitting-data-and-publishing-to-mqtt-topic-message/80293/4 "2023-08-03T15:44:48Z")

</div>

You probably don't want the `return msg` on the end of the function. That will send the current message after it has completed your loop. Change it to just `return`. In fact I usually use `return null` so that when I come back to the code I know that it was intentional to return nothing.

In addition though, don't you need to increment i by two each time, so that it uses element 0 as the topic, then element 2. At the moment it uses element 0, then trie to use element 1 for the topic and element 2 (which does not exist) as the payload.

---

<div class="post-metadata">

**Author:** ![gettmad](https://avatars.discourse-cdn.com/v4/letter/g/48db29/32.png) [@gettmad](https://discourse.nodered.org/u/gettmad)\
**Post date:** [3 August 2023 16:47 UTC](https://discourse.nodered.org/t/splitting-data-and-publishing-to-mqtt-topic-message/80293/5 "2023-08-03T16:47:46Z")

</div>

You sir, are the best.

This fixed my issue.

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![gettmad](https://avatars.discourse-cdn.com/v4/letter/g/48db29/32.png) [@gettmad](https://discourse.nodered.org/u/gettmad)\
**Post date:** [4 August 2023 12:44 UTC](https://discourse.nodered.org/t/splitting-data-and-publishing-to-mqtt-topic-message/80293/6 "2023-08-04T12:44:29Z")

</div>

Colin,

When applying this fix, it addressed the splitting of my payload, however, if my Topic has a '/' it is still splitting into further subtopics. Are you aware of a way to have it not split? I have attempted to use jsonStringify with no luck.

Appreciate your assistance.

---

<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:** [4 August 2023 13:08 UTC](https://discourse.nodered.org/t/splitting-data-and-publishing-to-mqtt-topic-message/80293/7 "2023-08-04T13:08:24Z")

</div>

Again, please, show us an example and explain what you want it to do.

---

<div class="post-metadata">

**Author:** ![gettmad](https://avatars.discourse-cdn.com/v4/letter/g/48db29/32.png) [@gettmad](https://discourse.nodered.org/u/gettmad)\
**Post date:** [4 August 2023 13:22 UTC](https://discourse.nodered.org/t/splitting-data-and-publishing-to-mqtt-topic-message/80293/8 "2023-08-04T13:22:50Z")

</div>

This is the Publish Function Nodes code:

```auto
var config1 = msg.payload;
flow.set('config1', msg.payload);

for (var i = 0; i < config1.length; i += 2) {
    var topic = 'FANUC Robot/Configuration/' + config1[i]; // Append the index to make the topic unique
    var payload = config1[i+1]
    node.send({topic: topic, payload: payload});
}

return 

```

This is the data being input into the Publish node:

```auto
{
  payload: ['EtherNet/IP Safety', 'R713'],
  topic: '',
  filename: 'C:\\Users\\PC\\Documents\\FANUC _ FANUC Robotics.html',
  parts: {
    id: '4e8d888cbc753cb4',
    type: 'array',
    count: 197,
    len: 1,
    index: 101
  },
  _msgid: '3b88cb49e0335c6f'
}

{
  payload: ['Ethernet/IP Adapter', 'R784'],
  topic: '',
  filename: 'C:\\Users\\PC\\Documents\\FANUC _ FANUC Robotics.html',
  parts: {
    id: '4e8d888cbc753cb4',
    type: 'array',
    count: 197,
    len: 1,
    index: 102
  },
  _msgid: '8d42415cdfcdb235'
}

```

Here is the message at the output of the Publish node:

```auto
{
  topic: 'FANUC Robot/Configuration/EtherNet/IP Safety',
  payload: 'R713',
  _msgid: '6d046d8ed801b5af'
}
4 Aug 09:12:15 - [info] [debug:debug 367]
{
  topic: 'FANUC Robot/Configuration/Ethernet/IP Adapter',
  payload: 'R784',
  _msgid: '2297eb13eea80268'
}

```

Which produces this result in MQTT:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/3/e376267e2aef0197bf7af7bc3d8bf19765966d8c.png)

I am looking for the data to be represented in the following form:  
**Ethernet/IP Safety** = R713  
**Ethernet IP Adapter** = R784

Where the bold above is the singular topic (not split into subtopics).

---

<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:** [4 August 2023 13:46 UTC](https://discourse.nodered.org/t/splitting-data-and-publishing-to-mqtt-topic-message/80293/9 "2023-08-04T13:46:10Z")

</div>

> [@gettmad](#):
>
> I am looking for the data to be represented in the following form:  
> **Ethernet/IP Safety** = R713  
> **Ethernet IP Adapter** = R784
> 
> Where the bold above is the singular topic (not split into subtopics).

That is the way MQTT works. If you don't want the subtopics then don't use `/`

---

<div class="post-metadata">

**Author:** ![gettmad](https://avatars.discourse-cdn.com/v4/letter/g/48db29/32.png) [@gettmad](https://discourse.nodered.org/u/gettmad)\
**Post date:** [4 August 2023 13:56 UTC](https://discourse.nodered.org/t/splitting-data-and-publishing-to-mqtt-topic-message/80293/10 "2023-08-04T13:56:00Z")

</div>

Unfortunately this is the way the data is extracted from the web server.

I was hoping for an easy fix however I believe it will require further tampering with. I was just wondering if there was a form I could represent the text in that it would not recognize the '/' operator as a new subtopic.

Thanks anyways.

---

<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:** [18 August 2023 13:56 UTC](https://discourse.nodered.org/t/splitting-data-and-publishing-to-mqtt-topic-message/80293/11 "2023-08-18T13:56:14Z")

</div>

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