arendst / Tasmota

Alternative firmware for ESP8266 and ESP32 based devices with easy configuration using webUI, OTA updates, automation using timers or rules, expandability and entirely local control over MQTT, HTTP, Serial or KNX. Full documentation at
https://tasmota.github.io/docs
GNU General Public License v3.0
22.2k stars 4.81k forks source link

Prometheus + INA226 I2C sensor does not handle negative current values #16041

Closed davidhorrock0949 closed 2 years ago

davidhorrock0949 commented 2 years ago

PROBLEM DESCRIPTION

Although the web client correctly displays negative values for current using an INA226 I2C module, the Prometheus metric is filtered out and does not display at /metrics if the value is less than zero. This is a problem for measuring bidirectional current (for example, in a battery charge/discharge system).

REQUESTED INFORMATION

Make sure your have performed every step and checked the applicable boxes before submitting your issue. Thank you!

18:56:06.450 RSL: RESULT = {"NAME":"Generic","GPIO":[1,1,1,1,1,1,1,1,1,1,1,1,1,1],"FLAG":0,"BASE":18} 18:56:06.670 RSL: RESULT = {"Module":{"0":"Generic"}} 18:56:06.924 RSL: RESULT = {"GPIO0":{"0":"None"},"GPIO1":{"0":"None"},"GPIO2":{"0":"None"},"GPIO3":{"0":"None"},"GPIO4":{"640":"I2C SDA"},"GPIO5":{"608":"I2C SCL"},"GPIO9":{"0":"None"},"GPIO10":{"0":"None"},"GPIO12":{"0":"None"},"GPIO13":{"0":"None"},"GPIO14":{"0":"None"},"GPIO15":{"0":"None"},"GPIO16":{"0":"None"},"GPIO17":{"0":"None"}}

- [x] If using rules, provide the output of this command: `Backlog Rule1; Rule2; Rule3`:
```lua
  Rules output here:

Rules not used
- [X ] Set `weblog` to 4 and then, when you experience your issue, provide the output of the Console log:
```lua
  Console output here:

Not applicable

TO REPRODUCE

Enable Prometheus and an INA226 module, any measured currents less than zero will not be logged in metrics.

EXPECTED BEHAVIOUR

Negative currents will show up in metrics.

SCREENSHOTS

N/A

ADDITIONAL CONTEXT

Negative Current is correctly displayed in the web interface:

INA226-1 Voltage | 13.32 V INA226-1 Current | -0.647 A INA226-1 Power | 8.7 W

However, current is not available as a prometheus metrics when current is negative:

TYPE tasmota_info gauge

tasmota_info{version="12.0.2.4",image="(TasmoCompiler-esp82664M)",build_timestamp="2022-07-21T12:49:47",devicename="Tasmota",friendlyname="Tasmota"} 1

TYPE tasmota_uptime_seconds gauge

tasmota_uptime_seconds{} 767

TYPE tasmota_boot_count counter

tasmota_boot_count{} 4

TYPE tasmota_flash_writes_total counter

tasmota_flash_writes_total{} 10

TYPE tasmota_wifi_station_info gauge

tasmota_wifi_station_info{bssid="9C:A5:70:7D:48:E5",ssid="mango"} 1

TYPE tasmota_wifi_station_signal_dbm gauge

tasmota_wifi_station_signal_dbm{mac_address="9C:A5:70:7D:48:E5"} -74

TYPE tasmota_memory_free_bytes gauge

tasmota_memory_free_bytes{memory="heap"} 19152

TYPE tasmota_energy_voltage_volts gauge

tasmota_energy_voltage_volts{} 0.00

TYPE tasmota_energy_current_amperes gauge

tasmota_energy_current_amperes{} 0.000

TYPE tasmota_energy_power_active_watts gauge

tasmota_energy_power_active_watts{} 0.0

TYPE tasmota_energy_power_kilowatts_daily counter

tasmota_energy_power_kilowatts_daily{} 0.000

TYPE tasmota_energy_power_kilowatts_total counter

tasmota_energy_power_kilowatts_total{} 0.000

TYPE tasmota_sensors_id_untyped gauge

tasmota_sensors_id_untyped{sensor="ina226-1",id="0"} 1

TYPE tasmota_sensors_voltage_volts gauge

tasmota_sensors_voltage_volts{sensor="ina226-1"} 13.30

TYPE tasmota_sensorspower gauge

tasmota_sensorspower{sensor="ina226-1"} 8.5

barbudor commented 2 years ago

Could you please copy/paste the json returned by Tasmota on the '/metrics' url?

barbudor commented 2 years ago

OK, I found the issue in this test https://github.com/arendst/Tasmota/blob/40a5d661fefd9fc8c5fb2d84f819d231767d6f90/tasmota/tasmota_xsns_sensor/xsns_75_prometheus.ino#L301 I can make a PR but I won't be able to test Would you be able to compile from my fork or should I provide you with a binary? If binary, do you need anything else than INA226 + prometheus to validate?

barbudor commented 2 years ago

I prepared a fix, can you please test with the attached tasmota-sensors binaries ? They are the standard tasmota-sensors + USE_INA226 + USE_PROMETHEUS

The files here : tasmota-sensors_test-ina226-prometheus.zip

Alternatively if you can compile yourself from my branch: https://github.com/barbudor/Tasmota/tree/prometheus_negative_values

If successfull, please report here : https://github.com/arendst/Tasmota/pull/16044

davidhorrock0949 commented 2 years ago

Thanks for the quick fix! I'll compile and test it here this evening and provide an update.

davidhorrock0949 commented 2 years ago

Awesome, that fixed it! Works perfectly now. Capture