Rule OK in 2.5, error in 3.1

Hi folks,

I’ve just migrated to 3.1, and a rule that was working before:

// dates from Weatherbit

rule "Dates from Weatherbit"
when
    Item HTTP_Weatherbit_Fore24H_Date received update or
    System started
then
    // state.toString
    val Weatherbit_24H_Date = HTTP_Weatherbit_Fore24H_Date.state.toString
    val Weatherbit_48H_Date = HTTP_Weatherbit_Fore48H_Date.state.toString
    val Weatherbit_72H_Date = HTTP_Weatherbit_Fore72H_Date.state.toString
    val Weatherbit_96H_Date = HTTP_Weatherbit_Fore96H_Date.state.toString
    val Weatherbit_120H_Date = HTTP_Weatherbit_Fore120H_Date.state.toString
    // from String to DateTime
    val DateTimeType Weatherbit_24_DateTime = DateTimeType.valueOf(Weatherbit_24H_Date)
    val DateTimeType Weatherbit_48_DateTime = DateTimeType.valueOf(Weatherbit_48H_Date)
    val DateTimeType Weatherbit_72_DateTime = DateTimeType.valueOf(Weatherbit_72H_Date)
    val DateTimeType Weatherbit_96_DateTime = DateTimeType.valueOf(Weatherbit_96H_Date)
    val DateTimeType Weatherbit_120_DateTime = DateTimeType.valueOf(Weatherbit_120H_Date)
    // Update proxy items
    Converted_Weatherbit_Fore24H_Date.postUpdate(Weatherbit_24_DateTime)
    Converted_Weatherbit_Fore48H_Date.postUpdate(Weatherbit_48_DateTime)
    Converted_Weatherbit_Fore72H_Date.postUpdate(Weatherbit_72_DateTime)
    Converted_Weatherbit_Fore96H_Date.postUpdate(Weatherbit_96_DateTime)
    Converted_Weatherbit_Fore120H_Date.postUpdate(Weatherbit_120_DateTime)
end

Now I’m receiving an ERROR message:

2021-03-15 15:16:30.245 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'date-4' failed: begin 0, end 10, length 4 in date

Any clue? :frowning:
Andrea

Are you sure this is the rule that is failing? It’s complaining about a rule with UID “date-4” and this rule is named “Dates from Weatherbit”. Furthermore the error looks like there is a call to substring to extract characters 0-10 but the string itself is only four characters long (NULL or UNDEF) I bet.

Make sure to check for NULL and UNDEF states before just blindly using an Item’s state in your Rules and you will probably find your problem. I’m going to bet one or more of your HTTP Items is NULL.

It’s amazing what you can learn from logging (hint hint).

In order for the call to valueOf to work it needs to be in ISO8601 format. You might be able to go straight to a DateTime Item from the binding itself and eliminate this Rule and the proxy Items. Instead of using a String Channel, use a DateTime Channel and just assign the extracted values to the Converted Items and be done.

As for the rule, there is a lot of repeated code here. If you put all your HTTP_Weatherbit Items into a Group you could reduce the rule to

when
    Member of HTTP_Weatherbit_Dates received update or
    System started
then
    val str = triggeringItem.state.toString
    val hour = triggeringItem.name.split("_").get(2)
    postUpdate("Converted_Weatherbit_"+hour+"_Date", str)
end

You don’t need to convert the String to a DateTimeType before passing it as an update to the DateTime Item if it’s already parsable.

1 Like

That’s a cobble up around using xxx.rules files in OH3. The rules from date.rules get labelled date-1, date-2, etc., ignoring the given “name”.

I was supposing the fourth rule inside date.rules

1 Like

Good to know. I’ve not used .rules files in OH 3 and back when I used Jython in OH 2 the UID would be randomly generated. I assumed that the stuff after rule would make up the UID now. Looks like that wasn’t correct.

@ariela, note that the reduced lines of code version of the code above doesn’t check for NULL/UNDEF either. But the nice thing is that now you can very easily add such a check as one or two lines of code instead of needing to write a line to check each of the Items.

Though I still suggest:

  • add logging so you know for sure which rule, which line, and the states of the Items where the failure occurs
  • move to a DateTime Channel and assign the value to a DateTime Item in the first place, eliminating the need for the rule and the proxy Items in the first place
1 Like