Number items: inValue and methods fail

Please help.

I am really struggling with Number item types.

I extract a number item in json format:
val Number dist = transform("JSONPATH", "$.trips[0].tripDetails[0].distance", json)

I would like to compare this:
if(dist.intValue >= 5000) {

But I get:
Rule 'Request Last trip Details': An error occurred during the script execution: Could not invoke method: java.lang.Number.intValue() on instance: 1248

I also tried
if(dist >= 5000) {
and
int instead of Number at val definition

But I get:
Rule 'Request Last trip Details': An error occurred during the script execution: Could not invoke method: org.eclipse.xtext.xbase.lib.IntegerExtensions.operator_greaterEqualsThan(int,int) on instance: null

But dist is not null - it’s logged correctly as
[pse.smarthome.model.script.car.rules] - ++++dist: >1248<

I don’t get it: dist is a number (int from my perspective). So why couldn’t I compare it then?

The following does not work either:
val Number dist = transform("JSONPATH", "$.trips[0].tripDetails[0].distance", json)
and
if(dist as DecimalType.intValue>= 5000) {

I got:
Rule 'Request Last trip Details': Could not cast 1248 to void; line 201, column 7, length 28

Same here:

val Number dist = transform("JSONPATH", "$.trips[0].tripDetails[0].distance", json)

CarTripDist.postUpdate(dist) // CarTripDist is Number item
if(CarTripDist.state >= 5000) {

I get:

Rule 'Request Last trip Details': An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.actions.BusEvent.postUpdate(org.eclipse.smarthome.core.items.Item,java.lang.Number) on instance: null

This does actually work:

val Number dist = transform("JSONPATH", "$.trips[0].tripDetails[0].distance", json)
CarTripDist.postUpdate(Float::parseFloat(dist))
if(CarTripDist.state >= 5000) {

However, I would like to know, the right syntax for the local variable dist for comparison > 5000.
Anny suggestion is greatly appreciated

Did you try?

val dist = transform("JSONPATH", "$.trips[0].tripDetails[0].distance", json) as Number
if(dist >= 5000) {

Transforms only return Strings. You have to explicitly parse a string into a number.

You are almost there with your last try.

val float dist = Float::parseFloat(transform(...

Note that will return a primitive float which is not a Number but it works with Numbers in operations.

@vzorglub the as operator doesn’t actually do anything to convert the Object to another type. Any Object can have many different types. For example MyNumberItem.state is of type Object, Command, State, DecimalType, and Number. So when we use as Number we are telling the rules DSL that of all the types that Object represents, you want to use the Number type. But the key is the Object has to be a Number already. transform only returns String Object so you can’t just say as Number because Number isn’t one of its types.

1 Like

Hi Rich,

I guess, I got it :wink:
That makes sense.
I will review my code and see if I can deduct a proper syntax for my case.
Thanks a lot

It does work - indeed.
Thanks again for your help.