# Natural Binding, Things and links to items discovery

**URL:** <https://community.openhab.org/t/natural-binding-things-and-links-to-items-discovery/101661>\
**Category:** Tutorials & Examples\
**Tags:** jsr223, jython, ngre\
**Created:** [July 1, 2020, 4:13pm UTC](https://community.openhab.org/t/natural-binding-things-and-links-to-items-discovery/101661 "2020-07-01T16:13:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![glhopital](https://community.openhab.org/user_avatar/community.openhab.org/glhopital/32/91632_2.png) [@glhopital](https://community.openhab.org/u/glhopital)\
**Post date:** [July 1, 2020, 4:13pm UTC](https://community.openhab.org/t/natural-binding-things-and-links-to-items-discovery/101661/1 "2020-07-01T16:13:33Z")

</div>

In order to ease items file definitions, did I propose this tools that create a group for each installed binding, containing a group for each thing defined, containing the list of linked items :

```auto
 from core.log import logging, LOG_PREFIX, log_traceback
from core.items import add_item, remove_item
from core.utils import validate_item
import requests
import sys

# Modify these variables to fit your system
URL_OPENHAB = "http://localhost:8080"

bindingTag = "BindingDiscovery"

def normalizeBindingId(id):
	id = "g" + id.capitalize()
	return id

def cleanBindingName(name):
	name = name.replace("Binding","").replace("Extensions","").replace("Extension","")
	name = name.strip()
	return name

def normalizeThingId(uid):
	groupname = []
	for element in uid:
		groupname.append(element.capitalize().replace("-","_"))
	groupname[0] = "g"
	return "".join(groupname)

def discoverLinks(log):
	log.info(u"Linked items discovery....")

	links = requests.get(url=URL_OPENHAB + "/rest/links").json()
	count = 0
	for link in links:
		count += 1
		channelUID = link["channelUID"]
		itemName = link["itemName"]

		thingUID = channelUID.split(":")
		del thingUID[-1]
		thingGroupName = normalizeThingId(thingUID)
		group = validate_item(thingGroupName)
		item = validate_item(itemName)

		if group != None and item != None:
			group.addMember(item)

	log.info(u"Discovered {} links.".format(count))

def discoverThings(log):
	log.info(u"Installed things discovery....")

	things = requests.get(url=URL_OPENHAB + "/rest/things").json()
	count = 0
	for thing in things:
		count += 1
		uid = thing["UID"].replace(":"," ").split(" ")
		bindingId = normalizeBindingId(uid[0])
		id = normalizeThingId(uid)
		name = thing["label"]

		add_item(id, item_type="Group", label=name, groups=[bindingId], category=uid[-1], tags=[bindingTag])

	log.info(u"Discovered {} things.".format(count))

def discoverBindings(log):
	log.info(u"Installed bindings discovery.....")

	bindings = requests.get(url=URL_OPENHAB + "/rest/bindings").json()
	count = 0
	for binding in bindings:
		count += 1
		id = normalizeBindingId(binding["id"])
		name = cleanBindingName(binding["name"])
		add_item(id, item_type="Group", label=name, groups=["gBindings"], category=binding["id"], tags=[bindingTag])
	
	log.info(u"Discovered {} bindings.".format(count))

def scriptLoaded(id):
	log = logging.getLogger("{}.autoDiscovery".format(LOG_PREFIX))
	try:
		log.info("Removing all auto generated groups and items...")
		for item in ir.getItemsByTag(bindingTag):
			remove_item(item)

		log.info("Creating top level group...")
		add_item("gBindings", item_type="Group", label="Installed bindings", groups=["gSystem"], category="api",tags=[bindingTag])

		discoverBindings(log)
		discoverThings(log)
		discoverLinks(log)

	except:
		log.error(sys.exc_info())

```

Hope this can help somebody

---

<div class="post-metadata">

**Author:** ![system](https://community-openhab-org.s3-eu-central-1.amazonaws.com/original/2X/6/6a52f2ac5d00e4c7b50471a354b3a9221188be90.png) [@system](https://community.openhab.org/u/system)\
**Post date:** [August 12, 2020, 8:13am UTC](https://community.openhab.org/t/natural-binding-things-and-links-to-items-discovery/101661/2 "2020-08-12T08:13:34Z")

</div>

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.
