Access ItemChannelLinkRegistry.getBoundThings() from Textual Rule

  • Platform information:
    • Hardware: Raspberry Pi Model 3B+
    • OS: Linux devpi 5.10.103-v7+ #1529 SMP Tue Mar 8 12:21:37 GMT 2022 armv7l GNU/Linux
    • Java Runtime Environment: OpenJDK Runtime Environment Zulu11.54+25-CA (build 11.0.14.1+1-LTS)
    • openHAB version: 3.2.0 - Release Build

Like a number of people on this forum, I want to check that a Thing associated with an Item is ONLINE. I understand that an item can link to multiple Things but in my case (tuya switch) they don’t. I have read a few posts on this forum but they are not textual rules-based answers (and I can’t figure it out).

I have tried this:

import org.osgi.framework.BundleContext.getService

rule "Test"
when
        Time cron "0,15,30,45 * * ? * * *" // Check every 15 seconds
then
        var itemChannelLinkRegistryService = getService("org.openhab.core.thing.link.ItemChannelLinkRegistry")
        logDebug("MP","itemChannelLinkRegistryService=" + itemChannelLinkRegistryService)
        var myThings=itemChannelLinkRegistryService.getBoundThings("test-thing")
        logDebug("MP","myThings=" + myThings)
end

but that results in:

2022-06-14 16:55:45.594 [DEBUG] [org.openhab.core.model.script.MP    ] - itemChannelLinkRegistryService=null
2022-06-14 16:55:45.597 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'al-1' failed: 'getBoundThings' is not a member of 'org.openhab.core.persistence.PersistenceService'; line 10, column 15, length 59 in al

Clearly, the getService function is failing to return anything. Can someone please tell me what I am doing wrong? Thanks.

Have you seen this post?

Yes, I’ve read that post but I’m not sure how that helps me determine the list of Things that an Item is linked to. Am I missing something?
I am trying to write a cron-based rule that loops through a group of Items and makes sure their linked Things are all online.
The ‘app’ I am writing allows the Thing names to be flexible. I know what the Item names are but not what the Thing names are (so I can’t write a trigger-based rule for the Things). Hope that makes sense.

From what I know you can’t do this using Rules DSL.

You’ll have to write this in one of the other languages like JS Scripting or jRuby. In JS Scripting you’d be able to use osgr.getService('org.openhab.core.thing.link.ItemChannelLinkRegistry');.

This smells a whole lot like an XY Problem though.

Look closer at error message again. It says:

Can’t call getBoundThings cause it is not member of org.openhab.core.persistence.PersistenceService

This means that service you got from getService call is not one which you asked for. Dunno exact syntax but you might need a class object or a filter or something yet else. Clearly you got a service which does not match your input!

I think the previous log message is more pertinent - itemChannelLinkRegistryService=null.
This indicates the getService call returned null.
I can only assume that OH has defaulted to the PersistenceService when I effectively called null.getBoundThings("test-thing").