[Solved] Gettting state of String item after using split

I’m using the SNMP binding to read String items (digital switches connected to a router):

Group gVGStatus
String vg_403_sw_1 "VG 403 [%s]" (gVGStatus) { snmp="<[192.164.3.1:public:1.3.6.1.4.1.30140.2.3.1.0:10000]" } 
String vg_403_sw_2 "VG 403 [%s]" (gVGStatus) { snmp="<[192.164.3.1:public:1.3.6.1.4.1.30140.2.3.2.0:10000]" } 
String vg_403_status "VG 403 Status [%s]"
String vg_404_sw_1 "VG 404 [%s]" (gVGStatus) { snmp="<[192.164.4.1:public:1.3.6.1.4.1.30140.2.3.1.0:10000]" } 
String vg_404_sw_2 "VG 404 [%s]" (gVGStatus) { snmp="<[192.164.4.1:public:1.3.6.1.4.1.30140.2.3.2.0:10000]" } 
String vg_404_status "VG 404 Status [%s]"
...

In a rule I need to evaluate both of these states (*_sw_1 and *_sw_2) to decide on the actual status of the device behind those two digital inputs. As I have to read data from a lot of routers I would like to have a generic rule, so decided to put them in groups and use the Member of trigger:

rule "VG Status"
when
    Member of gVGStatus changed
then
    val split = triggeringItem.name.split("_") // split the name of the item
    val vgstatusname0=split.get(0)
    val vgstatusname1=split.get(1)
    val vgstatusname2=split.get(2)
    val vgstatusname3=split.get(3)
    val newSw1=vgstatusname0.toString+"_"+vgstatusname1.toString+"_"+vgstatusname2.toString+"_1" //put the splitted parts together for first new item, for example vg_403_sw_1
    val newSw2=vgstatusname0.toString+"_"+vgstatusname1.toString+"_"+vgstatusname2.toString+"_2" //put the splitted parts together for second new item, for example vg_403_sw_2
    val newStatus=vgstatusname0.toString+"_"+vgstatusname1.toString+"_status"  //put the splitted parts together for a status item, for example vg_403_status
        if ((newSw1.state.toString=="0") && (newSw2.state.toString=="0")) {
           newStatus.postUpdate("status1")   
        }
        else if ((newSw1.state.toString=="1") && (newSw2.state.toString=="0")) {
            newStatus.postUpdate("status2")    
        }
        else if ((newSw1.state.toString=="0") && (newSw2.state.toString=="1")) {
            newStatus.postUpdate("status3")   
        }
        else if ((newSw1.state.toString=="1") && (newSw2.state.toString=="1")) {
            newStatus.postUpdate("status4")  
        }
end

When one of the items gets triggered (*_sw_X) I need to know the state of the corresponding second item (*_sw_Y).

Unfortunately as soon as I type the “state” to any of the newSwX.state.toString=="X" I get an error:

Rule ‘VG Status’: ‘state’ is not a member of ‘java.lang.String’; line 19, column 14, length 12

and in VSC:

The method or field state is undefined for the type String (org.eclipse.xtest.diagnostics.Diagnostic.linking [19,21]

I made sure all items are having a state. I’m on openHAB 2.5 Milestone1 on Win 10 for testing.
Thx for any hints.

You created a String and you are using that String as an Item, such is not possible ( a String has no State, that is a property ( correct terminology?) of an Item).
You could do something like:
postUpdate(newStatus,"status2")
Checking a String variable would work like this:
if ((newSw1=="1")...

I think this is what you want:

1 Like

Thanks a lot to both of you, in the link to that post I found the solution:

import org.eclipse.smarthome.model.script.ScriptServiceUtil

rule "VG Status"
when
    Member of gVGStatus changed
then
    val split = triggeringItem.name.split("_")
    val vgstatusname0=split.get(0)
    val vgstatusname1=split.get(1)
    val vgstatusname2=split.get(2)
    val newSw1=ScriptServiceUtil.getItemRegistry.getItem(vgstatusname0+"_"+vgstatusname1+"_"+vgstatusname2+"_1")
    val newSw2=ScriptServiceUtil.getItemRegistry.getItem(vgstatusname0+"_"+vgstatusname1+"_"+vgstatusname2+"_2")
	val newStatus=vgstatusname0+"_"+vgstatusname1+"_status" 
        if (newSw1.state=="0" && newSw2.state=="0") {
            newStatus.postUpdate("status1")   
        }
        else if ((newSw1.state=="1") && (newSw2.state=="0")) {
            newStatus.postUpdate("status2")    
        }
        else if ((newSw1.state=="0") && (newSw2.state=="1")) {
            newStatus.postUpdate("status3")   
        }
        else if ((newSw1.state=="1") && (newSw2.state=="1")) {
            newStatus.postUpdate("status4")  
        }
end

How do you use the import when using the rules in OH3, so by defining the When in the GUI and then creating a DSL rule for the Then.

Script execution of rule with UID 'checkLowBattery' failed:  ___ import  ___ org.openhab.core.model.script.ScriptServiceUtil
1. The method or field import is undefined; line 1, column 0, length 6
2. The method or field ScriptServiceUtil is undefined; line 12, column 383, length 17
3. Type mismatch: cannot convert from GenericItem to String; line 12, column 383, length 83
4. This expression is not allowed in this context, since it doesn't cause any side effects.; line 1, column 7, length 47

Things have changed in 2 years.

1 Like

I was not aware that it’s not possible to import a library in the gui. Everthing working fine now. Thx