So the ability to create Rules using the UI, the ability create Rules using Python, JavaScript, and Groovy, Node Red integration, HABApp, etc. are not improvements? The addition of Member of (which you are using here), triggeringItem, Item metadata, etc. were not improvements?
If you don’t want to write code, perhaps Node Red would be more appropriate for you. Or perhaps not upgrading would be a more appropriate choice. You can’t have improvements or new features or bug fixes with perfect backwards compatibility. IMO, the usability and capability of writing Rules in OH is hugely improved since 1.8.
It’s really hard to tell what this code does because of the lack of How to use code fences but this appears to just duplicate what events.log logs out. Maybe I don’t get it but it seems like a really complicated way to do:
grep 'MyItemName1\|MyItemName2` /var/log/openhab2/events.log
Which returns just those lines that reference MyItemName1 and MyItemName2. All this information already gets logged out by default and there are lots of tools available (there are powershell equivalents if you are on Windows) to only look at those lines that you care about at any given time.
There must be something I don’t understand here. I wouldn’t say it’s ugly but it is really not clear to me what this code does. It seems like it’s doing checks for things that are impossible. For example, the code keeps track of changes to the Item in a HashMap and then checks that HashMap later to see if the Item changed. But the Rule is triggered by a change. You would never have the case where the Rule triggered and the Item didn’t change state.
It looks like you are looping through all the members until you find a specific Item. Why not use filter? What’s the deal with the bitwiseAnd?
Maybe something like:
when
Member of SEC_AlarmMode changed // Member of is new since OH 1.8
then
val name = triggeringItem.name // triggeringItem is an implicit variable added since OH 1.8
// filter to get all the members whos bitwaiseAnd(1) is not 0 and loop
SYS_LogGroup.members.filter[ curLogGroup | (curLogGroup.state as Number).intValue.bitwiseAnd(1) != 0 ].forEach[ curLogGroup |
val logGroupName = curLogGroup.name.replace("SYS_LogGroup_", "")
// log if the triggeringItem is a member of curLogGroup, use the previousState implicit
// variable instead of keeping track of them in a hashmap
if(triggeringItem.groupNames.contains(logGroupName)){
logInfo("rules.sys." + name, "[" + logGroupName + "] " + name + " changed from " + previousState + " to " + triggeringItem.state) // previousState I think was around even in OH 1.8
}
]
end
It might make more sense if it’s broken into multiple lines:
when
Member of SEC_AlarmMode changed // Member of is new since OH 1.8
then
val name = triggeringItem.name // triggeringItem is an implicit variable added since OH 1.8
val activeLogGroups = SYS_LogGroup.members.filter[ curLogGroup |
(curLogGroup.state as Number).intValue.bitwiseAnd(1) != 0
]
activeLogGroups.forEach[ curLogGroup |
val logGroupName = curLogGroup.name.replace("SYS_LogGroup_", "")
// log if the triggeringItem is a member of curLogGroup, use the previousState implicit
// variable instead of keeping track of them in a hashmap
if(triggeringItem.groupNames.contains(logGroupName)){
logInfo("rules.sys." + name, "[" + logGroupName + "] " + name + " changed from " + previousState + " to " + triggeringItem.state) // previousState I think was around even in OH 1.8
}
]
end
I’m still not certain I get exactly what this does but I think the above code will do the same as your original code.
It might also be reasonable to go about it from the other direction.
val name = triggeringItem.name
// filter down to just those Groups that triggeringItem is a member of that
val activeLogGroups = triggeringItem.groupNames.filter[ grp |
SYS_LogGroup.members.findFirst[ logGrp |
(logGrp.state as Number).intValue.bitwiseAnd(1) != 0
]
]
activeLogGroups.forEach[ curLogGroup |
val logGroupName = curLogGroup.name.replace("SYS_LogGroup_", "")
logInfo("rules.sys." + name, "[" + logGroupName + "] " + name + " changed from " + previousState + " to " + triggeringItem.state)
]
I’m not 100% I got either of these exactly correct but they should show some ways to make the code more concise.