Renaming Tag Value Dependent on Another Tag

Hello, I’m configuring Telegraf and InfluxDB to monitor irrigation valves via MQTT. Each irrigation controller (device tag), has 4 switches (switch tag) that I’d like rename to something meaningful.

My current Telegraf configuration looks like:

[[inputs.mqtt_consumer]]
  alias = "mqtt.irrigation"
  servers = ["tcp://mosquitto:1883"]
  topics = [
    "irrigation/+/relay/#"
  ]
  data_format = "value"
  data_type = "integer"

[[inputs.mqtt_consumer.topic_parsing]]
  topic = "irrigation/+/relay/+"
  tags = "_/device/relay/switch"

And my output (to Influxdb or stdout) is in the form:

mqtt_consumer,device=bees-knees,ip_addr=402c3b0e020e,relay=relay,switch=1,topic=irrigation/bees-knees/relay/1 value=0i 1685298970699514515
mqtt_consumer,device=bees-knees,ip_addr=402c3b0e020e,relay=relay,switch=2,topic=irrigation/bees-knees/relay/2 value=1i 1685298970699536329
mqtt_consumer,device=bees-knees,ip_addr=402c3b0e020e,relay=relay,switch=3,topic=irrigation/bees-knees/relay/3 value=0i 1685298970699693583
mqtt_consumer,device=cats-meow,ip_addr=402c3b0e020e,relay=relay,switch=0,topic=irrigation/cats-meow/relay/0 value=0i 1685298970699710860
mqtt_consumer,device=cats-meow,ip_addr=402c3b0e020e,relay=relay,switch=1,topic=irrigation/cats-meow/relay/1 value=0i 168529897069972519

What I’d like to accomplish is renaming the switch tag from switch=0 to “Sprinkler_A” if device=bees-knees, and “0” becomes switch=Garden_West if device=cats-meow.

I know that processors.strings.replace and some form of tagpass is what’s needed, but I haven’t succeeded in getting this to work. If it would make more sense to do this transformation in InfluxDB, rather than Telegraf, I suppose that’s an option too. Thanks!

If you need to be able to make changes to a metric, based on some logic in a processor, I would suggest looking at the starlark processor: telegraf/plugins/processors/starlark at master · influxdata/telegraf · GitHub There are examples in the testdata directory.

Ok, this looks quite powerful - I’ll give that a try. As this is all running on a Raspberry Pi 4 (memory constrained), should I be aware of performance implications?

should I be aware of performance implications?

Well, there is no free lunch :wink: There can be implications for performance if you are parsing huge amounts of metrics. On the order of 10s of thousands per collection interval.

My suggestion is to use the ‘namepass’ or ‘tagpass’ mechanism to only send the metrics that you absolutely need through that processor, assuming you have many other metrics you are collecting.