# How do i get the linked item from channel

**URL:** <https://community.openhab.org/t/how-do-i-get-the-linked-item-from-channel/102865>\
**Category:** Scripts & Rules\
**Created:** [July 27, 2020, 3:06pm UTC](https://community.openhab.org/t/how-do-i-get-the-linked-item-from-channel/102865 "2020-07-27T15:06:25Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![lblabr](https://community.openhab.org/letter_avatar_proxy/v4/letter/l/57b2e6/32.png) [@lblabr](https://community.openhab.org/u/lblabr)\
**Post date:** [July 27, 2020, 3:06pm UTC](https://community.openhab.org/t/how-do-i-get-the-linked-item-from-channel/102865/1 "2020-07-27T15:06:25Z")

</div>

in a script i would like to get the item or itemname of the item linked to a named channel. is that possible ?

Description:  
I would like to write w generic rule for my energy optimzation. there are different optimisation assets. for the optimisation. for abtraction from the real assets i would like to store the additional informations in a optimsation thing (mqtt). i just would like to link the item needed to be written to an existing channel of the optimisation thing, so the connection is done. how do i get the information from the linked object in rules ? that i may update them, or may i write to the channel from rule ?

any hints are very welcome!

e.g.  
Thing:  
Battery (Sunspec)  
Items:  
Number PowerOutLimit\_Battery

Thing:  
Optimsation\_Battery (mqtt)  
Channel: Out  
LinkedItem PowerOutLimit

Item: LoadProfile

Rule:

- every 15min
- check setpoint from Loadprofile, write Setpoint to Item linked to Out-Channel

more or less somthing like this…

Missing:  
how to get linked Item

Thanks in advance

Lars

---

<div class="post-metadata">

**Author:** ![Bruce\_Osborne](https://community.openhab.org/user_avatar/community.openhab.org/bruce_osborne/32/38531_2.png) [@Bruce\_Osborne](https://community.openhab.org/u/Bruce_Osborne)\
**Post date:** [July 27, 2020, 3:19pm UTC](https://community.openhab.org/t/how-do-i-get-the-linked-item-from-channel/102865/2 "2020-07-27T15:19:10Z")

</div>

> [@lblabr](#):
>
> in a script i would like to get the item or itemname of the item linked to a named channel. is that possible ?

OpenHAB ( and any script) can only manage Items. It is recommended you name your Items intuitively.

Please study openHAB concepts.

---

<div class="post-metadata">

**Author:** ![lblabr](https://community.openhab.org/letter_avatar_proxy/v4/letter/l/57b2e6/32.png) [@lblabr](https://community.openhab.org/u/lblabr)\
**Post date:** [July 27, 2020, 3:24pm UTC](https://community.openhab.org/t/how-do-i-get-the-linked-item-from-channel/102865/3 "2020-07-27T15:24:07Z")

</div>

> [@Bruce\_Osborne](#):
>
> intuitively

is the problem … you may not be able for automated tasks anymore…

---

<div class="post-metadata">

**Author:** ![Bruce\_Osborne](https://community.openhab.org/user_avatar/community.openhab.org/bruce_osborne/32/38531_2.png) [@Bruce\_Osborne](https://community.openhab.org/u/Bruce_Osborne)\
**Post date:** [July 27, 2020, 3:26pm UTC](https://community.openhab.org/t/how-do-i-get-the-linked-item-from-channel/102865/4 "2020-07-27T15:26:12Z")

</div>

As long as you do not use the horrible Simple Mode you can do that.

Almost nobody here uses that mode.

---

<div class="post-metadata">

**Author:** ![rossko57](https://community.openhab.org/user_avatar/community.openhab.org/rossko57/32/70263_2.png) [@rossko57](https://community.openhab.org/u/rossko57)\
**Post date:** [July 27, 2020, 3:36pm UTC](https://community.openhab.org/t/how-do-i-get-the-linked-item-from-channel/102865/5 "2020-07-27T15:36:56Z")

</div>

There is some access to links and channels using REST API from rules.  
It’s not really clear how you would be finding out or gussing a channel name, that you couldn’t also do for Item name?

Bear in mind channels may be linked to no Items or several Items also.

---

<div class="post-metadata">

**Author:** ![lblabr](https://community.openhab.org/letter_avatar_proxy/v4/letter/l/57b2e6/32.png) [@lblabr](https://community.openhab.org/u/lblabr)\
**Post date:** [July 27, 2020, 3:40pm UTC](https://community.openhab.org/t/how-do-i-get-the-linked-item-from-channel/102865/6 "2020-07-27T15:40:21Z")

</div>

but still if i have pv-inverter, battery-inverter, pumps, i would like to have for each of the assets an loadprofile an 15 minutes basis, with mode, foreced mode and limits. i thought i hold the additional items in a special optimisation thing more or less related to the physical asset (thing) in the general rule i iterate over all optimsation things/item and do the neccesary tasks, depending on the loadprofile.i still need a way to connect energy managment assets linked with the physical assets… or tell the energy asset the relevant physical asset or item Out and In might be different datapoints on each energy asset.

what design do you prefer for such things ?

---

<div class="post-metadata">

**Author:** ![rossko57](https://community.openhab.org/user_avatar/community.openhab.org/rossko57/32/70263_2.png) [@rossko57](https://community.openhab.org/u/rossko57)\
**Post date:** [July 27, 2020, 3:48pm UTC](https://community.openhab.org/t/how-do-i-get-the-linked-item-from-channel/102865/7 "2020-07-27T15:48:34Z")

</div>

No idea what you are saying. Can you provide a sketchy example?

You can choose arbitrary names for your Items, but you can also choose names with meaning.  
Freds\_meter\_voltage  
Freds\_meter\_current  
It is easy to create a rule triggered from a change in current that can “find” the correct voltage Item, do sums with them, and post the result to an Item  
Freds\_meter\_kw  
Is that the concept you are looking for?

---

<div class="post-metadata">

**Author:** ![lblabr](https://community.openhab.org/letter_avatar_proxy/v4/letter/l/57b2e6/32.png) [@lblabr](https://community.openhab.org/u/lblabr)\
**Post date:** [July 27, 2020, 4:17pm UTC](https://community.openhab.org/t/how-do-i-get-the-linked-item-from-channel/102865/8 "2020-07-27T16:17:53Z")

</div>

no it’s more complicated…

i have think about energy prices on the market like epex, prices change each 15minutes. for that reason you need an optimzation of all energy assets for each specific 15 minutes…  
depending on prices self generation, prices you may need to operate your energy assets.. you self consumption might be not flexible over time but the pool pump or your ev might be. for each flexible assets it would like a specific logical layer for optimization calculations, the optimization calculations leeds into an load profile for each asset, with meta information about operating mode e.g. battery (on, off, auto, efficient, grid)  
an optimzation thing should hold the optimization informations, switch, items …

like (first shot): [https://public.grafana.seume70.efficient-energies.de/d/eOo-bd4Gz/90-batterieoptimierung-fahrplan-json?orgId=1](https://public.grafana.seume70.efficient-energies.de/d/eOo-bd4Gz/90-batterieoptimierung-fahrplan-json?orgId=1)

each 15 minutes load profile checked and related real asset controled …

---

<div class="post-metadata">

**Author:** ![rossko57](https://community.openhab.org/user_avatar/community.openhab.org/rossko57/32/70263_2.png) [@rossko57](https://community.openhab.org/u/rossko57)\
**Post date:** [July 27, 2020, 4:53pm UTC](https://community.openhab.org/t/how-do-i-get-the-linked-item-from-channel/102865/9 "2020-07-27T16:53:49Z")

</div>

Why do you think finding out which Item(s) are linked to a named channel will help? What associations are you trying to make/exploit? Are you familiar with the use of Item Groups in openHAB?

Bear in mind you can choose to name Items to reflect the name of a linked channel (like Simple mode does).  
Thus you could derive an Item name from a channel name by simple string substitution.

---

<div class="post-metadata">

**Author:** ![5iver](https://community.openhab.org/user_avatar/community.openhab.org/5iver/32/21201_2.png) [@5iver](https://community.openhab.org/u/5iver)\
**Post date:** [July 28, 2020, 8:36am UTC](https://community.openhab.org/t/how-do-i-get-the-linked-item-from-channel/102865/10 "2020-07-28T08:36:39Z")

</div>

It seems like an Item naming scheme is all you would need, although you may want to look into [metadata](https://openhab-scripters.github.io/openhab-helper-libraries/Python/Core/Packages%20and%20Modules/metadata.html) too. Since you asked, you can get the Items linked to a Channel using…

```auto
from core import osgi
from org.eclipse.smarthome.core.thing import ChannelUID

ITEM_CHANNEL_LINK_REGISTRY = osgi.get_service("org.eclipse.smarthome.core.thing.link.ItemChannelLinkRegistry")

for item in ITEM_CHANNEL_LINK_REGISTRY.getLinkedItemNames(ChannelUID("kodi:kodi:familyroom:control")):
    # do stuff with Item

```

With OH 3.0, you’ll need to replace org.eclipse.smarthome with org.openhab.

---

<div class="post-metadata">

**Author:** ![lblabr](https://community.openhab.org/letter_avatar_proxy/v4/letter/l/57b2e6/32.png) [@lblabr](https://community.openhab.org/u/lblabr)\
**Post date:** [July 28, 2020, 9:31am UTC](https://community.openhab.org/t/how-do-i-get-the-linked-item-from-channel/102865/11 "2020-07-28T09:31:08Z")

</div>

Hi Scott,

thank’s a lot, i alreaday have seen the metadata, but i need to think about, where to add the metadata…on the loadprofile item or somewhere else… i’m not clear at that point …

BR Lars

---

<div class="post-metadata">

**Author:** ![lblabr](https://community.openhab.org/letter_avatar_proxy/v4/letter/l/57b2e6/32.png) [@lblabr](https://community.openhab.org/u/lblabr)\
**Post date:** [July 28, 2020, 10:26am UTC](https://community.openhab.org/t/how-do-i-get-the-linked-item-from-channel/102865/12 "2020-07-28T10:26:30Z")

</div>

yes i’m familiar with groups more or less 😉 if i use the suggested simple mode on item linking, how do i get groups to automatic generated items ? is that possible ? maybe as default on the channel ?

---

<div class="post-metadata">

**Author:** ![rossko57](https://community.openhab.org/user_avatar/community.openhab.org/rossko57/32/70263_2.png) [@rossko57](https://community.openhab.org/u/rossko57)\
**Post date:** [July 28, 2020, 10:35am UTC](https://community.openhab.org/t/how-do-i-get-the-linked-item-from-channel/102865/13 "2020-07-28T10:35:33Z")

</div>

> [@lblabr](#):
>
> if i use the suggested simple mode on item linking,

I am not suggesting you use Simple Mode at all. It is a horror that will get in your way.

I was suggesting you copy its naming strategy only.  
Simple Mode ‘sees’ a Thing channel like  
**`solaredge:generic:PV:live#consumption`**  
and automatically creates and links an Item named  
**`solaredge_generic_PV_live_consumption`**

Simple Mode then hides this Item from you and prevents you editing it etc. which is why I do NOT suggest you actually use it.  
But there is nothing stopping you naming your own editable Items using the same convention.

But you can see that if you have the channel name, it is very easy to work out the associated Item name.  
I still have no idea why you want to do that, but there’s one way to do it.  
There are others, using REST API

---

<div class="post-metadata">

**Author:** ![lblabr](https://community.openhab.org/letter_avatar_proxy/v4/letter/l/57b2e6/32.png) [@lblabr](https://community.openhab.org/u/lblabr)\
**Post date:** [July 28, 2020, 10:40am UTC](https://community.openhab.org/t/how-do-i-get-the-linked-item-from-channel/102865/14 "2020-07-28T10:40:43Z")

</div>

sry, i didn’t used the simple mode in the past … i thought for correct naming of items, it would be better … but as you told it isn’t … so i just will following the naming convention

---

<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 28, 2020, 2:48pm UTC](https://community.openhab.org/t/how-do-i-get-the-linked-item-from-channel/102865/15 "2020-07-28T14:48:11Z")

</div>

You may find some hints [here](https://community.openhab.org/t/natural-binding-things-and-links-to-items-discovery/101661)

---

<div class="post-metadata">

**Author:** ![undef](https://community.openhab.org/letter_avatar_proxy/v4/letter/u/7feea3/32.png) [@undef](https://community.openhab.org/u/undef)\
**Post date:** [January 2, 2021, 9:55pm UTC](https://community.openhab.org/t/how-do-i-get-the-linked-item-from-channel/102865/16 "2021-01-02T21:55:47Z")

</div>

@glhopital , thanks for posting those script snippets.  
I’m especially interested in the discoverLinks function, but i’m running into problems immediately.

first this one:  
` failed: ImportError: No module named core in <script> at line number 1`  
and secondly, how would I call those functions from the the rule DSL?

What I’m trying to do is: retrieve a value from a channel, based on an item.  
e.g. I have an item, which has channel links to two sensor values (temperature and moisture). How could I use the values from the linked channel in a rule script?

thanks.

---

<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:** [January 4, 2021, 8:45am UTC](https://community.openhab.org/t/how-do-i-get-the-linked-item-from-channel/102865/17 "2021-01-04T08:45:37Z")

</div>

It may not work if you’re using OH3 because Jython Helper scripts are still not ported for this environment (see according post in the forum), so you may have to adapt it.  
From a script point of view, you can never get values from a channel, they have to be hosted by an item to make the value accessible.
