Float::parseFloat or Integer::parseInt , errors with my actually config

Hello guys,

i actually use the “Float::parseFloat” function to parse a Json value named “rating”.

var Number actor1_movie1_rating = Float::parseFloat(transform("JSONPATH", "$.result.movies.[0]['rating']", json_actor1_movies))

and then i make a if condition,

if (actor1_movie2_rating >= 0 && actor1_movie2_rating <= 2  )

and the “Item_actor1_movie2_rating_search” :

String Item_actor1_movie2_rating_search "Rating film1 des ausgeaehlten Actors [%s]"

sometimes i get a error, but i had not enough infos what the problem is.
but i have the suspicion that it is due to the value i get back via json.

the value can be from 0 to 10. but for example this value too “6.099999904632568”

is this a problem for the parse::Float ? should i use Interger::parseInt ? parseInt takes only the first Number or ?

Is rating an integer value or is it of type float? When it’s float, how many decimal places are used (I guess maximum one decimal place?)

You have more info than we do. What error?

the value can be from 0 to 10. but for example this value too “6.099999904632568”

i think this is the maximum decimal place. i cant see the value, its from my media database.

he then says rule with the UID can not be executed, and then there is the entire Json result of my json call.

So your JSONPATH transformation failed, and the still-hidden error has nothing to do with floats and integers?

I do not know exactly.

I have now adjusted my code.

var String actor1_movie1_rating = transform("JSONPATH", "$.result.movies.[0]['rating']", json_actor1_movies)

my if condition

 if (actor1_movie1_rating.startsWith("{"))
        if (actor1_movie1_rating >= 0 && actor1_movie1_rating <= 2  )
        if (actor1_movie1_rating >= 2 && actor1_movie1_rating <= 4  )
        if (actor1_movie1_rating >= 4 && actor1_movie1_rating <= 6  )
        if (actor1_movie1_rating >= 6 && actor1_movie1_rating <= 8  )
        if (actor1_movie1_rating >= 8 && actor1_movie1_rating <= 10  )
        if (actor1_movie1_rating == 10  )

then i get :

2021-04-14 21:59:19.345 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'Json_movies_by_actors_search-1' failed: class java.lang.Integer cannot be cast to class java.lang.String (java.lang.Integer and java.lang.String are in module java.base of loader 'bootstrap') in Json_movies_by_actors_search

the only thing i do with the rating is to see if a
 "{" is present by checking whether the var starts with "{".

if the value in the var is not "{" then I compare the values ​​in order to write certain paths in an item.

But I can't get rid of the error.

Neither do we. Please,just show the error you see in your actual openhab.log for sensible advice.

then read my post above at the end.

I was interested in this secret error message, including the JSON, in order to find out why the transformation failed.

but maybe you don’t care, fair enough.

But anyway …
var String actor1_movie1_rating = transform( ...
always returns a string, which is why you parsed it to a number in your original rule.
Now you don’t parse it, so
if (actor1_movie1_rating >= 0 ...
always fails because you cannot directly compare a string with a number like 0.

Looking for “{” to see if the transformation failed is a good idea!

Maybe void would suffice:

    val actor1_movie1_rating = transform("JSONPATH", "$.result.movies.[0]['rating']", json_actor1_movies)
    var String strRating = ""
    if (!(actor1_movie1_rating instanceof Number)) {
    } else {
        strRating = "0"                     // default, if no option is true
        if(actor1_movie1_rating >= 10)
            strRating = "5"
        else if(actor1_movie1_rating >= 8)
            strRating = "4"
        else if(actor1_movie1_rating >= 6)
            strRating = "3"
        else if(actor1_movie1_rating >= 4)
            strRating = "2"
        else if(actor1_movie1_rating >= 2)
            strRating = "1"
        Item_actor1_movie1_rating_search.sendCommand("/static/rating" + strRating + ".png")

actor1_movie1_rating is neither Number nor String, but an object. Now you can check if it’s of Type Number or not. I have to admit I did not check wether it works or not.