I wanted to have a label cell indicate in the “text” the state of “gAllDoors”, namely whether “all closed” or “number of open doors from total”.
Thanks to you, I was able to get an Item state count across a Group with the oh-repeater successfully, but the above seems a bit more involved. I have tried the script bellow, but now I’m wondering whether this can be accomplished the way I formulated it.
I was expecting to get “All doors closed” when the aggregated state of the “gAllDoors” is “CLOSED”, and when the group state changed to “OPEN”, to have for example “3/20 doors open”. Is this possible or am I completely off the reservation. Any input would be much appreciated.
Many thanks.
I have a widget on my overview page and it tells me how many doors/windows/blind/curtains are open and when they are all closed I get a green padlock.
You can modify it if you want.
It’s the last part of this expression that is the issue. Right now your expression, if you reduce it down to basic parts is
if all doors are closed return the string 'All doors closed',
but if not then return the string '=loop.door_source.filter(i => items[i.name].state === 'OPEN').length + '/20 doors open'
So, the problem is that the second return value is a string that looks like an expression, not a continuation of the original expression itself (the reason you don’t see that as the result in the label si that you’re missing one more closing ' in the expression so it’s throwing an error instead of finishing the evaluation).
You don’t want this whole return value to be a string, you want it to be the result of the filter method tacked on to a string. So, you don’t need the '...' around the whole return value, and you don’t want the = in front of it. This would be one option:
This is a fairly common use case, however, and there is a typical solution that you may find preferable. OH has a built-in way of handling this easily. Like you I have a group that tells me if any door is open (gPortals) by using an aggregation function. I have a second group that contains all the same door contacts (gPortal_Count), but I use a different aggregation function:
By treating the member base type as a Number the aggregation function gets 0 if a door is closed and 1 if a door is open, so the sum of those numerical states is the count of how many doors are open.
This is awesome! Thank you both for responding so quickly.
Greg, many thanks for sharing your widget and the oh-repeater examples. I can definitely use that as a template for some of my own applications.
Justin, your detailed response is much appreciated. Thanks for breaking down the whole expression. My syntax was way off…but I can definitely learn from the mistake.
Interestingly, I started out with Group aggregation as well, however I was trying to use the COUNT function across the Group, which apparently is not working in OH3. I was primarily using the file-based configuration without success. As a test, I tried configuring the Group through the Main UI and noticed the COUNT function is not listed in the drop-down menu. Seems like SUM is the way to go, and maybe not as convoluted. That said, it’s great to have alternatives…
Thanks again!