Mongo db cannot be cast to class java.lang.Number

Hello everyone,

I just updated to 4.1 and I’m in the process of switching from influxdb to mongodb persistence, because it looks more flexible. Which I need for my usecases ( I work with the data a lot).

After going to 4.1 or 4.1.1 (not sure about this, since I’m still not in production with mongodb) all values are written with quantities - OK fair. But when I query for the persistence I get this error:

2024-01-14 16:24:16.011 [DEBUG] [b.internal.MongoDBPersistenceService] - Query: { "item" : "FroniusSymoInverter_LoadPower" , "timestamp" : { "$gte" : { "$date" : "2024-01-13T15:24:15.989Z"} , "$lte" : { "$date" : "2024-01-14T15:24:15.989Z"}}}
2024-01-14 16:24:16.016 [ERROR] [internal.JSONResponseExceptionMapper] - Unexpected exception occurred while processing REST request.
java.lang.ClassCastException: class java.lang.String cannot be cast to class java.lang.Number (java.lang.String and java.lang.Number are in module java.base of loader 'bootstrap')
        at org.bson.BasicBSONObject.getDouble(BasicBSONObject.java:183) ~[?:?]
        at org.openhab.persistence.mongodb.internal.MongoDBPersistenceService.query(MongoDBPersistenceService.java:398) ~[?:?]
        at org.openhab.core.io.rest.core.internal.persistence.PersistenceResource.createDTO(PersistenceResource.java:425) ~[?:?]
        at org.openhab.core.io.rest.core.internal.persistence.PersistenceResource.getItemHistoryDTO(PersistenceResource.java:329) ~[?:?]
        at org.openhab.core.io.rest.core.internal.persistence.PersistenceResource.httpGetPersistenceItemData(PersistenceResource.java:278) ~[?:?]
        at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
        at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[?:?]
        at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
        at java.lang.reflect.Method.invoke(Method.java:568) ~[?:?]

Here a sample document (don’t care on the additional data downSample and numberValue - I use them to control some other processes and also graphing, because grafana does not deal with quantities in the values either ;-():

{
    _id: ObjectId('65a2acc00cf2d8ca2397095b'),
    item: 'FroniusSymoInverter_LoadPower',
    realName: 'FroniusSymoInverter_LoadPower',
    timestamp: ISODate('2024-01-13T15:31:12.281Z'),
    value: '-4322.74 W',
    downSample: null,
    numberValue: -4322.74
}

Does anybody have an idea how to fix this?

What I’ve done:

  1. update all values and removed the quantity → works
  2. update all values and added the quantity → fails (thought it could be due to the mix, since influxdb does not include the quantity)

For the time being, I will just drop the quantity again in the processing.

It would be great to hear, which direction this should be fixed. Either the mongodb PersistenceService can read values with quantity or the quantity is not written. Both options are fine, just need to know :wink:

Doing it w/o quantities would save some processing and simplify a couple of things, but since I solved this alread, I don’t care…

Regards,
René

Just to update and close this thread. I figured out, the MongoDB persistence cannot deal with units at all. So I created and adressed the following items in the MongoDB persistence:

All placed in this PR Upgrade DB driver, add more type handlings, fix QuantityType handling

I hope it gets accepted and in 4.2 or 4.3 this is completely adressed.