Get 'thing' by it's UID or Thing Name

Guys,
Can we get a thing by it’s UID or Name?
I’ve looked on the forum and the closest I can get is this, posted in another thread:

import org.openhab.core.thing
import org.openhab.core.automation.thingsupport
rule "test switch"
when Item Test_Switch changed
then
    t = Things.getThing("hue:bridge-api2:xxx-xxx-xxx");
    logInfo("test",t.getLabel());
end

I am getting this validation error:

Script execution of rule with UID 'test-3' failed: 'getThing' is not a member of 'java.lang.Class<org.openhab.core.model.script.actions.Things>

Am I missing a library here?
Checking the docs I see that Things has no member named GetThing.
Is this even possible?

You’re looking for the ThingRegistry, which has a get method.

'ThingRegistry' cannot be resolved to an item or type

Do I need some add-on that I don’t have? I just tried upgrading everything to it’s latest version (with config tool) and I still get that error.

RulesDSL (the script language you are using if you are writing rules with the rule...when...then... structure, makes getting to the various registries a multi-step process. You also need ScriptServiceUtil which will then get you an instance of a thing registry. The get method of ThingRegistery also requires a ThingUID, which you will have to construct from a string. So you’ll need two imports, something like this:

import org.eclipse.smarthome.model.script.ScriptServiceUtil
import org.eclipse.smarthome.core.thing.ThingUID
tr = ScriptServiceUtil.getInstance.thingRegistry
t = tr.get(new ThingUID("hue:bridge-api2:xxx-xxx-xxx"))

Just copied and pasted your code, I get this error:

04:26:22.202 [ERROR] [.internal.handler.ScriptActionHandler] - Script execution of rule with UID 'test-3' failed: The name 'ScriptServiceUtil' cannot be resolved to an item or type;

Additionally, how else can I do rules via script files?

I am literally willing to rewrite all my rules just to not have to use this awful scripting language. It’s like 10 lines of code = 10 hours of google. Every time.

Sorry about that, I haven’t used DSL in forever, it looks like I copied over some o my very oldest rules and didn’t change out the java path. The org.eclipse.smarthome stuff is long gone. The proper paths should be with org.openhab.core such as org.openhab.core.model.script.ScriptServiceUtil.

RulesDSL is, at this point, the least powerful of the scripting languages so if you’re trying to do anything at all complex it can get pretty tough (if not actually impossible), absolutely.

The good news is that you have several very good options. At this point the Blockly editor if very full featured. JSScripting (you will need the add-on installed if it is not) and JRuby (also add-on needed) can be done in files or in the UI and have extensive helper libraries for making things like this task easier. For example, the JSScripting docs are here:

https://openhab.github.io/openhab-js/index.html

And there’s a things object with a getThings method built right in to the helper library:

https://openhab.github.io/openhab-js/things.html#.getThing

The JRuby docs are here:

I don’t use JRuby, but I’m sure there’s a very simple thing interface as well.

If python’s you speed then you should look into HABapp.

Ok, changed the namespace and now I get:

Script execution of rule with UID 'test-1' failed: An error occurred during the script execution: Couldn't invoke 'assignValueTo' for feature JvmVoid:  (eProxyURI: test.rules#|::0.2.0.2.0.0::0::/1) in test

Couldn’t find any ‘JSS Scripting’ add on, I tried installing the openhab-js, no difference that I can see. Although I didn’t have the same options in my setup that the guide was referring to (disable cache, for example).

So are you saying there is a language I can use that still goes in a rules file?

There any tutorials on this that you know of?

I will literally try anything else.

Not sure what you are doing, but click on the Automation tab when you are looking for Add-ons.
You will need to follow the instructions - you cannot use .rules files anymore. The extension and path is different or better switch to GUI based scripts. It is all documented in the docs.

I’m trying to get a namespace to work, it really shouldn’t be this hard.

But I could still use samba shares to get to them?

What docs? What language?

Use JRuby

rule "test switch" do
  changed Test_Switch
  run do
    logger.info things["hue:bridge-api2:xxx-xxx-xxx"].label
  end
end

But jruby can also give you the thing that an item is linked to like this

logger.info Test_Switch.thing.label
1 Like