Create reusable 'function' for roller-shutters

Hello everybody,

First let me explain te current situation; I have 12 roller shutters around my house which are all be controlled by openhab. Every Rollershutter has:

Switch RS_KTCHN_Sidewindow_ControlUp "Kitchen Sidewindow ControlUp[%s]" { channel="modbus:data:slaveMK:coils1:module1_output1:switch" } //Output 1.1 on Wago
Switch RS_KTCHN_Sidewindow_ControlDown "Kitchen Sidewindow ControlDown[%s]" { channel="modbus:data:slaveMK:coils1:module1_output2:switch" } //Output 1.2 on Wago
Rollershutter RS_KTCHN_Sidewindow_Control "Rolluik Zijkant[%s]"


var Timer LVTMRRS_KTCHN_Sidewindow = null
var Integer LVRS_KTCHN_Sidewindow_UpTime = 36
var Integer LVRS_KTCHN_Sidewindow_DownTime = 33

rule "RS_KTCHN_Sidewindow_Control"
	when Item RS_KTCHN_Sidewindow_Control received command
	if(receivedCommand == UP && RS_KTCHN_Sidewindow_ControlUp.state != ON && RS_KTCHN_Sidewindow_ControlDown.state != ON){
		sendCommand(RS_KTCHN_Sidewindow_ControlUp, ON)
		LVTMRRS_KTCHN_Sidewindow = createTimer(now.plusSeconds(LVRS_KTCHN_Sidewindow_UpTime)) [|
            sendCommand(RS_KTCHN_Sidewindow_ControlUp, OFF)
	else if(receivedCommand == DOWN && RS_KTCHN_Sidewindow_ControlUp.state != ON && RS_KTCHN_Sidewindow_ControlDown.state != ON){
        sendCommand(RS_KTCHN_Sidewindow_ControlDown, ON)
		LVTMRRS_KTCHN_Sidewindow = createTimer(now.plusSeconds(LVRS_KTCHN_Sidewindow_DownTime)) [|
            sendCommand(RS_KTCHN_Sidewindow_ControlDown, OFF)
	else if(receivedCommand == STOP){
        if (LVTMRRS_KTCHN_Sidewindow !== null) {
            LVTMRRS_KTCHN_Sidewindow = null
		sendCommand(RS_KTCHN_Sidewindow_ControlUp, OFF)
		sendCommand(RS_KTCHN_Sidewindow_ControlDown, OFF)

The code above is been created for every Rollershutter separately, which makes a very big rule.

I would like to create a ‘function’ which I can ‘open’ with a couple of arguments.
I’ve searched several hours, but it looks like some examples are ‘older’ and cannot be used in OH2, some other examples gives a lot of errors in my IDE and in the LOG-files. I cannot create anything which works.

Can somebody point me in the right direction so I can make a working ‘function’.

Regards Jaco

You might put some repeating code into a lambda, see e.g.:

I use this rule to only close a shutter if the door behind isn’t open.
Shutter and door contact item are given as arguments such as in check_close_shutter.apply(EG_Kueche_Rolladen_Tuer,EG_Kueche_Tuer_Kontakt,closure)

val Functions$Function3 <RollershutterItem,ContactItem,NumberItem,Boolean> check_close_shutter = [ shutter, door, percentage |
        if (door.state != OPEN) {
                logDebug("rules", shutter.label + "(" + + ") wird auf " + percentage + " gefahren, shutter = " + shutter)
                var Number p = percentage
        } else
                logDebug("rules", shutter.label + " wird nicht geschlossen, da " + door.label + " geöffnet ist.")

        logDebug("rules", shutter.label + "(" + + ") erledigt")

There is a complete alternative approach; make one rule triggered by events on a Group member, use related Item names (as you already do) so the rule can deduce which Items are associated with the “current” Group member.