Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
gr8bit opened this issue Nov 5, 2022 · 3 comments
Open

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

gr8bit opened this issue Nov 5, 2022 · 3 comments

Comments

@gr8bit
Copy link

gr8bit commented Nov 5, 2022

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:

json_object_object_add(payload_obj, "value", json_object_new_double(rds.value()));

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?

@gr8bit gr8bit changed the title Absolute decimal values from meter become unprecise when sent to mqtt Decimal values from meter become unprecise when being sent to mqtt Nov 6, 2022
@r00t-
Copy link
Contributor

r00t- commented Dec 24, 2022

the debug output actually forces two digits of precision:

print(log_debug, "Reading: id=%s/%s value=%.2f ts=%lld", mtr->name(),

the influxdb api forces six digits:
value_str << " value=" << std::fixed << std::setprecision(6) << it->value();

mysmartgrid seems to only deal with integers:
json_object_array_add(json_tuple, json_object_new_int(value - _first_counter));

only the volkszaehler api seems to not limit the precision (like mqtt):
json_object_array_add(json_tuple, json_object_new_double(it->value()));

@r00t-
Copy link
Contributor

r00t- commented Dec 27, 2022

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-
Copy link
Contributor

r00t- commented Dec 27, 2022

note that a very related floating-point related issue is discussed here:
#500

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants