Convert boolean to int in JSON from mqtt_consumer


#1

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.


#2

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

#3

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