Habapp access openhab items generally

actually I’m searching for a way to access openhab items only by their name
without knowing what kind of item it is.

self.Presence = SwitchItem.get_item("home_presence")

That*s the normal way to access the home_presence item.

But actually I want access the item only by the name, so without the SwitchItem.
Then I want to figure out the item type (Switch, Dimmer, Roolershutter…) and
act with the item correctly.
So something like:

self.openhabitem = openhabitem.get_item("home_presence")

self openhabitem has then a description from the item ( type, etc)

Is there a way?

self.item = OpenhabItem.get_item("home_presence")

Have you set up your IDE so it does auto complete?
Then it’ll automatically suggest it when you type the imports.

i also know this:

self.item = Items.get_item("home_presence")


self.item = self.openhab.get_item("home_presence")

but to be honest i dont know whats the difference or when i have to use which one. only for example i use

self.item = self.openhab.get_item("home_presence").type

to find out if the item is a dimmer, number, …

1 Like

again (nearly same situation in the past ) it looks my Ide (pyCharm) to cause that Problems.
Even with the correct import for Openhabitem it will not run, and marks the call to openhabitem with a red underline.

If I try this form the console directly it works.

Yesterday with

self.Presence = SwitchItem.get_item("home_presence")

I only think that it will not work, because of the red underline and the Missing autocompletion.

Today in the terminal ( only venv started ) it works.

I removed HabApp from pyCharm and from the venv, reinstall it, and now it works.
Did someone has also Problems with pycharm?

But after that I have the same question as bastler.

So what is the diffrent and what the best to use?

self.item = Items.get_item("home_presence")

This will return a local HABApp Item (e.g. for sharing states between rule files or intermediate values).
It’s basivally a HABApp internal variable but with the benefit that you can trigger on it and it issues events.

This will send a query to openhab and return the response.
It’s almost always the one you should not use.

# generic openHAB item access
self.item = OpenhabItem.get_item("home_presence")
# typed item access
self.switch = SwitchItem.get_item('home_presence')

This is the correct one. It’s almost always the one you should use.
It returns the item from the HABApp item registry so there is no communication going on between HABApp and openHAB when you do this.
If you want to check if it’s a DimmerItem but you got it with OpenhabItem.get_item you can do:

if isinstance(self.item, DimmerItem):
    # is dimmer

ok so i used the “wrong” method most time :see_no_evil:

now i changed my rules all from self.openhab.get_item to OpenhabItem.get_item but then i see something strange:

it seems that all items in openhab that have no channel assigned (i call it virtual items) are not reported to habapp as long as they are not changed after a system reboot, even when they are persisted and have correct value in openhab.

example with this code:

    mode_value_a = NumberItem.get_item('iEK_Flur_PIr_Modus')
    self.log.debug('### mode_value_a: %s ###', mode_value_a)
    mode_value_b = Items.get_item('iEK_Flur_PIr_Modus')
    self.log.debug('### mode_value_b: %s ###', mode_value_b)
    mode_value_c = self.openhab.get_item('iEK_Flur_PIr_Modus')
    self.log.debug('### mode_value_c: %s ###', mode_value_c)
    mode_value_d = OpenhabItem.get_item('iEK_Flur_PIr_Modus')
    self.log.debug('### mode_value_d: %s ###', mode_value_d)

i have this in the log:

[2022-11-29 19:41:32,521] [My_HABApp]    DEBUG | ### mode_value_a: <NumberItem name: iEK_Flur_PIr_Modus, value: None, last_change: 2022-11-29T19:13:50.809196, last_update: 2022-11-29T19:13:50.809196> ###

[2022-11-29 19:41:32,522] [My_HABApp]    DEBUG | ### mode_value_b: <NumberItem name: iEK_Flur_PIr_Modus, value: None, last_change: 2022-11-29T19:13:50.809196, last_update: 2022-11-29T19:13:50.809196> ###

[2022-11-29 19:41:32,536] [My_HABApp]    DEBUG | ### mode_value_c: type='Number' name='iEK_Flur_PIr_Modus' label='Flur Lichtautomatik' category='pir' tags=[] link='http://localhost:8080/rest/items/iEK_Flur_PIr_Modus' state='2' groups=['iG_BtPersist'] members=[] transformed_state=None state_description=None command_description=None metadata={} editable=False group_type=None group_function=None ###

[2022-11-29 19:41:32,537] [My_HABApp]    DEBUG | ### mode_value_d: <NumberItem name: iEK_Flur_PIr_Modus, value: None, last_change: 2022-11-29T19:13:50.809196, last_update: 2022-11-29T19:13:50.809196> ###

… only the self.openhab.get_item reports the correct value

This was a copy and past error. I get the red underline with

self.Presence = OpenhabItem.get_item("home_presence")

But now after reinstall it works.

You wrote:

that openhabitem returns the item from the habapp registry.

But for my understanding, when is this transfered from openhab to the habapp item registry?
Is the copy to the habapp registry already done in the rule function?

   def __init__(self):

So during the rule init?

Middle section, so it’s not rule bound.