I’m trying to display the number of lights that are currently ON from a group inside a MainUI widget.
The group contains several Switch items (lights).
Not through the items object. That always has a very restricted amount of information about an item and will never return the members of a group.
You can use an oh-repeater to get all the members of an item. There are a couple different ways to do this, but the easiest is probably just to put a unique tag on the group item and use the repeater’s itemsWithTags source to get that one item. The object returned by the repeater is a complete representation of that item including the members array.
Even though this can be done in the widget it is not the best way to do it, in my opinion. I do this in numerous different situations and just use the group aggregation functions. Boolean values such as switches are treated as 0’s and 1’s so a simple sum aggregation gets you the number currently on:
You cannot. There is no variable assignment in widget expressions. Variables can only be set in an oh-context for a default value or as a result of a user action.
There are very convoluted ways to abuse an oh-repeater to get close to what you want to do, but to be honest, getting them up and running will take longer than just setting up some group items that do the counting for you automatically. I think the widget method would also be more prone to breaking whereas the group method will be robust and long-term stable.