HABApp simple test getting Item fails

I tried this simple test and got an assertion error, the item exists on openhab. I have at the moment a read-only connection to openhab.

Switch ivetaAtHome "Iveta is at Home" (gAnyAtHome)

Any idea what I am doing wrong?

import logging
from HABApp.core.items import Item

log = logging.getLogger('MyRule') 

atHome = Item.get_item('ivetaAtHome')

log.info("Iveta at Home: {}".format(atHome))

[2023-07-30 15:03:21,651] [             HABApp.Rules]    ERROR | File "/usr/local/lib/python3.10/site-packages/HABApp/core/items/base_item.py", line 32 in get_item
[2023-07-30 15:03:21,651] [             HABApp.Rules]    ERROR | --------------------------------------------------------------------------------
[2023-07-30 15:03:21,651] [             HABApp.Rules]    ERROR |      24 | @classmethod
[2023-07-30 15:03:21,651] [             HABApp.Rules]    ERROR |      25 | def get_item(cls, name: str):
[2023-07-30 15:03:21,652] [             HABApp.Rules]    ERROR |      26 |     """Returns an already existing item. If it does not exist or has a different item type an exception will occur.
[2023-07-30 15:03:21,652] [             HABApp.Rules]    ERROR |      27 | 
[2023-07-30 15:03:21,652] [             HABApp.Rules]    ERROR |      28 |     :param name: Name of the item
[2023-07-30 15:03:21,652] [             HABApp.Rules]    ERROR |      29 |     """
[2023-07-30 15:03:21,652] [             HABApp.Rules]    ERROR |      30 |     assert isinstance(name, str), type(name)
[2023-07-30 15:03:21,653] [             HABApp.Rules]    ERROR |      31 |     item = get_item(name)
[2023-07-30 15:03:21,653] [             HABApp.Rules]    ERROR | -->  32 |     assert isinstance(item, cls), f'{cls} != {type(item)}'
[2023-07-30 15:03:21,653] [             HABApp.Rules]    ERROR |      33 |     return item
[2023-07-30 15:03:21,654] [             HABApp.Rules]    ERROR |    ------------------------------------------------------------
[2023-07-30 15:03:21,654] [             HABApp.Rules]    ERROR |      cls = <class 'HABApp.core.items.item.Item'>
[2023-07-30 15:03:21,654] [             HABApp.Rules]    ERROR |      type(item) = <class 'HABApp.openhab.items.switch_item.SwitchItem'>
[2023-07-30 15:03:21,654] [             HABApp.Rules]    ERROR |      item = <SwitchItem name: ivetaAtHome, value: None, last_change: 2023-07-29T22:35:54.849306, last_update: 2023-07-29T22:35:54.849306>
[2023-07-30 15:03:21,654] [             HABApp.Rules]    ERROR |      name = 'ivetaAtHome'
[2023-07-30 15:03:21,654] [             HABApp.Rules]    ERROR |    ------------------------------------------------------------
[2023-07-30 15:03:21,654] [             HABApp.Rules]    ERROR | 
[2023-07-30 15:03:21,655] [             HABApp.Rules]    ERROR | --------------------------------------------------------------------------------
[2023-07-30 15:03:21,655] [             HABApp.Rules]    ERROR | Traceback (most recent call last):
[2023-07-30 15:03:21,655] [             HABApp.Rules]    ERROR |   File "/usr/local/lib/python3.10/site-packages/HABApp/rule_manager/rule_file.py", line 78, in load
[2023-07-30 15:03:21,655] [             HABApp.Rules]    ERROR |     self.create_rules(created_rules)
[2023-07-30 15:03:21,655] [             HABApp.Rules]    ERROR |   File "/usr/local/lib/python3.10/site-packages/HABApp/rule_manager/rule_file.py", line 68, in create_rules
[2023-07-30 15:03:21,655] [             HABApp.Rules]    ERROR |     runpy.run_path(str(self.path), run_name=str(self.path), init_globals=rule_hook.in_dict())
[2023-07-30 15:03:21,655] [             HABApp.Rules]    ERROR |   File "/usr/local/lib/python3.10/runpy.py", line 289, in run_path
[2023-07-30 15:03:21,655] [             HABApp.Rules]    ERROR |     return _run_module_code(code, init_globals, run_name,
[2023-07-30 15:03:21,655] [             HABApp.Rules]    ERROR |   File "/usr/local/lib/python3.10/runpy.py", line 96, in _run_module_code
[2023-07-30 15:03:21,656] [             HABApp.Rules]    ERROR |     _run_code(code, mod_globals, init_globals,
[2023-07-30 15:03:21,656] [             HABApp.Rules]    ERROR |   File "/usr/local/lib/python3.10/runpy.py", line 86, in _run_code
[2023-07-30 15:03:21,656] [             HABApp.Rules]    ERROR |     exec(code, run_globals)
[2023-07-30 15:03:21,656] [             HABApp.Rules]    ERROR |   File "/habapp/config/rules/tests/test.py", line 8, in test.py
[2023-07-30 15:03:21,656] [             HABApp.Rules]    ERROR |     my_item = Item.get_item('ivetaAtHome')                           # This will raise an exception if the item is not found
[2023-07-30 15:03:21,656] [             HABApp.Rules]    ERROR |   File "/usr/local/lib/python3.10/site-packages/HABApp/core/items/base_item.py", line 32, in get_item
[2023-07-30 15:03:21,657] [             HABApp.Rules]    ERROR |     assert isinstance(item, cls), f'{cls} != {type(item)}'
[2023-07-30 15:03:21,658] [             HABApp.Rules]    ERROR | AssertionError: <class 'HABApp.core.items.item.Item'> != <class 'HABApp.openhab.items.switch_item.SwitchItem'>
[2023-07-30 15:03:21,660] [             HABApp.Rules]  WARNING | Failed to load /habapp/config/rules/tests/test.py!

i cannot explain why because i myself do not understand the difference but if you use

atHome = OpenhabItem.get_item('ivetaAtHome')

or

atHome = SwitchItem.get_item('ivetaAtHome')

your code will work.

of course then you have to import

from HABApp.openhab.items import SwitchItem, OpenhabItem

That helped a little bit.
But still this code:

import logging

from HABApp.openhab.items import SwitchItem

log = logging.getLogger('MyRule') 

atHome = SwitchItem.get_item('ivetaAtHome') 

log.info("Iveta at Home - is_on(): {}".format(atHome.is_on()))  
log.info("Iveta at Home - is_off(): {}".format(atHome.is_off()))  

gives this result (both times false):

[2023-07-30 19:18:04,415] [                   MyRule]     INFO | Iveta at Home - is_on(): False
[2023-07-30 19:18:04,416] [                   MyRule]     INFO | Iveta at Home - is_off(): False

strange!

When I just want to log the item and add

log.info("Iveta at Home: {}".format(atHome)) 

I get

[2023-07-30 19:27:29,110] [                   MyRule]     INFO | Iveta at Home - is_on(): False
[2023-07-30 19:27:29,110] [                   MyRule]     INFO | Iveta at Home - is_on(): False
[2023-07-30 19:27:29,127] [             HABApp.Rules]    ERROR | Error "__str__ returned non-string (type NoneType)" in load:
[2023-07-30 19:27:29,128] [             HABApp.Rules]    ERROR | Could not load /habapp/config/rules/tests/test.py!
[2023-07-30 19:27:29,128] [             HABApp.Rules]    ERROR | File "/habapp/config/rules/tests/test.py", line 11 in test.py
[2023-07-30 19:27:29,129] [             HABApp.Rules]    ERROR | --------------------------------------------------------------------------------
[2023-07-30 19:27:29,129] [             HABApp.Rules]    ERROR |       7 | atHome = SwitchItem.get_item('ivetaAtHome') 
[2023-07-30 19:27:29,129] [             HABApp.Rules]    ERROR |       9 | log.info("Iveta at Home - is_on(): {}".format(atHome.is_on()))  
[2023-07-30 19:27:29,129] [             HABApp.Rules]    ERROR |      10 | log.info("Iveta at Home - is_on(): {}".format(atHome.is_on()))  
[2023-07-30 19:27:29,129] [             HABApp.Rules]    ERROR | -->  11 | log.info("Iveta at Home: {}".format(atHome))  
[2023-07-30 19:27:29,129] [             HABApp.Rules]    ERROR |    ------------------------------------------------------------
[2023-07-30 19:27:29,130] [             HABApp.Rules]    ERROR |      atHome = <SwitchItem name: ivetaAtHome, value: None, last_change: 2023-07-29T22:35:54.849306, last_update: 2023-07-29T22:35:54.849306>
[2023-07-30 19:27:29,130] [             HABApp.Rules]    ERROR |      log = <Logger MyRule (DEBUG)>
[2023-07-30 19:27:29,130] [             HABApp.Rules]    ERROR |    ------------------------------------------------------------
[2023-07-30 19:27:29,130] [             HABApp.Rules]    ERROR | 
[2023-07-30 19:27:29,131] [             HABApp.Rules]    ERROR | --------------------------------------------------------------------------------
[2023-07-30 19:27:29,131] [             HABApp.Rules]    ERROR | Traceback (most recent call last):
[2023-07-30 19:27:29,131] [             HABApp.Rules]    ERROR |   File "/usr/local/lib/python3.10/site-packages/HABApp/rule_manager/rule_file.py", line 78, in load
[2023-07-30 19:27:29,132] [             HABApp.Rules]    ERROR |     self.create_rules(created_rules)
[2023-07-30 19:27:29,132] [             HABApp.Rules]    ERROR |   File "/usr/local/lib/python3.10/site-packages/HABApp/rule_manager/rule_file.py", line 68, in create_rules
[2023-07-30 19:27:29,132] [             HABApp.Rules]    ERROR |     runpy.run_path(str(self.path), run_name=str(self.path), init_globals=rule_hook.in_dict())
[2023-07-30 19:27:29,132] [             HABApp.Rules]    ERROR |   File "/usr/local/lib/python3.10/runpy.py", line 289, in run_path
[2023-07-30 19:27:29,132] [             HABApp.Rules]    ERROR |     return _run_module_code(code, init_globals, run_name,
[2023-07-30 19:27:29,132] [             HABApp.Rules]    ERROR |   File "/usr/local/lib/python3.10/runpy.py", line 96, in _run_module_code
[2023-07-30 19:27:29,133] [             HABApp.Rules]    ERROR |     _run_code(code, mod_globals, init_globals,
[2023-07-30 19:27:29,133] [             HABApp.Rules]    ERROR |   File "/usr/local/lib/python3.10/runpy.py", line 86, in _run_code
[2023-07-30 19:27:29,133] [             HABApp.Rules]    ERROR |     exec(code, run_globals)
[2023-07-30 19:27:29,134] [             HABApp.Rules]    ERROR |   File "/habapp/config/rules/tests/test.py", line 11, in test.py
[2023-07-30 19:27:29,134] [             HABApp.Rules]    ERROR |     log.info("Iveta at Home: {}".format(atHome))
[2023-07-30 19:27:29,134] [             HABApp.Rules]    ERROR | TypeError: __str__ returned non-string (type NoneType)

the first two lines are the expected behaviour:

because if the switch is off the is_on() will return false

dont know why after that comes an error, maybe thats from the following code you did not show here?

strange to see

it seems your item has the value none

so either your item is not initialized or for any reason the value was not passed to habapp

After restarting the HABApp container, it is doing as it should for the suggestion

This

Still throws an error:

Traceback (most recent call last):
  File "/habapp/config/rules/tests/test.py", line 9, in <module>
    atHome = Item.get_item('ivetaAtHome')
  File "/usr/local/lib/python3.10/site-packages/HABApp/core/items/base_item.py", line 32, in get_item
    assert isinstance(item, cls), f'{cls} != {type(item)}'
AssertionError: <class 'HABApp.core.items.item.Item'> != <class 'HABApp.openhab.items.switch_item.SwitchItem'>

It might be a documentation error as this is an example from:
https://habapp.readthedocs.io/en/latest/rule.html#interacting-with-items

No it’s not, it’s working as expected.

If you call get_item the item class has to match (or be a subclass).
HABApp.core.items.Item is a HABApp internal item to share data e.g. between rules.

So what you do is “I want to get an existing item with the following name and it should be a HABApp internal item”. This obviously fails because the item is an item from openHAB.

1 Like

At least it’s misleading!
The readthedocs documentation server is not a help at all, as searching seems not to work at all.
I was not able to find things like SwitchItem, OpenhabItem

To be clear I am not complaining.

I should have looked at the available source code.

Is this the only way to share data, or is there a possibility of “global” variables?

If you work through “getting started” everything is explained properly. :man_shrugging:

Yes - search is currently broken and I’m still waiting for the RTD-Theme bugfix.
Under openHAB you find the item definitions.

You can but only inside the same file.
I never use global variables because with the internal item you get

  • Logging on the event bus
  • All the triggers and events
  • Can use it across files

Which automatically leads to better structure and cleaner code

2 Likes

True!

And: the internal Items can have any Python type!!
hard to see in the examples though.
Great. I love it!

1 Like