Usage of a procedure

I never worked with a procedure but to cleanup my code I was trying to create a procedure but get many errors and don’t know how to solve it. I also couldn’t find a description how to do this.
I tried to solve it in many ways based (2 are below) on what I found but without success so I hope someone can help.

//items file
//Color               KantoorLampenKleur                  "Kleur"                   <colorpicker>       (BG_Kantoor) 
//Group: Dimmer :AVG       Kantoorlampen                 "Alle lampen"       <light>

val Procedures$Procedure2<String, String> setColorForGroup = [String colorItemName, String groupName |
    val Iterable<GenericItem> groupItems = ScriptServiceUtil.getItemRegistry.getAll().filter[item | item.groupNames.contains(groupName)]

    var hue = (getGroupItem(colorItemName).state as HSBType).getHue 
    var sat = (getGroupItem(colorItemName).state as HSBType).getSaturation
    var brightness = (getGroupItem(colorItemName).state as HSBType).getBrightness 
    val HSBType newColor = new HSBType(hue, sat, brightness)
    groupItems.forEach[item | sendCommand(item, newColor.toString)
    ]
]

val Lampkleuraanpassen = [gebruikkleur Color, gebruikgroep Group|
    var hue = (gebruikkleur.state as HSBType).getHue
    var sat = (gebruikkleur.state as HSBType).getSaturation
    var brightness = (gebruikkleur.state as HSBType).getBrightness
    val HSBType color = new HSBType(hue, sat, brightness)
    gebruikgroep.members.forEach[ i | sendCommand(i, color.toString)]
]

rule "Kleur lampen aanpassenr"
when
     Item KantoorLampenKleur received command
then
   setColorForGroup.apply ("KamerLampenKleur", "Kantoorlampen")
   Lampkleuraanpassen (KamerLampenKleur, Kantoorlampen )
end

It’s hard for us to solve it if we don’t know what the errors are.

I couldn’t tell you what’s wrong but some things I notice:

This is excessive. If you are not building the name of the Group in the first place just pass in the actual Group and use:

val groupItems = group.members

If you only have the name of the Group then just use:

val groupItems = ScripServiceUtil.getItemRegistry.getItem(groupName).members

You don’t need to filter. The Group Item already has all it’s own members as a List.

Also notice I don’t specify the type. In Rules DSL never specify the type unless you absolutely have to. It causes problems otherwise.

What is this? If it’s a function that you are not showing then there are two problems:

  1. To call a function or procedure you need to use appy(), as in getGroupItem.apply(colorItemName).

  2. Global variables cannot see each other. If this is another lambda, you have to pass it to this procedure as an argument in order to call it.

What does it do?

getGroupItem(colorItemName).state is already an HSBType? Why create a new one? That’s kind of pointless, especially since all you do is call toString on it.

Why? If you sendCommand to the Group that command will get sent to all it’s members. There’s no need to loop over the members.

val  setColorForGroup = [String colorItemName, String groupName, Function$Function1 getGroupItem |
    val color = getGroupItem.apply(colorItemName).state as HSBType
    sendCommand(groupName, color.toString())
]

You’ve put the type after the variable name. And the way you are using gebruikkleur indicates it’s an Item, not a Color. There is no such thing as a Group type in openHAB. It’s GroupItem. If all you use is the HSBType in the first place, why not just pass that instead of the whole Item?

Same as above, why create a new HSBType and why loop through the members?

val Lampkleuraanpassen = [HSBType gebruikkleur, GenericItem gebruikgroep |
    gebruikgroep.sendCommand(gebruikkleur)
]

Of course, now this has become a one liner so you really aren’t saving anything by having it be a procedure in the first place any more.

Thanks.

Add a toString to send the command as a String. OH will parse it back to a HSBType.