Type mismatch: cannot convert BigDecimal to long

Dear all

Obviously I have a type mismatch issue:

var long currentTime = now.millis
var long timeElapsed = currentTime - LastUpdate.state as Number

I’m not very fluent with all the different types. Can anybody help? Btw: I need LastUpdate to be persisted, that is the reason why I use an item for that and not just a val

Any help s highly appreciated.

Have a look at BigDecimal#longValue() or BigDecimal#longValueExact()

Rules DSL is loosely typed. I think you’ll find your currentTime actually becomes an Integer type.

Forget using long, try

var currentTime = now.millis     // who cares what type really
var timeElapsed = currentTime - (LastUpdate.state as Number).intValue

Thank you all

By just not specifying the type, there is no error. However, I was just wondering, if by not specifying I loose some precision in the storing of data. As I said I‘m really beginner with all these data types and their representation.

DSL Rules essentially operate with Number as a “native” type. Really, that’s a BigDecimal and it’s hard to imagine any home automation use where you’d need more precision.

Sometimes you have to force Numbers into integer etc. to suit some java function or other.

1 Like

Thank you for the clarification!