# Can I get the Thing UID that is linked with an Item

**URL:** <https://community.openhab.org/t/can-i-get-the-thing-uid-that-is-linked-with-an-item/126089>\
**Category:** Beginners\
**Tags:** jython, oh3\
**Created:** [August 31, 2021, 10:08pm UTC](https://community.openhab.org/t/can-i-get-the-thing-uid-that-is-linked-with-an-item/126089 "2021-08-31T22:08:01Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![Mr-JR](https://community.openhab.org/user_avatar/community.openhab.org/mr-jr/32/75347_2.png) [@Mr-JR](https://community.openhab.org/u/Mr-JR)\
**Post date:** [September 1, 2021, 5:47pm UTC](https://community.openhab.org/t/can-i-get-the-thing-uid-that-is-linked-with-an-item/126089/5 "2021-09-01T17:47:59Z")

</div>

For anyone else that wants to try this, here is code that can be placed in your library that implements a get\_thing() function … major limitation: it only returns the first, if any, Thing UID that is associated with an Item also might not work for all Thing binding types

```auto
"""
This module allows discovery of first Thing UID for given Item.
"""

try:
    import typing as t
    if t.TYPE_CHECKING:
        try:
            from org.openhab.core.thing.link import ItemChannelLinkRegistry, ManagedItemChannelLinkProvider
        except:
            from org.eclipse.smarthome.core.thing.link import ItemChannelLinkRegistry, ManagedItemChannelLinkProvider
except:
    pass

from core import osgi
from core.log import getLogger
from core.utils import validate_item #, validate_channel_uid

ITEM_CHANNEL_LINK_REGISTRY = osgi.get_service(
        "org.openhab.core.thing.link.ItemChannelLinkRegistry"
    ) or osgi.get_service(
        "org.eclipse.smarthome.core.thing.link.ItemChannelLinkRegistry"
    ) # type: ItemChannelLinkRegistry

LOG = getLogger(u"get_thing")

def get_thing(item_or_item_name):
    """
    This function returns the first Thing UID from an Item using a
    ManagedItemChannelLinkProvider.

    Args:
        item_or_item_name (Item or str): the Item object or name to create
            the Link for

    Returns:
        Thing or None: the Thing UID or None
    """
    try:
        item = validate_item(item_or_item_name)
        if item is None:
            return None

        things = ITEM_CHANNEL_LINK_REGISTRY.getBoundChannels(item.name)
        if len(things) > 0:
            Thing,Channel = str(things.pop()).rsplit(":",1)
            return Thing
        else:
            return None
    except:
        import traceback
        LOG.warn(traceback.format_exc())
        return None

```

And here is what I used to test it:

```auto
import personal.get_thing
reload(personal.get_thing)
from personal.get_thing import get_thing

from org.openhab.core.thing import ThingUID

from org.slf4j import LoggerFactory
from configuration import LOG_PREFIX
log = LoggerFactory.getLogger("{}.test_get_thing".format(LOG_PREFIX))

def test_getthing(item):
    thing = get_thing(item)
    if (thing != None):
        log.info("Thing for item: " + item + " is: " + thing)
        log.info("Status for Thing is: " + str(things.get(ThingUID(thing)).status))
    else:
        log.info("No Thing found for item: " + item)

```

---

_[View the full topic](https://community.openhab.org/t/can-i-get-the-thing-uid-that-is-linked-with-an-item/126089)._
