How to use Contact Group Item in Rule?

Hi,

I am trying to Create Home Security System using

Group:Contact:OR(OPEN,CLOSED) doorsecsys "Total Doors Open [%d]"

I am able to Get Status of How many Doors Open using above feature.

If any 1 door is open. Total Doors Open will show 1. and so on…

Now i want to use this Group Feature in Security System. If security system is armed. I want to execute rule which will check if Count is > 0. Then it will turn on Test Switch and if 0 (all doors are closed) it will turn off switch.

rule "Alert If Any Door is Opened"
when
	Item doorsecsys received update
then
	if(doorsecsys != 0)
{
	dummytest.sendCommand(ON)
}
else
{	
	if(doorsecsys == 0)
	{
	dummytest.sendCommand(OFF)
	}
}	
end

Switch Gets on when count is not equal to 0. But it doesn’t get off when count is 0.

Please guide me.

That’s a misleading functionality…

A Group Item of Type Contact has up to three status, OPEN, CLOSED and NULL (the latter is “No status yet”)
Yes, you can get the Number of members in Status OPEN, but this is only for dynamic Label.

Your rule would be like this:

rule "Alert If Any Door is Opened"
when
    Item doorsecsys changed
then
    if(doorsecsys.state != CLOSED) {
        dummytest.sendCommand(ON)
    } else {
	dummytest.sendCommand(OFF)
    }
end

Please pay attention to the fact, that you don’t need the second if(), as there is either CLOSED or OPEN (the Item changed, it’s very unlikely that the Item will become NULL)
Further, pay attention to the .state.

  • doorsecsys -> Item itself (Last Update time stamp and actual Status)
  • doorsecsys.state -> Status of the item

If you want to get the number of opened doors, you have to use another method:

logInfo("myrule","Number of open doors is {}",doorsecsys.members.filter[m|m.state == OPEN].size)

Thanks that makes my Day…!!

What would the line be if I don’t want the number but the specific item that is open?

Greetings

Well, in fact, there is not “the” item, but always a list of items. This list may enumerate zero, one or more items, depending on the filter. so

val myItemList = MyGroupItem.members.filter[m|m.state == OPEN]

will result in an ItemList (not a group Item) with all members of MyGroupItem and state OPEN. Now you can get the number of Items:

val numberOfItems = myItemList.size 

or the first Item in list:

val firstItem = myItemList.head 

or get all names of all Items:

val myString = new StringBuilder
myItemList.forEach[i|
    myString.append(i.name + " ")
]
logInfo("logger","List of OPEN state: " + myString.toString)

or even manipulate all items:

myItemList.forEach[i|
    i.postUpdate(CLOSED)
]

Of course, the last code doesn’t make much sense with Contact Items :wink: