Get item's linked thing programmatically?

Is there a way of getting the thing that is linked with an item programmatically in a rule?

E.g. you have a switch that triggers a rule and you want to do something with the thing that the switch belongs to, but you have multiples of these kinds of things and their switches and you don’t want to write a separate rule for each and every single one.

Whatever you are trying to achieve, it is likely some kind of kludge.

There are no simple tools for traversing the hierarchy. But if the GUI can do, so can you.
Remember that starting with an Item, the next step is the channel link - you can’t go directly to Thing. Remember also that this will be a List - Items may link to multiple channels.

The hierarchy is mapped out in internal registers, so that’s where you must look.
Old OH2 posts, but show the kind of methods -

Newer -

Keyword ItemChannelLinkRegistry

I did explain exactly what I am trying to achieve: using a single rule, instead of having to write a rule for each and every item/thing separately. It’s silly and wasteful to duplicate the same rule over and over and over with only the Thing that the code refers to being the difference.

Yes in general, but not in this case.

Anyway, doesn’t look like there’s a good way of doing it, which is a shame.

Did you check the following link @rossko57 posted :

This shows a way how to get the thing an item is linked to. Isn’t that what you want ?

Yes, but it’s ugly and I’d have to learn to use this Jython-thing, which is why I said there’s apparently no good way for it. I did bookmark the link, but I’ll decide later, if I wanna get into Jython.

All of the different rule languages communicate with ‘native’ openHAB through Java objects, the same objects are available to each language.
So you might see in Jython …
from import ItemChannelLinkRegistry ...
a big clue to how to do thesame kind of thing in some other language, so long as you prepared to put more effort in than copy-paste.