[SOLVED] Rule problem with groups

Hello again,

I have the following problem with this rule:

rule "Betriebsart Heizung 2"
when
System started or
//Member of gHeizBetriebsart or
Item Heizung_KanalA_Betriebsart changed or
Item Heizung_KanalB_Betriebsart changed or
Item Heizung_KanalC_Betriebsart changed or
Item Heizung_KanalD_Betriebsart changed or
Item Heizung_KanalE_Betriebsart changed or
Item Heizung_KanalF_Betriebsart changed or
Item Heizung_KanalG_Betriebsart changed or
Item Heizung_KanalH_Betriebsart changed
then

val betriebsart = triggeringItem
val switchkomfort   = triggeringItem.name.substring(2) + "_Komfort"
val switchnacht     = triggeringItem.name.substring(2) + "_Nacht"
val switchfrost     = triggeringItem.name.substring(2) + "_Frost"


if (betriebsart.state.toString.contains("Frost")) {
    logInfo("Heizung","Betriebsart Heizung ist in Frostschutz")
    postUpdate(switchfrost, ON)
}

else{
    if(betriebsart.state==1) {
        switchkomfort.sendCommand(ON)
        switchnacht.sendCommand(OFF)
        switchfrost.sendCommand(OFF)}
    else if(betriebsart.state==2) {
        switchkomfort.sendCommand(OFF)
        switchnacht.sendCommand(OFF)
        switchfrost.sendCommand(OFF)}
    else if(betriebsart.state==3) {
        switchkomfort.sendCommand(OFF)
        switchnacht.sendCommand(ON)
        switchfrost.sendCommand(OFF)}
    else if(betriebsart.state==4) {
        switchkomfort.sendCommand(OFF)
        switchnacht.sendCommand(OFF)
        switchfrost.sendCommand(ON)}
}
end

I get this error on startup of the rule an when an item is triggered:

2018-11-15 22:58:43.609 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Betriebsart Heizung 2': 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

I have no idea what the Problem is. I have tried to design this rule with different solutions from this forum. Additionally the rule is not triggered with the “Member of trigger”.

I am very thankfull for any help.

Nice evening!

The value switchkomfort is of type String, and there is no sendCommand method on the String class.

You need to either get the item object, or use the sendCommand action, such as:

sendCommand(switchkomfort, ON)

As an aside, what are you trying to accomplish with this statement?

val switchkomfort   = triggeringItem.name.substring(2) + "_Komfort"

Using one of your items, Heizung_KanalA_Betriebsart, as an example, the above statement will produce this string:

izung_KanalA_Betriebsart_Komfort

Perhaps you are rather intending to extract the KanalA from the item name? In that case, you would use the split method.

Off the top of my head, I think it would be something like this.

    val String[] stringArray = triggeringItem.name.split('_')
    val switchkomfort = stringArray.get(1) + "_Komfort"

Thank you! You are right, I want to extract the Kanal statement.

I need for example:

Heizung_KanalA_Komfort for one of the switches.

See above. I added that to the end of my post. I think that’s the right syntax, but I haven’t actually tested it.

Thank you, tomorrow I will give it a try.

Ah, then my example won’t work. My example will give you KanalA_Komfort

You would want this instead.

val String[] stringArray = triggeringItem.name.split('_')
val switchkomfort = stringArray.get(0) + "_" + stringArray.get(1) + "_Komfort"```

But there’s probably a better way to do it using substring and indexOf.

This will do what you want. It will convert Heizung_KanalA_Betriebsart into Heizung_KanalA_Komfort

switchkomfort = triggeringItem.name.substring(0, s.lastIndexOf("_")) + "_Komfort"
1 Like

Hello Mark,

thanks for your support, but unfortunately the rule does not run correct. Still the error:

Error during the execution of startup rule 'Betriebsart Heizung 2': cannot invoke method public abstract java.lang.String org.eclipse.smarthome.core.items.Item.getName() on null

after loading the rule and when firering:

2018-11-16 10:19:40.774 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Betriebsart Heizung 2': The name 's' cannot be resolved to an item or type; line 38, column 54, length 1
2018-11-16 10:19:42.671 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Error during the execution of startup rule 'Betriebsart Heizung 2': cannot invoke method public abstract java.lang.String org.eclipse.smarthome.core.items.Item.getName() on null

I have no idea…. seems like there is someting with the s.lastIndexOf…… because VSCode highlighted the “s.” as undifined. what does the “s.” is for?

Actual rule design:

ule "Betriebsart Heizung 2"
when
System started or
//Member of gHeizBetriebsart or
Item Heizung_KanalA_Betriebsart changed or
Item Heizung_KanalB_Betriebsart changed or
Item Heizung_KanalC_Betriebsart changed or
Item Heizung_KanalD_Betriebsart changed or
Item Heizung_KanalE_Betriebsart changed or
Item Heizung_KanalF_Betriebsart changed or
Item Heizung_KanalG_Betriebsart changed or
Item Heizung_KanalH_Betriebsart changed
then

val betriebsart = triggeringItem
val switchkomfort = triggeringItem.name.substring(0, s.lastIndexOf("_")) + "_Komfort"
val switchnacht     = triggeringItem.name.substring(0, s.lastIndexOf("_")) + "_Nacht"
val switchfrost     = triggeringItem.name.substring(0, s.lastIndexOf("_")) + "_Frost"

logInfo("Heizung", "" +switchkomfort+ "")


if (betriebsart.state.toString.contains("Frost")) {
    logInfo("Heizung","Betriebsart Heizung ist in Frostschutz")
    postUpdate(switchfrost, "ON")
}

else{
    if(betriebsart.state==1) {
        sendCommand(switchkomfort, "ON") 
        sendCommand(switchnacht,"OFF")
        sendCommand(switchfrost,"OFF")}
    else if(betriebsart.state==2) {
        sendCommand(switchkomfort, "OFF") 
        sendCommand(switchnacht,"OFF")
        sendCommand(switchfrost,"OFF")}
    else if(betriebsart.state==3) {
        sendCommand(switchkomfort,"OFF") 
        sendCommand(switchnacht,"OFF")}
    else if(betriebsart.state==4) {
        sendCommand(switchkomfort, "OFF") 
        sendCommand(switchnacht,"OFF")
        sendCommand(switchfrost,"ON")}
}
end

Try that.
I removed the betriebsart val. Not needed just use triggeringItem
I added the s variable definition
When you have cascading if like you have, it is clearer to use a switch case statement

rule "Betriebsart Heizung 2"
when
    System started or
    //Member of gHeizBetriebsart or
    Item Heizung_KanalA_Betriebsart changed or
    Item Heizung_KanalB_Betriebsart changed or
    Item Heizung_KanalC_Betriebsart changed or
    Item Heizung_KanalD_Betriebsart changed or
    Item Heizung_KanalE_Betriebsart changed or
    Item Heizung_KanalF_Betriebsart changed or
    Item Heizung_KanalG_Betriebsart changed or
    Item Heizung_KanalH_Betriebsart changed
then
    val s = triggeringItem.name.toString
    val switchkomfort = triggeringItem.name.substring(0, s.lastIndexOf("_")) + "_Komfort"
    val switchnacht     = triggeringItem.name.substring(0, s.lastIndexOf("_")) + "_Nacht"
    val switchfrost     = triggeringItem.name.substring(0, s.lastIndexOf("_")) + "_Frost"

    logInfo("Heizung", "" + switchkomfort + "")

    if (triggeringItem.state.toString.contains("Frost")) {
        logInfo("Heizung","Betriebsart Heizung ist in Frostschutz")
        postUpdate(switchfrost, "ON")
    } else {
	switch triggeringItem.state {
		case 1 : {
            sendCommand(switchkomfort, "ON") 
            sendCommand(switchnacht,"OFF")
            sendCommand(switchfrost,"OFF") }
		case 2 : {
            sendCommand(switchkomfort, "OFF") 
            sendCommand(switchnacht,"OFF")
            sendCommand(switchfrost,"OFF") }
		case 3 : {
            sendCommand(switchkomfort,"OFF") 
            sendCommand(switchnacht,"OFF") }
		case 4 : {
            sendCommand(switchkomfort, "OFF") 
            sendCommand(switchnacht,"OFF")
            sendCommand(switchfrost,"ON") }
	}
}
end

Thank you Vincent, the main part is running. But I have one more problem:
actual rule:

rule "Betriebsart Heizung 2"
when
System started or
Member of gHeizBetriebsart changed
then

val s = triggeringItem.name.toString
val info2 = triggeringItem.name.substring(0, s.lastIndexOf("_"))
val switchkomfort = triggeringItem.name.substring(0, s.lastIndexOf("_")) + "_Komfort"
val switchnacht     = triggeringItem.name.substring(0, s.lastIndexOf("_")) + "_Nacht"
val switchfrost     = triggeringItem.name.substring(0, s.lastIndexOf("_")) + "_Frost"
val diagnose  = triggeringItem.name.substring(0, s.lastIndexOf("_")) + "_Diagnosetext"

//val x = triggeringItem.state

logInfo("Heizung", "" +info2+ " wird eingestellt!")
//logInfo("Heizung", "Wert:" +diagnose+ "")
//Thread::sleep(1000)

if (diagnose.state.toString.contains("Frost")) 
{
    logInfo("Heizung","Betriebsart Heizung ist in Frostschutz")
    postUpdate(switchfrost, "ON")
}

else{
    if(triggeringItem.state==1) {
        sendCommand(switchkomfort, "ON") 
        sendCommand(switchnacht,"OFF")
        sendCommand(switchfrost,"OFF")}
    else if(triggeringItem.state==2) {
        sendCommand(switchkomfort, "OFF") 
        sendCommand(switchnacht,"OFF")
        sendCommand(switchfrost,"OFF")}
    else if(triggeringItem.state==3) {
        sendCommand(switchkomfort,"OFF") 
        sendCommand(switchnacht,"OFF")}
    else if(triggeringItem.state==4) {
        sendCommand(switchkomfort, "OFF") 
        sendCommand(switchnacht,"OFF")
        sendCommand(switchfrost,"ON")}
}
end

My Problem is this part:

if (diagnose.state.toString.contains("Frost")) 
{
    logInfo("Heizung","Betriebsart Heizung ist in Frostschutz")
    postUpdate(switchfrost, "ON")
}

I get the following error:

Rule 'Betriebsart Heizung 2': 'state' is not a member of 'java.lang.String'; line 47, column 29, length 14```

Can you help my with this part?

You can do some self-help here. Yo’ve identified the problem area, the log tells you it is not happy about ‘state’.
So we can see it’s something about diagnose
Let’s look back, what kind of thing is it? We defined it earlier on as a string.
Items have .state methods, but strings do not. So there’s the problem.

So what can we do? Well, if diagnose is already a string, we can take away the parts trying to make it into a string
if (diagnose.contains(“Frost”))

1 Like

Thanks for catching that. I missed it when converting the code from my test rule. :clap:

Now these could be simplified to this.

    val switchkomfort = s.substring(0, s.lastIndexOf("_")) + "_Komfort"
    val switchnacht = s.substring(0, s.lastIndexOf("_")) + "_Nacht"
    val switchfrost = s.substring(0, s.lastIndexOf("_")) + "_Frost"

Put this at the TOP of the rules file:

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

Then replace your line with:

val testItem = ScriptServiceUtil.getItemRegistry.getItem(triggeringItem.name.substring(0, s.lastIndexOf("_")) + "_Diagnosetext")

Reference:

Thank you! That was the point!

Please mark the thread as solved, thanks