[ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'irriggation-6' failed: For input string: "NULL" in irriggation

If i get a wheater json forcast without any “rain” values script is running into error

1/ JSON (without any rain)

 Watering_algorithm: forecastJson[{"cod":"200","message":0,"cnt":8,"list":[{"dt":1651147200,"main":{"temp":10.61,"feels_like":8.82,"temp_min":9.48,"temp_max":10.61,"pressure":1028,"sea_level":1028,"grnd_level":1027,"humidity":42,"temp_kf":1.13},"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02d"}],"clouds":{"all":20},"wind":{"speed":3.26,"deg":37,"gust":2.81},"visibility":10000,"pop":0,"sys":{"pod":"d"},"dt_txt":"2022-04-28 12:00:00"},{"dt":1651158000,"main":{"temp":10.11,"feels_like":8.35,"temp_min":9.1,"temp_max":10.11,"pressure":1028,"sea_level":1028,"grnd_level":1026,"humidity":45,"temp_kf":1.01},"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02d"}],"clouds":{"all":21},"wind":{"speed":3.1,"deg":48,"gust":2.61},"visibility":10000,"pop":0,"sys":{"pod":"d"},"dt_txt":"2022-04-28 15:00:00"},{"dt":1651168800,"main":{"temp":7.27,"feels_like":6.09,"temp_min":5.6,"temp_max":7.27,"pressure":1027,"sea_level":1027,"grnd_level":1027,"humidity":56,"temp_kf":1.67},"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02d"}],"clouds":{"all":21},"wind":{"speed":1.92,"deg":31,"gust":1.96},"visibility":10000,"pop":0,"sys":{"pod":"d"},"dt_txt":"2022-04-28 18:00:00"},{"dt":1651179600,"main":{"temp":4.66,"feels_like":4.66,"temp_min":4.66,"temp_max":4.66,"pressure":1028,"sea_level":1028,"grnd_level":1027,"humidity":58,"temp_kf":0},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03n"}],"clouds":{"all":26},"wind":{"speed":0.69,"deg":224,"gust":0.69},"visibility":10000,"pop":0,"sys":{"pod":"n"},"dt_txt":"2022-04-28 21:00:00"},{"dt":1651190400,"main":{"temp":4.12,"feels_like":4.12,"temp_min":4.12,"temp_max":4.12,"pressure":1028,"sea_level":1028,"grnd_level":1027,"humidity":57,"temp_kf":0},"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02n"}],"clouds":{"all":23},"wind":{"speed":1.29,"deg":263,"gust":1.31},"visibility":10000,"pop":0,"sys":{"pod":"n"},"dt_txt":"2022-04-29 00:00:00"},{"dt":1651201200,"main":{"temp":3.6,"feels_like":2.52,"temp_min":3.6,"temp_max":3.6,"pressure":1027,"sea_level":1027,"grnd_level":1026,"humidity":65,"temp_kf":0},"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02n"}],"clouds":{"all":19},"wind":{"speed":1.38,"deg":275,"gust":1.39},"visibility":10000,"pop":0,"sys":{"pod":"n"},"dt_txt":"2022-04-29 03:00:00"},{"dt":1651212000,"main":{"temp":7.44,"feels_like":7.44,"temp_min":7.44,"temp_max":7.44,"pressure":1028,"sea_level":1028,"grnd_level":1027,"humidity":58,"temp_kf":0},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"clouds":{"all":25},"wind":{"speed":1.01,"deg":337,"gust":1.19},"visibility":10000,"pop":0,"sys":{"pod":"d"},"dt_txt":"2022-04-29 06:00:00"},{"dt":1651222800,"main":{"temp":10.2,"feels_like":8.61,"temp_min":10.2,"temp_max":10.2,"pressure":1028,"sea_level":1028,"grnd_level":1027,"humidity":51,"temp_kf":0},"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02d"}],"clouds":{"all":15},"wind":{"speed":2.2,"deg":69,"gust":1.75},"visibility":10000,"pop":0,"sys":{"pod":"d"},"dt_txt":"2022-04-29 09:00:00"}],"city":{"id":3099434,"name":"GdaĹ
                                                                                   sk","coord":{"lat":54.3521,"lon":18.6464},"country":"PL","population":0,"timezone":7200,"sunrise":1651115662,"sunset":1651169474}}]

var forecastUrl = openwheatherUrl + "forecast?id=" + cityId + "&APPID=" + APPID + "&units=metric&cnt=" + cnt

   

    logInfo( "FILE", "Watering_algorithm: Getting wheather forecast [" + forecastUrl + "]")

   

    // Trying get  wheather forecast 3 times (from time to time, first try fail)

    var forecastJson=""

    try {

        forecastJson = sendHttpGetRequest(forecastUrl)

        var getCounter=0

        while ( (forecastJson===null) && (getCounter<3)) {

        forecastJson = sendHttpGetRequest(forecastUrl)

        getCounter++

        logInfo( "FILE", "Watering_algorithm: getCounter[" + getCounter + "]")

        }

    } catch(Throwable t) {

        logInfo("FILE", "Watering_algorithm: Exception happen during getting heather data")

        forecastJson = null

    }

       

    logInfo( "FILE", "Watering_algorithm: forecastJson[" + forecastJson + "]")

   

    if ( !(forecastJson === null)) {    

       

        // When wheather forecast cannot be downloaded we get NULL

        if( ! (forecastJson==="<code>NULL</code>")) {

       

        // Extract only section about rainfall, looked at

        // https://community.openhab.org/t/solved-smhi-weather/22300/20

            var String[] rainValues  = transform("JSONPATH","$.list[*].rain.3h", forecastJson).replace("[", "").replace("]", "").split(",")

            rainfallMm = 0

            logInfo( "FILE", "Linijka 424 rainvalues: [" + rainValues + "]")

        // Check if there is at least one 'rain' section i json

        if(rainValues.get(0)!=="")  

            logInfo( "FILE", "Linijka 427")

       

            for (var i = 0; i < rainValues.length; i++) {

   

            logInfo( "FILE", "Linijka 431 petla")

            rainfallMm = rainfallMm  + (Double::parseDouble(rainValues.get(i)))

            logInfo( "FILE", "Linijka 433 petla")

            }

            logInfo( "FILE", "Watering_algorithm: rainfall forecast [" + rainfallMm + "] for next [" + cnt + "] periods, rainfall sum [" + rainfallMm + "]")            

        }   else {

        // could not get rainfall forecast, set rainfall=0

        logInfo( "FILE", "Watering_algorithm: Cannot get rainfall forecast")

        rainfallMm = 0

        }          

    } else {

        logInfo( "FILE", "Watering_algorithm: Wheather forecast is NULL - rainfallMm set to 0mm ")

        rainfallMm = 0

    }

2022-04-28 14:12:00.794 [INFO ] [org.openhab.core.model.script.FILE ] - Linijka 424 rainvalues: [[Ljava.lang.String;@188f8ad]
2022-04-28 14:12:00.796 [INFO ] [org.openhab.core.model.script.FILE ] - Linijka 427
2022-04-28 14:12:00.798 [INFO ] [org.openhab.core.model.script.FILE ] - Linijka 431 petla
2022-04-28 14:12:00.800 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘irriggation-6’ failed: For input string: “NULL” in irriggation

the problems is in this if… rainValues(0) is “NULL” but none of these operands is working as expected
/// != null, !== null, !== NULL, != NULL, != “NULL”, !== “NULL”,!== “”,!= “”

In Rules DSL, unnecessarily specifying the types of thing pretty much always leads to trouble.

var rainValues = transform(....

Once you do that, you’ll discover that transform never returns a String[]. It only ever returns a plain old String. So rainValues.get(0) is pretty much meaningless because you don’t have an array of Strings.

If you need to process more than one result from a JSON, either need to create a separate JSONPATH transform for each value you want (which is troublesome when you don’t know how many values there are) or you’ll have to use the Java JSON libraries to parse the JSON in Rules DSL.

thank you so much! as always very helpful :slight_smile: i think it will be better to get rid of “manual” openwheater http connection and use wheather binding which wiill parse json and put the values in items and then just sum it up in rules DSL.
Thanks!

can you please explain WHY if there is a .rain. string in json script works as expected ?


 Watering_algorithm: forecastJson[{"cod":"200","message":0,"cnt":8,"list":[{"dt":1651352400,"main":{"temp":10.98,"feels_like":9.62,"temp_min":8.9,"temp_max":10.98,"pressure":1021,"sea_level":1021,"grnd_level":1021,"humidity":57,"temp_kf":2.08},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03n"}],"clouds":{"all":33},"wind":{"speed":3.25,"deg":177,"gust":4.75},"visibility":10000,"pop":0.06,"sys":{"pod":"n"},"dt_txt":"2022-04-30 21:00:00"},{"dt":1651363200,"main":{"temp":9.9,"feels_like":9.01,"temp_min":8.84,"temp_max":9.9,"pressure":1021,"sea_level":1021,"grnd_level":1021,"humidity":66,"temp_kf":1.06},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04n"}],"clouds":{"all":67},"wind":{"speed":2.08,"deg":246,"gust":4.52},"visibility":10000,"pop":0.14,"sys":{"pod":"n"},"dt_txt":"2022-05-01 00:00:00"},{"dt":1651374000,"main":{"temp":8.18,"feels_like":6.61,"temp_min":8.18,"temp_max":8.18,"pressure":1021,"sea_level":1021,"grnd_level":1020,"humidity":75,"temp_kf":0},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10n"}],"clouds":{"all":100},"wind":{"speed":2.58,"deg":314,"gust":4.05},"visibility":10000,"pop":0.28,**"rain":{"3h":0.15}**,"sys":{"pod":"n"},"dt_txt":"2022-05-01 03:00:00"},{"dt":1651384800,"main":{"temp":9.04,"feels_like":7.64,"temp_min":9.04,"temp_max":9.04,"pressure":1022,"sea_level":1022,"grnd_level":1021,"humidity":66,"temp_kf":0},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"clouds":{"all":100},"wind":{"speed":2.56,"deg":284,"gust":5.26},"visibility":10000,"pop":0.24,**"rain":{"3h":0.1}**,"sys":{"pod":"d"},"dt_txt":"2022-05-01 06:00:00"},{"dt":1651395600,"main":{"temp":11.15,"feels_like":9.81,"temp_min":11.15,"temp_max":11.15,"pressure":1022,"sea_level":1022,"grnd_level":1021,"humidity":57,"temp_kf":0},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"clouds":{"all":100},"wind":{"speed":2.8,"deg":304,"gust":4.34},"visibility":10000,"pop":0.3,**"rain":{"3h":0.16}**,"sys":{"pod":"d"},"dt_txt":"2022-05-01 09:00:00"},{"dt":1651406400,"main":{"temp":11.46,"feels_like":10.25,"temp_min":11.46,"temp_max":11.46,"pressure":1021,"sea_level":1021,"grnd_level":1021,"humidity":61,"temp_kf":0},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],"clouds":{"all":100},"wind":{"speed":5.21,"deg":353,"gust":6.16},"visibility":10000,"pop":0,"sys":{"pod":"d"},"dt_txt":"2022-05-01 12:00:00"},{"dt":1651417200,"main":{"temp":9.5,"feels_like":6.65,"temp_min":9.5,"temp_max":9.5,"pressure":1021,"sea_level":1021,"grnd_level":1021,"humidity":72,"temp_kf":0},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],"clouds":{"all":100},"wind":{"speed":5.87,"deg":11,"gust":6.97},"visibility":10000,"pop":0,"sys":{"pod":"d"},"dt_txt":"2022-05-01 15:00:00"},{"dt":1651428000,"main":{"temp":7.06,"feels_like":4.35,"temp_min":7.06,"temp_max":7.06,"pressure":1022,"sea_level":1022,"grnd_level":1021,"humidity":84,"temp_kf":0},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],"clouds":{"all":100},"wind":{"speed":4.07,"deg":15,"gust":7.07},"visibility":10000,"pop":0,"sys":{"pod":"d"},"dt_txt":"2022-05-01 18:00:00"}],"city":{"id":3099434,"name":"GdaĹ
 sk","coord":{"lat":54.3521,"lon":18.6464},"country":"PL","population":0,"timezone":7200,"sunrise":1651288205,"sunset":1651342501}}]
2022-04-30 21:07:00.934 [INFO ] [org.openhab.core.model.script.FILE  ] - Linijka 424 rainvalues: [[Ljava.lang.String;@890875]
2022-04-30 21:07:00.939 [INFO ] [org.openhab.core.model.script.FILE  ] - Linijka 425 first array element  1: [0.15]
2022-04-30 21:07:00.943 [INFO ] [org.openhab.core.model.script.FILE  ] - Linijka 426 array lenght : [3]
2022-04-30 21:07:00.946 [INFO ] [org.openhab.core.model.script.FILE  ] - Linijka 432
2022-04-30 21:07:00.950 [INFO ] [org.openhab.core.model.script.FILE  ] - Linijka 436 petla
2022-04-30 21:07:00.955 [INFO ] [org.openhab.core.model.script.FILE  ] - Linijka 438 petla
2022-04-30 21:07:00.959 [INFO ] [org.openhab.core.model.script.FILE  ] - Linijka 436 petla
2022-04-30 21:07:00.963 [INFO ] [org.openhab.core.model.script.FILE  ] - Linijka 438 petla
2022-04-30 21:07:00.967 [INFO ] [org.openhab.core.model.script.FILE  ] - Linijka 436 petla
2022-04-30 21:07:00.971 [INFO ] [org.openhab.core.model.script.FILE  ] - Linijka 438 petla
2022-04-30 21:07:00.978 [INFO ] [org.openhab.core.model.script.FILE  ] - Watering_algorithm: rainfall forecast [0.41000000000000003] for next [8] periods, rainfall sum [0.41000000000000003]
2022-04-30 21:07:00.983 [INFO ] [org.openhab.core.model.script.FILE  ] -  ======= Wheather forecast =======
2022-04-30 21:07:00.988 [INFO ] [org.openhab.core.model.script.FILE  ] - Watering_algorithm: rainfallMm         [0.41000000000000003]
2022-04-30 21:07:00.995 [INFO ] [org.openhab.core.model.script.FILE  ] - Watering_algorithm: minimumReqRainfall [3.0]
2022-04-30 21:07:00.999 [INFO ] [org.openhab.core.model.script.FILE  ] - linia 478
2022-04-30 21:07:01.006 [INFO ] [org.openhab.core.model.script.FILE  ] - Watering_algorithm: lack of  [2.59] mm
2022-04-30 21:07:01.015 [INFO ] [org.openhab.core.model.script.FILE  ] -  ======= End of Watering algorithm  =======

probles was missing “{” after if condition :frowning: