diff --git a/bundles/org.openhab.transform.basicprofiles/src/main/java/org/openhab/transform/basicprofiles/internal/profiles/StateFilterProfile.java b/bundles/org.openhab.transform.basicprofiles/src/main/java/org/openhab/transform/basicprofiles/internal/profiles/StateFilterProfile.java index e48236a0704..e482a1ba58a 100644 --- a/bundles/org.openhab.transform.basicprofiles/src/main/java/org/openhab/transform/basicprofiles/internal/profiles/StateFilterProfile.java +++ b/bundles/org.openhab.transform.basicprofiles/src/main/java/org/openhab/transform/basicprofiles/internal/profiles/StateFilterProfile.java @@ -271,33 +271,37 @@ public class StateFilterProfile implements StateProfile { // This allows comparing compatible types, e.g. PercentType vs OnOffType parsedValue = parsedValue.as(state.getClass()); } + } - // If the values can't be converted to a type, check to see if it's an Item name - if (parsedValue == null) { - try { - Item valueItem = itemRegistry.getItem(value); - if (valueItem != null) { // ItemRegistry.getItem can return null in tests - parsedValue = valueItem.getState(); - // Don't convert QuantityType to other types - if (!(parsedValue instanceof QuantityType)) { - parsedValue = parsedValue.as(state.getClass()); - } - logger.debug("Condition value: '{}' is an item state: '{}' ({})", value, parsedValue, - parsedValue == null ? "null" : parsedValue.getClass().getSimpleName()); + // From hereon, don't override this.parsedValue, + // so it gets checked against Item's state on each call + State parsedValue = this.parsedValue; + + // If the values couldn't be converted to a type, check to see if it's an Item name + if (parsedValue == null) { + try { + Item valueItem = itemRegistry.getItem(value); + if (valueItem != null) { // ItemRegistry.getItem can return null in tests + parsedValue = valueItem.getState(); + // Don't convert QuantityType to other types + if (!(parsedValue instanceof QuantityType)) { + parsedValue = parsedValue.as(state.getClass()); } - } catch (ItemNotFoundException ignore) { + logger.debug("Condition value: '{}' is an item state: '{}' ({})", value, parsedValue, + parsedValue == null ? "null" : parsedValue.getClass().getSimpleName()); } + } catch (ItemNotFoundException ignore) { } + } - if (parsedValue == null) { - if (comparisonType == ComparisonType.NEQ || comparisonType == ComparisonType.NEQ_ALT) { - // They're not even type compatible, so return true for NEQ comparison - return true; - } else { - logger.debug("Condition value: '{}' is not compatible with state '{}' ({})", value, state, - state.getClass().getSimpleName()); - return false; - } + if (parsedValue == null) { + if (comparisonType == ComparisonType.NEQ || comparisonType == ComparisonType.NEQ_ALT) { + // They're not even type compatible, so return true for NEQ comparison + return true; + } else { + logger.debug("Condition value: '{}' is not compatible with state '{}' ({})", value, state, + state.getClass().getSimpleName()); + return false; } } diff --git a/bundles/org.openhab.transform.basicprofiles/src/test/java/org/openhab/transform/basicprofiles/internal/profiles/StateFilterProfileTest.java b/bundles/org.openhab.transform.basicprofiles/src/test/java/org/openhab/transform/basicprofiles/internal/profiles/StateFilterProfileTest.java index 425a23273e7..e803b602aa9 100644 --- a/bundles/org.openhab.transform.basicprofiles/src/test/java/org/openhab/transform/basicprofiles/internal/profiles/StateFilterProfileTest.java +++ b/bundles/org.openhab.transform.basicprofiles/src/test/java/org/openhab/transform/basicprofiles/internal/profiles/StateFilterProfileTest.java @@ -615,7 +615,6 @@ public class StateFilterProfileTest { String linkedItemName = linkedItem.getName(); String itemName = item.getName(); - item.setState(state); when(mockContext.getConfiguration()).thenReturn(new Configuration(Map.of("conditions", operator + itemName))); when(mockItemRegistry.getItem(itemName)).thenReturn(item); @@ -623,7 +622,13 @@ public class StateFilterProfileTest { when(mockItemChannelLink.getItemName()).thenReturn(linkedItemName); StateFilterProfile profile = new StateFilterProfile(mockCallback, mockContext, mockItemRegistry); + item.setState(UnDefType.UNDEF); + profile.onStateUpdateFromHandler(inputState); + reset(mockCallback); + when(mockCallback.getItemChannelLink()).thenReturn(mockItemChannelLink); + + item.setState(state); profile.onStateUpdateFromHandler(inputState); verify(mockCallback, times(expected ? 1 : 0)).sendUpdate(eq(inputState)); }