HABApp - Easy automation with openHAB

I think this is the guide most are using:

I used apt to install. The official download is here for other methods.

I have not tried that but it appears the API is using some sort of OAuth2 token based authentication.

"/auth/token":{"post":{
        "tags":["auth"],
	"summary":"Get access and refresh tokens.",
	"operationId":"getToken",
	"parameters":[{"name":"useCookie","in":"query","schema":{"type":"boolean"}}],
	"requestBody":{"content":{
		"application/x-www-form-urlencoded":{"schema":{
			"type":"object",
			"properties":{
				"grant_type":{"type":"string"},
				"code":{"type":"string"},
				"redirect_uri":{"type":"string"},
				"client_id":{"type":"string"},
				"refresh_token":{"type":"string"},
				"code_verifier":{"type":"string"}
		}}}
	}},
	"responses":{"200":{"description":"OK"}}
}},

@Spaceman_Spiff

I just started testing with a clean OH 2.5.9 install on my test Pi with HABApp 0.15.2 & Python 3.7.3. The first simple rule in the docs gives an error.

(habapp) root@raspberrypi:/opt/habapp/config/rules# python first.py
Traceback (most recent call last):
  File "first.py", line 16, in <module>
    MyFirstRule()
  File "first.py", line 6, in __init__
    super().__init__()
  File "/opt/habapp/lib/python3.7/site-packages/HABApp/rule/rule.py", line 43, in __init__
    __vars = sys._getframe(depth).f_globals
ValueError: call stack is not deep enough
(habapp) root@raspberrypi:/opt/habapp/config/rules#

If you have made the installation you have to start HABApp, not execute the rule directly.
HABApp will automatically load the rule.
You can observe this in the logs accordingly.

HABApp was running. Does it need to be restarted to pick up new rules?

Is this primarily for rules or to replace other text files such as Items and sitemaps?

If HABApp is started it will automatically load and execute the rules located in the rules folder (which is configured in the config.yml and typically a different folder than the rules folder from openhab) You just drop or edit the files in the folder - thatā€™s it.
In the HABApp.log you can observe how things are loaded/unloaded.

There is no possibility to create sitemaps, but you can replace many *.items files and definitely all rule files with it.
It is mainly intended as a python rule engine, item and thing interaction is just a bonus.

Example with default configuration:
I started HABApp with /opt/habapp/bin/habapp -c /opt/openhab/conf/habapp.
Now my rules are in /opt/openhab/conf/habapp/rules (and subfolders).
I just edit or copy them into the folder and they get picked up automatically as soon as they are changed.
In /opt/openhab/conf/habapp/log I can find all relevant log files and observe what is happening.

1 Like

Thanks.

It appears when starting with systemd it is starting but the logs are not updating. The config path is correct.

Troubleshooting now. Likely a local issue.

Make sure the /opt/habapp/bin/habapp is existing before the start because it will not get created.
But if it doesnā€™t exist HABApp will throw an error message and not start.

Look into the created config.yml and logging.yml to make sure the folder entries are correct.

It initially ran from the CLI with --config instead of -c and logged properly.

The switches -c and --config are interchangeable so this doesnā€™t matter.
Maybe the user you are running it with does not have write access to the folders?

I am running from CLI as root. It is again logging but no entry for my rule

Rule:

import HABApp

# Rules are classes that inherit from HABApp.Rule
class MyFirstRule(HABApp.Rule):
    def __init__(self):
        super().__init__()

        # Use run_soon to schedule things directly after instantiation,
        # don't do blocking things in __init__
        self.run_soon(self.say_something)

    def say_something(self):
        print('That was easy!')

# Rules
MyFirstRule()

log:

[2020-10-04 08:50:52,627] [                   HABApp]     INFO | HABApp Version 0.15.2
[2020-10-04 08:50:52,627] [   HABApp.mqtt.connection]     INFO | MQTT disabled
[2020-10-04 08:50:52,628] [    HABApp.RuleParameters]     INFO | Parameter files disabled: Folder /opt/habapp/config/param does not exist!
[2020-10-04 08:50:53,326] [HABApp.openhab.connection]     INFO | Connected to OpenHAB instance 0254c3b7-dc4b-43e7-8840-4a184596aa46
[2020-10-04 08:50:53,423] [     HABApp.openhab.items]     INFO | Updated 0 Items
[2020-10-04 08:50:53,534] [     HABApp.openhab.items]     INFO | Updated 2 Things
[2020-10-04 08:51:53,517] [    HABApp.openhab.things]     INFO | +----------------------------------------------------------------+
[2020-10-04 08:51:53,517] [    HABApp.openhab.things]     INFO | |                             Things                             |
[2020-10-04 08:51:53,518] [    HABApp.openhab.things]     INFO | +--------+------------+----------+------------+------------------+
[2020-10-04 08:51:53,518] [    HABApp.openhab.things]     INFO | | Status |   Label    | Location | Thing type |    Thing UID     |
[2020-10-04 08:51:53,518] [    HABApp.openhab.things]     INFO | +--------+------------+----------+------------+------------------+
[2020-10-04 08:51:53,518] [    HABApp.openhab.things]     INFO | | ONLINE | Local Moon |          | astro:moon | astro:moon:local |
[2020-10-04 08:51:53,518] [    HABApp.openhab.things]     INFO | | ONLINE | Local Sun  |          | astro:sun  | astro:sun:local  |
[2020-10-04 08:51:53,518] [    HABApp.openhab.things]     INFO | +--------+------------+----------+------------+------------------+

logging.yml is the default and config.yml is only lightly customized.

EDI:
BTW, the documentation has no information on what definition is expected for the default HABApp_Ping Item.

Set the wait_for_openhab in the config.yml to false or create an *.items file with an arbitrary item.


    general:
        listen_only: False  # If True  HABApp will not change any value on the openhab instance.
                            # Useful for testing rules from another machine.
        wait_for_openhab: False  # If True HABApp will wait for items from the openHAB instance
                                 # before loading any rules on startup

Itā€™s NumberItem, Iā€™ll add it right away.

1 Like

Log now shows the rule added & text printed on the console, not in the log.

I recommend people only start habapp from the CLI for the first rules so there is a console to display the output. Either that or have it output to a log file.

Perhaps have it added by default if it does not exist?

Also, the default waits for Items to load but shutdown hangs of there are no Items to load.

@Spaceman_Spiff
There appears to be a shutdown error. I currently only have 2 Things with no items or rules on OH.

(habapp) root@raspberrypi:/opt/habapp/config/log# fg
habapp --config /opt/habapp/config      (wd: /opt/habapp)
Shutting down ...
Exception ignored in: <function ClientSession.__del__ at 0x75175e88>
Traceback (most recent call last):
  File "/opt/habapp/lib/python3.7/site-packages/aiohttp/client.py", line 309, in __del__
TypeError: 'NoneType' object is not callable
Exception ignored in: <function BaseConnector.__del__ at 0x7513f858>
Traceback (most recent call last):
  File "/opt/habapp/lib/python3.7/site-packages/aiohttp/connector.py", line 276, in __del__
TypeError: 'NoneType' object is not callable

You can use the standard python logging mechanism to log to your own files as described here.
Iā€™ll add the following line to getting started:
It is recommended to use HABApp from the console for these examples so the print output can be observed.

I know that the shutdown can be a bit flaky, but it should be no problem and all resources will be freed properly.
The hanging shutdown will be fixed with the next release.

I used your systemd script which runs as user openhab
I needed to run sudo chown -R openhab:openhab /opt/habapp to permit write access to my /opt/habapp/config folder. Perhaps a warning should be added to the documentaiton.

@Spaceman_Spiff

events.log file does not appear to get written until habapp is stopped.

Hm - if you start HABApp in the empty folder everything should be created properly and if there are missing permissions it should go down with an appropriate error message.

Hower:
Logging seems to work fine, you have posted a snipped above.
It was just a hunch but the real reason was the delayed load because you didnā€™t have any items.

The event file is buffered and will flush per default after 10 logged entries.
While this is fine for a running system if you test things out the delay might be annoying.
You can change the buffer capacity to 0 in the logging.yml under BufferEventFile

1 Like

I initially ran from the CLI as root. I therefore later had to change permissions.