# How to write data to multiple buckets

**URL:** <https://community.influxdata.com/t/how-to-write-data-to-multiple-buckets/57773>\
**Category:** Telegraf\
**Tags:** influxdb, telegraf, mqtt, json\
**Created:** [August 7, 2025, 3:58pm UTC](https://community.influxdata.com/t/how-to-write-data-to-multiple-buckets/57773 "2025-08-07T15:58:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nils\_Franke](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/nils_franke/32/15016_2.png) [@Nils\_Franke](https://community.influxdata.com/u/Nils_Franke)\
**Post date:** [August 7, 2025, 3:58pm UTC](https://community.influxdata.com/t/how-to-write-data-to-multiple-buckets/57773/1 "2025-08-07T15:58:08Z")

</div>

Hi there!

I recieve different messages (jsonv2) from one sensor and would like to put different messages into different buckets.

This is my config at the moment:

```auto
[[outputs.influxdb_v2]]
  urls = ["http://127.0.0.1:8086"]
  token = "mytoken"
  organization = "myorg"
  bucket = "untouchedBucket"

##(1065)
  [[inputs.mqtt_consumer.json_v2]]
    name_override = "package_data"
    [[inputs.mqtt_consumer.json_v2.tag]]
      path = "msg_id"
      data_type = "int"
    [[inputs.mqtt_consumer.json_v2.tag]]
      rename = "device_id"
      path = "device_info.device_id"
    [[inputs.mqtt_consumer.json_v2.tag]]
      rename = "mac"
      path = "device_info.mac"
    [[inputs.mqtt_consumer.json_v2.field]]
      rename = "switch_state"
      path = "data.switch_state"
      data_type = "bool"
      optional = true
    [[inputs.mqtt_consumer.json_v2.field]]
      rename = "load_state"
      path = "data.load_state"
      data_type = "bool"
      optional = true
    [[inputs.mqtt_consumer.json_v2.field]]
      rename = "overload_state"
      path = "data.overload_state"
      data_type = "bool"
      optional = true
    [[inputs.mqtt_consumer.json_v2.field]]
      rename = "overcurrent_state"
      path = "data.overcurrent_state"
      data_type = "bool"
      optional = true
    [[inputs.mqtt_consumer.json_v2.field]]
      rename = "overvoltage_state"
      path = "data.overvoltage_state"
      data_type = "bool"
      optional = true
    [[inputs.mqtt_consumer.json_v2.field]]
      rename = "time_plug"
      path = "data.timestamp"
      data_type = "string"

##(1066)
  [[inputs.mqtt_consumer.json_v2]]
    name_override = "package_data"
    [[inputs.mqtt_consumer.json_v2.tag]]
      path = "msg_id"
      data_type = "int"
    [[inputs.mqtt_consumer.json_v2.tag]]
      rename = "device_id"
      path = "device_info.device_id"
    [[inputs.mqtt_consumer.json_v2.tag]]
      rename = "mac"
      path = "device_info.mac"
    [[inputs.mqtt_consumer.json_v2.field]]
      rename = "switch_state"
      path = "data.switch_state"
      data_type = "bool"
      optional = true
    [[inputs.mqtt_consumer.json_v2.field]]
      rename = "countdown"
      path = "data.countdown"
      data_type = "int"
      optional = true
    [[inputs.mqtt_consumer.json_v2.field]]
      rename = "time_plug"
      path = "data.timestamp"
      data_type = "string"

```

I would like to have message 1065 still being written to “untouchedbucket”, while message 1066 should go to “touchedbucket”.

How should my config look like?  
Can somebody give me an example?  
Do I have to run a second Telegraf instance to do this?

---

<div class="post-metadata">

**Author:** ![scott](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/scott/32/16492_2.png) [@scott](https://community.influxdata.com/u/scott)\
**Post date:** [August 8, 2025, 3:36pm UTC](https://community.influxdata.com/t/how-to-write-data-to-multiple-buckets/57773/2 "2025-08-08T15:36:37Z")

</div>

@Nils_Franke You should be able to do it with a single agent, but you need to do the following:

1. Update each set of metrics so Telegraf can differentiate them. You could add distinct measurement names or a distinct tag to each. For example:

- **Use distinct measurement names**

- **Add distinct tags** _(my recommendation)_

1. Add a second `influxdb_v2` output plugin that outputs to the “touchedbucket.”

2. Add filters to your two output plugins to only write points based on the message differentiator. For example, if you use the distinct tags:

There’s more information about [Telegraf metric filtering](https://docs.influxdata.com/telegraf/v1/configuration/#metric-filtering) in the documentation.

---

<div class="post-metadata">

**Author:** ![Nils\_Franke](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/nils_franke/32/15016_2.png) [@Nils\_Franke](https://community.influxdata.com/u/Nils_Franke)\
**Post date:** [August 12, 2025, 11:56am UTC](https://community.influxdata.com/t/how-to-write-data-to-multiple-buckets/57773/3 "2025-08-12T11:56:25Z")

</div>

Thank you for your quick reply!

Unfortunately, going with the tagpass option Telegraf fails to start:

```ini
##Telegraf fails to start

[[outputs.influxdb_v2]]
  urls = ["http://127.0.0.1:8086"]
  token = "mytoken"
  organization = "myorg"
  bucket = "untouchedBucket"
  [outputs.influxdb_v2.tagpass]
    message = ["1065"]

[[outputs.influxdb_v2]]
  urls = ["http://127.0.0.1:8086"]
  token = "mytoken"
  organization = "myorg"
  bucket = "touchedBucket"
  [outputs.influxdb_v2.tagpass]
    message = ["1066"]

##(1065)
  [[inputs.mqtt_consumer.json_v2]]
    name_override = "package_data"
    [[inputs.mqtt_consumer.json_v2.tag]]
      message = "1065"

# ...

##(1066)
  [[inputs.mqtt_consumer.json_v2]]
    name_override = "package_data"
    [[inputs.mqtt_consumer.json_v2.tag]]
      message = "1066"

```

Going with the namepass/nameoverride option, Telegraf starts without any errors but both messages get into both buckets:

```ini
##Starts without errors, sends everything round robin

[[outputs.influxdb_v2]]
  urls = ["http://127.0.0.1:8086"]
  token = "mytoken"
  organization = "myorg"
  bucket = "untouchedBucket"
  [outputs.influxdb_v2.namepass]
    namepass = ["package_data_1065"]

[[outputs.influxdb_v2]]
  urls = ["http://127.0.0.1:8086"]
  token = "mytoken"
  organization = "myorg"
  bucket = "touchedBucket"
  [outputs.influxdb_v2.namepass]
    namepass = ["package_data_1066"]

##(1065)
  [[inputs.mqtt_consumer.json_v2]]
    name_override = "package_data_1065"

# ...

##(1066)
  [[inputs.mqtt_consumer.json_v2]]
    name_override = "package_data_1066"

```

Did I miss something out?

Btw I am using InfluxDB 2.7.11 and Telegraf 1.35.3

---

<div class="post-metadata">

**Author:** ![scott](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/scott/32/16492_2.png) [@scott](https://community.influxdata.com/u/scott)\
**Post date:** [August 12, 2025, 1:54pm UTC](https://community.influxdata.com/t/how-to-write-data-to-multiple-buckets/57773/4 "2025-08-12T13:54:07Z")

</div>

Does Telegraf output an error message when it fails to start?

---

<div class="post-metadata">

**Author:** ![Nils\_Franke](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/nils_franke/32/15016_2.png) [@Nils\_Franke](https://community.influxdata.com/u/Nils_Franke)\
**Post date:** [August 12, 2025, 2:35pm UTC](https://community.influxdata.com/t/how-to-write-data-to-multiple-buckets/57773/5 "2025-08-12T14:35:04Z")

</div>

The telegraf.log itself only gave the two shutdown messages and then stayed empty until  
the restart few minutes later. Without failing config it started up normal:

```auto
2025-08-12T11:02:02Z I! [processors.execd] Process /usr/bin/python3 shut down
2025-08-12T11:02:02Z I! [agent] Hang on, flushing any cached metrics before shutdown
2025-08-12T11:02:02Z I! [agent] Stopping running outputs
2025-08-12T11:06:15Z I! Loading config: /etc/telegraf/telegraf.conf

```

this is what systemd gave me:

```auto
× telegraf.service - Telegraf
     Loaded: loaded (/lib/systemd/system/telegraf.service; enabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Tue 2025-08-12 13:02:04 CEST; 1min 48s ago
       Docs: https://github.com/influxdata/telegraf
    Process: 53019 ExecStart=/usr/bin/telegraf -config /etc/telegraf/telegraf.conf -config-directory /etc/telegraf/tele>
   Main PID: 53019 (code=exited, status=1/FAILURE)
        CPU: 57ms

Aug 12 13:02:04 Fred systemd[1]: telegraf.service: Scheduled restart job, restart counter is at 5.
Aug 12 13:02:04 Fred systemd[1]: Stopped Telegraf.
Aug 12 13:02:04 Fred systemd[1]: telegraf.service: Start request repeated too quickly.
Aug 12 13:02:04 Fred systemd[1]: telegraf.service: Failed with result 'exit-code'.
Aug 12 13:02:04 Fred systemd[1]: Failed to start Telegraf.

```

---

<div class="post-metadata">

**Author:** ![JeroenVH](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/jeroenvh/32/20157_2.png) [@JeroenVH](https://community.influxdata.com/u/JeroenVH)\
**Post date:** [September 23, 2025, 9:34am UTC](https://community.influxdata.com/t/how-to-write-data-to-multiple-buckets/57773/6 "2025-09-23T09:34:18Z")

</div>

Hi @Nils_Franke ,

If creating a duplicate of the data is not a requirement and you only want to send the different metrics to a different bucket, then using `bucket_tag` in the influxdb configuration might be a better route.

With that you can define the influxdb bucket as a tag. You can use the tag method that @scott described.
