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

**URL:** <https://community.openhab.org/t/float-parsefloat-or-integer-parseint-errors-with-my-actually-config/120835>\
**Category:** Scripts & Rules\
**Created:** [April 14, 2021, 6:55pm UTC](https://community.openhab.org/t/float-parsefloat-or-integer-parseint-errors-with-my-actually-config/120835 "2021-04-14T18:55:31Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![marc1310](https://community.openhab.org/user_avatar/community.openhab.org/marc1310/32/41876_2.png) [@marc1310](https://community.openhab.org/u/marc1310)\
**Post date:** [April 14, 2021, 6:55pm UTC](https://community.openhab.org/t/float-parsefloat-or-integer-parseint-errors-with-my-actually-config/120835/1 "2021-04-14T18:55:31Z")

</div>

Hello guys,

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

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

```

and then i make a if condition,

```yaml
if (actor1_movie2_rating >= 0 && actor1_movie2_rating <= 2 )
{
Item_actor1_movie2_rating_search.sendCommand("/static/rating0.png") 
}

```

and the “Item\_actor1\_movie2\_rating\_search” :

```yaml
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 ?

---

<div class="post-metadata">

**Author:** ![Udo\_Hartmann](https://community.openhab.org/user_avatar/community.openhab.org/udo_hartmann/32/70871_2.png) [@Udo\_Hartmann](https://community.openhab.org/u/Udo_Hartmann)\
**Post date:** [April 14, 2021, 7:13pm UTC](https://community.openhab.org/t/float-parsefloat-or-integer-parseint-errors-with-my-actually-config/120835/2 "2021-04-14T19:13:25Z")

</div>

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?)

---

<div class="post-metadata">

**Author:** ![rossko57](https://community.openhab.org/user_avatar/community.openhab.org/rossko57/32/70263_2.png) [@rossko57](https://community.openhab.org/u/rossko57)\
**Post date:** [April 14, 2021, 7:14pm UTC](https://community.openhab.org/t/float-parsefloat-or-integer-parseint-errors-with-my-actually-config/120835/3 "2021-04-14T19:14:11Z")

</div>

> [@marc1310](#):
>
> sometimes i get a error, but i had not enough infos what the problem is.

You have more info than we do. What error?

---

<div class="post-metadata">

**Author:** ![marc1310](https://community.openhab.org/user_avatar/community.openhab.org/marc1310/32/41876_2.png) [@marc1310](https://community.openhab.org/u/marc1310)\
**Post date:** [April 14, 2021, 7:17pm UTC](https://community.openhab.org/t/float-parsefloat-or-integer-parseint-errors-with-my-actually-config/120835/4 "2021-04-14T19:17:34Z")

</div>

> [@marc1310](#):
>
> the value can be from 0 to 10. but for example this value too “6.099999904632568”

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.

---

<div class="post-metadata">

**Author:** ![marc1310](https://community.openhab.org/user_avatar/community.openhab.org/marc1310/32/41876_2.png) [@marc1310](https://community.openhab.org/u/marc1310)\
**Post date:** [April 14, 2021, 7:18pm UTC](https://community.openhab.org/t/float-parsefloat-or-integer-parseint-errors-with-my-actually-config/120835/5 "2021-04-14T19:18:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rossko57](https://community.openhab.org/user_avatar/community.openhab.org/rossko57/32/70263_2.png) [@rossko57](https://community.openhab.org/u/rossko57)\
**Post date:** [April 14, 2021, 8:00pm UTC](https://community.openhab.org/t/float-parsefloat-or-integer-parseint-errors-with-my-actually-config/120835/6 "2021-04-14T20:00:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![marc1310](https://community.openhab.org/user_avatar/community.openhab.org/marc1310/32/41876_2.png) [@marc1310](https://community.openhab.org/u/marc1310)\
**Post date:** [April 14, 2021, 8:07pm UTC](https://community.openhab.org/t/float-parsefloat-or-integer-parseint-errors-with-my-actually-config/120835/7 "2021-04-14T20:07:21Z")

</div>

I do not know exactly.

I have now adjusted my code.

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

```

my if condition

````yaml
 if (actor1_movie1_rating.startsWith("{"))
    { 
        Item_actor1_movie1_rating_search.postUpdate("0")   
    }
    else
    {
        if (actor1_movie1_rating >= 0 && actor1_movie1_rating <= 2 )
        {
        Item_actor1_movie1_rating_search.sendCommand("/static/rating0.png") 
        }
        if (actor1_movie1_rating >= 2 && actor1_movie1_rating <= 4 )
        {
        Item_actor1_movie1_rating_search.sendCommand("/static/rating1.png") 
        }
        if (actor1_movie1_rating >= 4 && actor1_movie1_rating <= 6 )
        {
        Item_actor1_movie1_rating_search.sendCommand("/static/rating2.png") 
        }
        if (actor1_movie1_rating >= 6 && actor1_movie1_rating <= 8 )
        {
        Item_actor1_movie1_rating_search.sendCommand("/static/rating3.png") 
        }
        if (actor1_movie1_rating >= 8 && actor1_movie1_rating <= 10 )
        {
        Item_actor1_movie1_rating_search.sendCommand("/static/rating4.png") 
        }
        if (actor1_movie1_rating == 10 )
        {
        Item_actor1_movie1_rating_search.sendCommand("/static/rating5.png") 
        }
    }```

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.
````

---

<div class="post-metadata">

**Author:** ![rossko57](https://community.openhab.org/user_avatar/community.openhab.org/rossko57/32/70263_2.png) [@rossko57](https://community.openhab.org/u/rossko57)\
**Post date:** [April 14, 2021, 8:13pm UTC](https://community.openhab.org/t/float-parsefloat-or-integer-parseint-errors-with-my-actually-config/120835/8 "2021-04-14T20:13:30Z")

</div>

> [@marc1310](#):
>
> I do not know exactly.

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

---

<div class="post-metadata">

**Author:** ![marc1310](https://community.openhab.org/user_avatar/community.openhab.org/marc1310/32/41876_2.png) [@marc1310](https://community.openhab.org/u/marc1310)\
**Post date:** [April 14, 2021, 8:14pm UTC](https://community.openhab.org/t/float-parsefloat-or-integer-parseint-errors-with-my-actually-config/120835/9 "2021-04-14T20:14:28Z")

</div>

> [@rossko57](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![rossko57](https://community.openhab.org/user_avatar/community.openhab.org/rossko57/32/70263_2.png) [@rossko57](https://community.openhab.org/u/rossko57)\
**Post date:** [April 14, 2021, 8:35pm UTC](https://community.openhab.org/t/float-parsefloat-or-integer-parseint-errors-with-my-actually-config/120835/10 "2021-04-14T20:35:29Z")

</div>

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

> [@marc1310](#):
>
> he then says rule with the UID can not be executed, and then there is the entire Json result of my json call.

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!

---

<div class="post-metadata">

**Author:** ![Udo\_Hartmann](https://community.openhab.org/user_avatar/community.openhab.org/udo_hartmann/32/70871_2.png) [@Udo\_Hartmann](https://community.openhab.org/u/Udo_Hartmann)\
**Post date:** [April 14, 2021, 9:26pm UTC](https://community.openhab.org/t/float-parsefloat-or-integer-parseint-errors-with-my-actually-config/120835/11 "2021-04-14T21:26:23Z")

</div>

Maybe void would suffice:

```auto
    val actor1_movie1_rating = transform("JSONPATH", "$.result.movies.[0]['rating']", json_actor1_movies)
    var String strRating = ""
    if (!(actor1_movie1_rating instanceof Number)) {
        Item_actor1_movie1_rating_search.postUpdate("0")
    } 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.
