setting up mqtt telegraf

Setting Up MQTT Telegraf: The Ultimate Guide to Ingesting MQTT Data into Your Monitoring Pipeline

By John Taylor
AD

Integrating MQTT with Telegraf provides a robust solution for collecting and forwarding IoT sensor data or application metrics. This pipeline allows you to subscribe to lightweight messaging streams, parse the payload, and ship the metrics to time-series databases like InfluxDB or monitoring platforms. The configuration centers on the MQTT Consumer Input Plugin, which connects to a broker, listens to specific topics, and transforms the incoming messages into structured data points.

Understanding the MQTT Telegraf Architecture

Before diving into the setup, it is essential to understand how the components interact. Telegraf acts as the client, maintaining a persistent connection to the MQTT broker. It subscribes to a topic filter, waiting for messages that match the pattern. Upon receiving a message, the plugin triggers an input function that decodes the payload, which is often JSON, and maps the values to fields. This architecture ensures low latency data ingestion while leveraging the publish-subscribe model for scalability.

Prerequisites and Broker Configuration

To proceed, you need a running instance of Telegraf and a reachable MQTT broker. Popular brokers include Mosquitto, EMQX, and HiveMQ. Ensure that the broker allows connections from the Telegraf server's IP address and that the relevant topics are being published. If you are testing locally, you can run Mosquitto via Docker. The Telegraf configuration will reside in a separate file, typically located in the `/etc/telegraf/telegraf.d/` directory, where you will define the specific MQTT settings.

AD
MQTT: Get started with IoT protocols - Open Source For You
MQTT: Get started with IoT protocols - Open Source For You
How to Get Started with MQTT
How to Get Started with MQTT
MQTT & MicroPython
MQTT & MicroPython

Configuring the Input Plugin

The core of the setup is the `mqtt_consumer` input plugin configuration. You must specify the broker address using the `servers` directive, usually in the form of `tcp://localhost:1883`. You then define a list of `topics` to subscribe to, where each topic is paired with a desired ordering guarantee, such as `at_least_once`. Depending on your Quality of Service (QoS) level, you might also configure `connection_tag` to differentiate between connections and set a `data_format` to interpret the raw bytes correctly.

Topic Subscription and Data Mapping

When subscribing to topics, you can use wildcards like `sensors/+/temperature` to capture a broad range of data. The plugin allows you to extract values deeply nested within the message using JSON key paths. For instance, if your payload contains `{"temp": 22.5, "humidity": 45}`, you can map these to fields. It is crucial to define the `data_format` as `json` and specify the `json_string_keys` to ensure Telegraf correctly parses the structure into numerical types rather than leaving them as strings.

MQTT Deep Dive
MQTT Deep Dive
Easy MQTT Server Setup
Easy MQTT Server Setup
How to Setup Mosquitto MQTT on AWS
How to Setup Mosquitto MQTT on AWS

Authentication and Secure Connections

For production environments, securing the connection is non-negotiable. You should configure TLS/SSL to encrypt traffic between Telegraf and the broker. This involves setting the `tls_ca` or `tls_cert` and `tls_key` parameters within the input block. If the broker requires client authentication, you will provide credentials via the `username` and `password` fields. Ensure that these secrets are managed securely, leveraging Telegraf's secret filtering capabilities to prevent credentials from appearing in logs or metrics output.

Advanced Tagging and Aggregation

To enrich your metrics, you can utilize the `tag_keys` directive to promote specific values from the payload into tags. This is useful for filtering and grouping in downstream systems. For example, if the MQTT message includes a `location` field, you can tag it as `location=warehouse1`. Furthermore, you can configure `send_delta` to only send changes in value, reducing network overhead. The `interval` setting controls how frequently the input checks for new messages, balancing performance with data freshness.

AD

Testing and Validation

After writing the configuration, validate the syntax using `telegraf --config telegraf.conf --test` to ensure there are no parsing errors. Start the Telegraf service and monitor the logs for successful connection messages. To verify the data flow, use an MQTT client like `mosquitto_sub` to inspect the raw messages on the broker. Simultaneously, check the output destination, such as InfluxDB, to confirm that the points are arriving with the correct measurement name, tags, and fields. Adjust the topic filters or field mappings as necessary based on this verification.

What is MQTT Protocol and How MQTT works? Applications
What is MQTT Protocol and How MQTT works? Applications
MQTT Introduction and Tutorial Part One - Bald Engineer
MQTT Introduction and Tutorial Part One - Bald Engineer
Minimal MQTT: Building A Broker
Minimal MQTT: Building A Broker
What Is MQTT and How It Works - controlcircuitry.com
What Is MQTT and How It Works - controlcircuitry.com
a computer screen with an image of a web page and the text mott server on it
a computer screen with an image of a web page and the text mott server on it
Learn How to Use MQTT.fx with ThingSpeak
Learn How to Use MQTT.fx with ThingSpeak
MQTT in Nodered
MQTT in Nodered
MQTT – A Practical Guide to IOT Protocol | Prototype
MQTT – A Practical Guide to IOT Protocol | Prototype
MQTT - Lightweight Messaging Protocol Cheat Sheet
MQTT - Lightweight Messaging Protocol Cheat Sheet
MQTT Integration
MQTT Integration
Client Challenge
Client Challenge
Setting up an MQTT Broker on Your Raspberry Pi
Setting up an MQTT Broker on Your Raspberry Pi
How to use MQTT explorer
How to use MQTT explorer
Remote control from OBS and M5GO using mqtt and OBS websock
Remote control from OBS and M5GO using mqtt and OBS websock
how to use mott with the raspberry pi and esp866 8 steps with pictures
how to use mott with the raspberry pi and esp866 8 steps with pictures
Give Your Smart Home A Green Thumb With MQTT
Give Your Smart Home A Green Thumb With MQTT
The Universal MQTT Remote
The Universal MQTT Remote
Uploading Data to ThingSpeak With MQTT
Uploading Data to ThingSpeak With MQTT
Mastering Secure MQTT with Python
Mastering Secure MQTT with Python
MQTT Essentials | 2026 Guide | All The Core Concepts & Basics Explained
MQTT Essentials | 2026 Guide | All The Core Concepts & Basics Explained
AD