Skip to content

feat: Add offset option for scaling measurement values#52

Merged
C8Y-Mario merged 5 commits intothin-edge:mainfrom
wilbersl:feature-scaled-value-offset
Feb 23, 2026
Merged

feat: Add offset option for scaling measurement values#52
C8Y-Mario merged 5 commits intothin-edge:mainfrom
wilbersl:feature-scaled-value-offset

Conversation

@wilbersl
Copy link
Contributor

@wilbersl wilbersl commented Feb 6, 2026

Add additive scaling option (offset) to register definition

Description

This PR introduces an additional scaling option called offset to the register definition.

The offset is applied after all existing multiplication-based scaling factors (multiplier, divisor, decimalShiftRight) have been processed.

This enables linear value transformations of the form:

            scaled_value = (
                value
                * (register_def.get("multiplier") or 1)
                * (10 ** (register_def.get("decimalshiftright") or 0))
                / (register_def.get("divisor") or 1)
            ) + (register_def.get("offset") or 0)

Motivation

Some registers cannot be represented correctly using multiplication-only scaling.

Typical use cases include:

  • Temperature sensors with fixed offsets (e.g. -40°C)
  • Analog values with shifted zero points
  • Vendor-specific calibration offsets

Adding offset allows these cases to be modeled cleanly while keeping the existing scaling model intact.

Implementation Details

  • Added optional offset field to the register definition
  • Applied offset after all existing scaling operations
  • Default value is 0
  • No changes required for existing register definitions

Backward Compatibility

  • Fully backward compatible
  • Registers without offset behave exactly as before

Example

[[device.registers]]
number=1
startbit=0
nobits=16
signed=true
multiplier=1
divisor=1
decimalshiftright=0
offset=-40
input=false
name="Test_Int16"
measurementmapping.templatestring="{\"Test\":{\"Temp\":%% }}" 

@wilbersl wilbersl deployed to Test Pull Request February 10, 2026 10:49 — with GitHub Actions Active
@@ -1,5 +1,6 @@
#!/usr/bin/env python3
"""Cumulocity IoT c8y_Coils operation handler"""

Copy link
Contributor

Choose a reason for hiding this comment

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

Ideally formatting commits should be done in a different PR if the PR is including a new function (e.g. commit 0ed05c6), otherwise it makes it harder to review as there is some additional "noise" for unrelated topics. But we can keep this commit for now in the PR, but it would be helpful for the future.

@github-actions
Copy link

Robot Results

✅ Passed ❌ Failed ⏭️ Skipped Total Pass % ⏱️ Duration
24 0 0 24 100 49.511497s

Passed Tests

Name ⏱️ Duration Suite
Device should support the operation c8y_ModbusConfiguration 0.211 s Device
Set values via c8y_ModbusConfiguration Operation 2.529 s Operation
Poll rate and transmit rate should be updated for the Device 0.248 s Operation
Poll rate and transmit rate should be updated on the Device 2.498 s Operation
Device should support the operation c8y_ModbusDevice 0.131 s Device
Device should support the operation c8y_SerialConfiguration 0.131 s Device
Set values via c8y_SerialConfiguration Operation 2.488 s Operation
Serial configuration should be updated for the Device 0.252 s Operation
Serial configuration should be updated on the Device 2.507 s Operation
Device should have installed software tedge-modbus-plugin 0.156 s Debian
Service should be active 2.510 s Debian
ReInstall Modbus Plugin 23.821 s Debian
Device should have the fragment c8y_ModbusConfiguration 0.264 s Device
ChildDevice TestCase1 should be created 0.251 s Device
ChildDevice TestCase1 should have the fragment c8y_ModbusDevice 0.630 s Device
Service tedge-modbus-plugin should be enabled 0.130 s Device
Device should set supported operations for writing to registers and coils 0.126 s Operations
Device should write to a register using explicit address format 2.527 s Operations
Device should write to a register referenced by name 2.505 s Operations
Device should write float value to register referenced by name 2.530 s Operations
Device should write to a coil 2.534 s Operations
ChildDevice TestCase1 should have a Test.Int16 Measurement 0.268 s Telemetry
ChildDevice TestCase1 should have a Test.Float32 Measurement 0.260 s Telemetry
ChildDevice TestCase1 should have Alarms of type TestAlarm on Coil Value 1 0.001 s Telemetry

Copy link
Contributor

@reubenmiller reubenmiller left a comment

Choose a reason for hiding this comment

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

Thanks for the PR @wilbersl, only one minor comment for the future, but overall a very nice addition

@reubenmiller reubenmiller changed the title Feature: Add offset option for scaling measurement values feat: Add offset option for scaling measurement values Feb 23, 2026
@C8Y-Mario C8Y-Mario added this pull request to the merge queue Feb 23, 2026
Merged via the queue into thin-edge:main with commit 072f2e0 Feb 23, 2026
5 checks passed
@wilbersl
Copy link
Contributor Author

Always happy to add stuff ;) ! @reubenmiller Thanks for the comment, I'll try to do as you told. As I mostly work alone, I'm not really trained in proper team workflow.

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

Successfully merging this pull request may close these issues.

3 participants