One Rule - Multiple rooms switched each by motion sensors

Hi all,
in the last months I was searching a rule for switching lights in several rooms each by motion sensor - switching the light as well by the phase of the day. I did not use the design patter TimeOfDay, because the day phase should be triggered as well by the light intensity (Hue Motion Sensors are in use)
Did not find any, so developed one. At first it is on a test base.
No Binding in use - just virtual switches. Next step is to make it work with my items.

If you would like, please leave a comment or any suggestions for improvement.

Items

//Groups for filtering
Group:Switch:OR(ON,OFF) glightonoff "Gruppe Licht an/aus [%s]" <switch>
Group:Dimmer:AVG glightdimmer "Gruppendimmer [%s]" <light>
Group:Switch:OR(ON,OFF) gDaytime "Gruppenschalter Tag [%s]" <switch>
Group:Switch:OR(ON,OFF) gPresenceMS "Gruppenschalter Bewegungsmelder [%s]" <switch>
Group:Switch:OR(ON,OFF) gTimer "Gruppe Timer" <clock>

//Light switches
Switch lightonoff1_Room1 "Licht an/aus 1 [%s]" <switch> (glightonoff)
Switch lightonoff2_Room2 "Licht an/aus 2 [%s]" <switch> (glightonoff)
Switch lightonoff3_Room3 "Licht an/aus 3 [%s]" <switch> (glightonoff)

//Light Dimmer
Dimmer lightbright_Room1 "Helligkeit 1 [%d]"  <light> (glightdimmer)
Dimmer lightbright_Room2 "Helligkeit 2 [%d]"  <light> (glightdimmer)
Dimmer lightbright_Room3 "Helligkeit 3 [%d]"  <light> (glightdimmer)
Switch lightswitch "Testschalter [%s]"  <switch> (glightonoff)

//Day Night Switches
Switch DaytimeSwitch_Day " Tageszeit Schalter Tag [%s]" <switch> (gDaytime)
Switch DaytimeSwitch_Night " Tageszeit Schalter Nacht [%s]" <switch> (gDaytime)

//Daytime output by rule
String Daytime "Die Tageszeit ist [%s]" (gDaytime)

//Switches simulating Motionsensors
Switch Test_Room1_PresenceSwitch "Bewegungsmelder 1 [%s]" <switch> (gPresenceMS)
Switch Test_Room2_PresenceSwitch "Bewegungsmelder 2 [%s]" <switch> (gPresenceMS)
Switch Test_Room3_PresenceSwitch "Bewegungsmelder 3 [%s]" <switch> (gPresenceMS)

//Expire Switches
Switch Timer_Room1_Switch "Timer Expire Room1 [%s]" <clock> (gTimer) { expire="5s,command=OFF" }
Switch Timer_Room2_Switch "Timer Expire Room2 [%s]" <clock> (gTimer) { expire="5s,command=OFF" }
Switch Timer_Room3_Switch "Timer Expire Room3 [%s]" <clock> (gTimer) { expire="5s,command=OFF" }

Rules:

//Rule for setting Daytime

rule "DayTime Switch"
when

    Member of gDaytime changed

then

if(DaytimeSwitch_Day.state == OFF && DaytimeSwitch_Night.state == OFF){
    Daytime.sendCommand("Bett")
}

if(DaytimeSwitch_Day.state == ON && DaytimeSwitch_Night.state == OFF){
    Daytime.sendCommand("Morgen")
}

if(DaytimeSwitch_Day.state == ON && DaytimeSwitch_Night.state == ON){
    Daytime.sendCommand("Tag")
}

if(DaytimeSwitch_Day.state == OFF && DaytimeSwitch_Night.state == ON){
    Daytime.sendCommand("Nacht")
}
end

//Rule triggering light depending of triggered motion sensor

rule "one timer rule for all"
when

    Member of gPresenceMS changed

then

val valRoomNumberTrigger = (triggeringItem.name.split("_").get(1)).toString
val valRoomLightTrigger = glightonoff.members.filter[ i | i.name.contains(valRoomNumberTrigger)]
val valRoomLightDimmer = glightdimmer.members.filter[ i | i.name.contains(valRoomNumberTrigger)]
val StringBuilder valLogPresence = new StringBuilder
val valRoomTimer = gTimer.members.filter[ i | i.name.contains(valRoomNumberTrigger)]

if(triggeringItem.state == ON){
        switch (Daytime.state) {
            case "Morgen":{
                if(valRoomNumberTrigger == "Room1"){
                    valRoomTimer.forEach[ i | i.postUpdate(OFF)]
                    valRoomLightTrigger.forEach[ i | i.sendCommand(ON)]
                    valRoomLightDimmer.forEach[ i | i.sendCommand(80)]
                }

                if(valRoomNumberTrigger == "Room2"){
                    valRoomTimer.forEach[ i | i.postUpdate(OFF)]
                    valRoomLightTrigger.forEach[ i | i.sendCommand(ON)]
                    valRoomLightDimmer.forEach[ i | i.sendCommand(30)]
                }

                if(valRoomNumberTrigger == "Room3"){
                    valRoomTimer.forEach[ i | i.postUpdate(OFF)]
                    valRoomLightTrigger.forEach[ i | i.sendCommand(ON)]
                    valRoomLightDimmer.forEach[ i | i.sendCommand(100)]
                }
            }

            case "Bett":{
                if(valRoomNumberTrigger == "Room1"){
                    valRoomTimer.forEach[ i | i.postUpdate(OFF)]
                    valRoomLightTrigger.forEach[ i | i.sendCommand(OFF)]
                    valRoomLightDimmer.forEach[ i | i.sendCommand(20)]
                }

                if(valRoomNumberTrigger == "Room2"){
                    valRoomTimer.forEach[ i | i.postUpdate(OFF)]
                    valRoomLightTrigger.forEach[ i | i.sendCommand(ON)]
                    valRoomLightDimmer.forEach[ i | i.sendCommand(OFF)]
                }

                if(valRoomNumberTrigger == "Room3"){
                    valRoomTimer.forEach[ i | i.postUpdate(OFF)]
                    valRoomLightTrigger.forEach[ i | i.sendCommand(OFF)]
                    valRoomLightDimmer.forEach[ i | i.sendCommand(10)]
                }
            }

            case "Nacht":{
                if(valRoomNumberTrigger == "Room1"){
                    valRoomTimer.forEach[ i | i.postUpdate(OFF)]
                    valRoomLightTrigger.forEach[ i | i.sendCommand(ON)]
                    valRoomLightDimmer.forEach[ i | i.sendCommand(80)]
                }

                if(valRoomNumberTrigger == "Room2"){
                    valRoomTimer.forEach[ i | i.postUpdate(OFF)]
                    valRoomLightTrigger.forEach[ i | i.sendCommand(ON)]
                    valRoomLightDimmer.forEach[ i | i.sendCommand(80)]
                }

                if(valRoomNumberTrigger == "Room3"){
                    valRoomTimer.forEach[ i | i.postUpdate(OFF)]
                    valRoomLightTrigger.forEach[ i | i.sendCommand(ON)]
                    valRoomLightDimmer.forEach[ i | i.sendCommand(80)]
                }
            }

            case "Tag":{
                if(valRoomNumberTrigger == "Room1"){
                    valRoomTimer.forEach[ i | i.postUpdate(OFF)]
                    valRoomLightTrigger.forEach[ i | i.sendCommand(OFF)]
                    valRoomLightDimmer.forEach[ i | i.sendCommand(40)]
                }

                if(valRoomNumberTrigger == "Room2"){
                    valRoomTimer.forEach[ i | i.postUpdate(OFF)]
                    valRoomLightTrigger.forEach[ i | i.sendCommand(OFF)]
                    valRoomLightDimmer.forEach[ i | i.sendCommand(40)]
                }

                if(valRoomNumberTrigger == "Room3"){
                    valRoomTimer.forEach[ i | i.postUpdate(OFF)]
                    valRoomLightTrigger.forEach[ i | i.sendCommand(OFF)]
                    valRoomLightDimmer.forEach[ i | i.sendCommand(40)]
                }
            }
        }
}

if(triggeringItem.state == OFF){
    valRoomTimer.forEach[ i | i.sendCommand(ON)]
}   

logInfo("TestTimerRule", "The item " + triggeringItem.name + " received command " + triggeringItem.state)
logInfo("RoomNumber","triggerd room: " + valRoomNumberTrigger)
logInfo("LightNumber", "triggered light: " + valRoomLightTrigger)
logInfo("DimmerNumber" , "Dimmed light: " + valRoomLightDimmer)
logInfo("RoomTimer", "Timer Room " + valRoomTimer + " started")

   valLogPresence.append("\r LightSwitch No. :\n\r")
                valRoomLightTrigger.forEach[ i | 
                valLogPresence.append(i.name + " changed to: " + i.state +"\n") ]
                valRoomLightDimmer.forEach[ i | 
                valLogPresence.append(i.name + " changed to: " + i.state +"\n") ]

    logInfo("Log_PresenceTimer", valLogPresence.toString)

end

//Switching off lights depending of expiring timer switch

rule "global rule timer end"

when

    Member of gTimer received command OFF

then

val valRoomNumberTrigger = (triggeringItem.name.split("_").get(1)).toString
val valRoomLightTrigger = glightonoff.members.filter[ i | i.name.contains(valRoomNumberTrigger)]
val valRoomLightDimmer = glightdimmer.members.filter[ i | i.name.contains(valRoomNumberTrigger)]
val StringBuilder valLogPresence = new StringBuilder
val valRoomTimer = gTimer.members.filter[ i | i.name.contains(valRoomNumberTrigger)]

       valRoomLightTrigger.forEach[ i | i.sendCommand(OFF)]
       valRoomLightDimmer.forEach[ i | i.sendCommand(OFF)]
          valLogPresence.append("\r LightSwitch No. :\n\r")

                valRoomLightTrigger.forEach[ i | 
                valLogPresence.append(i.name + " changed to: " + i.state +"\n") ]
                valRoomLightDimmer.forEach[ i | 
                valLogPresence.append(i.name + " changed to: " + i.state +"\n") ]

    logInfo("Log_PresenceTimer", valLogPresence.toString)

end

Sitemap:

sitemap test label="Test" {

    Frame label="Test Moving Sensors" {
        Group item=gPresenceMS
        Switch item=Test_Room1_PresenceSwitch
        Switch item=Test_Room2_PresenceSwitch
        Switch item=Test_Room3_PresenceSwitch
    }
    
    Frame label="Test Daytime Switches" {
        Switch item=DaytimeSwitch_Day
        Switch item=DaytimeSwitch_Night
        Text item=Daytime
    }

    Frame label="Test Light Switches" icon="switch" {
        Switch item=lightonoff1_Room1
        Switch item=lightonoff2_Room2
        Switch item=lightonoff3_Room3
        Switch item=lightswitch
    }

    Frame label="Test Light Dimmer" icon="light" {
        Slider item=lightbright_Room1
        Slider item=lightbright_Room2
        Slider item=lightbright_Room3
    }

      Frame label="Test Timer" {
        Group item=gTimer
        Switch item=Timer_Room1_Switch
        Switch item=Timer_Room2_Switch
        Switch item=Timer_Room3_Switch
    }

    Frame label="Test Group Switches" {
        Switch item=glightonoff
        Slider item=glightdimmer
    }
}