Scripting Errors

  • Platform information:
    • Hardware:Pi 3
    • OS: Raspbian GNU/Linux 9 (stretch)
    • Java Runtime Environment: which java platform is used and what version
    • openHAB version: openHAB 2.3.0-1 (Release Build)
  • Issue of the topic:Error executing scripts in rules
  1. Unable to convert Number type to Integer. I understand the base type is Decimal, used the intValue() method but gives error as mentioned below. Current workaround is to use strVal = item.state.toString() and then convert to integer using Integer.valueOf(strVal)

  2. Error in sending command to an item using string (both literal or variable) i.e. sendCommand (“itemName” ON) or sendCommand( strvVariable, OFF), Error given in 2 below. Current Workaround is to findmembers in the group using members.findFirst and then use item.sendCommand(…)

  • If logs where generated please post these here using code fences:
    1 For intValue error
    Rule ‘UpdateDelayLightState’: ‘intValue’ is not a member of ‘org.eclipse.smarthome.core.library.types.DecimalType’; line 61, column 24, length 29
  1. For sendCommand log as follows :
    Rule ‘Remove Load When Offline’: An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.actions.BusEvent.sendCommand(java.lang.String,java.lang.String) on instance: null
val num = (YourStringItem.state as Number)
YourStringItem. ... ("ON")
  1. You should only very rarely need to convert a DecimalType/Number to an int and you should avoid doing so in all other circumstances. So as Harry illustrates, cast the state to Number and just use it.
    if(MyNumberItem.state as Number < 50)
    val result = MyNumberItem.state as Number + 25
    val result = MyNumberItem1.state as Number - MyNumberItem2.state as Number

You should only have to call .intValue when passing the state of a Number Item to a method that requires a primitive int. Most commonly that will be in calls to the methods or calls to Math::* methods.

  1. See this. If you know the name of the Item when you are writing the Rule, you should use the sendCommand method, not the sendCommand Action. Again, see Harry’s examples.

Hi Rich,
Thanks for the reply, got the first one, for the second one (sendCommand) I am writing a rule that handles when devices go offline and update the state to OFF, therefore the names while known are dependent on which item triggers the Group rule. I have a naming convention that allows me to derive the name of the item to send the command to based on the triggering item, since this is a string I wanted to avoid doing the group lookup as mentioned in your article Group based rules design pattern and call sendCommand( stringItemName, OFF) as the target items for which I want to change the state are not all in one group. I am still not sure why the sendCommand (String, state) does not work.

The action requires two Strings. Try “OFF” or calling toString on the only you pass to the action.

Also make sure that your item name is valid and exactly matches the items name.