If you are just getting started, I highly recommend starting with the JS Scripting add-on if you want to use JavaScript over the built in JavaScript. It provides a much more recent version of ECMAScript (11 vs. 5.1) and the helper library comes with it and is much better.
I usually let my rules drive my Groups when writing code like this. Without more specific details I couldn’t say if it’s the best or not.
Given your Item names, you may not need most if any of these Groups at all. openhab-js presents a nice interface to the ItemRegistry as your primary way to access Items. It doesn’t just inject them all into your rule like Rules DSL does.
So, if your naming is consistent you might be able to do something simpler like:
let setpointItem = items.getItem(event.itemName.replace('Calendar', 'Setpoint'));
You don’t need to mess with navigating a bunch of Groups for this. Just name your Items so that you can derive the names of all the associated Items given the name of any one of them. See Design Pattern: Associated Items.
Another approach you can use is to leverage the Semantic Model. Put the calendar Item as well as all the HVAC control and status Items for one HVAC into the same equipment Group (which is what you would want to do anyway if using the Semantic Model). Then you can use Semantics.getEquipment(items.getItem(event.itemName))
to get the parent Equipment Group and with that you’ll have all the Items that are a part of that equipment. See actions - Documentation.
In JS Scripting, they have gone to great lengths to only ever give you JavaScript Objects to deal with. So, according to the docs when you use items.getItem(itemName)
you get back a JavaScript Item Object. Calling .members
on that Item returns a JavaScript Array<Item>
which is an array of those JavaScript Objects. So you can and should use any standard JavaScript Array
operation to search through, sort, iterate, etc. the members of the Group. It’s all JavaScript. JavaScript Array Iteration