Hi there and thanks for being such a great, supportive & friendly community! My openHAB setup was greatly inspired by the many posts & tutorials on this site - yet I am still struggling a bit with setting up “reusable rules” with the help of Lambdas.
What I am trying to accomplish is to set the brightness level and toggle of some of my lights. With single lights this code works really well:
val itemToggle = [ GenericItem item, String logMessage |
if (item.state == OFF || item.state == NULL) {
logInfo(item.name, "Switched on. (" + logMessage + ")")
item.sendCommand(ON)
} else if (item.state == ON) {
logInfo(item.name, "Switched off. (" + logMessage + ")")
item.sendCommand(OFF)
}
]
val itemBrightnessLevelUp = [ GenericItem item, int brightnessStep |
var brightness = if (item.state == NULL) 0 else item.state as Number + brightnessStep
if (brightness <= 100) item.sendCommand(brightness)
]
val itemBrightnessLevelDown = [ GenericItem item, int brightnessStep |
var brightness = if (item.state == NULL) 0 else item.state as Number - brightnessStep
if (brightness >= 0) item.sendCommand(brightness)
]
and
// Global brightness step when changing the dimmer value
var brightnessStep = 20
rule "Remote (001): Remote for playing around"
when
Item ZigbeeTradfriRemote001Action received update
then
switch (ZigbeeTradfriRemote001Action.state) {
case "toggle": itemToggle.apply(zwave_livingroom_tv, "Outlet at TV - manually switched.")
case "brightness_up_click": itemBrightnessLevelUp.apply(ZigbeeTradfriLED001FloorUpperLevelBrightness, brightnessStep)
case "brightness_down_click": itemBrightnessLevelDown.apply(ZigbeeTradfriLED001FloorUpperLevelBrightness, brightnessStep)
case "arrow_right_click",
case "arrow_left_click": itemToggle.apply(ZigbeeTradfriLED001FloorUpperLevelSwitch, "Floor light - manually switched")
}
end
What I want to do now is to switch groups. But there is no “GenericGroup” object type avaliable - at least not that I know of. What I found was a post by @rlkoshak where he describes the use of groups with filters (Rule which iterates through group(s) of similar items). I tried to modify the code a bit and see if that could work, but it doesn’t (LEDBedroom being my group of all light (brightness & switch) items in the bedroom):
rule "Remote (002): Bedroom lighting"
when
Item ZigbeeTradfriRemote002Action received update
then
switch (ZigbeeTradfriRemote002Action.state) {
case "toggle": LEDBedroom.getAllMembers.filter[ i | i instanceof SwitchItem ].forEach[ filteredItem | itemToggle.apply(filteredItem, "Bedroom lighting- manually switched") ]
case "brightness_up_click": LEDBedroom.getAllMembers.filter[ i | i instanceof DimmerItem ].forEach[ filteredItem | itemBrightnessLevelUp.apply(filteredItem, brightnessStep) ]
case "brightness_down_click": LEDBedroom.getAllMembers.filter[ i | i instanceof DimmerItem ].forEach[ filteredItem | itemBrightnessLevelDown.apply(filteredItem, brightnessStep) ]
}
end
The logs are talking about this:
There is no context to infer the closure’s argument types from. Consider typing the arguments or put the closures into a typed context.
I read somewhere that openHAB tries to “guess” which type of object I am using, but casting didn’t help or just threw another round of error messages. Do you know what I could do here?
I would love to use reusable code.