Asign Timestamp from mqtt via Telegraf

Hi everyone!

We are having few problems writing the timestamp from mqtt to InfluxDB through Telegraf.

This is the part of the configuration file that applies the input from mqtt queue:

[[inputs.mqtt_consumer]]
  ## Broker URLs for the MQTT server or cluster.  To connect to multiple
 servers = ["tcp://*********"]
  ## Topics that will be subscribed to.
  topics = ["SQLSERVER/#"]
  qos = 0
  # Enable extracting tag values from MQTT topics
  data_format = "json_v2"
    [inputs.mqtt_consumer.tags]
        BucketDestino = "Ordenes"
    [[inputs.mqtt_consumer.topic_parsing]]
        data_format = "json_v2"
        topic = "SQLSERVER/#"
        [[inputs.mqtt_consumer.json_v2]]
            [[inputs.mqtt_consumer.json_v2.field]]      
                path = "vnivelc"
                type = "string"

Where can we indicate that the timestamp is “FecAsig”?

The topic is " SQLSERVER" and this is the following structure from mqtt:

SQLSERVER
{
“vnivelc”: “C1”
“FecAsig”: “2024-09-16 08:04:12.0”
}

Hope you know how to handle this.

Best Regards

Jon M.

You can configure this depending on the data format, for json_v2 see: telegraf/plugins/parsers/json_v2 at master · influxdata/telegraf · GitHub

Hi Giovanni,

I appreciate your help, we have this link in consideration. However, we still can’t figure it out how to apply into our configuration.

Could you give us how would you apply it.

Thank you beforehand.

Finally, after searching for many sites and by trial and error we found out how to assign the field “FecAsig” as timestamp; here is how:

[[inputs.mqtt_consumer]]
  ## Broker URLs for the MQTT server or cluster.  To connect to multiple
 servers = ["tcp://******************"]
  ## Topics that will be subscribed to.
  topics = ["SQLSERVER/#"]
  qos = 0
  # Enable extracting tag values from MQTT topics
  data_format = "json_v2"
  name_override = "Rollos"
  topic_tag = ""
    [inputs.mqtt_consumer.tags]
        BucketDestino = "Rollos"
    [[inputs.mqtt_consumer.topic_parsing]]
        data_format = "json_v2"
        [[inputs.mqtt_consumer.json_v2]]
          timestamp_path = "FecAsig"
            timestamp_format = "unix"
            timestamp_timezone = "Europe/Madrid"
        [[inputs.mqtt_consumer.json_v2.field]]
                path = "vnivelc"
                type = "string"

However, JSON_V2 only allow timestamp format as unix, unix_ms or unix_ns so it is mandatory for the data to come this way.

Hope someone helps this topic.

Best regards

1 Like

You can use any time format supported by GO, not only the unix ones (which are the simplest even if not human readable), the GO docs are not very helpful unless you know what to look for, but any of the strings in the link will work.

here is the list of supported formats (extracted by the GO docs)

Layout      = "01/02 03:04:05PM '06 -0700" // The reference time, in numerical order.
ANSIC       = "Mon Jan _2 15:04:05 2006"
UnixDate    = "Mon Jan _2 15:04:05 MST 2006"
RubyDate    = "Mon Jan 02 15:04:05 -0700 2006"
RFC822      = "02 Jan 06 15:04 MST"
RFC822Z     = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone
RFC850      = "Monday, 02-Jan-06 15:04:05 MST"
RFC1123     = "Mon, 02 Jan 2006 15:04:05 MST"
RFC1123Z    = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone
RFC3339     = "2006-01-02T15:04:05Z07:00"
RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
Kitchen     = "3:04PM"
// Handy time stamps.
Stamp      = "Jan _2 15:04:05"
StampMilli = "Jan _2 15:04:05.000"
StampMicro = "Jan _2 15:04:05.000000"
StampNano  = "Jan _2 15:04:05.000000000"
DateTime   = "2006-01-02 15:04:05"
DateOnly   = "2006-01-02"
TimeOnly   = "15:04:05"

in the Telegraf config it should look like this

timestamp_format = "2006-01-02 15:04:05"

Thank you Giovanni,

We didn’t know GO supports so much date types.

One more question: the miliseconds can be added to the datetime format???
I mean “2006-01-02 15:04:05.250” for example.

I don’t think so, afaik those are the available formats for correct parsing, but feel free to try that out

You’re right @Giovanni_Luisotto, miliseconds can’t be parsed.

Another question that comes to mind; we have two topics (“SQLSERVER” and “Linea Piloto”).
How can we add two topic data into the same bucket???

The fields “vnivelc” and “FecAsig” comes from SQLSERVER and we have a field “Maquina” that comes from “Linea Piloto”. Looking on the code from above how can we assign each field,tag and timestamp to each topic???