Try to use my OH2 rules on OH3.
But I receive the error “The name ‘ScriptServiceUtil’ cannot be resolved to an item”
Is this no more supported?
Toni
It’s still there. How are you trying to use it? What language?
Hi Rich, I’m using old fashioned DSL. “ScriptServiceUtil” is imported at start of file.
rule Test
when
Time cron "0,5,10,15,20,25,30,35,40,45,50,55 * * ? * *"
then
gRLall.members.forEach[GroupItem actRLgroup |
logInfo("Test", "Hallo")
val String actGrpName =actRLgroup.name.substring(1,4)
val SwitchItem actItemSkip = ScriptServiceUtil.getItemRegistry?.getItem(actGrpName+"_Skip") as SwitchItem
]
end
And more simplyfied:
import org.eclipse.smarthome.model.script.ScriptServiceUtil
rule Test
when
Time cron "0,5,10,15,20,25,30,35,40,45,50,55 * * ? * *"
then
val SwitchItem actItemSkip = ScriptServiceUtil.getItemRegistry.getItem("RL1_Skip") as SwitchItem
end
Don’t you see an error for that import?
As was mentioned in the breaking changes when OH 3 was released, there is no more org.eclipse.smarthome anywhere in openHAB any longer. Usually you can replace that with org.openhab.
import org.openhab.model.script.ScriptServiceUtil
Sorry - missed that. But still the same problem:
import org.openhab.model.script.ScriptServiceUtil
rule Test
when
Time cron "0,5,10,15,20,25,30,35,40,45,50,55 * * ? * *"
then
val SwitchItem actItemSkip = ScriptServiceUtil.getItemRegistry.getItem("RL1_Skip") as SwitchItem
end
Script execution of rule with UID ‘test-1’ failed: The name ‘ScriptServiceUtil’ cannot be resolved to an item or type; line 8, column 31, length 17 in test
- org.openhab.core.model.script.ScriptServiceUtil
It’s missing the “core” part.
When in doubt, check the JavaDocs.
http://www.openhab.org/javadoc/latest/org/openhab/core/model/script/scriptserviceutil
Sorry, I have the same problem and do not understand how to fix this.
Once again, with the correct path
import org.openhab.core.model.script.ScriptServiceUtil
Oh, sorry, I tried to understand the JavaDocs… Thank you very much Rich
Rich, thanks for your help
I have a similar error trying to import ScriptServiceUtil in my DSL rule :
Script execution of rule with UID 'c6a1251eab' failed: ___ import ___ org.openhab.core.model.script.ScriptServiceUtil
This is the line that I copied from the conversation:
import org.openhab.core.model.script.ScriptServiceUtil
I still get the same error.
I’m on 3.0.1
Any suggestions?
Thanks!
But where did you put that line?
You can’t do imports inside the body of a rule.
So, in a xxx.rules file you put them standalone at the head of the file.
In a UI entered rule, there is no access to “outside the body”. You can’t do imports in a UI DSL rule.
I put that line at the very top of the rules script in the UI. So does this mean that if I need to access “ScriptServiceUtil” I’ll have to use an external rules file?
Thanks,
Bjarne
No, you can still use stuff without importing them. You just have to use the full name. Where ever you use ScriptServiceUtil
you would use org.openhab.core.model.script.ScriptServiceUtil
instead.
Just tried. It worked (of course)… I do feel a bit dirty not importing my references up front. I assume I would need to move the JavaScript or Python to do that or is this a “UI Rules Engine” thing?
Thanks!
Both Python (from x import y
) and JavaScript (Java.type("org.openhab....")
) allow the imports to occur anywhere, not just the top of the file. So you can import anything at any point in the code.
However, in Rules DSL the main reason to pull the ScriptServiceUtil is to gain access to the ItemRegistry. In JavaScript and Python, the ItemRegistry is already available in the rule as the variable ir
. Furthermore, in JavaScript and Python you can get any Item’s state using just that Item’s name as a String using the items
dict.
var MySwitchState = items["Foo_"+event.itemName]
So in practice about the only time you need to access the ItemRegistry is when you want to get the members of a Group or other things that require actually having the Item Object to accomplish.
I guess that is one more reason to start using JavaScript or Python.
Additional question: How would I turn debugging on when testing rules built in the UI?
Thanks!
The same way you do now. Modify the log4j.xml file or use the karaf console to change the logging level for the loggers you are using to DEBUG.