Master Light Switch Rule, Color Temperature by Sun elevation, Press-and-hold dimming

Anyone willingt o help me with this one?

Goal

  • To create a master rule file with lambdas to reduce code repetition and make changes to functionality on a global level.
  • Re-write Color Temperature by Sun elevation in Rules DSL, and add smoothing to the offset created by coarse cloudiness data.
  • Re-write dimming function to be a “press and hold” type experience.

What I’ve got so far:
a rule file with about 800 lines of code that control the functionality of multiple dimmers and switches of various brands (Hue, Aqora etc) that control various types of lights (CCT, On/Off etc).

Lambdas
I have used lambdas for the dimming and on/off functions, but I would like to use a few variables at the start of each rule to autofill a lot of the code required to trigger the lambas in the first place. I think this alone could reduce the length of code by a few hundred lines of code.
But when I try to create a variable, the lambda does not accept the STRING as an GenericItemType. Can anyone explain how to do this?

Is it poissible to run a lambda within a lambda? And if so, can it pass through the items it took in? It’s not working as I’d expect.

Color Temperature by Sun elevation
The reason I am using such a convoluted ON command in genericCctLight_On, is because I have another rule I run that sets a global color temperature based on Sun elevation to a Calculated_ColorTemperature item, and then send this value to any light that is ON. I love this rule, but I’m sure it could be prettier than this, and rewritten in DSL.

You might also notice that I’ve not included the Cloud_Factor in my final output as I was experiencing massive steps because the local weather data was very coarse data. I want to make it only take a maximum step of about 100K each time the rule is run.
Maybe someone can help with this?

**javascript

var SunElevation = itemRegistry.getItem("AstroSunData_Elevation").getState();
var Clouds = itemRegistry
  .getItem("Localweatherandforecast_Cloudiness")
  .getState();
var outputstart = 2200; //min color temp
var outputend = 5600; //max color temp
var inputstart = 0; //min sun elevation
var inputend = 40; //max sun elevation
var output =
  outputstart +
  ((outputend - outputstart) / (inputend - inputstart)) *
    (SunElevation - inputstart);
var CloudFactor = output * Clouds * 0.003;

var newColourValue = Math.min(Math.max(output, 2200), 5600)

events.sendCommand('Calculated_ColorTemperature', newColourValue);

var lightLevelGroup = itemRegistry.getItem("gAll_Lights_Brightness");

lightLevelGroup.members.forEach(function (item) {
  if (item.getState() > 0) {
    var thingName = item.getName().match(/^(.*)_.*/)[1];
    events.sendCommand(
      thingName + "_ColorTemperature",
      newColourValue
    );
  }
});

Dimming
I am currently using a predefined series of steps between brightness values for Dim Up and Dim Down. I would like to replace this with a press and hold function. I’m sure this can be done with loops and timers etc, but I can’t figure it out.

Current code
Anyway, without further ado, here is my code in full. I still have another 3 or 4 switches to add to this, so you can see the need for streamlining…

//Lambdas for reusable light functions.

val switch_On = [
    GenericItem brightness
    |
    if (brightness.state != ON) {brightness.sendCommand(ON)}
    logInfo("Lights", getLocation(brightness) + " " + getEquipment(brightness) + " turned ON")
]

val cct_On = [
    GenericItem brightness,
    GenericItem scene_brightness,
    GenericItem color_temp
    |
    if (brightness.state == 0) {
        color_temp.sendCommand(Calculated_ColorTemperature.state.toString)
        if (brightness.state != scene_brightness.state) {
            brightness.sendCommand(scene_brightness.state.toString)
        }
        logInfo("Lights", getLocation(brightness).label + " " + getEquipment(brightness).label + " set to " + scene_brightness.state)
    } else if (brightness.state != 100) {
        brightness.sendCommand(100)
        logInfo("Lights", getLocation(brightness).label + " " + getEquipment(brightness).label + " set to 100")

    }
]

val light_Off = [
    GenericItem brightness
    |
    if (brightness.state != OFF) { brightness.sendCommand(OFF) }
    logInfo("Lights", getLocation(brightness).label + " " + getEquipment(brightness).label + " turned OFF")
]

val light_dimUp = [
    GenericItem brightness
    |
    if (brightness.state >= 75 && brightness.state < 100) {brightness.sendCommand(100)}
    else if (brightness.state >= 50 && brightness.state < 75) {brightness.sendCommand(75)}
    else if (brightness.state >= 25 && brightness.state < 50) {brightness.sendCommand(50)}
    else if (brightness.state >= 10 && brightness.state < 25) {brightness.sendCommand(25)}
    else if (brightness.state >= 5 && brightness.state < 10) {brightness.sendCommand(10)}
    else if (brightness.state >= 0 && brightness.state < 5) {brightness.sendCommand(5)}
    logInfo("Lights", getLocation(brightness).label + " " + getEquipment(brightness).label + " dimmed UP")
]

val light_dimDown = [
    GenericItem brightness
    |
    if (brightness.state > 75 && brightness.state <= 100) {brightness.sendCommand(75)}
    else if (brightness.state > 50 && brightness.state <= 75) {brightness.sendCommand(50)}
    else if (brightness.state > 25 && brightness.state <= 50) {brightness.sendCommand(25)}
    else if (brightness.state > 10 && brightness.state <= 25) {brightness.sendCommand(10)}
    else if (brightness.state > 5 && brightness.state <= 10) {brightness.sendCommand(5)}
    else if (brightness.state > 1 && brightness.state <= 5) {brightness.sendCommand(1)}
    else if (brightness.state > 0 && brightness.state <= 1) {brightness.sendCommand(0)}
    logInfo("Lights", getLocation(brightness).label + " " + getEquipment(brightness).label + " dimmed DOWN")
]

val light_min = [
    GenericItem brightness 
    |
    brightness.sendCommand(1)
    logInfo("Lights", getLocation(brightness).label + " " + getEquipment(brightness).label + " set to Minimum")
]

rule "Wall Switch - Back Room, Hue Dimmer 1"
when
    Item BackRoom_WallSwitch_Button changed    
then
    if (newState == 1000) {         // 1000=On INITIAL PRESS
        
    } else if (newState == 1001) {  // 1001=On LONG PRESS

    } else if (newState == 1002) {  // 1002=On
        cct_On.apply(
                BackRoom_Ceiling_Light_Brightness,
                BackRoom_Ceiling_Light_SceneBrightness,
                BackRoom_Ceiling_Light_ColorTemperature
        )
    } else if (newState == 1003) {  // 1003=On LONG RELEASE

    } else if (newState == 2000) {  // 2000=Dim Up INITIAL PRESS

    } else if (newState == 2001) {  // 2001=Dim Up LONG PRESS

    } else if (newState == 2002) {  // 2002=Dim Up
        light_dimUp.apply(BackRoom_Ceiling_Light_Brightness)

    } else if (newState == 2003) {  // 2003=Dim Up LONG RELEASE

    } else if (newState == 3000) {  // 3000=Dim Down INITIAL PRESS

    } else if (newState == 3001) {  // 3001=Dim Down LONG PRESS
        light_min.apply(BackRoom_Ceiling_Light_Brightness)

    } else if (newState == 3002) {  // 3002=Dim Down
        light_dimDown.apply(BackRoom_Ceiling_Light_Brightness)

    } else if (newState == 3003) {  // 3003=Dim Down LONG RELEASE

    } else if (newState == 4000) {  // 4000=Off INITIAL PRESS

    } else if (newState == 4001) {  // 4001=Off LONG PRESS

    } else if (newState == 4002) {  // 4002=Off
        light_Off.apply(BackRoom_Ceiling_Light_Brightness)

    } else if (newState == 4003) {  // 4003=Off LONG RELEASE

    }
    
end

rule "Wall Switch - Kitchen Sink (Hue Dimmer 2)"
when
    Item Kitchen_Door_Left_WallSwitch_Button changed   
then
    if (newState == 1000) {         // 1000=On INITIAL PRESS
        
    } else if (newState == 1001) {  // 1001=On LONG PRESS

    } else if (newState == 1002) {  // 1002=On
        cct_On.apply(
            Kitchen_Sink_Ceiling_Light_Brightness,
            Kitchen_Sink_Ceiling_Light_SceneBrightness,
            Kitchen_Sink_Ceiling_Light_ColorTemperature
        )

    } else if (newState == 1003) {  // 1003=On LONG RELEASE

    } else if (newState == 2000) {  // 2000=Dim Up INITIAL PRESS

    } else if (newState == 2001) {  // 2001=Dim Up LONG PRESS

    } else if (newState == 2002) {  // 2002=Dim Up
        light_dimUp.apply(Kitchen_Sink_Ceiling_Light_Brightness)

    } else if (newState == 2003) {  // 2003=Dim Up LONG RELEASE

    } else if (newState == 3000) {  // 3000=Dim Down INITIAL PRESS

    } else if (newState == 3001) {  // 3001=Dim Down LONG PRESS
        light_min.apply(Kitchen_Sink_Ceiling_Light_Brightness)

    } else if (newState == 3002) {  // 3002=Dim Down
        light_dimDown.apply(Kitchen_Sink_Ceiling_Light_Brightness)

    } else if (newState == 3003) {  // 3003=Dim Down LONG RELEASE

    } else if (newState == 4000) {  // 4000=Off INITIAL PRESS

    } else if (newState == 4001) {  // 4001=Off LONG PRESS

    } else if (newState == 4002) {  // 4002=Off
        light_Off.apply(Kitchen_Sink_Ceiling_Light_Brightness)

    } else if (newState == 4003) {  // 4003=Off LONG RELEASE

    }
    
end

rule "Wall Switch - Kitchen Endboard (Hue Dimmer 1)"
when
    Item Kitchen_Door_Right_WallSwitch_Button changed   
then
    if (newState == 1000) {         // 1000=On INITIAL PRESS
        
    } else if (newState == 1001) {  // 1001=On LONG PRESS

    } else if (newState == 1002) {  // 1002=On
        cct_On.apply(
                Kitchen_Endboard_Ceiling_Light_Brightness,
                Kitchen_Endboard_Ceiling_Light_SceneBrightness,
                Kitchen_Endboard_Ceiling_Light_ColorTemperature
                )
    } else if (newState == 1003) {  // 1003=On LONG RELEASE

    } else if (newState == 2000) {  // 2000=Dim Up INITIAL PRESS

    } else if (newState == 2001) {  // 2001=Dim Up LONG PRESS

    } else if (newState == 2002) {  // 2002=Dim Up
        light_dimUp.apply(Kitchen_Endboard_Ceiling_Light_Brightness)

    } else if (newState == 2003) {  // 2003=Dim Up LONG RELEASE

    } else if (newState == 3000) {  // 3000=Dim Down INITIAL PRESS

    } else if (newState == 3001) {  // 3001=Dim Down LONG PRESS
        light_min.apply(Kitchen_Endboard_Ceiling_Light_Brightness)

    } else if (newState == 3002) {  // 3002=Dim Down
        light_dimDown.apply(Kitchen_Endboard_Ceiling_Light_Brightness)

    } else if (newState == 3003) {  // 3003=Dim Down LONG RELEASE
        
    } else if (newState == 4000) {  // 4000=Off INITIAL PRESS

    } else if (newState == 4001) {  // 4001=Off LONG PRESS

    } else if (newState == 4002) {  // 4002=Off
        light_Off.apply(Kitchen_Endboard_Ceiling_Light_Brightness)

    } else if (newState == 4003) {  // 4003=Off LONG RELEASE

    }
    
end

rule "Wall Switch - Back Hall (Aqora 2 Gang)"
when
    Item BackHall_WallSwitch_Button received update
then
    if (newState == 1001) {         // 1001=R Up LONG
        cct_On.apply(
            BackHall_Ceiling_Light_Brightness,
            BackHall_Ceiling_Light_SceneBrightness,
            BackHall_Ceiling_Light_ColorTemperature
        )
    } else if (newState == 1002) {  // 1002=R Up 1
        if (BackHall_Ceiling_Light_Brightness.state == 0) {
            cct_On.apply(
                BackHall_Ceiling_Light_Brightness,
                BackHall_Ceiling_Light_SceneBrightness,
                BackHall_Ceiling_Light_ColorTemperature
            )
        } else {
            light_dimUp.apply(BackHall_Ceiling_Light_Brightness)
        }
    } else if (newState == 1003) {  // 1003=R Up LONG RELEASE

    } else if (newState == 1004) {  // 1004=R Up 2
        cct_On.apply(
            BackHall_Ceiling_Light_Brightness,
            BackHall_Ceiling_Light_SceneBrightness,
            BackHall_Ceiling_Light_ColorTemperature
        )
    } else if (newState == 1005) {  // 1005=R Up 3
        cct_On.apply(
            BackHall_Ceiling_Light_Brightness,
            BackHall_Ceiling_Light_SceneBrightness,
            BackHall_Ceiling_Light_ColorTemperature
        )

    } else if (newState == 2001) {  // 2001=R Down LONG
        light_Off.apply(BackHall_Ceiling_Light_Brightness)
        
    } else if (newState == 2002) {  // 2002=R Down 1
        light_dimDown.apply(BackHall_Ceiling_Light_Brightness)

    } else if (newState == 2003) {  // 2003=R Down LONG RELEASE

    } else if (newState == 2004) {  // 2004=R Down 2
        light_min.apply(BackHall_Ceiling_Light_Brightness)
    } else if (newState == 2005) {  // 2005=R Down 3
        light_min.apply(BathRoom_Ceiling_Light_Brightness)
    } else if (newState == 3001) {  // 3001=L Up LONG

    } else if (newState == 3002) {  // 3002=L Up 1
        if (BathRoom_Ceiling_Light_Brightness.state == 0) {
            cct_On.apply(
                BathRoom_Ceiling_Light_Brightness,
                BathRoom_Ceiling_Light_SceneBrightness,
                BathRoom_Ceiling_Light_ColorTemperature
            )
        } else {
            light_dimUp.apply(BathRoom_Ceiling_Light_Brightness)
        }

    } else if (newState == 3003) {  // 3003=L Up LONG RELEASE

    } else if (newState == 3004) {  // 3004=L Up 2
        cct_On.apply(
            BathRoom_Ceiling_Light_Brightness,
            BathRoom_Ceiling_Light_SceneBrightness,
            BathRoom_Ceiling_Light_ColorTemperature
        )       
    } else if (newState == 3005) {  // 3005=L Up 3
        cct_On.apply(
            BathRoom_Ceiling_Light_Brightness,
            BathRoom_Ceiling_Light_SceneBrightness,
            BathRoom_Ceiling_Light_ColorTemperature
        )    
    } else if (newState == 4001) {  // 4001=L Down LONG
        light_Off.apply(BathRoom_Ceiling_Light_Brightness)   

    } else if (newState == 4002) {  // 4002=L Down 1
        light_dimDown.apply(BathRoom_Ceiling_Light_Brightness)   

    } else if (newState == 4003) {  // 4003=L Down LONG RELEASE

    } else if (newState == 4004) {  // 4004=L Down 2
        light_min.apply(BathRoom_Ceiling_Light_Brightness)   

    } else if (newState == 4005) {  // 4005=L Down 3
        light_Off.apply(BathRoom_Ceiling_Light_Brightness)    
    }
end

rule "Wall Switch - Bath Room (Aqora 2 Gang)"
when
    Item BathRoom_WallSwitch_Button received update
then
    if (newState == 1001) {         // 1001=R Up LONG
        cct_On.apply(
            BathRoom_Ceiling_Light_Brightness,
            BathRoom_Ceiling_Light_SceneBrightness,
            BathRoom_Ceiling_Light_ColorTemperature
        )
    } else if (newState == 1002) {  // 1002=R Up 1
        if (BathRoom_Ceiling_Light_Brightness.state == 0) {
            cct_On.apply(
                BathRoom_Ceiling_Light_Brightness,
                BathRoom_Ceiling_Light_SceneBrightness,
                BathRoom_Ceiling_Light_ColorTemperature
            )
        } else {
            light_dimUp.apply(BathRoom_Ceiling_Light_Brightness)
        }
    } else if (newState == 1003) {  // 1003=R Up LONG RELEASE

    } else if (newState == 1004) {  // 1004=R Up 2
        cct_On.apply(
            BathRoom_Ceiling_Light_Brightness,
            BathRoom_Ceiling_Light_SceneBrightness,
            BathRoom_Ceiling_Light_ColorTemperature
        )
    } else if (newState == 1005) {  // 1005=R Up 3
        cct_On.apply(
            BathRoom_Ceiling_Light_Brightness,
            BathRoom_Ceiling_Light_SceneBrightness,
            BathRoom_Ceiling_Light_ColorTemperature
        )

    } else if (newState == 2001) {  // 2001=R Down LONG
        light_Off.apply(BathRoom_Ceiling_Light_Brightness)
        
    } else if (newState == 2002) {  // 2002=R Down 1
        light_dimDown.apply(BathRoom_Ceiling_Light_Brightness)

    } else if (newState == 2003) {  // 2003=R Down LONG RELEASE

    } else if (newState == 2004) {  // 2004=R Down 2
        light_min.apply(BathRoom_Ceiling_Light_Brightness)
        
    } else if (newState == 2005) {  // 2005=R Down 3
        light_min.apply(BathRoom_Ceiling_Light_Brightness)

    // Below buttons for Colour change.    
    } else if (newState == 3001) {  // 3001=L Up LONG

    } else if (newState == 3002) {  // 3002=L Up 1
    
    } else if (newState == 3003) {  // 3003=L Up LONG RELEASE

    } else if (newState == 3004) {  // 3004=L Up 2
      
    } else if (newState == 3005) {  // 3005=L Up 3
     
    } else if (newState == 4001) {  // 4001=L Down LONG
     
    } else if (newState == 4002) {  // 4002=L Down 1
      
    } else if (newState == 4003) {  // 4003=L Down LONG RELEASE

    } else if (newState == 4004) {  // 4004=L Down 2
   
    } else if (newState == 4005) {  // 4005=L Down 3
   
    }
end


rule "Wall Switch - Hall (Aqora 3 Gang)"
when
    Item Hall_WallSwitch_Button received update
then
    if (newState == 1001) {         // 1001=R Up LONG
        cct_On.apply(
            Hall_FrontDoor_Light_Brightness,
            Hall_FrontDoor_Light_SceneBrightness,
            Hall_FrontDoor_Light_ColorTemperature
        )
    } else if (newState == 1002) {  // 1002=R Up 1
        if (Hall_FrontDoor_Light_Brightness.state == 0) {
            cct_On.apply(
                Hall_FrontDoor_Light_Brightness,
                Hall_FrontDoor_Light_SceneBrightness,
                Hall_FrontDoor_Light_ColorTemperature
            )
        } else {
            light_dimUp.apply(Hall_FrontDoor_Light_Brightness)
        }
    } else if (newState == 1003) {  // 1003=R Up LONG RELEASE

    } else if (newState == 1004) {  // 1004=R Up 2
        cct_On.apply(
            Hall_FrontDoor_Light_Brightness,
            Hall_FrontDoor_Light_SceneBrightness,
            Hall_FrontDoor_Light_ColorTemperature
        )
    } else if (newState == 1005) {  // 1005=R Up 3
        cct_On.apply(
            Hall_FrontDoor_Light_Brightness,
            Hall_FrontDoor_Light_SceneBrightness,
            Hall_FrontDoor_Light_ColorTemperature
        )

    } else if (newState == 2001) {  // 2001=R Down LONG
        light_Off.apply(Hall_FrontDoor_Light_Brightness)
        
    } else if (newState == 2002) {  // 2002=R Down 1
        light_dimDown.apply(Hall_FrontDoor_Light_Brightness)

    } else if (newState == 2003) {  // 2003=R Down LONG RELEASE

    } else if (newState == 2004) {  // 2004=R Down 2
        light_min.apply(Hall_FrontDoor_Light_Brightness)

    } else if (newState == 2005) {  // 2005=R Down 3
        light_min.apply(Hall_FrontDoor_Light_Brightness)

    } else if (newState == 3001) {  // 3001=M Up LONG
        cct_On.apply(
                Hall_FrontDoor_Light_Brightness,
                Hall_FrontDoor_Light_SceneBrightness,
                Hall_FrontDoor_Light_ColorTemperature
            )
    } else if (newState == 3002) {  // 3002=M Up 1
        if (Landing_Ceiling_Light_Brightness.state == 0) {
            cct_On.apply(
                Landing_Ceiling_Light_Brightness,
                Landing_Ceiling_Light_SceneBrightness,
                Landing_Ceiling_Light_ColorTemperature
            )
        } else {
            light_dimUp.apply(Landing_Ceiling_Light_Brightness)
        }
    } else if (newState == 3003) {  // 3003=M Up LONG RELEASE

    } else if (newState == 3004) {  // 3004=M Up 2
        cct_On.apply(
            Landing_Ceiling_Light_Brightness,
            Landing_Ceiling_Light_SceneBrightness,
            Landing_Ceiling_Light_ColorTemperature
        )       
    } else if (newState == 3005) {  // 3005=M Up 3
        cct_On.apply(
            Landing_Ceiling_Light_Brightness,
            Landing_Ceiling_Light_SceneBrightness,
            Landing_Ceiling_Light_ColorTemperature
        )    
    } else if (newState == 4001) {  // 4001=M Down LONG
        light_Off.apply(Landing_Ceiling_Light_Brightness)   

    } else if (newState == 4002) {  // 4002=M Down 1
        light_dimDown.apply(Landing_Ceiling_Light_Brightness)   

    } else if (newState == 4003) {  // 4003=M Down LONG RELEASE

    } else if (newState == 4004) {  // 4004=M Down 2
        light_min.apply(Landing_Ceiling_Light_Brightness)   

    } else if (newState == 4005) {  // 4005=M Down 3
        light_Off.apply(Landing_Ceiling_Light_Brightness)    

    } else if (newState == 5001) {  // 5001=L Up LONG
        cct_On.apply(
            Hall_Ceiling_Light_Brightness,
            Hall_Hall_Ceiling_Light_SceneBrightness,
            Hall_Hall_Ceiling_Light_ColorTemperature
        )
    } else if (newState == 5002) {  // 5002=L Up 1
        if (Hall_Hall_Ceiling_Light_Brightness.state == 0) {
            cct_On.apply(
                Hall_Ceiling_Light_Brightness,
                Hall_Hall_Ceiling_Light_SceneBrightness,
                Hall_Hall_Ceiling_Light_ColorTemperature
            )
        } else {
            light_dimUp.apply(Hall_Hall_Ceiling_Light_Brightness)
        }
    } else if (newState == 5003) {  // 5003=L Up LONG RELEASE

    } else if (newState == 5004) {  // 5004=L Up 2
        cct_On.apply(
            Hall_Ceiling_Light_Brightness,
            Hall_Ceiling_Light_SceneBrightness,
            Hall_Ceiling_Light_ColorTemperature
        )       
    } else if (newState == 5005) {  // 5005=L Up 3
        cct_On.apply(
            Hall_Ceiling_Light_Brightness,
            Hall_Ceiling_Light_SceneBrightness,
            Hall_Ceiling_Light_ColorTemperature
        )    
    } else if (newState == 6001) {  // 6001=L Down LONG
        light_Off.apply(Hall_Ceiling_Light_Brightness)   

    } else if (newState == 6002) {  // 6002=L Down 1
        light_dimDown.apply(Hall_Ceiling_Light_Brightness)   

    } else if (newState == 6003) {  // 6003=L Down LONG RELEASE

    } else if (newState == 6004) {  // 6004=L Down 2
    light_min.apply(Hall_Ceiling_Light_Brightness)   
    } else if (newState == 6005) {  // 6005=L Down 3
        light_Off.apply(Hall_Ceiling_Light_Brightness)
    }
end

rule "Wall Switch - Landing (Traadfri Dimmer)"
when
    Item Landing_WallSwitch_Button received update
then
    if (newState == 1001) {         // 1001=
      
    } else if (newState == 1002) {  // 1002=Up1
        cct_On.apply(
            Landing_Ceiling_Light_Brightness,
            Landing_Ceiling_Light_SceneBrightness,
            Landing_Ceiling_Light_ColorTemperature
        )
    } else if (newState == 1003) {  // 1003=

    } else if (newState == 1004) {  // 1004=
       
    } else if (newState == 1005) {  // 1005=
        
    } else if (newState == 2001) {  // 1001=
      
    } else if (newState == 2002) {  // 1002=Up1
        light_Off.apply(Landing_Ceiling_Light_Brightness)
    } else if (newState == 2003) {  // 1003=

    } else if (newState == 2004) {  // 1004=
       
    } else if (newState == 2005) {  // 1005=
    
    }
end

rule "Wall Switch - Sitting Room (Aqora Smart Switch)"
when
    Item SittingRoom_WallSwitch_Button received update 
then
    if (newState == 1001) {         // 1001=Hold
        TVSamsung_Power.sendCommand(OFF)

    } else if (newState == 1002) {  // 1002=1Clk
        if (SittingRoom_Side_Lamps.state == OFF) {
            switch_On.apply(SittingRoom_Side_Lamps)
        } else {
            light_Off.apply(SittingRoom_Side_Lamps)
        }
    } else if (newState == 1003) {  // 1003=

    } else if (newState == 1004) {  // 1004=2Clk
        if (TVSamsung_KeyCode == "KEY_PLAY") {
            TVSamsung_KeyCode.sendCommand("KEY_PAUSE")
        } else {
            TVSamsung_KeyCode.sendCommand("KEY_PLAY")
        }
    } else if (newState == 1005) {  // 1005=3Clk
        
    }
end

rule "Wall Switch - Jimmys Room, Bedside (Aqora Smart Switch)"
when
    Item JimmysRoom_Bedside_WallSwitch_Button received update 
then
    if (newState == 1001) {         // 1001=Hold
        if (SceneSelection.state == NIGHT) {
            light_Off.apply(JimmysRoom_Bedside_Lamp_OnOff)
            light_Off.apply(gAll_Lights_Brightness)
        } else {
            light_Off.apply(JimmysRoom_Bedside_Lamp_OnOff)
        }
    } else if (newState == 1002) {  // 1002=1Clk
        if (JimmysRoom_Bedside_Lamp_OnOff.state == ON) {
            if (SceneSelection.state == NIGHT) {
                light_Off.apply(JimmysRoom_Bedside_Lamp_OnOff)
                light_Off.apply(gAll_Lights_Brightness)
            } else {
                light_Off.apply(JimmysRoom_Bedside_Lamp_OnOff)
            }
        } else {
            switch_On.apply(JimmysRoom_Bedside_Lamp_OnOff)
        }
    } else if (newState == 1003) {  // 1003=

    } else if (newState == 1004) {  // 1004=2Clk
        if (JimmysRoom_Bedside_Lamp_OnOff.state == ON) {
            if (SceneSelection.state == NIGHT) {
                light_Off.apply(JimmysRoom_Bedside_Lamp_OnOff)
                light_Off.apply(gAll_Lights_Brightness)
            } else {
                light_Off.apply(JimmysRoom_Bedside_Lamp_OnOff)
            }
        } else {
            switch_On.apply(JimmysRoom_Bedside_Lamp_OnOff)
        }
    } else if (newState == 1005) {  // 1005=3Clk
        if (JimmysRoom_Bedside_Lamp_OnOff.state == ON) {
            if (SceneSelection.state == NIGHT) {
                light_Off.apply(JimmysRoom_Bedside_Lamp_OnOff)
                light_Off.apply(gAll_Lights_Brightness)
            } else {
                light_Off.apply(JimmysRoom_Bedside_Lamp_OnOff)
            }
        } else {
            switch_On.apply(JimmysRoom_Bedside_Lamp_OnOff)
        }
    }
end

Looking forward to hearing some good ideas…

Can’t be done with Rules DSL.

For JavaScript or Python just put your library of functions in $OH_CONF/automation/lib/<language>/personal and import it. See Search results for 'JavaScript libraries' - openHAB Community for a fairly comprehensive example using JavaScript. The same over all approach would been used for Python only three syntax odd different

Note, lambda is kinda of a specific term and it means a function that can be treated as a variable and passed around. Rules DSL doesn’t support proper functions, only labdas. But the other supported languages all support proper functions, which are much more suited to this use case.

So either you need to keep all your rules inn the same .rules file so they can all see the lambdas, or you need to use a different language for your rules.

I only quickly scanned through some of this code, on my phone, but I think you can replace a lot of theses arguments to the lambdas with call to the getLocation() and getEquipment() Actions and then semantic model.

Which bit?

I have done this already.

This worked great. Chopped it down to about 600 lines instead of 800

you can’t import rules dsl files into other roles dsl files. You cannot call lambdas defined in another. rules file.

Is there an equivivant to this in yaml when building the UI?
for example, I want to name the items in an oh-repeater - I want to do something like
title: '=getLocation(loop.item.name)`

Not that I’m aware of. You can embed that sort of information into Item metadata and access that but I see no way in the docs to access the parent equipment or location.

Can you direct me to any info about this? I can’t find anything in the docs or forum that helps.
Both how to create custom Metadata (Whenever I try to create custom Metadata to experiment with, nothing is created after I click save.)
And how to call it from an oh-repeater using yaml.

I’m guessing something along the lines of -
**See the line with title: =items[loop.item.name].metadata("FriendlyName")

- component: f7-card
      config:
        title: TRV batteries        
      slots:
        default:
          - component: oh-list
            slots:
              default:
                - component: oh-repeater
                  config:
                    for: item
                    sourceType: itemsWithTags
                    itemTags: LowBattery,
                    filter: loop.item.name.includes("TRV") == true
                  slots:
                    default:
                      - component: oh-list-item
                        config:
                          icon: '=(loop.item.name.includes("BatteryLevel")) ? "oh:batterylevel" : "oh:lowbattery"'
                          iconUseState: true
                          badge: =items[loop.item.name].displayState
                          badge-color: '=(items[loop.item.name].state > 80 || items[loop.item.name].state == "OFF") ? "green" : (items[loop.item.name].state < 10 || items[loop.item.name].state == "ON") ? "red" : "yellow"'
                          title: =items[loop.item.name].metadata("FriendlyName")
                          item: =loop.item.name


                           

I think I’ve just answered my own question. Seems metadata is created, but is not visible in the UI. How do I find and edit these metadata entries once I make them?

It’s created. There is just no way right now to show the custom metadata in the UI. The problem is the APIs can only give you metadata that you already know the namespace for. Obviously, the UI has no way of knowing the namespace for metadata that you defined yourself ahead of time so it can’t show your custom metadata in a list.
But you can:

  • click on “add custom metadata” again and enter the same namespace again it will bring it up
  • query for it in the REST API Docs in the Item’s section
  • in /var/lib/openhab/jsondb/org.openhab.core.items.Metadata.json
  • interact with the metadata in your rules (JavaScript or Python only, not supported in Rules DSL).

All of these will show the metadata that you’ve defined.

The options is fetchMetadata and there are a few examples scattered throughout the forum:

Search for fetchMetadata and you’ll find lots more.

Thank you.

The answer I needed for my yaml code was

title: =loop.item.metadata.FriendlyName.value

I think access org.openhab.core.items.Metadata.json is the best option for me. Can I edit this directly?

Another alternative to using metadata in my case would be to just parse the name that I already have without needing to add extra metadata steps.

All of my items have a consistent naming convention.
If the name of an item is returned as ‘Landing_WallSwitch_BatteryLevel’, is it possible to convert it to ‘Landing Wall Switch’ using yaml code?

Only if openHAB is not running when you edit it. And if you have one } or , out of place you’ll hose the whole file. I do not recommend editing the file directly. If you want an easier way to add metadata than using MainUI, I recommend using the REST API docs. The JSON will always be valid and you can add/remove/update metadata while OH is running.

See OH 3 Tips and Tricks which has a section about using the REST API to make bulk updates and editing the JSONDB files if all else fails.

Building Pages - Components & Widgets | openHAB shows all the expressions that are available to you in a widget. JavaScript String Reference shows all you can do with a JavaScript String Object.