[SOLVED] Groups for Beginners?

Hi all,

I would like to understand how groups work. Thank you for looking at this.

I’m only one day in to use OpenHab, and have become stuck on getting Groups to work. I’ve read the User Manual, and find it a little less than clear, and not complete examples, just isolated parts. I’ve also read the latest 30 posts with the tag Groups, and have familiarised myself with some of the issues, but have not become any clearer on solutions.

By way of example, I would like to count the number of active devices connected and in doing so, try and get my understanding of groups > 0.

default.items:

Group gAll
Group:Switch:OR(ON, OFF) gDevices "All Devices [%d]" (gAll)
Switch Presence_Phone "iPhone" <network> (gDevices) { channel="network:servicedevice:10_0_0_34_80:online" } 
Switch Presence_Laptop "MacBook" <network> (gDevices) { channel="network:servicedevice:10_0_0_153_80:online" } 
Number Connected_Devices "Connected devices [%d]" <network> (gDevices)

default.sitemap:

sitemap default label="mySitemap" 
{
    Switch item=Presence_Phone label="iPhone" icon="switch"
    Switch item=Presence_Laptop label="MacBook" icon="switch"
    Text item=Connected_Devices label="Connected devices [%d]" icon="presence"
}

With one device connected, and one device disconnected, I would expect a value of ‘1’.
The way this is set up at the moment, I get only a dash “-” on the “Connected devices” item.

Edit: OH2.2.0, Smarthome 0.9.0, Rules Engine (Experimental) in Paper UI

Try one of this

    Text   item=gDevices label="Connected devices [%d]" icon="presence"
    Group  item=gDevices
    Switch item=gDevices

at first two important things on Groups:

  • if you’d like to have some action (like Group:Switch:OR(ON, OFF), you should only add the identical itemtypes to the Group.
    In your case, you added Switches (which work with your Group definition) and Number (which breaks the Group definition)
  • Within a Group Definition you can have functions (like you had with your “Group:Switch:OR(ON, OFF)” definition), but those only work with the same itemtype, if I’m correct

In my recollection you can only use numeric functions with a Group consisting of Number items. For example I use a Group “gRTR” and in there I AVG my room temperature items.

So, in your case the item “Connected_Devices” is just that - an item. You must have some kind of action to change the item’s state. I would add a rule, which fires periodically or onChange of an item within gDevices and it counts your online devices and updates the Connected_Devices item.

rule "connected devices"
when
    Item gDevices received update
then
    Connected_Devices.sendCommand(gDevices.members.filter[s|s.state==ON].size)
end
1 Like

I use this to show the open windows or switched on lights on my sitemap:

			Switch item=gLicht mappings=[OFF="alles aus"]
			Text item=gFensterkontakte

gLicht and gFensterkontakte are groups. I get this on my sitemap:

Currently i have 3 lights which are switched on and 4 windows are opened.

My items-file:

Group:Contact:OR(OPEN, CLOSED)gFensterkontakte   "Fensterkontakte [%d]" <contact-open>
Group:Switch:OR(ON, OFF) gLicht "Licht [(%d)]"
4 Likes

wow, didn’t know that trick for the sitemap! That’s great!

Thanks for all the responses, and especially binderth.

That worked! and I learned my first lesson about rules as a bonus. Now i’ll go and figure out how that rule works element by element.

I completed removed that Connected_Devices item from as it seemed to be redundant , AND it also was causing an error (an unknown element?)

default.items

Group gAll
Group:Switch:OR(ON, OFF) gDevices "Active Devices [%d]" (gAll)
Switch Presence_Phone "iPhone" <network> (gDevices) { channel="network:servicedevice:10_0_0_34_80:online" } 
Switch Presence_Laptop "MacBook" <network> (gDevices) { channel="network:servicedevice:10_0_0_153_80:online" } 

default.sitemap

sitemap default label="mySitemap"
{
    Switch 	item=Presence_Phone 		label="iPhone" 				icon="switch"
    Switch 	item=Presence_Laptop 		label="MacBook" 				icon="switch"
    Text 	item=gDevices 			label="Connected devices [%d]" 		icon="presence"
}

default.rules

rule "connected devices"
when
    Item gDevices changed
then
    gDevices.sendCommand(gDevices.members.filter[s|s.state==ON].size)
end

Basic UI
26

1 Like

You don’t need the rule to count devices, this is done by group definition.
.items:

Group:Switch:OR(ON, OFF) gDevices "Active Devices [%d]" <presence> (gAll)
Switch Presence_Phone "iPhone [%s]" <network> (gDevices) { channel="network:servicedevice:10_0_0_34_80:online" } 
Switch Presence_Laptop "MacBook [%s]" <network> (gDevices) { channel="network:servicedevice:10_0_0_153_80:online" } 

.sitemap:

sitemap default label="mySitemap"
{
    Text item=gDevices
    Text item=Presence_Phone   // read only, so use 
    Text item=Presence_Laptop  // text instead switch
}

As a bonus, change the sitemap to following:

sitemap default label="mySitemap"
{
    Text item=gDevices
    {
        Text item=Presence_Phone visibility=[Presence_Phone==ON]
        Text item=Presence_Laptop visibility=[Presence_Laptop==ON]
    }
}

and see what’s happening :slight_smile:

1 Like

@halloween’s answer is the best one. I didn’t know you could do that either. But it is possible to define a Number Group with Switches or Contacts (maybe both) and use SUM as the function to get the number that is OPEN/ON.

Group:Number:SUM gDoorCounts "Open Doors [%d]"
Contact vGarageOpener1 "Garage Door Opener 1 is [MAP(en.map):%s]"  <garagedoor> (gDoorSensors,gDoorCounts)
1 Like