Find out if an item is in another group

I’ve got a rule that picks out an Item based upon how long its been run for, and if its ON, I then want to check whether that Item is in another Group so I can apply a different set of conditions to it.

Is there an easy way to do this? Something like

 if(MyItem IS IN THIS GROUP){
 do something } else { do something different}

Google suggests that I have to iterate through the group, filtering as I go, whereas I want to work on a single item

Items File

 Group:Number:AVG gChargerTimes
 Group:Switch:AND(OFF,ON) gChargers
 Group gSmallBattery
 Switch Charger1	(gChargers,gSmallBattery)
 Switch Charger2	(gChargers)
 Switch Charger3	(gChargers,gSmallBattery)

Rules File

 rule "when something happens check the charger powers"
 gChargerTimes.members.sortBy[ -(state as Number) ].forEach[ NumberItem rT |
val offchargerName = rT.name.toString.split('Mins').get(1)
var offchargerItem = gInverterChargers.members.findFirst[ i | i.name == offchargerName ] as GenericItem
 
  // in here I want to check if the item is a member of gSmallBattery and then process it differently	

 if (offchargerItem.state == ON && ChargeOFF > UsablePower.state as Number){
      offchargeritem.sendCommand(OFF)
   }
   ]
  end
 MyItem.groups.contains("gSmallBattery")

I’m going from memory so there might be something wrong with this but it should be close.

Thanks for the reply.
The method or field groups is undefined for the type GenericItem according to VSCode
Adding a log statement tells me that the GenericItem variable does contain the group information,

Maybe get groups. If you search the forum for “contains” I’m sure you will find an example.

Thanks for the pointer, this works

if (offchargerItem.toString.contains("gSmallBattery")){//do stuff}

Rich may have been going for…

if (offchargerItem.getGroupNames.contains("gSmallBattery")) {//do stuff}

or you could do this…

if (gSmallBattery.members.contains(offChargerItem)) {//do stuff}
2 Likes