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