HABApp - streamlining rule development?

Hi all,

Been using HABApp and am very happy with it, but I recognize that my development processes are pretty rudimentary. I’m using PyCharm and using logging/frontail to troubleshoot.

I suspect that there are ways to execute code directly in PyCharm but not sure how to configure it.

Alternatively, it seems like I should be able to do directly in python from the command line having activated the virtualenv (I think) but not sure how to get HABApp to read the config file when imported. It doesn’t seem to connect to my openHAB instance.

Is there a guide for IDE novices out there that I can’t find, or any tips? I’d be happy to try to put together a guide as I get it figured out.

What you always can do is use pycharm to start HABApp and point it to a config file (-c switch).
Then you can debug your rules as usual.If you set read_only to true you can keep the new rules running alongside and see if there are any errors.

I also recommend to process errors that occur during the rules (as described in the docs) and push them e.g. to the mobile phone so you get at least some kind of notification that something went wrong.

Imho it comes down to the question with what you are struggling.
If it’s python syntax / compile errors then you could start writing test cases, use type hints, etc…
For me it’s 80% of the time logical errors (e.g. something doesn’t behave as I thought) and these errors are almost impossible to catch with tests so I do what every programmer does best:
deploy to production untested :wink:

1 Like

Thanks - I guess this is what I’m struggling to do. I don’t have HABApp installed on my Mac but I loaded the module in PyCharm per your setup guide so I get hints in the editor.

In PyCharm, if I go into console and run python it is aware that HABApp is installed but I don’t know how to feed it a config file. Same in the python console.

Would love to be a professional developer to be good at this, but just a hack!

You can start it like this

1 Like

Cool… I never would have figured that out. Either the guide was added after I got started or I was too naive to know what it was going to do when I first started.

A follow-up question:
it seems I should create a second config file to point to a development area so when run it doesn’t load up all my rules a second time, which I’ll do, but when launching it most of the scripts had errors for missing python dependencies that I’ve installed in the virtual environment. It appears to be set up to use the virtual environment’s interpreter, but do I need to install all those dependencies locally?

Ok, I’ve spent 2-3h trying to get this to work…

I can get HABApp running in debug mode…
I can look at the logs created as defined in the Configuration and see that it is connected properly, downloading things and items.
I can then spin up a python console and import HABApp
But when I try to get the value of an item, it gives an ‘item not found’ error in the console, and also in the debugger, so it is clear (I think) that it is talking to the proper HABApp instance.

But I can’t figure out why it doesn’t like an item name, pulled direct from one of my rules.

You need to install these dependencies in the virtual environment, too.


Can you start it with the “Play”/“Bug” Button next to the run configuration?
That way you are sure that you pass and load the correct configuration file.
There is no need to start it with the python console.

Is it an openhab item or a HABApp internal item?

Thanks @Spaceman_Spiff - I think I have it setup right as a configuration, as I get good logs when running it with the play or bug button. It doesn’t work if I set it to start with the python console (I think I need a working directory set).

It is an openhab item I am trying to connect to. Is there an easy way to see if it is connected to properly? For example, if I look under HABApp in Special Variables to the right of the console, and navigate to Config/location/elevation, should it show the value from my config.yml? If it should, it doesn’t (reads 0.0)

There is a log entry in the HABApp.log which shows how many items were loaded from openHAB.
That might be a start.

Yes. My guess is you created a dummy config somewhere and that’s what is getting loaded by HABapp and you are not connecting to openhab at all.

I see items and things discovered in the HABApp.log on my Mac at the location specified in the config.yml, so it appears the configuration, when run, loads correctly.

So is it possible that when I import HABApp in the python console that it uses a different config file?

I’ll have to poke around and see if I can find where that file would be…

here is a screenshot:

HABApp running in a window
Terminal to Mac showing HABApp writing to log files
Python Console running, using same interpreter
HABApp imported, but didn’t read config file

I must be missing something dumb…

Everything is fine.
You receive events before the items are fully synced (last two log lines).
Once the Items are synced the warnings are gone.

Your rules should only run when the sync is complete so this is no problem.

but I still can’t get a value of an openhab item from the python console…

/Volumes/openHAB-conf/venv/bin/python "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/pydevconsole.py" --mode=client --port=55396
import sys; print('Python %s on %s' % (sys.version, sys.platform))
sys.path.extend(['/Volumes/openHAB-conf'])
PyDev console: starting.
Python 3.8.2 (default, Jun  8 2021, 11:59:35) 
[Clang 12.0.5 (clang-1205.0.22.11)] on darwin
import HABApp
from HABApp.openhab.items import StringItem, GroupItem, SwitchItem, DimmerItem, NumberItem
DimmerItem.get_item('Hue_P8_BasementStairs_Brightness').get_value()
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Volumes/openHAB-conf/venv/lib/python3.8/site-packages/HABApp/core/items/base_item.py", line 25, in get_item
    item = HABApp.core.Items.get_item(name)
  File "/Volumes/openHAB-conf/venv/lib/python3.8/site-packages/HABApp/core/Items.py", line 28, in get_item
    raise ItemNotFoundException(name) from None
HABApp.core.Items.ItemNotFoundException: Item Hue_P8_BasementStairs_Brightness does not exist!

I can, however, create a rule in the development folder and it runs and can get values from openhab.

I just can’t interact from the console, which really was the goal…

How did you get the idea that this would work at all.
There are background tasks that have to be created (sse event reader, item sync, scheduler, etc).
In the console you just import a package.

You can use breakpoints etc there

just seemed like there was a lot of magic in the IDE that I hadn’t taken advantage of. I do a lot of my basic debugging in a python console and just thought I could import it and go like I do with other modules I’ve installed. I like being able to quickly modify a statement until I start getting the results I expect from the console, and a lot of my errors have to do with item states.

Seems like I’ll need to learn a bit more than I was expecting, but I suspect it will be more helpful in the long run. Thanks for bearing with me!

Could you provide an example?
PyCharm should already underline your statements red if they won’t work and show a red indicator in the scrollbar.

As I said, you can create breakpoints in the rule file and then inspect things with the variable explorer.
It should be even quicker as with the console.