[SOLVED] Error during splitting a string in a rule

Hello all,
I have an easy problem for you all…

My string looks like

180.0

I am trying to split the string at the “.” but there is a error display which I don’t understand

string_weewx_windDir.postUpdate(transform("JS", "compassrose.js", MQTT_number_weewx_windDir.state.toString) + "(" +MQTT_number_weewx_windDir.state.toString.split('.').get(0) + "°)")

error:

Error during the execution of rule '5min': 0

I would split the rule into some lines and insert a loginfo after every line, so you can see, which thing causes the error.

That´s the way i look for errors in complex rules.

it’s only this line in the rule…and it is exactly at the split position if i rmove the split command it works

rule "5min"
    when
        Time cron "0 0/2 * * * ?"
    then
      string_weewx_windDir.postUpdate(transform("JS", "compassrose.js", MQTT_number_weewx_windDir.state.toString) + "(" +MQTT_number_weewx_windDir.state.toString.split('.').get(0) + "°)")
end

Maybe you can try the following:

make a val of this command and only insert the val into the rule.


then
      val example = MQTT_number_weewx_windDir.state.toString.split('.').get(0)
      string_weewx_windDir.postUpdate(transform("JS", "compassrose.js", MQTT_number_weewx_windDir.state.toString) + "(" + example + "°)")

Also insert a loginfo after the val-declaration, then you can see what you really get.

1 Like

What do you mean with

Also insert a loginfo after the val-declaration, then you can see what you really get.

val example = MQTT_number_weewx_windDir.state.toString.split('.').get(0)
        logInfo("5min  is", example)
        string_weewx_windDir.postUpdate(transform("JS", "compassrose.js", MQTT_number_weewx_windDir.state.toString) + "(" + example + "°)")

error:

2019-05-02 08:22:00.036 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule '5min': 0

Insert another loginfo before the val-declaration with:

loginfo("test", MQTT_number_weewx_windDir)

So you will get not only the state of the item. You will get a long information about the whole item.


I use this code below to get the parts of the phone-numbers from my fritzbox and it works, it´s the same syntax like in your code.

				val tel00 = Call_Incoming_No.state.toString.split(',').get(0)
				val tel01 = Call_Incoming_No.state.toString.split(',').get(1)

your suggestion leads to

2019-05-02 11:22:00.025 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule '5min': An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.actions.LogAction.logInfo(java.lang.String,java.lang.String,java.lang.Object[]) on instance: null

But

logInfo("test", MQTT_number_weewx_windDir.toString())

2019-05-02 11:23:00.275 [INFO ] [.eclipse.smarthome.model.script.test] - MQTT_number_weewx_windDir (Type=NumberItem, State=45.0, Label=Windrichtung, Category=null)

The correct syntax for gettiung the state would be.

logInfo("test", MQTT_number_weewx_windDir.state.toString())

However, are you sure that the error is coming from that part? What’s about the “transform…”?

the transform is working…if iI put this

string_weewx_windDir.postUpdate(transform("JS", "compassrose.js", MQTT_number_weewx_windDir.state.toString) + "(" +MQTT_number_weewx_windDir.state.toString.substring(0,3) + "°)")

this is correctwith no error

However if the state is above 99 the substring would need to be .substring(0,4).

That’s why I tried to use split then it should be always fine

I found this

https://community.openhab.org/t/solved-split-a-string-into-array-list/50935/6

Nur im the Log IT says

Invalid escape sequence

Solution:

You need to escape the dot if you want to split on a literal dot:

val example = MQTT_number_weewx_windDir.state.toString.split('\\.').get(0)

Otherwise you are splitting on the regex . , which means “any character”.
Note the double backslash needed to create a single backslash in the regex.

1 Like