HabApp Beginner's questions: Searchpath for modules?

I waited for the 1.0 release of habApp and installed it immediately after release.
I installed it twice, once on the same vm where openhab 3.3 is running, debian buster, headless. And then I installed it inside of PyCharm on a Macbook. I followed the docu on how to do that.
The idea was to use the IDE for development and then copy everything onto the production system. Same Python version on both machines, 3.10.5

I’m likely to make silly mistakes as I never used HabApp or PyCharm before, I’ve written things in python but mostly in editors like joe or nano or in VSCode (jython for OH-rules)

I now have the inconvenient situation that I have to use different name to access a module on the production install or the devel. install

So, on the PyCharm install the lib folder is at
/Users/dp/PycharmProjects/habApp/conf/lib/ and the rules sit at /Users/dp/PycharmProjects/habApp/conf/rules/

config.yml is at
/Users/dp/PycharmProjects/habApp/conf/config.yml

and it defines the directories like so:

directories:
  lib: lib
  rules: rules

Now I create a file test.py in the lib folder with a minimal content of just one variable,
toast = "taste"
Next I create demo.py in the rules folder and I start it with:
from test import toast
The IDE gives me a red underline below toast and hover says:
"Cannot find reference ‘toast’ in ‘_init_.py’ "

However, by trial and error I discovered that this works:
from conf.lib.test import toast
Now the popup on hover is glad to preview :

conf.lib.test
toast: str = "taste"

On the production install however it is exactly the other way, as expected test.py in the lib folder can be found with from test import toast and conf.lib.test is not a valid reference.

I could live with either but having to edit/change the imports for each rule file that goes from dev. to prod is irritating.

So I wonder if it’s just me, what kind of stupid mistake I may have made and if anyone has an idea to work around the confusion.

The import should work on both systems the same and the correct one is
from test import toast

PyCharm will propably give you issues but you can just add the lib folder to the source folders of the project. Then these errors will be gone.
Also note that lib is for static things so every time you change something you have to restart HABApp. You’ll also see a log entry that the path has been added to the system.


Typically a user library is not needed and you can get similar behavior by just creating a rule.
If you are a novice you probably don’t need it (even I don’t need it :smiley: ).

1 Like

thanks for the fast reply!

sorry, but: how exactly?

I already learned to restart HABApp after changes to the modul, I want it to hold a data structure which should survive changes in a rule and be available to different rules, too.

Just create an HABApp item for that. You can access it from every rule.
That’s what they are here for.

I found it in the preferences and now marked both lib and rules as source root. Both the normal import and the conf.lib. praefix get accepted as valid now so I could remove the freaks and I’ll keep my eyes open for whichever issues PyCharm will give me in return :wink:

Keeping the use of HABApp items in mind as a possible alternative.

Ah, talking about alternatives -

in OH/jython I can fine tune the trigger for a rule with something like
@when("Member of gPirStatesRooms received command ON")

but I failed to create an exact equivalent for it with HABApp, instead I ended with iterating over the members of the group

        for mbr in self.pirGroup.members:
            mbr.listen_event(self.checkMove, ValueUpdateEventFilter())

and filtering other commands with an if clause in the callback handler. Which get’s the job done but is more verbose and gives me the feeling I’m far from the best way to do it.

ValueUpdateEventFilter() will only trigger on ValueUpdateEvent, hence the name.
If you want to trigger on ItemCommandEvent you have to use the appropriate filter.

With HABApp there is no need to use groups since you can build the names dynamically before getting the item. Typically you would

  • pass the required items/item names into the rule as a parameter
  • stick to a naming scheme and only pass the prefix into the rule

If you insist on using groups your example would be how you use them.

1 Like