kpetremann / mqtt-exporter

Simple generic MQTT Prometheus exporter for IoT working out of the box
https://hub.docker.com/r/kpetrem/mqtt-exporter
MIT License
110 stars 30 forks source link

Shelly Plus H&T topic format not working with exporter #62

Closed r0ckarong closed 11 months ago

r0ckarong commented 11 months ago

I just bought two of these devices and updated them to Firmware 1.0.8 as suggested by the Web UI. I configured MQTT fine and the messages are showing up in my mosquitto broker instance.

What I can't seem to make work is the other end to write this stuff to my prometheus instance.

For my devices, the shelly topic structure looks like this:

Screenshot from 2023-12-07 18-26-38

For me it is not <prefix>/<name>/sensor/temperature it's <prefix>/<name>/status/temperature:0.

I've got another mqtt exporter tool working for my tasmota device and it's working fine there. I just don't understand what I'm doing wrong with your tool that it just seems to do nothing.

Here are the relevant snippets of my configs:

docker-compose.yaml

[...]
  prometheus:
    container_name: prometheus
    image: prom/prometheus:v2.48.0
    restart: unless-stopped
    volumes:
      - ./prometheus/:/etc/prometheus/
      # Make sure the folder on the filesystem is owned by 65534:65534
      - ./prometheus/tsdb:/tsdb/
    ports:
      - 9090:9090
    command:
      - '--storage.tsdb.retention.time=2y'
      - '--storage.tsdb.path=/tsdb/'
      - '--web.enable-admin-api'
      - '--config.file=/etc/prometheus/prometheus.yaml'

[...]
  mqtt-exporter:
    image: kpetrem/mqtt-exporter
    container_name: shelly-mqtt-exporter
    ports:
      - 9093:9093
    environment:
      MQTT_ADDRESS: mqtt5
      MQTT_USERNAME: NOOPE
      MQTT_PASSWORD: NOOPE
      MQTT_EXPOSE_CLIENT_ID: true
      MQTT_TOPIC: shellies/+/status/#
      PROMETHEUS_PREFIX: shelly_
      PROMETHEUS_PORT: 9090
    restart: unless-stopped

prometheus.yaml

global:
  scrape_interval: 15s
  evaluation_interval: 15s

scrape_configs:
[...]
  - job_name: "shelly"
    static_configs:
      - targets: ['shelly-mqtt-exporter:9093']
r0ckarong commented 11 months ago

Here is a bit of the full mqtt data that gets sent

{
  "src": "shellyplusht-a0a3b3de6b4c",
  "dst": "shellies/shellyplusht-a0a3b3de6b4c/events",
  "method": "NotifyFullStatus",
  "params": {
    "ts": 1.86,
    "ble": {},
    "cloud": {
      "connected": false
    },
    "devicepower:0": {
      "id": 0,
      "battery": {
        "V": 6.39,
        "percent": 100
      },
      "external": {
        "present": false
      }
    },
    "ht_ui": {},
    "humidity:0": {
      "id": 0,
      "rh": 43.8
    },
    "mqtt": {
      "connected": true
    },
    "sys": {
      "mac": "A0A3B3DE6B4C",
      "restart_required": false,
      "time": null,
      "unixtime": null,
      "uptime": 1,
      "ram_size": 246612,
      "ram_free": 165028,
      "fs_size": 458752,
      "fs_free": 176128,
      "cfg_rev": 23,
      "kvs_rev": 1,
      "webhook_rev": 0,
      "available_updates": {},
      "wakeup_reason": {
        "boot": "deepsleep_wake",
        "cause": "status_update"
      },
      "wakeup_period": 7200
    },
    "temperature:0": {
      "id": 0,
      "tC": 20.2,
      "tF": 68.3
    },
    "wifi": {
      "sta_ip": "192.168.178.110",
      "status": "got ip",
      "ssid": "EnairaSukram",
      "rssi": -55
    },
    "ws": {
      "connected": false
    }
  }
}
kpetremann commented 11 months ago

Hi @r0ckarong

Thanks for raising the issue and for the details you provided.

Could you please also send the log with debug level?

I'll troubleshoot it this weekend.

r0ckarong commented 11 months ago

Can you give me an idea on how to get the "correct" log? I'm using all of this on the Synology Container manager and this just gives me one line.

Where in the container is the log stored? I've set LOG_LEVEL: DEBUG in the compose file.

r0ckarong commented 11 months ago

Ok nevermind, figured out the problem.

The exporter was subscribed correctly to shellies/+/status/# and the log showed the right values but I had some confusion about the ports. I configured port mapping for docker but then didn't realize I had to configure the custom port for the exporter too. So my prometheus config was trying to scrape from port 9093 but the exporter was still using default 9000.

kpetremann commented 11 months ago

Happy you found the root cause. Cheers