String manipulation in rules

My instructions for the simulation was not detailed enough :

To perform the simulation, the switch must be triggered twice :

You have a Switch somewhere on your sitemap :

Switch item=Declenche_switch label="Rule test" 

First, you trigger the switch to ON : all the Contact and Rollershutter items get initialized.
Second, you trigger the switch to OFF : the rule "Rolladen..." is fired.
(In case Declenche_switch is already ON when you start, trigger it three times : -> OFF ( cause errors) -> ON -> OFF)

Sorry, maybe I was not that clear, when I create my last answer.

For sure, I pressed it twice.

Find here attached the full log:

11:11:06.258 [INFO ] [smarthome.event.ItemCommandEvent     ] - Item 'Declenche_switch' received command ON
11:11:06.395 [INFO ] [smarthome.event.ItemStateChangedEvent] - Declenche_switch changed from OFF to ON
11:11:09.537 [INFO ] [smarthome.event.ItemStateChangedEvent] - EG_wz_switch_3_long_test changed from OFF to ON
11:11:11.982 [INFO ] [smarthome.event.ItemStateChangedEvent] - network_device_192_168_2_1_time changed from 10.0 to 9.0
11:11:17.481 [INFO ] [smarthome.event.ItemCommandEvent     ] - Item 'Declenche_switch' received command OFF
11:11:17.535 [INFO ] [.eclipse.smarthome.model.script.DEBUG] - windowname = EG_wz_fk_tuer_test
11:11:17.637 [INFO ] [smarthome.event.ItemStateChangedEvent] - Declenche_switch changed from ON to OFF
11:11:17.796 [ERROR] [untime.internal.engine.RuleEngineImpl] - Rule 'Rolladen Wohnzimmer schliessen': cannot invoke method public abstract org.eclipse.smarthome.core.types.State org.eclipse.smarthome.core.items.Item.getState() on null
11:11:17.923 [INFO ] [smarthome.event.ItemStateChangedEvent] - EG_wz_switch_3_long_test changed from ON to OFF

Very odd !

First, to be sure (it does no harm even if it should not be necessary), try :

if (gH_Contact_global.members.filter [fk | fk.name == windowName].head.state as OpenClosedType != OPEN){

Then, last chance (sort of “Reinstall Windows” when you have no more ideas
), restart OpenHAB after emptying userdata/cache and userdata/tmp as usual. It already worked for me when I had weird issues after modifying rules files especially with lambdas inside.

Thanks Alain,

Rebooting and cleaning the userdata/cache, didn’t solve the issue. but removing the .state was the fix.

Test.rules

rule "Test_ON (prepares simulation)"
when 
	Item Declenche_switch changed to ON
then 
    EG_wz_switch_3_long_test.postUpdate(ON)
    EG_wz_fk_tuer_test.postUpdate(CLOSED)
    OG1_sz_fk_tuer_test.postUpdate(OPEN)
    OG1_az_fk_tuer_test.postUpdate(CLOSED)

    EG_wz_rs_tuer_test.postUpdate(UP)
    OG1_sz_rs_tuer_test.postUpdate(UP)
    OG1_az_rs_tuer_test.postUpdate(UP)
end

rule "Test_OFF"
when 
	Item Declenche_switch changed to OFF
then 
    EG_wz_switch_3_long_test.postUpdate(OFF)
end 

rule "Rolladen Wohnzimmer schliessen"
when 
    Item EG_wz_switch_3_long_test received update OFF
then
    gH_Rolladen_wz_switch_test.members.forEach[rs|
    var nameParts = rs.name.split("_")
    val windowName = nameParts.get(0) + "_" + nameParts.get(1) + "_fk_" + nameParts.get(3) +  "_" + nameParts.get(4)
    logInfo("DEBUG", "windowname = " + windowName)
    //logInfo ("Fensterkontakt der offen is:", windowName)
        if (gh_Contact_global_test.members.filter [fk | fk.name == windowName].head != OPEN){
           logInfo("DEBUG", "Fensterkontakt " + windowName + " is CLOSED")
           postUpdate(rs,DOWN)
           }
           else {
               logInfo("DEBUG", "Fensterkontakt " + windowName + " is OPEN")
           }]
end

Logging:

15:07:45.384 [INFO ] [smarthome.event.ItemCommandEvent     ] - Item 'Declenche_switch' received command ON
15:07:45.476 [INFO ] [smarthome.event.ItemStateChangedEvent] - Declenche_switch changed from NULL to ON
15:07:49.296 [INFO ] [smarthome.event.ItemStateChangedEvent] - EG_wz_switch_3_long_test changed from NULL to ON
15:07:49.400 [INFO ] [smarthome.event.ItemStateChangedEvent] - EG_wz_fk_tuer_test changed from NULL to CLOSED
15:07:49.470 [INFO ] [smarthome.event.ItemStateChangedEvent] - OG1_sz_fk_tuer_test changed from NULL to OPEN
15:07:49.545 [INFO ] [smarthome.event.ItemStateChangedEvent] - EG_wz_rs_tuer_test changed from NULL to 0
15:07:49.617 [INFO ] [smarthome.event.ItemStateChangedEvent] - OG1_az_fk_tuer_test changed from NULL to CLOSED
15:07:49.686 [INFO ] [smarthome.event.ItemStateChangedEvent] - OG1_az_rs_tuer_test changed from NULL to 0
15:07:49.757 [INFO ] [smarthome.event.ItemStateChangedEvent] - OG1_sz_rs_tuer_test changed from NULL to 0
15:08:02.249 [INFO ] [smarthome.event.ItemCommandEvent     ] - Item 'Declenche_switch' received command OFF
15:08:02.282 [INFO ] [.eclipse.smarthome.model.script.DEBUG] - Los geht's
15:08:02.331 [INFO ] [smarthome.event.ItemStateChangedEvent] - Declenche_switch changed from ON to OFF
15:08:02.439 [INFO ] [.eclipse.smarthome.model.script.DEBUG] - windowname = EG_wz_fk_tuer_test
15:08:02.663 [INFO ] [smarthome.event.ItemStateChangedEvent] - EG_wz_switch_3_long_test changed from ON to OFF
15:08:02.797 [INFO ] [.eclipse.smarthome.model.script.DEBUG] - Fensterkontakt EG_wz_fk_tuer_test is CLOSED
15:08:02.977 [INFO ] [smarthome.event.ItemStateChangedEvent] - EG_wz_rs_tuer_test changed from 0 to 100
15:08:03.001 [INFO ] [.eclipse.smarthome.model.script.DEBUG] - windowname = OG1_sz_fk_tuer_test
15:08:03.173 [INFO ] [.eclipse.smarthome.model.script.DEBUG] - Fensterkontakt OG1_sz_fk_tuer_test is CLOSED
15:08:03.303 [INFO ] [smarthome.event.ItemStateChangedEvent] - OG1_sz_rs_tuer_test changed from 0 to 100
15:08:03.319 [INFO ] [.eclipse.smarthome.model.script.DEBUG] - windowname = OG1_az_fk_tuer_test
15:08:03.496 [INFO ] [.eclipse.smarthome.model.script.DEBUG] - Fensterkontakt OG1_az_fk_tuer_test is CLOSED
15:08:03.632 [INFO ] [smarthome.event.ItemStateChangedEvent] - OG1_az_rs_tuer_test changed from 0 to 100

If I now adapt the rule to my live roller shutter, still the same error:

Group.items

Group:Rollershutter:OR(UP,DOWN) gH_Rolladen_switch "Alle Rolladen" <rollershutter>
Group:Rollershutter:OR(UP,DOWN) gH_Rolladen_sz_switch "Rolladen Schlafzimmer" <rollershutter>
Group:Rollershutter:OR(UP,DOWN) gH_Rolladen_wz_switch "Rolladen Wohnzimmer" <rollershutter>
Group gH_Contact_global
//Group:Contact:OR(OPEN,CLOSED) gH_Contact_global 'Fenster offen [(%d)]' <contact> -- disabled for testing
Group gH_Rolladen (gH_Rolladen_switch)
Group gEG_Rolladen (gH_Rolladen)
Group gEG_Rolladen_wz (gH_Rolladen,gH_Rolladen_wz_switch)
Group gOG1_Rolladen (gH_Rolladen)
Group gOG1_Rolladen_sz (gH_Rolladen,gH_Rolladen_sz_switch)
Group gOG2_Rolladen (gH_Rolladen)

Rolladen.items

Rollershutter EG_az_rs_fenster "Rollade Spielzimmer" <rollershutter> (gEG_az,gEG_Rolladen_wz) ["Lighting"] {channel="homematic:HG-HM-LC-Bl1PBU-FM::1#LEVEL"}

//--> Wohnzimmer
Rollershutter EG_wz_rs_seite "Rollade Wohnzimmer Seite" <rollershutter> (gEG_wz,gEG_Rolladen_wz) ["Lighting"] {channel="homematic:HG-HM-LC-Bl1PBU-FM:1#LEVEL"}
Rollershutter EG_wz_rs_tuer "Rollade Wohnzimer TĂŒr" <rollershutter> (gEG_wz,gEG_Rolladen_wz) ["Lighting"] {channel="homematic:HG-HM-LC-Bl1PBU-FM:1#LEVEL"}

Contact EG_az_fk_tuer "Fensterkontakt SpielzimmertĂŒr" <window> (gEG_az,gH_Contact_global) {channel="homematic:HG-HM-Sec-SC-2:1#STATE"}
Contact EG_wz_fk_tuer "Fensterkontakt WohnzimmertĂŒr" <window> (gEG_wz,gH_Contact_global) {channel="homematic:HG-HM-Sec-SC:1#STATE"}
and some other window contacts along different rooms with the same setup, belonging to gH_Contact as well. 

Rolladen.rules

var String windowName = ""
var String rsString = ""

rule "Rolladen Wohnzimmer schliessen"
when 
    Item EG_wz_switch_3_long received update OFF
then
    gH_Rolladen_wz_switch.members.forEach[rs|
    var nameParts = rs.name.split("_")
    val windowName = nameParts.get(0) + "_" + nameParts.get(1) + "_fk_" + nameParts.get(3)
    logInfo("DEBUG", "windowname = " + windowName)
    //logInfo ("Fensterkontakt der offen is:", windowName)
        if (gH_Contact_global.members.filter [fk | fk.name == windowName].head != OPEN){
           logInfo("DEBUG", "Fensterkontakt " + windowName + " is CLOSED")
           sendCommand(rs,DOWN)
           }
           else {
               logInfo("DEBUG", "Fensterkontakt " + windowName + " is OPEN")
           }]
end

Logging:

15:09:02.313 [INFO ] [smarthome.event.ChannelTriggeredEvent] - homematic:HG-HM-PB-6-WM55:3#PRESS triggered LONG_RELEASE
15:09:02.408 [INFO ] [smarthome.event.ChannelTriggeredEvent] - homematic:HG-HM-PB-6-WM55:3#PRESS triggered LONG
15:09:02.497 [INFO ] [smarthome.event.ItemStateChangedEvent] - homematic_HG_HM_PB_6_WM55__3_PRESS_LONG changed from OFF to ON
15:09:02.581 [INFO ] [smarthome.event.ItemStateChangedEvent] - EG_wz_switch_3_long changed from OFF to ON
15:09:02.656 [INFO ] [smarthome.event.ChannelTriggeredEvent] - homematic:HG-HM-PB-6-WM55::3#PRESS triggered LONG_RELEASE
15:09:04.338 [INFO ] [smarthome.event.ItemStateChangedEvent] - homematic_HG_HM_PB_6_WM55__3_PRESS_LONG changed from ON to OFF
15:09:04.426 [INFO ] [smarthome.event.ItemStateChangedEvent] - EG_wz_switch_3_long changed from ON to OFF
15:09:10.458 [ERROR] [untime.internal.engine.RuleEngineImpl] - Rule 'Rolladen Wohnzimmer schliessen': 3

Any ideas/suggestions left?
Help is really appreciated.

I am very sorry but the simulation did not work after you removed the .state !

Look at the output : the 3 contacts say that they are CLOSED but in the rule Test_ON, OG1_sz_fk_tuer_test is initialized with OPEN. You cannot compare xxxx.head which is an item with OPEN which is a state. They cannot be equal, so the comparison xxx.head != OPEN is always true and and you get always CLOSED in the logInfo.

But the good news is that I think I understand why it did not work when you made the first simulation : for some reason, in the _Rolladen_rule, you used the group gh_Contact_global_test, and I suppose you did not modify the items declaration which refer to (gH_Contact_global_test) and gh is not the same as gH.

I reproduced this typo and I got the same error message as you : cannot invoke method public abstract
, error due to the fact that the gh-group name is unknown.

So I suggest that you fix this typo and put back the .state and it should work.

Do the same in your rule and also, at the beginning, suppress the declaration var String windowName = “” which could be in conflict with val windowName.

Good luck !

Thanks,

Got your point re. the head.state -> corrected it.
I saw the typo, when I did the testings, but didn’t mentioned it, that I changed it.

Finally I got it - the group gH_Rolladen_wz_switch is a nested group, containing other groups, but no real members (items).
If I link the rule directly to a group with items (i.e. gH_Rolladen_wz) the rule flies.

Not that nice, as my understanding of groups and items from the Windows world is, that to link members/items to at least groups as possible. The rest should be done via groups - but I understand as well, that I have to reconsider my understanding for openHAB or have not the full understanding of groups.

OK, I think you complexified your hierarchy of Groups. In a rule “Rolladen Wohnzimmer” you should refer to a group containing only the rollershutters of the living room. And the same for the other rooms if necessary. Try to simplify.

Best regards

After some googling

If you change it from members.forEach[rs|
] to allMembers.forEach[rs|
] nested groups work as well.

Great - I am happy. :grinning: