Openhab-js getItemsByTag

  • Platform information:
    • Hardware: Raspberry Pi 4
    • OS: openhabian
    • openHAB version: 3.3
  • Issue of the topic: please be detailed explaining your issue

In openhab-js (ECMA Script 2021), I’m trying to select an array of Items which have a certain tag (say “Automation”) from the members of a Group Item.

So while

items.getItemsByTag("Automation")

correctly gives me all Items with that tag, I can’t get:

items.getItem("GroupItemName").members.getItemsByTag("Automation")

to work…

The script terminates with

....members.getItemsByTag is not a function

Can getItemsByTag only be applied to the full items collection or how can I basically otherwise filter an array of Items by a specific tag?

Many thanks ofor any advise.

In the js helper library, items is a specially defined object with several unique functions such as getItemsByTag. The results of members is an array of items; this is different and therefore you need a different approach. Fortunately, js arrays have a filter method to do exactly what you want: pare down an array by following some rule or test. In this case, your test is whether or not Automation is found in each item’s array of tags.

items.getItem("GroupItemName").members.filter( x => x.tags.indexOf("Automation") >= 0)

*Typed on my phone, small errors almost assured and minor modification likely required

3 Likes