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.

Didn’t find anything in https://www.openhab.org/addons/automation/jsscripting/#things which leads me back to the question: what scripting engine to I have to include with OpenHab 4.2.2 in order to prevent the error:
Script execution of rule with UID ‘Tibberswitch-1’ failed: The name ‘osgi’ cannot be resolved to an item or type
I want to use var thingMgr = osgi.getService(‘org.openhab.core.thing.ThingManager’) in a rule but cannaot figure out the required package to make this work.

You need to step back.

If the ID of the rule is Tibberswitch-1 that means you’ve defined the rule in a file named Tibberswitch.rules under `$OH_CONF/rules. Only Rules DSL is supported in this type of file in this location. Everything you need to know to write rules here is documeted at Textual Rules | openHAB.

osgi.getService doesn’t exist in Rules DSL. I’m not sure if you can get access to the ThingManger in Rules DSL.

If you want to use JS Scripting, you must follow the requuirements for that language which are documented at JavaScript Scripting - Automation | openHAB. The tl;dr is you must define the rules using rule builder or JSRule in a file ending in .js at $OH_CONF/automation/js.

What are you looking for there?

What are you trying to accomplish? You don’t need osgi to access Things in JS Scripting. For example, to get all the Things that are OFFLINE:

var offlineThings = things.getThings().filter( t -> t.status == "OFFLINE");

things is your interface to the ThingManager. You already have access to it.

1 Like