How to write measurements in different tables

i wrote this configfile

Global Agent Configuration

[agent]
interval = “1m”

Input Plugins

[[inputs.mqtt_consumer]]
name_prefix = “sensors_”
servers = [“localhost:1883”]
qos = 0
connection_timeout = “30s”
topics = [
“/m1/#”,
“/m2/#”,
“/m3/#”,
“/m4/#”,
“/id_machine/#”,
“/1/#”,
“/2/#”,

]
persistent_session = false
data_format = “value”
data_type = “float”

Output Plugins

[[outputs.influxdb]]
database = “testdb”
urls = [ “http://localhost:8086” ]

that writes all information in one single table “sensors_mqtt_consumer” i dont know how to modify it to be able to write every single topic in one table .
any help ?

While I don’t understand the need to necessarily split the data between different tables, it should be possible. I’ll give you a few ideas - without guarantee - to try out for yourself.
I would try a processors plugin.

  • Option 1: It definitely works with a processors.execd plugin. But you have to write it yourself in any programming or scripting language.
  • Option 2: It should also work with a processors.starlark plugin. The advantage would be that the starlark script code can be located directly in the config file.
  • Option 3: It could also work with the processors.override or processors.rename plugins. Approximately as outlined below.
[[processors.rename]]
  fieldpass = ["topics/m1*"]  # how your topic field is named
  [[processors.rename.replace]]
    measurement = "sensors_mqtt_consumer"
    dest = "topics_m1"  # your new table/measurement name

[[processors.rename]]
  fieldpass = ["topics/id_machine*"]  # how your topic field is named
  [[processors.rename.replace]]
    measurement = "sensors_mqtt_consumer"
    dest = "topics_id_machine"  # your new table/measurement name

[[processors.rename]]
  fieldpass = ["topics/1*"]  # how your topic field is named
  [[processors.rename.replace]]
    measurement = "sensors_mqtt_consumer"
    dest = "topics_1"  # your new table/measurement name

# and so on...
1 Like

Here is a suggestion how the whole thing could work with a processors.starlark plugin. No guarantee if it works exactly like this, I did not test it, but only wrote it down theoretically. If it doesn’t work, tinker a bit more yourself. :wink:

[[processors.starlark]]
  # Source of the Starlark script.
  source = '''

# adjust the field names in the first row, they have to match exactly!

renames = {
  "/m1/#"        : "meas_m1",
  "/m2/#"        : "meas_m2",
  "/m3/#"        : "meas_m3",
  "/m4/#"        : "meas_m4",
  "/id_machine/#": "meas_id",
  "/1/#"         : "meas_1",
  "/2/#"         : "meas_2",
}

def apply(metric):
    # Initialize a list of new metrics
    metrics = []
    # iterate through all fields of the current metric
    for k, v in metric.fields.items():
        if k in renames:
          new_metric = Metric(renames[k]) # Create a new metric
          new_metric.fields[renames[k]] = v
          metrics.append(new_metric) # Add metric to the list of metrics
    return metrics  # Return the new list of metrics
'''
1 Like