Convert boolean to int in JSON from mqtt_consumer

I’m running zigbee2mqtt, mosquitto, telegraf and influxdb in docker containers, and have configured telegraf inputs.mqtt_consumer to receive JSON format messages from some topics in mosquitto and write them to influxdb. I’m using Grafana to view the resulting data (I hope it’s not heretical to mention anything other than Chronograf here… :slight_smile:).

One of the topics contains messages such as:

{"contact":true,"linkquality":136,"battery":100,"voltage":3035}

Telegraf drops the boolean field and just stores linkquality, battery and voltage to influxdb.

Is there some way I could configure Telegraf to convert the boolean field to an int (1 for true, 0 for false) so that I can chart these values in Grafana?

I’ve tried to do it with processors.converter, processors.regex and processors.enum, none of which had any effect.

Thanks,
Rob.

1 Like

Hi @DrRob,

You do not need to convert your boolean to an integer. You need to tell the JSON parser that you want it to process the non-number fields using json_string_fields

You can see the sample output below, and you can find my example code here.

# telegraf.conf
[[inputs.mqtt_consumer]]
    data_format = "json"
    json_string_fields = ["contact"]
# influxql
> SELECT "temperature", "something", "contact", "my_field" FROM "telegraf"."autogen"."influxmqtt_consumer"  where contact = true limit 5;
name: influxmqtt_consumer
time                temperature something contact my_field
----                ----------- --------- ------- --------
1549888912218749500 20.5        1.5       true    awesome
1549888917181607600 20.5        1.5       true    awesome
1549888922336646900 20.5        1.5       true    awesome
1549888927698123200 20.5        1.5       true    awesome
1549888932631822100 20.5        1.5       true    awesome
1 Like

Oh! :man_facepalming: That was so much easier than I expected. Thanks!

1 Like

I’m trying to do the same.
Is it correct that:

  1. Chronograf can’t visualize strings
  2. I can’t map those strings into integers using a query?

How can I configure Telegraf to store all true/false from MQTT as 1/0?
Also, is there a way to just store everything as a field without having to list all the field names?
If I add some new sensor I don’t want to lose data just because those names are not set up.

You should be able to configure telegraf to store true/false as 1/0 by using the enum processor

Thanks, that works. Would be nicer if field could be a regex.

[[processors.enum]]
  order = 2
  [[processors.enum.mapping]]
    field = "contact"
    [processors.enum.mapping.value_mappings]
      false = 0
      true = 1
  [[processors.enum.mapping]]
    field = "occupancy"
    [processors.enum.mapping.value_mappings]
      false = 0
      true = 1

Hello @DrRob,

I am looking for same configuration: zigbee@mqtt → mqtt-broker → telegraf → influxdb

Could you provide your telegraf configuration please?

I recently post with my problem: MQTT input in Telegraf

Hope I can get it woking.

Regards.