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
108 stars 30 forks source link

Shelly metrics reporting as zero #22

Closed Merlin83b closed 2 years ago

Merlin83b commented 2 years ago

I'm trying to use mqtt-exporter to expose some values from a Shelly EM. Running with LOG_LEVEL=DEBUG, I see the following output with each MQTT message published:

DEBUG:mqtt-exporter:failed to parse payload as JSON: "b'off'"
DEBUG:mqtt-exporter:new value for mqtt_power: 1647.29
DEBUG:mqtt-exporter:new value for mqtt_reactive_power: -458.5
DEBUG:mqtt-exporter:new value for mqtt_voltage: 241.64
DEBUG:mqtt-exporter:new value for mqtt_total: 30483.3
DEBUG:mqtt-exporter:new value for mqtt_total_returned: 0.0
DEBUG:mqtt-exporter:new value for mqtt_power: 0.0
DEBUG:mqtt-exporter:new value for mqtt_reactive_power: 0.0
DEBUG:mqtt-exporter:new value for mqtt_voltage: 241.64
DEBUG:mqtt-exporter:new value for mqtt_total: 0.0
DEBUG:mqtt-exporter:new value for mqtt_total_returned: 0.0
DEBUG:mqtt-exporter:new value for mqtt_energy: 1677
DEBUG:mqtt-exporter:new value for mqtt_returned_energy: 0
DEBUG:mqtt-exporter:new value for mqtt_energy: 0
DEBUG:mqtt-exporter:new value for mqtt_returned_energy: 0

All good so far. Then, on the /metrics output, the following:

# HELP mqtt_message_total Counter of received messages
# TYPE mqtt_message_total counter
mqtt_message_total{topic="shellies_shellyem"} 68.0
# HELP mqtt_message_created Counter of received messages
# TYPE mqtt_message_created gauge
mqtt_message_created{topic="shellies_shellyem"} 1.6490785172786949e+09
# HELP mqtt_power metric generated from MQTT message.
# TYPE mqtt_power gauge
mqtt_power{topic="shellies_shellyem"} 0.0
# HELP mqtt_reactive_power metric generated from MQTT message.
# TYPE mqtt_reactive_power gauge
mqtt_reactive_power{topic="shellies_shellyem"} 0.0
# HELP mqtt_voltage metric generated from MQTT message.
# TYPE mqtt_voltage gauge
mqtt_voltage{topic="shellies_shellyem"} 240.1
# HELP mqtt_total metric generated from MQTT message.
# TYPE mqtt_total gauge
mqtt_total{topic="shellies_shellyem"} 0.0
# HELP mqtt_total_returned metric generated from MQTT message.
# TYPE mqtt_total_returned gauge
mqtt_total_returned{topic="shellies_shellyem"} 0.0
# HELP mqtt_energy metric generated from MQTT message.
# TYPE mqtt_energy gauge
mqtt_energy{topic="shellies_shellyem-"} 0.0
# HELP mqtt_returned_energy metric generated from MQTT message.
# TYPE mqtt_returned_energy gauge
mqtt_returned_energy{topic="shellies_shellyem"} 0.0

So it seems that most of the values are being lost at output time. Are you able to see why this is happening?

kpetremann commented 2 years ago

Hello @Merlin83b,

Can you send the original messages from MQTT?

According the logs, the metrics are "duplicated" as they have the same device id or name. Let's take the power metric for instance.

In the logs there is:

DEBUG:mqtt-exporter:new value for mqtt_power: 1647.29
DEBUG:mqtt-exporter:new value for mqtt_power: 0.0

but at the end you only get:

mqtt_power{topic="shellies_shellyem"} 0.0

Looks like you devices have the same name/identifier (shellye). So you only get the last power metric as it overrides it.

Please check your MQTT configuration on your Shelly devices.

Merlin83b commented 2 years ago

In a separate window I have mosquitto_sub -h localhost -u user -P password -v -t "shellies/shellyem/emeter/0/power" running and get the following for the MQTT messages:

shellies/shellyem-BCFF4DFCFBFD/emeter/0/power 1447.54
shellies/shellyem-BCFF4DFCFBFD/emeter/0/power 1432.74
shellies/shellyem-BCFF4DFCFBFD/emeter/0/power 1424.27
shellies/shellyem-BCFF4DFCFBFD/emeter/0/power 1399.98
shellies/shellyem-BCFF4DFCFBFD/emeter/0/power 1393.41
shellies/shellyem-BCFF4DFCFBFD/emeter/0/power 1412.84
shellies/shellyem-BCFF4DFCFBFD/emeter/0/power 1409.69
shellies/shellyem-BCFF4DFCFBFD/emeter/0/power 1424.67
shellies/shellyem-BCFF4DFCFBFD/emeter/0/power 1419.98
shellies/shellyem-BCFF4DFCFBFD/emeter/0/power 1427.57

Would you like this running with moquitto_sub in debug mode??

Merlin83b commented 2 years ago

Aha, I think I've just realised that it's likely because the Shelly EM has two power monitors, form the above there is also emeter/1/power and it's likely that that's overwriting the value.

Merlin83b commented 2 years ago

Yep, that does it. Adjusting the topic to MQTT_TOPIC=shellies/+/emeter/0/# has sorted it for my use case.

kpetremann commented 2 years ago

perfect :)