How to get Thing uuid from Item name in JS Scripting?

A quick and easy work around that doesn’t take digging into the API is to add the Channel ID as a tag or Item metadata. Obviously this is a workaround, not a permament solution, but it can get you past this hurdle for now.

I personally configure a Status Item on the equipment and use that. I use Threshold Alert and Open Reminder [4.0.0.0;5.9.9.9] and a rule that triggers on Thing status changes to update that Status Item. Then I use the semantic part of the Item class to get to and check the status Item in my rules. As a bonus I have an oh_repeater widget that shows my all the equipment that is offline using that status Item.

To import and use the ItemChannelRegistry in your rule you can use the following code:

var itemChannelLinkRegistry = require('@runtime/provider').itemChannelLinkRegistry;
var allLinks = utils.javaSetToJsArray(itemChannelLinkRegistry.getAll());
allLinks.map(l => l.toString()).filter(l => l.startsWith('AdGuard_Protection')).map(l => l.split(' -> ')[1]).forEach(l => console.info(l));

The first line imports the registry.

The second line gets all the links. I couldn’t get the native methods on ItemChannelLinkRegistry to work but we can use the more generic methods for now. We use the utils to convert the Java Set to a JS Array to make things easier.

The last line converts all the links to Strings of the format <Item_Name> -> <Channel UID>. Then we filter down to just those that mention the Item (in this case 'AdGuard_Protection`) and finally we split those and extract just the Channel UID and log them out one by one.

Everything up to the last “:” is the Thing ID.

What happens if there is more than one Channel linked to the Item?

Possibly. That doc is automatically generated based on the comments in the source code. The source code does have comments so that class should have generated the blank page. Something might be wrong with the source code to documentation generation.

But that ItemChannelLink class wouldn’t have been useful in this case because the getChannelLink() method requires both the Item name and the Channel UID and you need both.

I could be worth filing an issue to add the above as a method to the Item to more easily get the links and things from the Item as well, though there might already be an issue open for that. I remember seeing something like that go by.