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

Fix 64-bit modbus sensor register reads #25672

Merged
merged 3 commits into from
Aug 10, 2019

Conversation

tomilehto
Copy link
Contributor

@tomilehto tomilehto commented Aug 3, 2019

Description:

Previously when reading four 16-bit modbus registers as a sensor
value, slave output was stored first as 64-bit integer, but before returning
that value was converted to double precision floating point. This
caused rounding errors for integer values bigger than 2^53.

After this change floating point conversion is done only if user
has configured scaling or offset to use floating points. In the case
where scale and offset are integers (also the default) only integer
arithmetic is used and 64-bit register values will be read correctly.

This PR adds also unit tests for modbus sensor value conversions
that were missing completely.

Related issue (if applicable): fixes #25671

Pull request with documentation for home-assistant.io (if applicable): home-assistant/home-assistant.io#10043

Example entry for configuration.yaml (if applicable):

sensor:
  - platform: modbus
    registers:
     # default good
      - name: nilan_date
        slave: 1
        register: 4722
        register_type: holding
        count: 4
        data_type: uint
     # still good with integer scale and offset
      - name: nilan_date
        slave: 1
        register: 4722
        register_type: holding
        count: 4
        data_type: uint
        scale: 1
        offset: 86400
     # bad when floating point scale or offset
      - name: nilan_date
        slave: 1
        register: 4722
        register_type: holding
        count: 4
        data_type: uint
        scale: 1.0
        offset: 0.0

Checklist:

  • The code change is tested and works locally.
  • Local tests pass with tox. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly. Update and include derived files by running python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt by running python3 -m script.gen_requirements_all.
  • Untested files have been added to .coveragerc.

If the code does not interact with devices:

  • Tests have been added to verify that the new code works.

homeassistant/helpers/config_validation.py Outdated Show resolved Hide resolved
tests/components/modbus/test_modbus_sensor.py Outdated Show resolved Hide resolved
tests/components/modbus/test_modbus_sensor.py Outdated Show resolved Hide resolved
tests/components/modbus/test_modbus_sensor.py Outdated Show resolved Hide resolved
tests/components/modbus/test_modbus_sensor.py Outdated Show resolved Hide resolved
@tomilehto
Copy link
Contributor Author

All review comments addressed and checks pass.

@MartinHjelmare MartinHjelmare changed the title Fix 64-bit modbus sensor register reads (#25761) Fix 64-bit modbus sensor register reads Aug 9, 2019
When reading four 16-bit modbus registers as a sensor value,
slave output is stored first as 64-bit integer, but before returning
that value is converted to double precision floating point. This
causes rounding errors for integer values bigger than 2^53.

After this change floating point conversion is done only if user
has configured scaling or offset using floating points.
@tomilehto tomilehto force-pushed the fix-64bit-modbus-register-read branch from 7c44081 to 88dc1b7 Compare August 9, 2019 22:20
@MartinHjelmare
Copy link
Member

Please don't squash commits after review has started to make it easier for readers to track changes.

@tomilehto
Copy link
Contributor Author

Please don't squash commits after review has started to make it easier for readers to track changes.

Ok. I guess I am too used to using gerrit.

Copy link
Member

@MartinHjelmare MartinHjelmare left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@MartinHjelmare
Copy link
Member

Can be merged when build passes.

@balloob balloob merged commit b79f133 into home-assistant:dev Aug 10, 2019
@lock lock bot locked and limited conversation to collaborators Aug 11, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Modbus sensor fails to read 64-bit register correctly
4 participants