(SOLVED) How can I detect an Item type(Switch or RollerShutter) in a rule?

runtimeInfo:
  version: 3.1.0
  buildString: Release Build
locale: tr-TR
systemInfo:
  configFolder: /etc/openhab
  userdataFolder: /var/lib/openhab
  logFolder: /var/log/openhab
  javaVersion: 11.0.12
  javaVendor: Azul Systems, Inc.
  javaVendorVersion: Zulu11.50+19-CA
  osName: Linux
  osVersion: 5.10.60-sunxi
  osArchitecture: arm
  availableProcessors: 4
  freeMemory: 91725960
  totalMemory: 194772992
bindings:
  - astro
  - icalendar
  - icloud
  - kodi
  - remoteopenhab
  - serial
  - systeminfo

Hello All,
In the rule below, I want to match the items defined in openhab with the identifiers I obtained from the string and affect the relevant item.
if the defined item is a switch(OnOffType), it’s fine, I’m sending “ON” or “OFF”, but I don’t know if this item is “OnOffType” or “UpDownType”.
If I can learn the item type in the rule, I want to send commands according to the item type.

//import org.eclipse.smarthome.model.script.ScriptServiceUtil
import org.openhab.core.model.script.ScriptServiceUtil
rule "React on String Data (CANBus) change/update"
when
    Item CANBus received update   // your condition here
then
    var String CANBusData = CANBus.state.toString    
    val parts = CANBusData.split(";")
    var dTYPE = Integer::parseInt(parts.get(0).split("=").get(1)) as Number
    var dID   = Integer::parseInt(parts.get(1).split("=").get(1)) as Number
    var pID   = Integer::parseInt(parts.get(2).split("=").get(1)) as Number
    var String cID = parts.get(3).split("=").get(1) 
    val name  = "In"+ dID + "_P" + pID

    logInfo("CANBus", "(Parts                     : '{}'", parts)
    logInfo("CANBus", "(CANBUS) Input.Device.Type : '{}'", dTYPE)
	logInfo("CANBus", "(CANBUS) Input.Device.ID   : '{}'", dID)
    logInfo("CANBus", "(CANBUS) Input.Port.ID     : '{}'", pID)
	logInfo("CANBus", "(CANBUS) Command.ID        : '{}'", cID)
    logInfo("CANBus", "(CANBUS) Item.Name         : '{}'", name)
   
    if (name.Type instanceof OnOffType  ) {
        logInfo("name type is Switch")
        if (cID == "01" ) 
            name.sendCommand("ON")  
	    else  
            name.sendCommand("OFF")
        }
	else if(name.Type instanceof UpDownType ) {
           logInfo("name type is RollerShutter")
            if (cID == "01" ) 
                name.sendCommand("UP")  
	        else  
                name.sendCommand("DOWN")
           }
end
==> /var/log/openhab/events.log <==
2021-09-18 01:57:24.754 [INFO ] [openhab.event.ChannelTriggeredEvent ] - serial:serialBridge:SB1:data triggered PRESSED
2021-09-18 01:57:24.764 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'CANBus' changed from DTYPE=11;DID=01;PID=001;CID=00;
 to DTYPE=11;DID=01;PID=001;CID=01;
==> /var/log/openhab/openhab.log <==
2021-09-18 01:57:24.791 [INFO ] [org.openhab.core.model.script.CANBus] - (Parts                     : '[DTYPE=11, DID=01, PID=001, CID=01, 
]'
2021-09-18 01:57:24.797 [INFO ] [org.openhab.core.model.script.CANBus] - (CANBUS) Input.Device.Type : '11'
2021-09-18 01:57:24.803 [INFO ] [org.openhab.core.model.script.CANBus] - (CANBUS) Input.Device.ID   : '1'
2021-09-18 01:57:24.809 [INFO ] [org.openhab.core.model.script.CANBus] - (CANBUS) Input.Port.ID     : '1'
2021-09-18 01:57:24.815 [INFO ] [org.openhab.core.model.script.CANBus] - (CANBUS) Command.ID        : '01'
2021-09-18 01:57:24.821 [INFO ] [org.openhab.core.model.script.CANBus] - (CANBUS) Item.Name         : 'In1_P1'
2021-09-18 01:57:24.827 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'Input-1' failed: 'Type' is not a member of 'java.lang.String'; line 23, column 9, length 9 in Input
==> /var/log/openhab/events.log <==
2021-09-18 01:57:26.530 [INFO ] [openhab.event.ChannelTriggeredEvent ] - serial:serialBridge:SB1:data triggered PRESSED
2021-09-18 01:57:26.535 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'CANBus' changed from DTYPE=11;DID=01;PID=001;CID=01;
 to DTYPE=11;DID=01;PID=001;CID=00;
==> /var/log/openhab/openhab.log <==
2021-09-18 01:57:26.548 [INFO ] [org.openhab.core.model.script.CANBus] - (Parts                     : '[DTYPE=11, DID=01, PID=001, CID=00, 
]'
2021-09-18 01:57:26.551 [INFO ] [org.openhab.core.model.script.CANBus] - (CANBUS) Input.Device.Type : '11'
2021-09-18 01:57:26.555 [INFO ] [org.openhab.core.model.script.CANBus] - (CANBUS) Input.Device.ID   : '1'
2021-09-18 01:57:26.558 [INFO ] [org.openhab.core.model.script.CANBus] - (CANBUS) Input.Port.ID     : '1'
2021-09-18 01:57:26.561 [INFO ] [org.openhab.core.model.script.CANBus] - (CANBUS) Command.ID        : '00'
2021-09-18 01:57:26.564 [INFO ] [org.openhab.core.model.script.CANBus] - (CANBUS) Item.Name         : 'In1_P1'
2021-09-18 01:57:26.567 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'Input-1' failed: 'Type' is not a member of 'java.lang.String'; line 23, column 9, length 9 in Input

I found 2 methods, but I could not manage to implement them.

getType()

getAcceptedDataTypes()

How can I do that?

Thank you for your time and assists…

Veli

Those are not Item types. An Item type would be a Switch, for example.

if (someItem instanceof SwitchItem)

You may send a Switch an OnOffType command

Rossko57,

It was very helpful, thank you.

i found these documents and I am working on those. İ didn’t understand clearly all of them but I will.

GenericItem

Now I can query the types of Items and the commands they can accept.


//import org.eclipse.smarthome.model.script.ScriptServiceUtil
import org.openhab.core.model.script.ScriptServiceUtil
rule "React on String Data (CANBus) change/update"
when
    Item CANBus received update   // your condition here
then
    var String CANBusData = CANBus.state.toString    
    val parts = CANBusData.split(";")
    var dTYPE = Integer::parseInt(parts.get(0).split("=").get(1)) as Number
    var dID   = Integer::parseInt(parts.get(1).split("=").get(1)) as Number
    var pID   = Integer::parseInt(parts.get(2).split("=").get(1)) as Number
    var String cID = parts.get(3).split("=").get(1) 
    var name  = ScriptServiceUtil.getItemRegistry.getItem("In"+ dID + "_P" + pID)

    logInfo("CANBus", "(Parts                     : '{}'", parts)
    logInfo("CANBus", "(CANBUS) Input.Device.Type : '{}'", dTYPE)
	logInfo("CANBus", "(CANBUS) Input.Device.ID   : '{}'", dID)
    logInfo("CANBus", "(CANBUS) Input.Port.ID     : '{}'", pID)
	logInfo("CANBus", "(CANBUS) Command.ID        : '{}'", cID)
    logInfo("CANBus", "(CANBUS) Item.Name         : '{}'", name)
    logInfo("CANBus", "(CANBUS) Item.getType      : '{}'", name.getType)
    logInfo("CANBus", "(CANBUS) Item.getAcceptedCommandTypes: '{}'", name.getAcceptedCommandTypes)
    logInfo("CANBus", "(CANBUS) Item.getAcceptedDataTypes: '{}'", name.getAcceptedDataTypes)
    logInfo("CANBus", "(CANBUS) Item.getState: '{}'", name.getState)
    logInfo("CANBus", "(CANBUS) Item.getStateAs: '{}'", name.getStateAs)
 

     if (name instanceof SwitchItem ) {
        logInfo("name type is Switch")
        if (name.getState == "ON" ) 
            name.sendCommand("ON")  
	    else   
            name.sendCommand("OFF")
        }
	else if(name.getType == "100" || name.getType == "0") {
           logInfo("name type is RollerShutter")
            if (name.getStateAs == "UP" ) 
                name.sendCommand("DOWN")  
	        else  
                name.sendCommand("UP")
           }
    else {logInfo("Couldn't compare")}

    /*
    if (name.getType =="ON" || name.getType =="OFF"  ) {
        logInfo("name type is Switch")
        if (name.getState == "ON" ) 
            name.sendCommand("ON")  
	    else   
            name.sendCommand("OFF")
        }
	else if(name.getType == "100" || name.getType == "0") {
           logInfo("name type is RollerShutter")
            if (name.getStateAs == "UP" ) 
                name.sendCommand("DOWN")  
	        else  
                name.sendCommand("UP")
           }
    else {logInfo("Couldn't compare")}
    */
end

I’m having trouble comparing this query result within the If blog and couldn’t find where to look to find out. I Tried some alternatives but the result is the same, failed…

if (name.getType instanceof SwitchType ) {

   }
2021-09-18 11:15:17.737 [INFO ] [openhab.event.ChannelTriggeredEvent ] - serial:serialBridge:SB1:data triggered PRESSED
2021-09-18 11:15:17.748 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'CANBus' changed from DTYPE=11;DID=01;PID=001;CID=00;
 to DTYPE=11;DID=01;PID=001;CID=01;
==> /var/log/openhab/openhab.log <==
2021-09-18 11:15:18.169 [INFO ] [org.openhab.core.model.script.CANBus] - (Parts                     : '[DTYPE=11, DID=01, PID=001, CID=01, 
]'
2021-09-18 11:15:18.172 [INFO ] [org.openhab.core.model.script.CANBus] - (CANBUS) Input.Device.Type : '11'
2021-09-18 11:15:18.176 [INFO ] [org.openhab.core.model.script.CANBus] - (CANBUS) Input.Device.ID   : '1'
2021-09-18 11:15:18.179 [INFO ] [org.openhab.core.model.script.CANBus] - (CANBUS) Input.Port.ID     : '1'
2021-09-18 11:15:18.182 [INFO ] [org.openhab.core.model.script.CANBus] - (CANBUS) Command.ID        : '01'
2021-09-18 11:15:18.185 [INFO ] [org.openhab.core.model.script.CANBus] - (CANBUS) Item.Name         : 'In1_P1 (Type=SwitchItem, State=ON, Label=P1, Category=, Tags=[Point])'
2021-09-18 11:15:18.188 [INFO ] [org.openhab.core.model.script.CANBus] - (CANBUS) Item.getType      : 'Switch'
2021-09-18 11:15:18.192 [INFO ] [org.openhab.core.model.script.CANBus] - (CANBUS) Item.getAcceptedCommandTypes: '[class org.openhab.core.library.types.OnOffType, class org.openhab.core.types.RefreshType]'
2021-09-18 11:15:18.195 [INFO ] [org.openhab.core.model.script.CANBus] - (CANBUS) Item.getAcceptedDataTypes: '[class org.openhab.core.library.types.OnOffType, class org.openhab.core.types.UnDefType]'
2021-09-18 11:15:18.198 [INFO ] [org.openhab.core.model.script.CANBus] - (CANBUS) Item.getState: 'ON'
2021-09-18 11:15:18.201 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'Input-1' failed: An error occurred during the script execution: index=0, size=0 in Input
    if (name.getType instanceof SwitchItem ) {
        
        }		
==> /var/log/openhab/events.log <==
2021-09-18 11:17:52.349 [INFO ] [openhab.event.ChannelTriggeredEvent ] - serial:serialBridge:SB1:data triggered PRESSED
2021-09-18 11:17:52.359 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'CANBus' changed from DTYPE=11;DID=01;PID=001;CID=01;
 to DTYPE=11;DID=01;PID=001;CID=00;
==> /var/log/openhab/openhab.log <==
2021-09-18 11:17:52.384 [INFO ] [org.openhab.core.model.script.CANBus] - (Parts                     : '[DTYPE=11, DID=01, PID=001, CID=00, 
]'
2021-09-18 11:17:52.390 [INFO ] [org.openhab.core.model.script.CANBus] - (CANBUS) Input.Device.Type : '11'
2021-09-18 11:17:52.396 [INFO ] [org.openhab.core.model.script.CANBus] - (CANBUS) Input.Device.ID   : '1'
2021-09-18 11:17:52.402 [INFO ] [org.openhab.core.model.script.CANBus] - (CANBUS) Input.Port.ID     : '1'
2021-09-18 11:17:52.407 [INFO ] [org.openhab.core.model.script.CANBus] - (CANBUS) Command.ID        : '00'
2021-09-18 11:17:52.413 [INFO ] [org.openhab.core.model.script.CANBus] - (CANBUS) Item.Name         : 'In1_P1 (Type=SwitchItem, State=ON, Label=P1, Category=, Tags=[Point])'
2021-09-18 11:17:52.419 [INFO ] [org.openhab.core.model.script.CANBus] - (CANBUS) Item.getType      : 'Switch'
2021-09-18 11:17:52.425 [INFO ] [org.openhab.core.model.script.CANBus] - (CANBUS) Item.getAcceptedCommandTypes: '[class org.openhab.core.library.types.OnOffType, class org.openhab.core.types.RefreshType]'
2021-09-18 11:17:52.432 [INFO ] [org.openhab.core.model.script.CANBus] - (CANBUS) Item.getAcceptedDataTypes: '[class org.openhab.core.library.types.OnOffType, class org.openhab.core.types.UnDefType]'
2021-09-18 11:17:52.438 [INFO ] [org.openhab.core.model.script.CANBus] - (CANBUS) Item.getState: 'ON'
2021-09-18 11:17:52.444 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'Input-1' failed: An error occurred during the script execution: index=0, size=0 in Input
    if (name instanceof SwitchItem ) {

        }
==> /var/log/openhab/events.log <==
2021-09-18 11:18:24.725 [INFO ] [openhab.event.ChannelTriggeredEvent ] - serial:serialBridge:SB1:data triggered PRESSED
2021-09-18 11:18:24.734 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'CANBus' changed from DTYPE=11;DID=01;PID=001;CID=00;
 to DTYPE=11;DID=01;PID=001;CID=01;
==> /var/log/openhab/openhab.log <==
2021-09-18 11:18:24.759 [INFO ] [org.openhab.core.model.script.CANBus] - (Parts                     : '[DTYPE=11, DID=01, PID=001, CID=01, 
]'
2021-09-18 11:18:24.766 [INFO ] [org.openhab.core.model.script.CANBus] - (CANBUS) Input.Device.Type : '11'
2021-09-18 11:18:24.771 [INFO ] [org.openhab.core.model.script.CANBus] - (CANBUS) Input.Device.ID   : '1'
2021-09-18 11:18:24.777 [INFO ] [org.openhab.core.model.script.CANBus] - (CANBUS) Input.Port.ID     : '1'
2021-09-18 11:18:24.783 [INFO ] [org.openhab.core.model.script.CANBus] - (CANBUS) Command.ID        : '01'
2021-09-18 11:18:24.789 [INFO ] [org.openhab.core.model.script.CANBus] - (CANBUS) Item.Name         : 'In1_P1 (Type=SwitchItem, State=ON, Label=P1, Category=, Tags=[Point])'
2021-09-18 11:18:24.795 [INFO ] [org.openhab.core.model.script.CANBus] - (CANBUS) Item.getType      : 'Switch'
2021-09-18 11:18:24.801 [INFO ] [org.openhab.core.model.script.CANBus] - (CANBUS) Item.getAcceptedCommandTypes: '[class org.openhab.core.library.types.OnOffType, class org.openhab.core.types.RefreshType]'
2021-09-18 11:18:24.807 [INFO ] [org.openhab.core.model.script.CANBus] - (CANBUS) Item.getAcceptedDataTypes: '[class org.openhab.core.library.types.OnOffType, class org.openhab.core.types.UnDefType]'
2021-09-18 11:18:24.813 [INFO ] [org.openhab.core.model.script.CANBus] - (CANBUS) Item.getState: 'ON'
2021-09-18 11:18:24.819 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'Input-1' failed: An error occurred during the script execution: index=0, size=0 in Input

name.??? I don’t fully understand the subject.
The name.getType method returns a string and I couldn’t figure out how to compare this returned information.

By the way,

we can learn the item.state by

item.getState

and/or

item.getStateAs

2021-09-18 11:17:52.419 [INFO ] [org.openhab.core.model.script.CANBus] - (CANBUS) Item.getType      : 'Switch'
2021-09-18 11:17:52.425 [INFO ] [org.openhab.core.model.script.CANBus] - (CANBUS) Item.getAcceptedCommandTypes: '[class org.openhab.core.library.types.OnOffType, class org.openhab.core.types.RefreshType]'
2021-09-18 11:17:52.432 [INFO ] [org.openhab.core.model.script.CANBus] - (CANBUS) Item.getAcceptedDataTypes: '[class org.openhab.core.library.types.OnOffType, class org.openhab.core.types.UnDefType]'
2021-09-18 11:17:52.438 [INFO ] [org.openhab.core.model.script.CANBus] - (CANBUS) Item.getState: 'ON'

A Switch would usually have a state of ON, which is an OnOffType, Not “ON” which is a string.

People mostly use the short form here
if (name.state == ON )

You can command with strings if you like, the command parser will try to parse it into something it wants, like ON.

I’m not sure what this is intended to do. I wouldn’t expect any Item to return type “100”.

Get Item state as … what? .getStateAs() would like a parameter, please.

someDimmer.getStateAs(OnOffType)

Because your rule blows up on that error, it never gets to the if() stuff. Errors stop execution.

Rossko57, Your help was very instructive and useful. Now I can do what I want on the input side.
Thank you.

My last code is below;

import org.openhab.core.model.script.ScriptServiceUtil
var Number Log_Flag = 0
rule "React on String Data (CANBus) change/update"
when
    Item CANBus received update   // your condition here
then
    var String CANBusData = CANBus.state.toString    
    val parts       = CANBusData.split(";")
    var dTYPE       = Integer::parseInt(parts.get(0).split("=").get(1)) as Number
    var dID         = Integer::parseInt(parts.get(1).split("=").get(1)) as Number
    var pID         = Integer::parseInt(parts.get(2).split("=").get(1)) as Number
    var String cID  = parts.get(3).split("=").get(1) 
    var name        = ScriptServiceUtil.getItemRegistry.getItem("In"+ dID + "_P" + pID)
    if (Log_Flag == 1) {
        logInfo("CANBus", "(Parts                     : '{}'", parts)
        logInfo("CANBus", "(CANBUS) Input.Device.Type : '{}'", dTYPE)
	    logInfo("CANBus", "(CANBUS) Input.Device.ID   : '{}'", dID)
        logInfo("CANBus", "(CANBUS) Input.Port.ID     : '{}'", pID)
	    logInfo("CANBus", "(CANBUS) Command.ID        : '{}'", cID)
        logInfo("CANBus", "(CANBUS) Item.Name         : '{}'", name)
        logInfo("CANBus", "(CANBUS) Item.getType      : '{}'", name.getType)
        logInfo("CANBus", "(CANBUS) Item.getAcceptedCommandTypes: '{}'", name.getAcceptedCommandTypes)
        logInfo("CANBus", "(CANBUS) Item.getAcceptedDataTypes: '{}'", name.getAcceptedDataTypes)
        logInfo("CANBus", "(CANBUS) Item.getState: '{}'", name.getState)
    }
       
    if (name.getType == "Switch" ){
        if (name.state == OFF ) 
            name.sendCommand(ON)  
	    else   
            name.sendCommand(OFF)
    } 
    else if (name.getType == "Rollershutter" ){
            if(RS_Direction.state==ON ){ // short STOP, Long UP/DOWN
                if (pID > 40){
                    if (name.getState == 100 || name.getState == DOWN ) 
                        name.sendCommand(UP) 
	                else 
                        name.sendCommand(DOWN)
                }            
                else {
                    name.sendCommand(STOP)
                }
            }
            if(RS_Direction.state==OFF ){ // short UP/DOWN, Long STOP
                if (pID < 40){
                    if (name.getState == 100 || name.getState == DOWN ) 
                        name.sendCommand(UP) 
	                else 
                        name.sendCommand(DOWN)
                }            
                else {
                    name.sendCommand(STOP)
                }
            }
    }

end
2 Likes