Osgi.*, java.* not working in a Rule

  • Platform information:
    • Hardware: armv7l, 2GB, 32GB sdcard
    • OS: Linux openhabian 5.10.103-v7l
    • Java Runtime Environment: OpenJDK Runtime Environment (build 11.0.18+10-post-Raspbian-1deb10u1)
    • openHAB version: 3.4.2 Release
  • Issue of the topic: I want to do changes on “Things” and therefore want to use a call like:
osgi.getService('org.openhab.core.thing.ThingManager')

But I always get an error log when executing the rule like:

Script execution of rule with UID 'solar-5' failed: The name 'osgi' cannot be resolved to an item or type

I tried using: “import org.osgi.framework” in front of rule file. But no success

The same problem occurs with any other example code using “Java.type(…)” calls telling me Java not known.

Strange thing is, that in VS Code all the autocompletion stuff on these work beatiful, suggesting proper methods etc. But my rules do not know these :frowning:
Thanks for helping me on a such a basic question!

Which rules add-on? If Nashorn (ECMAScript 5.1), see JSR223 Scripting | openHAB. The ThingRegistry is imported by default as things.

If GraalVM JS Scripting, see JavaScript Scripting - Automation | openHAB . In that the ThingRegistry is available through the built in helper library.

The two are very different environments.

I am confused, I not use scripts but rules. In openhab UI under rules it says:
application/vnd.openhab.dsl.rule
I have not installed any seperate plugin for running scripts.
So how do I find out which way works for me?
Thx

The code you are using isn’t Rules DSL. It’s not going to work. As far as I’m aware, the ThingRegistry is not available in Rules DSL.