Identify itemname based on part of a name

See my Assocaited Items Design Pattern.

Assuming that all your Items are a member of a Group then:

gTex_Zone.filter[sw|sw.name.contains("some string")

First, I might recommend coming up with a more meaningful naming pattern. Numbers make a lot of sense to computers but before long you will be scratching your head wondering “What light was Tex_Zone_Z025_102 again?” It is just as easy for OH and your Rules to deal with human meaningful names and then your code becomes more self-documenting.

There are lots of approaches to naming patterns and several threads on this forum on the topic. My latest approach is:

If the name starts with:

  • g - Group
  • v - sensor value or state
  • a - actuator
  • t - Expire based timer

The first word is the general functionality the Item addresses (e.g. Network, Lighting, etc). I’m not always diligent about this part and have a lot of legacy Items from before I adopted this convention.

I separate the parts of the name by _.

The last parts of the name can be anything depending on context.

So, for example, I have some code that generates an alert when certain remote sensors stop reporting. The relevant Items for one of those devices (named manticore in my DNS):

Group gManticore_SensorReporter (gSensorGroups)

Switch vNetwork_Manticore "Manticore Network [MAP(admin.map):%s]"
  <switch> 
  { channel="network:device:manticore:online", expire="1m" }

Switch vManticore_SensorReporter_Online "Manticore sensorReporter [MAP(admin.map):%s]"
    <network> (gSensorStatus)
    { mqtt="<[chimera:status/sensor-reporters:command:OFF:.*manticore sensorReporter is dead.*]", expire="10m,command=OFF" }    

String vManticore_SensorReporter_Uptime "Manticore sensorReporter Uptime [%s]"
    <clock> (gManticore_SensorReporter)
    { mqtt="<[chimera:status/manticore/heartbeat/string:state:default]" }

And then in my rules I can do things like:

// Given vManticore_SensorReporter_Uptime which receives a heartbeat I can:

// Get the parts of the name
var uptimeSplit = uptime.name.split("_")

// Get the Group:
var groupName = uptimeSplit.get(0).replace('v','g') + "_" + uptimeSplit.get(1)
gSensorGroups.members.filter[gr|gr.name == groupName

// Get the associated Online Item
var online = uptimeSplit.get(0) + "_" + uptimeSplit.get(1) + "_Online"

As long as the naming is consistent and easy to split apart and they are put into Groups it is relatively easy to pull associated items.

2 Likes