[Rules DSL] Get item from string name!

I’m curious to see an example where other means could not be used to ensure that the Item exists. Using the ItemRegistry may be the easiest, but it comes with a performance hit, so it would be best to find another way. That said, there are other useful ItemRegistry methods. In Jython, I use this to check for the existence of an Item…

if ir.getItems("Current_Timestamp") == []:

This is not used in a rule, but in a script that is creating Items if they do not already exist. This uses ItemRegistry.getItems() (note the ‘s’), which returns a collection of Items matching a regex. The regex being a literal string. The same could be done in a DSL rule using…

if (ScriptServiceUtil.getItemRegistry.getItems("item_name_to_check_for_existence") == newArrayList) {

This could potentially be used as another workaround for checking existence of an Item given an Item name as a string. For example…

val test = ScriptServiceUtil.getItemRegistry.getItems("bad_item_name")
if (test == newArrayList) {
    logInfo("Rules", "Bad item")
} else {
    logInfo("Rules", "Test [{}]", test.get(0).name)
    test.get(0).sendCommand(ON)
}
2 Likes