MQTT input in Telegraf

Hello,

I have a MQTT broker to receive some sensor values (temperature, pressure, humidity…) and send to Home Assistant.

I want to store this data in InfluxDB with Telegraf. So first I need to ensure that I can read this data from mosquitto broker with Telegraf.

The mosquitto and telegraf are in docker, the mosquitto log say that the telegraf is well connected:

1572644970: New connection from 172.18.0.6 on port 1883.
1572644970: New client connected from 172.18.0.6 as telegraf (p2, c1, k60, u’my_user’).

And this is the telegraf log:

2019-11-01T21:49:30Z I! [inputs.mqtt_consumer] Connected [tcp://mosquitto:1883]

This is the configuration:

telegraf.conf

[[inputs.mqtt_consumer]]
servers = [“tcp://mosquitto:1883”]
topics = [“temperature/linkquality/humidity/pressure/battery/voltage”,]
qos = 0
connection_timeout = “30s”
persistent_session = false
client_id = “telegraf”
username = “my_user”
password = “my_password”
data_format = “json”

mosquitto.conf:

{
“logins”: [
{
“username”: “my_user”,
“password”: “my_password”
}
],
“anonymous”: false,
“customize”: {
“active”: false,
“folder”: “mosquitto”
},
}

How could I check that all the sensor data is arriving to telegraf? Is there any easy way to check?

I am new in the software and I will appreciate your help.

Regards.

Hi,

I think your ‘topics’ declaration looks wrong. Each entry in this list is a topic path, not a list of topics separated by ‘/’.

Here are the non-comment lines from my telegraf.conf for comparison:

[global_tags]
[agent]
  interval = "10s"
  round_interval = true
  metric_batch_size = 1000
  metric_buffer_limit = 10000
  collection_jitter = "0s"
  flush_interval = "10s"
  flush_jitter = "0s"
  precision = ""
  debug = false
  quiet = false
  logfile = ""
  hostname = "telegraf"
  omit_hostname = false
[[outputs.influxdb]]
urls = ["http://serf:8086"]
database = "zigbee"
[[outputs.file]]
files = ["stdout"]
data_format = "json"
[[inputs.mqtt_consumer]]
servers = ["tcp://serf:1883"]
topics = [
  "zigbee2mqtt/Router",
  "zigbee2mqtt/HPRouter",
  "zigbee2mqtt/Door",
  "zigbee2mqtt/Living room",
  "zigbee2mqtt/Landing",
  "zigbee2mqtt/Loft2",
  "zigbee2mqtt/Motion",
  "zigbee2mqtt/Greenhouse"
]
username = "mosquitto"
password = "notreallymypassword"
data_format = "json"
json_string_fields = ["contact", "occupancy"]

HTH,
Rob.

1 Like

Thanks!

This works, thank you very much.

Last question, how did you configure InlfuxDB? you just use database name (zigbee), where did you declare in InfluxDB?

Best regards.

Hi,

Regrettably, I don’t have the influxdb config recorded in a file - I must have set it up manually after initial startup (Bad DrRob! Slap wrist.)

There might be some clues here:

$ curl -sG 'http://serf:8086/query?db=zigbee' --data-urlencode "q=SHOW MEASUREMENTS"| python -mjson.tool 
{
    "results": [
        {
            "series": [
                {
                    "columns": [
                        "name"
                    ],
                    "name": "measurements",
                    "values": [
                        [
                            "cpu"
                        ],
                        [
                            "disk"
                        ],
                        [
                            "diskio"
                        ],
                        [
                            "kernel"
                        ],
                        [
                            "mem"
                        ],
                        [
                            "mqtt_consumer"
                        ],
                        [
                            "processes"
                        ],
                        [
                            "swap"
                        ],
                        [
                            "system"
                        ]
                    ]
                }
            ],
            "statement_id": 0
        }
    ]
}

(note that I’ve got some other databases in there as well as the one that MQTT messages are sent to)

and:

$ curl -sG 'http://serf:8086/query?db=zigbee' --data-urlencode "q=SHOW SERIES FROM mqtt_consumer"| python -mjson.tool
{
    "results": [
        {
            "series": [
                {
                    "columns": [
                        "key"
                    ],
                    "values": [
                        [
                            "mqtt_consumer,host=telegraf,topic=zigbee2mqtt/Door"
                        ],
                        [
                            "mqtt_consumer,host=telegraf,topic=zigbee2mqtt/Greenhouse"
                        ],
                        [
                            "mqtt_consumer,host=telegraf,topic=zigbee2mqtt/HPRouter"
                        ],
                        [
                            "mqtt_consumer,host=telegraf,topic=zigbee2mqtt/Landing"
                        ],
                        [
                            "mqtt_consumer,host=telegraf,topic=zigbee2mqtt/Living\\ room"
                        ],
                        [
                            "mqtt_consumer,host=telegraf,topic=zigbee2mqtt/Loft2"
                        ],
                        [
                            "mqtt_consumer,host=telegraf,topic=zigbee2mqtt/Motion"
                        ],
                        [
                            "mqtt_consumer,host=telegraf,topic=zigbee2mqtt/Router"
                        ],
                        [
                            "mqtt_consumer,host=telegraf,topic=zigbee2mqtt/Third"
                        ]
                    ]
                }
            ],
            "statement_id": 0
        }
    ]
}

I don’t remember doing much setup. I’m sure I haven’t had to do anything any time I’ve added a new sensor. When I first started using InfluxDB I was running it on a Pi - I think it was this tutorial that I followed: Raspberry Pi Data Logger with InfluxDB and Grafana | John Whittington's Blog . There was very little InfluxDB setup required there.

I hope you can figure out what you need to do for yours with that - let me know!

Rob.

@zorrua,

Thanks for your question! I recommend setting debug=true in your telegegraf config if you haven’t already. :wink:

Also, InfluxDB is schema-on-write which means that the schema will be created when you attempt to write data to it. As long as the database exists, it will start writing data to it. Note that if I remember correctly, the mqtt input plugin will by default write to the mqtt_consumer measurement in the telegraf database.

Best, dg