mqtt_exporter icon indicating copy to clipboard operation
mqtt_exporter copied to clipboard

Json

Open leomargontier opened this issue 1 year ago • 2 comments

How to manage a json metric (device, value, timestamp) ?

leomargontier avatar Sep 15 '23 16:09 leomargontier

@leomargontier sorry for the late reply.

I typically use regex to parse out.

e.g to get the uptime from a tasmota TELE/State:

{"Time":"2023-11-01T11:02:36","Uptime":"44T11:20:54","UptimeSec":3842454,"Heap":100,"SleepMode":"Dynamic","Sleep":50,"LoadAvg":19,"MqttCount":28,"Berry":{"HeapUsed":14,"Objects":293},"POWER1":"OFF","POWER2":"OFF","POWER3":"ON","POWER4":"OFF","POWER5":"OFF","Wifi":{"AP":1,"SSId":"iot","BSSId":"D0:21:F9:E6:2C:FD","Channel":1,"Mode":"11n","RSSI":94,"Signal":-53,"LinkCount":1,"Downtime":"0T00:00:03"}}

I am using:

  - name: "tasmota_uptimesec"
    help: "Tasmota uptime in seconds"
    type: "gauge"
    topic: "esp/+/tele/STATE" # this topic may contain several results
    label_configs:
      - source_labels:  ['__value__']
        action:         'keep'
        regex:          '.*"UptimeSec":([-]?[0-9]+)'
      - source_labels:  ['__msg_topic__']
        target_label:   '__topic__'
      - source_labels:  ['__msg_topic__']
        target_label:   'controller'
        regex:          'esp/([a-zA-Z0-9]+)/tele/STATE'
        replacement:    '\1'
        action:         'replace'
      - source_labels: ['__value__']
        target_label:   '__value__'
        regex:          '.*"UptimeSec":([-]?[0-9]+).*'
        replacement:    '\1'
        action:         'replace'

I know this looks more like a workaround, but working well. Have some build in json parser would be nice, but it is not there. PRs are welcome ;-)

poggenpower avatar Nov 01 '23 10:11 poggenpower

Ahh I saw you did a PR, thanks! Would you mind to create an example with explaining comment. and PR it?

poggenpower avatar Nov 01 '23 10:11 poggenpower