mqtt.to() function

The mqtt.to() function is experimental and subject to change at any time. By using this function, you accept the risks of experimental functions.

The mqtt.to() function outputs data to an MQTT broker using MQTT protocol.

*Function type: Output*

  1. import "experimental/mqtt"
  2. mqtt.to(
  3. broker: "tcp://localhost:8883",
  4. topic: "example-topic",
  5. message: "Example message",
  6. qos: 0,
  7. clientid: "flux-mqtt",
  8. username: "username",
  9. password: "password",
  10. name: "name-example",
  11. timeout: 1s,
  12. timeColumn: "_time",
  13. tagColumns: ["tag1", "tag2"],
  14. valueColumns: ["_value"]
  15. )

Parameters

broker

The MQTT broker connection string.

*Data type: String*

topic

The MQTT topic to send data to.

*Data type: String*

message

The message or payload to send to the MQTT broker. The default payload is an output table. If there are multiple output tables, it sends each table as a separate MQTT message.

When you specify a message, the function sends the message string only (no output table).

*Data type: String*

qos

The MQTT Quality of Service (QoS) level. Values range from [0-2]. Default is 0.

*Data type: Integer*

clientid

The MQTT client ID.

*Data type: String*

username

The username to send to the MQTT broker. Username is only required if the broker requires authentication. If you provide a username, you must provide a password.

*Data type: String*

password

The password to send to the MQTT broker. Password is only required if the broker requires authentication. If you provide a password, you must provide a username.

*Data type: String*

name

(Optional) The name for the MQTT message.

*Data type: String*

timeout

The MQTT connection timeout. Default is 1s.

*Data type: Duration*

timeColumn

The column to use as time values in the output line protocol. Default is "_time".

*Data type: String*

tagColumns

The columns to use as tag sets in the output line protocol. Default is [].

Data type: Array of strings

valueColumns

The columns to use as field values in the output line protocol. Default is ["_value"].

Data type: Array of strings

Examples

Send data to an MQTT endpoint

  1. import "experimental/mqtt"
  2. from(bucket: "example-bucket")
  3. |> range(start: -5m)
  4. |> filter(fn: (r) => r._measurement == "airSensor")
  5. |> mqtt.to(
  6. broker: "tcp://localhost:8883",
  7. topic: "air-sensors",
  8. clientid: "sensor-12a4",
  9. tagColumns: ["sensorID"],
  10. valueColumns: ["_value"]
  11. )