Help on lambda rule for lighting scenes

Hello,

I have lighting rules that I would like to use lambda expressions for to save duplication of code. Will I have to pass in all the required items the rule will act on, or is there a way to avoid that since the item names all follow a set format? Would I be able to pass in “All_Lamps” or “G_Livingroom_Lamps” and have the code append _Whitemode, _Discomode and _Dimmer where appropriate to form the required item names?

I’ve pasted two of the four rules (as they are identical except for the items they act on).

Any guidance would be greatly appreciated.

rule "Light Scenes0"
when
Item Lights_scene0 received command
then
if (receivedCommand==0) {
    sendCommand(All_Lamps_Whitemode, ON)
    sendCommand(All_Lamps_Dimmer, 100)

}
if (receivedCommand==1){
        sendCommand(All_Lamps_Nightmode, ON)
}
if (receivedCommand==2){
     sendCommand(All_Lamps, ON)
      sendCommand(All_Lamps_Discomode, INCREASE)
}
if (receivedCommand==3){
        sendCommand(All_Lamps_Whitemode, OFF)
        sendCommand(All_Lamps_Dimmer, 25)
}
if (receivedCommand==4){
        sendCommand(All_Lamps,OFF)
}
end

rule "Light Scenes1"
when
Item Lights_scene1 received command
then
if (receivedCommand==0) {
        sendCommand(G_Livingroom_Lamps1_Whitemode, ON)
        sendCommand(G_Livingroom_Lamps1_Dimmer, 100)

}
if (receivedCommand==1){
        sendCommand(G_Livingroom_Lamps1_Nightmode, ON)
}
if (receivedCommand==2){
        sendCommand(G_Livingroom_Lamps1, ON)
        sendCommand(G_Livingroom_Lamps1_Discomode, INCREASE)
}
if (receivedCommand==3){
        sendCommand(G_Livingroom_Lamps1_Whitemode, OFF)
        sendCommand(G_Livingroom_Lamps1_Dimmer, 25)
}
if (receivedCommand==4){
        sendCommand(G_Livingroom_Lamps1, OFF)
}
end'

Items

Switch  All_Lamps               "All Lamps"     (Lighting)      {milight="bridge1;6"}
Switch  All_Lamps_Whitemode     "All Lamps - Whitemode" (Fancy_All, Lighting)   {milight="bridge1;6;whiteMode"}
Switch  All_Lamps_Nightmode     "All Lamps - Nightmode" (Fancy_All, Lighting)      {milight="bridge1;6;nightMode"}
Switch  All_Lamps_Discomode     "All Lamps - Discomode" (Fancy_All, Lighting)      {milight="bridge1;6;discoMode"}
Dimmer  All_Lamps_Dimmer        "All Lamps - Dimmer"    (Fancy_All, Lighting)   {milight="bridge1;6;brightness"}
Number Lights_scene0    "All"     <switch> (Lighting)

Switch  G_Livingroom_Lamps1     "Lamps 1"       (G_Livingroom, Lighting)       {milight="bridge1;7"}
Switch  G_Livingroom_Lamps1_Nightmode "Lamps 1 - Night" {milight="bridge1;7;nightMode"}
Switch  G_Livingroom_Lamps1_Whitemode   "Lamps 1 - White" {milight="bridge1;7;whiteMode"}
Switch  G_Livingroom_Lamps1_Discomode   "Lamps 1 - Disco" {milight="bridge1;7;discoMode"}
Dimmer  G_Livingroom_Lamps1_Dimmer      "Lamps 1 - Dimmer"      {milight="bridge1;7;brightness"}
Number Lights_scene1    "1"     <switch>        (Lighting)'

Site Map

Switch item=Lights_scene0       mappings=[0="On", 1="Night",2="Disco", 3="Dim", 4="Off"]
Switch item=Lights_scene1       mappings=[0="On", 1="Night",2="Disco", 3="Dim", 4="Off"]

There are two ways you can go about this.

To create the new name just use String concatenation:

val itemName = base + "_Dimmer"

Once you have the name you can send commands or updates using the actions (note use a String form for the command):

sendCommand(itemName, "ON")
postUpdate(itemName, "100")

You can also pull the Item out of a Group by name.

val itm = myGroup.filter[i | i.name == itemName].head
1 Like