# Multiple topics to one influxdb mesurement

**URL:** https://discourse.nodered.org/t/multiple-topics-to-one-influxdb-mesurement/80797
**Category:** General
**Tags:** database, function-node, mqtt
**Created:** [25 August 2023 14:17 UTC](https://discourse.nodered.org/t/multiple-topics-to-one-influxdb-mesurement/80797 "2023-08-25T14:17:57Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![speerwerfer](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@speerwerfer](https://discourse.nodered.org/u/speerwerfer)
#### Post date: [25 August 2023 14:17 UTC](https://discourse.nodered.org/t/multiple-topics-to-one-influxdb-mesurement/80797/1 "2023-08-25T14:17:57Z")

</div>

Hello,

i use a mqtt node to receive multiple topics (pv-inverter/#) from an mqqt brocker (sensor data from microconverter).

pv-inverter/ (14 topics)  
pv-inverter/HM800/ (6 Topics)  
pv-inverter/HM800/ch1 (7 Topics)  
pv-inverter/HM800/ch2 (7 Topics)  
pv-inverter/HM800/ch0 (21 Topics)  
pv-inverter/HM800/alarm (5 Topics)  
pv-inverter/status (1 Topics)  
pv-inverter/status/total (4 Topics)

Now i want to write all sensor data with the same timestamp from the different messages into one influxdb mesurement with one field for every single sensor data/mqtt message.

Example - this three messages comes at the same time.

```auto
{"topic":"pv-inverter/HM800/ch0/PF_AC","payload":"1","qos":0,"retain":false,"_msgid":"640830c6aa400679"}
{"topic":"pv-inverter/HM800/ch0/ALARM_MES_ID","payload":"21150","qos":0,"retain":false,"_msgid":"7e4a7d0a06bb4b7f"}
{"topic":"pv-inverter/total/P_DC","payload":"146.6","qos":0,"retain":false,"_msgid":"1b8cfe8bc63187c5"}

```

To inject this sensor data to influxdb i want to use a single msg

```auto
{
    topic: 'HM-800', 
    payload: {
        PF_AC: pv-inverter/HM800/ch0/PF_AC
        ALARM_MSG_ID: pv-inverter/HM800/ch0/ALARM_MES_ID
        P_DC: pv-inverter/total/P_DC
}

```

what is the right/shortest way?

---

<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: [25 August 2023 14:30 UTC](https://discourse.nodered.org/t/multiple-topics-to-one-influxdb-mesurement/80797/2 "2023-08-25T14:30:13Z")

</div>

Welcome to the forum @speerwerfer

> [@speerwerfer](#):
>
> Now i want to write all sensor data with the same timestamp from the different messages into one influxdb mesurement with one field for every single sensor data/mqtt message.

Why do you want to do that? Why not just write them to separate measurements as you receive them?

---

<div class="post-metadata">

### Author: ![speerwerfer](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@speerwerfer](https://discourse.nodered.org/u/speerwerfer)
#### Post date: [25 August 2023 16:51 UTC](https://discourse.nodered.org/t/multiple-topics-to-one-influxdb-mesurement/80797/3 "2023-08-25T16:51:53Z")

</div>

Thank you for the welcome

> [@Colin](#):
>
> Why do you want to do that? Why not just write them to separate measurements as you receive them?

My idea is, all sensor data reported at the same time by a device should also have the same timestamp in the influxdb.

If I have for example a weather station, then I thought so far that one collects these sensor data all in a mesurment.  
The Bucket is Haussensorik (House sensors)  
The Mesurement is for example Wetterstation (weatherstation)

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

---

<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: [25 August 2023 17:28 UTC](https://discourse.nodered.org/t/multiple-topics-to-one-influxdb-mesurement/80797/4 "2023-08-25T17:28:27Z")

</div>

Does the inverter publish them to MQTT directly?

---

<div class="post-metadata">

### Author: ![speerwerfer](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@speerwerfer](https://discourse.nodered.org/u/speerwerfer)
#### Post date: [25 August 2023 17:32 UTC](https://discourse.nodered.org/t/multiple-topics-to-one-influxdb-mesurement/80797/5 "2023-08-25T17:32:24Z")

</div>

in principle, yes. there is a receiving unit that receives the data from the microinverter via a proprietary protocol and passes it on to the brocker via mqtt. Unfortunately I can not modify the structure of the topics.

---

<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: [25 August 2023 17:46 UTC](https://discourse.nodered.org/t/multiple-topics-to-one-influxdb-mesurement/80797/6 "2023-08-25T17:46:10Z")

</div>

You can use a Join node in key/value mode to get them all into one message. Then you can reorganise the contents for influxdb.

See [this article in the cookbook](https://cookbook.nodered.org/basic/join-streams) for an example of how to join messages into one object.

---

<div class="post-metadata">

### Author: ![speerwerfer](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@speerwerfer](https://discourse.nodered.org/u/speerwerfer)
#### Post date: [25 August 2023 18:47 UTC](https://discourse.nodered.org/t/multiple-topics-to-one-influxdb-mesurement/80797/7 "2023-08-25T18:47:07Z")

</div>

Thanks!  
Is there an easy way to shorten the names of the elements? Here is the complete mqtt topic now.  
Or should I change the names via a function node?

See the Result:

```auto
{
    "topic": "pv-anlage/HM-800/114190547764/0/temperature",
    "payload": {
        "pv-inverter/HM800/ 14190547764/name": "HM800",
        "pv-inverter/HM-800/114190547764/device/bootloaderversion": "104",
        "pv-inverter/HM-800/114190547764/device/fwbuildversion": "10010",
        "pv-inverter/HM-800/114190547764/device/fwbuilddatetime": "2021-11-01 08:42:00",
        "pv-inverter/HM-800/114190547764/device/hwpartnumber": "269565952",
        "pv-inverter/HM-800/114190547764/device/hwversion": "01.10",
        "pv-inverter/HM-800/114190547764/status/limit_relative": "100.00",
        "pv-inverter/HM-800/114190547764/status/limit_absolute": "800.00"
    },
    "qos": 0,
    "retain": false,
    "_msgid": "5b6f8b7e7290847e"
}

```

---

<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: [25 August 2023 21:00 UTC](https://discourse.nodered.org/t/multiple-topics-to-one-influxdb-mesurement/80797/8 "2023-08-25T21:00:27Z")

</div>

> [@speerwerfer](#):
>
> Is there an easy way to shorten the names of the elements?

If you want the fields to be name, bootloaderversion, etc then in a function node you can iterate the keys using `Object.keys(obj).forEach()` as described in [Iterating Through an Object with `forEach()` - Mastering JS](https://masteringjs.io/tutorials/fundamentals/foreach-object), then for each one use split("/") on the property name and take the last element of the array as the new key name. Build a new object with the new field names and then replace the payload with the new one.

Should not several of those be tags rather than fields in influx? Also make sure you change the ones that are numbers from Strings to Numbers, though I am surprised that the MQTT node does not do that for you if you tell it to auto-detect the type.

---

<div class="post-metadata">

### Author: ![speerwerfer](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@speerwerfer](https://discourse.nodered.org/u/speerwerfer)
#### Post date: [26 August 2023 08:00 UTC](https://discourse.nodered.org/t/multiple-topics-to-one-influxdb-mesurement/80797/9 "2023-08-26T08:00:09Z")

</div>

Once again, thank you!

> [@Colin](#):
>
> Should not several of those be tags rather than fields in influx?

Yes, I still need to think about the database/table design.

> [@Colin](#):
>
> Also make sure you change the ones that are numbers from Strings to Numbers, though I am surprised that the MQTT node does not do that for you if you tell it to auto-detect the type.

Yes, if I use auto-detect, then only strings are generated. If I set "a parsed JSON Object", then the numbers are recognized. With the strings there are then errors. I can process them with Catch

---

<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: [26 August 2023 15:31 UTC](https://discourse.nodered.org/t/multiple-topics-to-one-influxdb-mesurement/80797/10 "2023-08-26T15:31:22Z")

</div>

> [@speerwerfer](#):
>
> With the strings there are then errors. I can process them with Catch

I don't understand what you mean by that.

---

<div class="post-metadata">

### Author: ![speerwerfer](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@speerwerfer](https://discourse.nodered.org/u/speerwerfer)
#### Post date: [27 August 2023 13:27 UTC](https://discourse.nodered.org/t/multiple-topics-to-one-influxdb-mesurement/80797/11 "2023-08-27T13:27:38Z")

</div>

sorry, english is not my first language 🙂

When i use in the mqtt node "output: auto-detect" then all messages contain the data type string.  
When i use "output: a parsed JSON Object" then I receive only the messages that contain a number. Messages containing a string are not forwarded. The messages with a string as payload cause an error. I can then catch this error with a "Catch Node" and convert it to a correct message with a string as payload.

This is the Output from the "Catch Node"

```auto
{
    "payload": "2021-11-01 08:42:00",
    "topic": "pv-anlage/HM-800/114XXXXXXXXX/device/fwbuilddatetime",
    "qos": 0,
    "retain": false,
    "error": {
        "message": "Failed to parse JSON string",
        "source": {
            "id": "352394800a5ce834",
            "type": "mqtt in",
            "count": 1
        }
    },
    "_msgid": "10a31f3352d1c2f2"
}

```

---

<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: [27 August 2023 14:16 UTC](https://discourse.nodered.org/t/multiple-topics-to-one-influxdb-mesurement/80797/12 "2023-08-27T14:16:37Z")

</div>

This is probably the option you want  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/6/96805252ed2dd5de45022110e251e4fb2ddc12f0.png)

If you have not got that option then perhaps you are on an old version of node-red.

---

<div class="post-metadata">

### Author: ![speerwerfer](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@speerwerfer](https://discourse.nodered.org/u/speerwerfer)
#### Post date: [27 August 2023 19:13 UTC](https://discourse.nodered.org/t/multiple-topics-to-one-influxdb-mesurement/80797/13 "2023-08-27T19:13:56Z")

</div>

> [@Colin](#):
>
> If you have not got that option then perhaps you are on an old version of node-red.

Great, that's 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 September 2023 19:14 UTC](https://discourse.nodered.org/t/multiple-topics-to-one-influxdb-mesurement/80797/14 "2023-09-10T19:14:24Z")

</div>

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