QH3: rule to test array length as a result of split()

  • openHABian 3.3.4 on rPi4 with 4GB

I get an error on a rule where I am looking for an index that does not exist; e.g. blah.split("|").get(3)

In other languages I can say: uin8_t num_elements = blah.split("|") and num_elements contains the number of elements in the array.

So, this:

var split_result = Integer::parseInt(ws_Notification.state.toString.split("|"))

… does not work.

2023-05-01 00:13:24.690 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'weather-1' failed: Index 2 out of bounds for length 1 in weather

Is there a similar way in OH to achieve the desired outcome?

Why? Because I want to test if there are three elements before trying to assing it to a variable.

Thanks.

I figured it out:

rule "test split result"
    when
        //         s m   h D M DoW Y
        Time cron "1 0/1 * ? * *   *"
    then
        var string_to_split = "str1|str2|str3"
        var split_result = string_to_split.split("\\|")
        var num_arr_elements = split_result.length()

        logInfo(LOG_PREFIX + "00.01", "split_result............: {}", split_result)
        logInfo(LOG_PREFIX + "00.01", "num_arr_elements........: {}", num_arr_elements)

        if (num_arr_elements < 3) {}
end