Skip to content

Commit 20610fb

Browse files
authored
Fix persistence threshold filter with UoM (#4881)
* Add tests with UoM conversion For `PersistenceThresholdFilter` add a test case that fails when using temperatures in °C and a threshold in Kelvin. The threshold should ignore the offset (0 °C = 273,15 K). Add another test with length to increase coverage. Signed-off-by: Christoph Gehrke <cgdev@duck.com> * Fix PersistenceThresholdFilter UoM conversion Use `toUnitRelative()` instead of `toUnit()` for delta (the difference of old and current state). In particular for temperatures an offset of different units (like °C and K) should be ignored when considering the delta. Signed-off-by: Christoph Gehrke <cgdev@duck.com> --------- Signed-off-by: Christoph Gehrke <cgdev@duck.com>
1 parent cc95a6d commit 20610fb

2 files changed

Lines changed: 5 additions & 1 deletion

File tree

bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/filter/PersistenceThresholdFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public boolean apply(Item item) {
104104
}
105105
} else if (!unit.isBlank()) {
106106
// consider unit only if not relative threshold
107-
delta = delta.toUnit(unit);
107+
delta = delta.toUnitRelative(unit);
108108
if (delta == null) {
109109
throw new UnconvertibleException("");
110110
}

bundles/org.openhab.core.persistence/src/test/java/org/openhab/core/persistence/filter/PersistenceThresholdFilterTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ private static Stream<Arguments> argumentProvider() {
102102
// quantity type, below threshold, absolute, with unit
103103
Arguments.of(new QuantityType<>("10 m"), new QuantityType<>("10.002 m"), "mm", false, false),
104104
// quantity type, above threshold, absolute, with unit
105+
Arguments.of(new QuantityType<>("1 ft"), new QuantityType<>("11 in"), "mm", false, true),
106+
// quantity type, below threshold, absolute, with unit
107+
Arguments.of(new QuantityType<>("0 °C"), new QuantityType<>("0 °C"), "K", false, false),
108+
// quantity type, above threshold, absolute, with unit
105109
Arguments.of(new QuantityType<>("-10 °C"), new QuantityType<>("5 °C"), "K", false, true));
106110
}
107111

0 commit comments

Comments
 (0)