# Collecting metrics via mqtt\_consumer

**URL:** <https://community.influxdata.com/t/collecting-metrics-via-mqtt-consumer/18490>\
**Category:** Telegraf\
**Created:** [February 22, 2021, 5:54am UTC](https://community.influxdata.com/t/collecting-metrics-via-mqtt-consumer/18490 "2021-02-22T05:54:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![borpin](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/borpin/32/5257_2.png) [@borpin](https://community.influxdata.com/u/borpin)\
**Post date:** [February 22, 2021, 5:54am UTC](https://community.influxdata.com/t/collecting-metrics-via-mqtt-consumer/18490/1 "2021-02-22T05:54:33Z")

</div>

I am collecting metrics via the `mqtt_consumer` input and am wondering if I have this set up in an optimal way. It seems to me this results in a database table with lots of gaps.

MQTT setup

```auto
[[inputs.mqtt_consumer]]
  servers = ["tcp://192.168.7.50:1883"]
  name_override = "emon_input"

  topics = [
    "emon/tank",
    "emon/Fineoffset-WHx080-236",
  ]
# topic_tag = ""

```

Results

![image](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/2X/e/edb1de344b770cbe0d4ffb5155530e551868cc6e.png)

The `mqtt_consumer` is the measurement.

For each row in the table, I have 2 tags (`host` and `topic`) with the `topic` tag having one of 2 values - the 2 topics I subscribe to.

Each row has a field for a combination of the `fields` from the 2 MQTT Topics i.e. each row has a field for all the potential metrics from both topics.

When data comes in from topic 1, the row is partially filled with the values from that topic leaving the other fields empty.

1. Have I got that right?
2. This seems very inefficient - is it the InfluxDB way?
3. I think I want each `topic` as it’s own measurement for efficiency. If so, how?

---

<div class="post-metadata">

**Author:** ![borpin](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/borpin/32/5257_2.png) [@borpin](https://community.influxdata.com/u/borpin)\
**Post date:** [February 28, 2021, 2:22pm UTC](https://community.influxdata.com/t/collecting-metrics-via-mqtt-consumer/18490/2 "2021-02-28T14:22:46Z")

</div>

Any suggestions how lo load MQTT Topics into measurements without multiple consumers?

---

<div class="post-metadata">

**Author:** ![reimda](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/reimda/32/3723_2.png) [@reimda](https://community.influxdata.com/u/reimda)\
**Post date:** [March 25, 2021, 7:47pm UTC](https://community.influxdata.com/t/collecting-metrics-via-mqtt-consumer/18490/3 "2021-03-25T19:47:46Z")

</div>

I think the simplest way to get what you want is to make another instance of the mqtt\_consumer plugin, but since you asked for alternatives, here are two ways:

First, take a look at the processor plugins. [telegraf/plugins/processors at release-1.18 · influxdata/telegraf · GitHub](https://github.com/influxdata/telegraf/tree/release-1.18/plugins/processors) You could use clone to make a copy of the metric, then use fieldpass to remove one field in the original and the other field in the copy. [telegraf/CONFIGURATION.md at master · influxdata/telegraf · GitHub](https://github.com/influxdata/telegraf/blob/master/docs/CONFIGURATION.md#modifiers)

Another approach would be to use the starlark processor. With it you can write a short script in a python-like language to do the same thing. There are many examples in the startlark plugin documentation: [telegraf/plugins/processors/starlark at release-1.18 · influxdata/telegraf · GitHub](https://github.com/influxdata/telegraf/tree/release-1.18/plugins/processors/starlark)

---

<div class="post-metadata">

**Author:** ![Franky1](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/franky1/32/6666_2.png) [@Franky1](https://community.influxdata.com/u/Franky1)\
**Post date:** [March 26, 2021, 9:54am UTC](https://community.influxdata.com/t/collecting-metrics-via-mqtt-consumer/18490/4 "2021-03-26T09:54:13Z")

</div>

> [@borpin](#):
>
> Any suggestions how lo load MQTT Topics into measurements without multiple consumers?

As @reimda already said, i would also go for a `processors` plugin in Telegraf.  
If you need any help, please provide the actual snippet of your mqtt input plugin config and a short snippet of your collected data in influx line protocol format.
