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

Modbus input - negative values not supported with FLOAT32 #7317

Closed
kolmodin opened this issue Apr 11, 2020 · 5 comments
Closed

Modbus input - negative values not supported with FLOAT32 #7317

kolmodin opened this issue Apr 11, 2020 · 5 comments
Labels
area/modbus bug unexpected problem or unintended behavior

Comments

@kolmodin
Copy link

Relevant telegraf.conf:

[[inputs.modbus]]
holding_registers = [
  { name = "outdoor_temperature",   byte_order = "AB",   data_type = "FLOAT32",   scale=0.1,     address = [903]},
]

Related issue: #7266.

The register holds a temperature scaled 10x. I want to store the decimals so I use FLOAT32.
When the temperature outdoors is 5.3° the register holds the int 53, and I want to use scaling to store 5.3.
When the temperature is below 0° the register holds a signed integer representing a negative value.
The problem is when the modbus plugin treats negative values.

Expected behavior:

The modbus plugin should handle negative values as well as positive. When the register holds -13 it should be intepreted as -1.3 after scaling.

Actual behavior:

Negative values are treated as unsigned. With a few negative degrees it stores the float 6554.0.
I see no way around this in telegraf except using the type INT16 instead of FLOAT32 and thus throw away the decimals.

Additional info:

FLOAT32 is handled here. It assumes a UINT16 but INT16 values are also possible.

@danielnelson danielnelson added area/modbus bug unexpected problem or unintended behavior labels May 11, 2020
@sensor-freak
Copy link
Contributor

sensor-freak commented Jul 17, 2020

It seems to me that there is a need to differentiate between two scenarios regarding the handling of FLOAT32 fields:

  1. the old behaviour: read as unsigned int, convert to float, scale
  2. the new behaviour: read as signed int, convert to float, scale

Since there are for sure some people relying on the old behaviour, things will break for them when the new behaviour is introduced. But on the other hand, if we do not change anything, there will be people who cannot use this type at all.

Maybe we should resolve this conflict by introducing an additional type SFLOAT32, which implements the new behaviour. Old configurations will not be affected, and new configurations could make use of signed values.

Btw: FLOAT32 is misnamed, since the field value could be generated from 16, 32 or 64 byte integer values. Instead we should name it FIXED FLOAT (and SFLOAT) and UFIXED in the future. Or maybe even better add UFLOAT and FLOAT and leave FLOAT32 as it is.

Furthermore, we should think about deprecating FLOAT32, because it is not clear enough what this type should be used for.

@sensor-freak
Copy link
Contributor

I have updated PR #7869 which should now solve the issue from @kolmodin. That PR should now also supersede PR #7488.

ssoroka pushed a commit that referenced this issue Jul 27, 2020
ssoroka pushed a commit that referenced this issue Aug 29, 2020
idohalevi pushed a commit to idohalevi/telegraf that referenced this issue Sep 29, 2020
@srebhan
Copy link
Member

srebhan commented Nov 17, 2020

@kolmodin can you verify that with the change of @sensor-freak and changing the datatype to FIXED your issue is solved? If so, please close this ticket! :-)

@kolmodin
Copy link
Author

kolmodin commented Dec 4, 2020

Awesome! I've reviewed the code and it looks like it adresses the issues I was experiencing. When can I expect these changes in a released version of telegraf? I'm using it for thermometer data and it's December - we're having some minus (celcius) degrees here :)

@kolmodin
Copy link
Author

kolmodin commented Dec 6, 2020

I upgraded to 1.16.3 and changed to FIXED. It seems to have worked, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/modbus bug unexpected problem or unintended behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants