Filter members of a group item by non-semantic tag

Is it possible to filter a group item based on non-semantic tags? i.e.

items.getItem("gWindows").members
  .filter(i => i.tags == "LivingRoom")
  .forEach(i => console.log("Filter Results: ", i.name + " is a match!"))
console.log("Window filter", "no match found")

the group item is fictitious, I’m just using it as an example. I have manually added “LivingRoom” to some items as a non-semantic tag. the script always returns “no match found”

If I knew how to filter on Location, I would do that, but I have not been able to find any reference to that on the internet or in the forums. Maybe my googling skills are as lacking as my javascript skills.

The tags property is an array, not a just a string. So it will never equal the one string value you are interested in (even if that array has only that string value in it).

The javascript test to see if a string is found in the elements of an array is includes() (and indexOf() is also an option):

So, you want:

i=> i.tags.includes("LivingRoom")

in your filter.

The logic is not too dissimilar, but instead of tags you have to access the semantic metadata. The added complication comes from how the semantic data is structured. Because the point items (those that you typically want in a rule to get some value or perform some action) are separated from the location information by the intervening equipment item.

The most common way to accomplish this is to do exactly what you are doing with the living room tag, only with an equipment type tag (e.g., window) and use the semantic metadata to filter for the location. Then from that list of equipment you can get each point item that you want either because of naming convention or item type or tag some other property.

There’s a more efficient and readable method (in my opinion) that works in certain cases. Here’s what I do to get all the light switches in a location:

var locationList = items.getItem(locationItem).descendents;
var locationLights = locationList.filter(x => ((x.type == 'SwitchItem') && (x.tags.indexOf('Light') >= 0)));

The descendents list gets not only members of the group like members does, but all of the members of any groups within the initial group etc. all the way down. So descendents of a location item gets all equipment and point items in that location. Then the second line just filters all those items to extract only the type of item with the semantic tag that I want (in this case Switch items with the tag Light).

Thanks Justin, I appreciate your help on this. I can finally stop pulling my hair out.