openhab-addons: [mongodb] persistence query gives ClassCastException for QuantityTypes
Expected Behavior
If I query the historic states in analyze, graphing or use the persistence interface, I want to get the stored states and no error š
Current Behavior
In Openhab I query the history for an item and want to get the stored states for a given timerange. In the MainUI - I navigate to the settings->items and select an item with a type of Number:Energy and want analyze itās value, but nothing gets returned, but in the log I see:
2024-01-20 07:05:05.242 [DEBUG] [b.internal.MongoDBPersistenceService] - Query: { "item" : "Erdgeschoss_Kueche_Kuehlschrank_meter_totalKWH_currentyear" , "timestamp" : { "$gte" : { "$date" : "2024-01-19T06:05:02.691Z"} , "$lte" : { "$date" : "2024-01-20T06:05:02.690Z"}}}
2024-01-20 07:05:05.251 [ERROR] [internal.JSONResponseExceptionMapper] - Unexpected exception occurred while processing REST request.
java.lang.ClassCastException: null
Possible Solution
I checked the code for the MongoDB persistence service and see that in https://github.com/openhab/openhab-addons/blob/9e1f87db86409ec2ea1a36ea0de64bbd14c79608/bundles/org.openhab.persistence.mongodb/src/main/java/org/openhab/persistence/mongodb/internal/MongoDBPersistenceService.java
Method public Iterable<HistoricItem> query(FilterCriteria filter) {
while (cursor.hasNext()) {
BasicDBObject obj = (BasicDBObject) cursor.next();
final State state;
if (item instanceof NumberItem) {
state = new DecimalType(obj.getDouble(FIELD_VALUE));
} else if (item instanceof DimmerItem) {
state = new PercentType(obj.getInt(FIELD_VALUE));
} else if (item instanceof SwitchItem) {
state = OnOffType.valueOf(obj.getString(FIELD_VALUE));
} else if (item instanceof ContactItem) {
state = OpenClosedType.valueOf(obj.getString(FIELD_VALUE));
} else if (item instanceof RollershutterItem) {
state = new PercentType(obj.getInt(FIELD_VALUE));
} else if (item instanceof DateTimeItem) {
state = new DateTimeType(
ZonedDateTime.ofInstant(obj.getDate(FIELD_VALUE).toInstant(), ZoneId.systemDefault()));
} else {
state = new StringType(obj.getString(FIELD_VALUE));
}
Does not deal with quantity types as well as the write operation does not - it uses a private Object convertValue(State state) { with
Object value;
if (state instanceof PercentType type) {
value = type.toBigDecimal().doubleValue();
} else if (state instanceof DateTimeType type) {
value = Date.from(type.getZonedDateTime().toInstant());
} else if (state instanceof DecimalType type) {
value = type.toBigDecimal().doubleValue();
} else {
value = state.toString();
}
return value;
}
Both will handle Quantity Types as āStringā, but for sure all other components do expect a QuantityType like it is done in the influxdb persistence service:
public static Object stateToObject(State state) {
Object value;
if (state instanceof HSBType) {
value = state.toString();
} else if (state instanceof PointType) {
value = state.toString();
} else if (state instanceof DecimalType type) {
value = type.toBigDecimal();
} else if (state instanceof QuantityType<?> type) {
value = type.toBigDecimal();
} else if (state instanceof OnOffType) {
value = state == OnOffType.ON ? DIGITAL_VALUE_ON : DIGITAL_VALUE_OFF;
} else if (state instanceof OpenClosedType) {
value = state == OpenClosedType.OPEN ? DIGITAL_VALUE_ON : DIGITAL_VALUE_OFF;
} else if (state instanceof DateTimeType type) {
value = type.getZonedDateTime().toInstant().toEpochMilli();
} else {
value = state.toFullString();
}
return value;
}
Iām unsure, whether to fix this in the read or write section of the mongodb persistence service
Steps to Reproduce (for Bugs)
Create a Number:Energy typed item, pump values in and try to graph it.
Context
Iām trying to use the mongodb persistence service instead of influxdb.
Your Environment
- Version used: (e.g., openHAB and add-on versions) 4.1.1-alpine
- Environment name and version - official docker container
- Operating System and version alpine
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 19 (18 by maintainers)
Commits related to this issue
- [mongodb] Upgrade DB driver, add more type handlings, fix QuantityType handling (#16333) * #16308 #16310 Upgraded MongoDB driver, added initial unit tests * #16308 #16310 Refactored the MongoDBPersi... — committed to openhab/openhab-addons by ulbi 3 years ago
- [mongodb] Upgrade DB driver, add more type handlings, fix QuantityType handling (#16333) * #16308 #16310 Upgraded MongoDB driver, added initial unit tests * #16308 #16310 Refactored the MongoDBPersis... — committed to austvik/openhab-addons by ulbi 3 years ago
- [mongodb] Upgrade DB driver, add more type handlings, fix QuantityType handling (#16333) * #16308 #16310 Upgraded MongoDB driver, added initial unit tests * #16308 #16310 Refactored the MongoDBPersi... — committed to joni1993/openhab-addons by ulbi 3 years ago
- [mongodb] Upgrade DB driver, add more type handlings, fix QuantityType handling (#16333) * #16308 #16310 Upgraded MongoDB driver, added initial unit tests * #16308 #16310 Refactored the MongoDBPersis... — committed to QDinsight/openhab-addons by ulbi 3 years ago
Would do similar to the influxdb implementation.
You should fix it in both, so that
QuantityTypecan be persisted and restored.