Use of groups, triggeringitem, determining item that triggered rule and getting name

@rlkoshak has been an immense help to me with this stuff so I’m trying not to bug him as much via PM and this may be of use to someone else. It’s probably already been done but either my search skills on this are lacking or I don’t know how to ask the question right.

In consolidating rules I’m trying to create one rule to handle what I have now as 9 rules to control vents in 9 rooms. So i’ve created groups and assigned them to their corresponding items
Based on what item triggers the rule I need to grab that base name of that item so that I can manipulate it and use it to get and set states on other involved items
The items / groups:

Group gRoomTemps
Group gVentMode
Group gSetpoint
Group gUseHVAC

Members:
masterVentMode
masterSetpointPx
masterTemperaturePx
masterUseHVAC

nurseryVentMode
nurserySetpointPx
nurseryTemperaturePx
nurseryUseHVAC

(replace master/nursery with any other room name)
Member of gRoomTemps changed or
Member of gVentMode changed or
Member of gSetpoint changed or
Member of gUseHVAC changed

Since there are 4 groups (with their members) that can kick off the rule, how can I determine which room to use as the base name?

val baseName =
I could probably do it in some cobbled together way that takes up 20 lines but I’m sure there’s some incredibly simple way that I don’t know.

And the advantage of not doing it over PM is the whole community can find and benefit from the help. :slight_smile:

This is why I recommended to use “_” to separate the parts of Item names. If you use:

master_VentMode
master_SetpointPX
master_TemperaturePX
master_UseHVAC

nursery_VentMode
...

Then it becomes super simple:

val baseName = triggeringItem.name.split("_").get(0)

Without you need something ugly and unflexible like

val baseName = triggeringItem.name.replace("VentMode","").replace("SetpointPx","").replace("TemperaturePx","").replace("UseHVAC","")

And any time you add a new Item associated with that room you will need to update this Rule and another replace.

I made up a voice for you and while I composed this topic i said in your pretend voice those exact words

1 Like

What about

if ( triggeringItem.name.contains("Temp"))
    val baseName  = triggeringItem.name.replace("TemperaturePx","")
if (triggeringItem.name.contains("Set"))
   val baseName = triggeringItem.name.replace("Setpoint","")

etc
? that seems to work… Not terribly pretty not terribly ugly…

That would work. It takes 10x more lines of code and has the same unflexibility as chaining the replace. If you need to add more Items to this room, you will need to change the code.

lol thanks, i was halfway through it when you first replied. yours, as always, is a much better idea than mine.

say, how do i grab the first 3 letters of the name of an item in a group?

Scratch that, i found it in a thread about truncating text messages:

val whatever = triggeringItem.name.substring(0, 3)