Yes it is still this way in OH 4 for Rules DSL. In all the other rules languages to include Blockly, the ItemRegistry is available and can be accessed without importing ScruiptServiceUtil to get it. In some languages, like JS Scripting, the only way to get an Item is through it’s string name.
Performance shouldn’t be an issue either way.
That’s not how it’s implemented in Rules DSL. Rules DSL uses the raw ItemRegistry which throws an ItemNotFoundException if the Item doesn’t exist. Other languages have different ways to handle it but most throw an exception.
If you want to catch the exception, you can use a try/catch
try {
val testItem = ScriptServiceUtil.getItemRegistry.getItem("Virtual_Switch_1")
// the rest of the code that uses testItem
} catch(org.openhab.core.items.ItemLookupException e) {
// what to do if the Item doesn't exist
}
I’m not sure what you’d do in the catch clause beyond logging that the Item doesn’t exist, which is going to happen anyway if you don’t catch the exception.
All things considered, if you are doing stuff like this it might be a sign you’ve outgrown the capabilities of Rules DSL. You might consider moving new development to another language such as Blockly, JS Scripting or jRuby.
Yes I know. JS would be my prefered language. But I have to wait always about 30s for compilation on my Raspberry 3. This is the main reason why I don’t want to develop with JS and will stay at the moment with DSL.
I would also prefer JS over DSL, but was put off when it was first introduced around v3. That is of course some time ago, so can you recommend any good guides to get started with JS?
thanks
Converting all DSL rules to js was easier than I thought as the code is very similiar to js.
I converted around 20 rules within a couple of hours.
If you change the syntax for send/postcommand, getstate and loginfo, you probably have done already 80% of the conversion.