From f51025ff871f279db95cb44501ec7b5710618965 Mon Sep 17 00:00:00 2001 From: Arne Seime Date: Wed, 17 Jul 2024 10:40:25 +0200 Subject: [PATCH] Dont try to parse UndefType values as QuantityType for precondition comparisons --- .../jrule/internal/engine/JRuleEngine.java | 46 +++++++++++-------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/openhab/automation/jrule/internal/engine/JRuleEngine.java b/src/main/java/org/openhab/automation/jrule/internal/engine/JRuleEngine.java index db72e86f..abbb6a5c 100644 --- a/src/main/java/org/openhab/automation/jrule/internal/engine/JRuleEngine.java +++ b/src/main/java/org/openhab/automation/jrule/internal/engine/JRuleEngine.java @@ -76,6 +76,8 @@ import org.openhab.core.library.types.QuantityType; import org.openhab.core.persistence.PersistenceServiceRegistry; import org.openhab.core.scheduler.CronScheduler; +import org.openhab.core.types.State; +import org.openhab.core.types.UnDefType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.slf4j.MDC; @@ -285,37 +287,45 @@ public boolean matchPrecondition(JRuleExecutionContext jRuleExecutionContext) { } catch (ItemNotFoundException e) { throw new JRuleItemNotFoundException("Cannot find item for precondition", e); } - final String state = item.getState().toString(); - if (context.getEq().isPresent() && context.getEq().filter(state::equals).isEmpty()) { - logDebug("precondition mismatch: {} = {}", state, context.getEq().get()); + State state = item.getState(); + final String stateAsString = state.toString(); + if (context.getEq().isPresent() && context.getEq().filter(stateAsString::equals).isEmpty()) { + logDebug("precondition mismatch: {} = {}", stateAsString, context.getEq().get()); return false; } - if (context.getNeq().isPresent() && context.getNeq().filter(ref -> !state.equals(ref)).isEmpty()) { - logDebug("precondition mismatch: {} != {}", state, context.getNeq().get()); + if (context.getNeq().isPresent() && context.getNeq().filter(ref -> !stateAsString.equals(ref)).isEmpty()) { + logDebug("precondition mismatch: {} != {}", stateAsString, context.getNeq().get()); return false; } - if (context.getLt().isPresent() - && context.getLt().filter(ref -> QuantityType.valueOf(state).doubleValue() < ref).isEmpty()) { - logDebug("precondition mismatch: {} < {}", state, context.getLt().get()); + + // No need to compare numbers if the state is not a number + if (state instanceof UnDefType) { + logDebug("precondition mismatch: {} cannot be compared as number", stateAsString); + return false; + } + + if (context.getLt().isPresent() && context.getLt() + .filter(ref -> QuantityType.valueOf(stateAsString).doubleValue() < ref).isEmpty()) { + logDebug("precondition mismatch: {} < {}", stateAsString, context.getLt().get()); return false; } - if (context.getLte().isPresent() - && context.getLte().filter(ref -> QuantityType.valueOf(state).doubleValue() <= ref).isEmpty()) { - logDebug("precondition mismatch: {} <= {}", state, context.getLte().get()); + if (context.getLte().isPresent() && context.getLte() + .filter(ref -> QuantityType.valueOf(stateAsString).doubleValue() <= ref).isEmpty()) { + logDebug("precondition mismatch: {} <= {}", stateAsString, context.getLte().get()); return false; } - if (context.getGt().isPresent() - && context.getGt().filter(ref -> QuantityType.valueOf(state).doubleValue() > ref).isEmpty()) { - logDebug("precondition mismatch: {} > {}", state, context.getGt().get()); + if (context.getGt().isPresent() && context.getGt() + .filter(ref -> QuantityType.valueOf(stateAsString).doubleValue() > ref).isEmpty()) { + logDebug("precondition mismatch: {} > {}", stateAsString, context.getGt().get()); return false; } - if (context.getGte().isPresent() - && context.getGte().filter(ref -> QuantityType.valueOf(state).doubleValue() >= ref).isEmpty()) { - logDebug("precondition mismatch: {} >= {}", state, context.getGte().get()); + if (context.getGte().isPresent() && context.getGte() + .filter(ref -> QuantityType.valueOf(stateAsString).doubleValue() >= ref).isEmpty()) { + logDebug("precondition mismatch: {} >= {}", stateAsString, context.getGte().get()); return false; } - logDebug("precondition match: {} matches {}", state, context); + logDebug("precondition match: {} matches {}", stateAsString, context); return true; }); }