volkszaehler / vzlogger

Logging utility for various meters & sensors
http://wiki.volkszaehler.org/software/controller/vzlogger
GNU General Public License v3.0
145 stars 124 forks source link

Decimal values from meter become unprecise when being sent to mqtt #547

Open gr8bit opened 1 year ago

gr8bit commented 1 year ago

Software: vzlogger v0.8.1 based on heads/master-0-g27eb8d1566 from Wed, 28 Sep 2022 16:27:45 +0200 Device: Energy meter via IR D0

When reading absolute counter values from my energy meter, they are being transmitted correctly as the debug mode shows. When they're submitted to mqtt though, they seem to get the "float inaccuracy treatment" and are sometimes sent with lots of digits after the comma:

Bildschirm­foto 2022-11-05 um 23 59 00

As 37298.16 represented in double float is 37298.160000000003492459654808 (which matches the digits shown above), is there any configuration I can use to prevent this from happening? Like setting the maximum precision on the channel (which I know is 2 digits)?

Edit: I followed the code to this line which renders the value vor MQTT publish: https://github.com/volkszaehler/vzlogger/blob/76e868feede73b3f86b63d031768f71a227a903c/src/mqtt.cpp#L303

The publish() method gets the channel struct already, so adding a scale or precision option to the channel configuration and then applying that in the above line (using json_object_new_double_s) might be a possibility. What do you think?

r00t- commented 1 year ago

the debug output actually forces two digits of precision: https://github.com/volkszaehler/vzlogger/blob/76e868feede73b3f86b63d031768f71a227a903c/src/threads.cpp#L87 the influxdb api forces six digits: https://github.com/volkszaehler/vzlogger/blob/76e868feede73b3f86b63d031768f71a227a903c/src/api/InfluxDB.cpp#L301 mysmartgrid seems to only deal with integers: https://github.com/volkszaehler/vzlogger/blob/76e868feede73b3f86b63d031768f71a227a903c/src/api/MySmartGrid.cpp#L525 only the volkszaehler api seems to not limit the precision (like mqtt): https://github.com/volkszaehler/vzlogger/blob/76e868feede73b3f86b63d031768f71a227a903c/src/api/Volkszaehler.cpp#L242

r00t- commented 1 year ago

i very much dislike the wording of the ticket title, the numbers are not "imprecise", rather, too much precision is used in the binary->decimal conversion. the causes are descibed here: https://en.wikipedia.org/wiki/Floating-point_arithmetic#Representable_numbers,_conversion_and_rounding but i'm still not sure what the correct term for the effect is. "quantization error" is my best guess so far.

r00t- commented 1 year ago

note that a very related floating-point related issue is discussed here: https://github.com/volkszaehler/vzlogger/issues/500