How to split a mqtt json into multiple metrics

I have an mqtt telegraf input source containing a json string. The “message” contains two metrics separated by a comma as follows:


I would like to split the “message” data into two metrics called “spd” and “dir” to be outputted to InfluxDB. I am aware of the processors.split function in Telegraf however do not have the experience to implement it.

Any help would be greatly appreciated.

Hello @simonsmart9,
Yes absolutely.
Split as far as I understand I think you might have to use the starlark or execd processor plugin for that type of splitting.
Here’s a similar example.\

Many thanks @Anaisdg
Starlark did the trick. For anyone who is trying something similar, herewith the code.

  servers = ["tcp://localhost:1883"]
  topics = ["higham/#"]
  connection_timeout = "30s"
  username = "mymqttusername"
  password = "mymqttpassword"
  data_format = "json_v2"

     measurement_name = "higham_anemometer"
          path = "message"
          rename = "higham_message"
          type = "string"

namepass = ['higham_anemometer']
source = '''
def apply(metric):
    higham_data = metric.fields['higham_message']
    metric.fields['higham_spd'] = higham_data.split(",")[0]
    metric.fields['higham_dir'] = higham_data.split(",")[1]
    return metric

@simonsmart9 just as a heads-up, since v1.28.0 (released yesterday) we now do have a split processor which is exactly targetting your use-case to split a metric into multiple ones…