Dynamic configuration of group membership from UI

First, some background of what I am trying to achieve:

I run OpenHAB 2.2 development version, build #1058

I have a DayNightMode item that keeps track of what part of the day it currently is. When this changes (e.g. because the sun sets), lights are turned on or off depending on group membership in gMorningLights, gEveningLights and so on. This works pretty well, but I tend to change group membership for switch items quite often and having to do this in code is somewhat tedious.

So, my idea is to have a “dynamic configuration” in the UI. Therefore, I try to dynamically create a SwitchItem for each mode in DayNightMode and for each member of the group gLights, but I have not had much success so far.

This is my current test code:

default.items:

Group gTest
Switch TestSwitch "Test switch"
...

Rules file:

val List<SwitchItem> dynItemList = newArrayList()

...

rule "Konfiguration av lampor"
when
    Item TestSwitch changed to ON
then
    gLights.allMembers.forEach[lightItem |
        var SwitchItem dynItem = new SwitchItem(lightItem.name + "_Ctl")
        dynItem.label = lightItem.label
        logInfo("test", "Created new item: " + dynItem)
        gTest.addMember(dynItem)
    ]
end

The items are actually created, but seems to be destroyed as soon as the rule finishes execution. This shows up in the log when I try to view the group in the UI:

2017-11-23 13:56:05.272 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item 'Qubino_Flush_2_Relays_Switch1_Ctl' for widget org.eclipse.smarthome.model.sitemap.Switch
2017-11-23 13:56:05.278 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item for widget org.eclipse.smarthome.model.sitemap.Switch
2017-11-23 13:56:05.284 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item 'Qubino_Flush_2_Relays_Switch1_Ctl' for widget org.eclipse.smarthome.model.sitemap.Switch
2017-11-23 13:56:05.292 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item 'Qubino_Flush_2_Relays_Switch1_Ctl' for widget org.eclipse.smarthome.model.sitemap.Switch
2017-11-23 13:56:05.297 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item 'Qubino_Flush_2_Relays_Switch1_Ctl' for widget org.eclipse.smarthome.model.sitemap.Switch
2017-11-23 13:56:05.302 [WARN ] [basic.internal.render.SwitchRenderer] - Cannot determine item type of 'Telldus_Switch_1_Ctl'
org.eclipse.smarthome.core.items.ItemNotFoundException: Item 'Telldus_Switch_1_Ctl' could not be found in the item registry
	at org.eclipse.smarthome.core.internal.items.ItemRegistryImpl.getItem(ItemRegistryImpl.java:61) [98:org.eclipse.smarthome.core:0.9.0.201709260841]
	at org.eclipse.smarthome.ui.internal.items.ItemUIRegistryImpl.getItem(ItemUIRegistryImpl.java:710) [137:org.eclipse.smarthome.ui:0.9.0.201709260841]
	at org.eclipse.smarthome.ui.basic.internal.render.SwitchRenderer.renderWidget(SwitchRenderer.java:50) [180:org.eclipse.smarthome.ui.basic:0.9.0.201709260841]
    ...

I added the ArrayList in an attempt to keep the garbage collector from removing the SwitchItem objects, but apparently I don’t know much about scoping and object lifetime in the Xtend/OpenHAB rules language.

Any ideas?