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

Support for getting number value from QuantityType (item of type Number:XXX) #14

Merged
merged 3 commits into from
Jan 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.openhab.core.library.types.OpenClosedType;
import org.openhab.core.library.types.PercentType;
import org.openhab.core.library.types.PlayPauseType;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.library.types.StopMoveType;
import org.openhab.core.library.types.StringType;
import org.openhab.core.library.types.UpDownType;
Expand Down Expand Up @@ -397,6 +398,8 @@ public Double getStateFromItemAsDouble(String name) {
}
if (state instanceof PercentType) {
return ((PercentType) state).doubleValue();
} else if (state instanceof QuantityType) {
return ((QuantityType) state).doubleValue();
} else {
DecimalType decimalType = state.as(DecimalType.class);
return decimalType != null ? decimalType.doubleValue() : null;
Expand Down
23 changes: 13 additions & 10 deletions src/main/java/org/openhab/automation/jrule/rules/JRuleEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
import org.openhab.automation.jrule.rules.value.JRuleOnOffValue;
import org.openhab.automation.jrule.rules.value.JRuleOpenClosedValue;
import org.openhab.automation.jrule.rules.value.JRuleUpDownValue;
import org.openhab.core.library.types.QuantityType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* The {@link JRuleEvent}
Expand All @@ -23,6 +26,8 @@
*/
public class JRuleEvent {

private static final Logger logger = LoggerFactory.getLogger(JRuleEvent.class);

private final String value;

private String memberName;
Expand Down Expand Up @@ -53,23 +58,21 @@ public JRuleUpDownValue getValueAsUpDownValue() {
}

public Double getValueAsDouble() {
Double d = null;
try {
d = Double.parseDouble(value);
} catch (NumberFormatException x) {
// ignore
return QuantityType.valueOf(value).doubleValue();
} catch (Exception e) {
logger.warn("Error converting {} to double: {}", value, e.getMessage());
return null;
}
return d;
}

public Integer getValueAsInteger() {
Integer i = null;
try {
i = Integer.parseInt(value);
} catch (NumberFormatException x) {
// ignore
return QuantityType.valueOf(value).intValue();
} catch (Exception e) {
logger.warn("Error converting {} to int: {}", value, e.getMessage());
return null;
}
return i;
}

public String getMemberName() {
Expand Down