HABApp - Easy automation with openHAB

Seb, is the use of authorization token on your radar (so that we don’t have to turn off authentication in OH 3)?

@yfaway Sort of:
I’ve a small PR open since 11th of January that fixes the performance issues for basic auth in openhab but somehow the openhab maintainers are very selective of whom they want to contribute and refuse to review the PR (this is the second PR which got some improvements, the first PR was opened even earlier).
This would have been a quick fix for all the basic auth issues and make HABApp work with the expected speed again.

Using a token in the header is on my list, but I have to do some investigation first because I’m using multiple libraries and I am currently worried that I have to implement the handling in one of them (SSE event listener) myself.


It definitely is.
On the one hand it adds additional complexity since it’s an additional tool and also not everything is exposed through the rest api (e.g. actions) so for very rare cases it’s necessary to work with proxy items.
On the other hand it’s a complete home automation rule engine written in python and while it does almost everything different than the build in rule engine it (imho) does everything better:
Error messages, timers, multiple “expires” per item, time stamps for last_change and last_update of an item, dynamic rule creation, accessing item per name, parameter files, textual thing configuration etc…

@dan12345
It’s a steep learning curve but I really encourage you to try it out. I’ve been on the jsr223 track since openhab 1.8 and after the first enthusiasm it almost killed my excitement for home automation.
You can install HABApp on your main PC and connect remotely to your openhab instance (if you want in listen_only mode so it doesn’t make changes). That way you can play around and try to get the hang of it without having to make changes to your installation.
This rule shows you that it’s possible to write very compact and readable code that covers a somewhat complex use case.

1 Like

Thanks - I’d missed that post… it’s really helpful. When I have a spare weekend I should have a real think about transitioning.

I will do - thanks!

One question: How do i acces the members of a group?

self.openhab.get_item('group_name') will return the complete openhab item definition.
For groups there is an object “members” which will contain all items.

Be aware that this is a sync call (a request which goes to openhab), so even though this shouldn’t take too long depending on the item it might add up.
If you use it to set up your rules or use the item names to create a list of items it should be no problem.

Thanks a lot. That exactly my intention. Put ally my thermostats in a group and use the groups to create the rules.

It would be great is rules are reloaded if the numer of members in the group changes, but i this that is not possible.

Thanks
Thomas

I am not sure but maybe openhab issues a ItemChangedEvent when you add/remove items to the group. You can trigger on it and then issue a FileReloadEvent which will make HABApp reload your rule file.
If you want to create rule classes you have to use HABApp.openhab.interface.get_item('group_name') to get the definition.

Two additional remarks:

  1. How often do you really add thermostats? Is it really work the effort to implement some logic or may it enough to touch the rule file when you added a thermostat to openhab?

  2. As always I’ll recommend to use a good naming scheme or parameter files to setup rules instead of using groups. That way you’ll always have a defined set of rules and get error messages if your items do not exist instead of a rule just disappearing.

Thanks for your hints. On your remarks:

to 1: Yeah i see your point. But as always: It is good to know many possibilities to do things and i love to have thing totally automated. At the end: If have included all thermostats i will never touch the group again, because there are no places for new thermostats left :slight_smile:

to 2: Naming schemes are a difficult thing, but i think mine is OK. I see location of the things and within a class of thing the differnet item have the same extension like stae, temperature, …

Hi all,

again, can’t figure out what I’m doing wrong. So maybe posting will help me solve it on my own (which often happens) or I get some generous help from the community.

I’ve gotten HABApp installed. I can run the two test scripts and see output on the console, but if I try to interact with an OpenHAB item, I am unsuccessful:

import HABApp
from HABApp.core.items import Item

class MyFirstRule(HABApp.Rule):
    def __init__(self):
        super().__init__()
        # Get the item or create it if it does not exist
        self.my_item = Item.get_create_item('T_TestString')

        self.run_soon(self.say_something)

    def say_something(self):
        # Post updates to the item through the internal event bus
        self.my_item.post_value('Test')
        self.my_item.post_value('Change')

        # The item value can be used in comparisons through this shortcut ...
        if self.my_item == 'Change':
            print('Item value is "Change"')
        # ... which is the same as this:
        if self.my_item.value == 'Change':
            print('Item.value is "Change"')
 
print('hello')
MyFirstRule()

If I use an existing item in my OpenHAB environment, it fails… if I add a digit to the end of the name to make it a new item, the script works. From the error in the logs, it does look like it retrieves a value from T_TestString, however, before failing.

[2021-02-22 09:17:37,081] [             HABApp.Rules]    ERROR | File "/etc/openhab/habapp/rules/test.py", line 9, in __init__
[2021-02-22 09:17:37,081] [             HABApp.Rules]    ERROR |     6    def __init__(self):
[2021-02-22 09:17:37,081] [             HABApp.Rules]    ERROR |     7        super().__init__()
[2021-02-22 09:17:37,082] [             HABApp.Rules]    ERROR |     8        # Get the item or create it if it does not exist
[2021-02-22 09:17:37,082] [             HABApp.Rules]    ERROR | --> 9        self.my_item = Item.get_create_item('T_TestString')
[2021-02-22 09:17:37,082] [             HABApp.Rules]    ERROR |     10
[2021-02-22 09:17:37,082] [             HABApp.Rules]    ERROR |     ..................................................
[2021-02-22 09:17:37,082] [             HABApp.Rules]    ERROR |      self = </etc/openhab/habapp/rules/test.py.MyFirstRule object at 0xb236fad0>
[2021-02-22 09:17:37,083] [             HABApp.Rules]    ERROR |      self.my_item = # AttributeError
[2021-02-22 09:17:37,083] [             HABApp.Rules]    ERROR |           self = </etc/openhab/habapp/rules/test.py.MyFirstRule object at 0xb236fad0>
[2021-02-22 09:17:37,083] [             HABApp.Rules]    ERROR |     ..................................................
[2021-02-22 09:17:37,083] [             HABApp.Rules]    ERROR |
[2021-02-22 09:17:37,083] [             HABApp.Rules]    ERROR | File "/opt/habapp/lib/python3.7/site-packages/HABApp/core/items/item.py", line 24, in get_create_item
[2021-02-22 09:17:37,084] [             HABApp.Rules]    ERROR |     9    def get_create_item(cls, name: str, initial_value=None):
[2021-02-22 09:17:37,084] [             HABApp.Rules]    ERROR |  (...)
[2021-02-22 09:17:37,084] [             HABApp.Rules]    ERROR |     20       except HABApp.core.Items.ItemNotFoundException:
[2021-02-22 09:17:37,084] [             HABApp.Rules]    ERROR |     21           item = cls(name, initial_value)
[2021-02-22 09:17:37,085] [             HABApp.Rules]    ERROR |     22           HABApp.core.Items.add_item(item)
[2021-02-22 09:17:37,085] [             HABApp.Rules]    ERROR |     23
[2021-02-22 09:17:37,085] [             HABApp.Rules]    ERROR | --> 24       assert isinstance(item, cls), f'{cls} != {type(item)}'
[2021-02-22 09:17:37,085] [             HABApp.Rules]    ERROR |     25       return item
[2021-02-22 09:17:37,085] [             HABApp.Rules]    ERROR |     ..................................................
[2021-02-22 09:17:37,086] [             HABApp.Rules]    ERROR |      cls = <class 'HABApp.core.items.item.Item'>
[2021-02-22 09:17:37,086] [             HABApp.Rules]    ERROR |      name = 'T_TestString'
[2021-02-22 09:17:37,086] [             HABApp.Rules]    ERROR |      initial_value = None
[2021-02-22 09:17:37,086] [             HABApp.Rules]    ERROR |      HABApp.core.Items.ItemNotFoundException = <class 'HABApp.core.Items.ItemNotFoundException'>
[2021-02-22 09:17:37,087] [             HABApp.Rules]    ERROR |      item = <StringItem name: T_TestString, value: Hello!, last_change: 2021-02-22 09:14:16.396357, last_update: 2021-02-22 09:14:16.396357>

Not sure if I have something configured wrong or it is a failure of my understanding of how this is supposed to work.

Hi,

Item.get_create_item('T_TestString') does create a local item which only exists in HABApp.
You can use those to share stuff between rules/files and as general proxy items.
If you want to use the items from openhab you have to do it like this:

from HABApp.openhab.items import StringItem
....
self.my_item = StringItem.get_item('T_TestString')

It fails because you try to create a local item and for the same name there is already an existing openhab item.

Just an pointer from my battle ground - the SSE stuff required me to get cookie processing. Not sure which side of SSE you are (OH or middle ground) - but that was the way for OH to actually pass auth stuff.

1 Like

Hi Seb, I do not think HABapp supports event like this in Xtend ah?

Item PARTITION1_ARM_MODE received command 1

Is it a limitation of the OH REST API?

Of course it’s possible with HABApp.
It’s even possible to listen to any part of the event and combine them as desired (see second part of events description).

For the most common events I have created convenience filters that provide type hints.
E.g.

  • ValueUpdateEventFilter and ValueChangeEventFilter
  • MqttValueUpdateEventFilter and MqttValueChangeEventFilter
  • ItemStateEventFilter and ItemStateChangedEventFilter

@Spaceman_Spiff Thanks, i am learning from every post :slight_smile:

What do you think about the idea to create example how to move DSL condition like received Command to HABApp? I think that will make it mich easier to move over.

I think it’s a great idea!
Maybe we can start a thread and you guys post rule samples and we transfer them to HABApp. Once we have a couple of examples I can add it to the docs.
Unfortunately I can’t do it myself since I haven’t been writing DSL rules since OH1.7 :smiley:
What do you think? Would you (guys) be willing to contribute?

Hopefully i will manage to start a thread this evening.

2 Likes

It is done! See the following Post [wiki] HABAPP - A journey to start - Rules triggers - Apps & Services / 3rd Party - openHAB Community is the first post to collect HABApp informations

Hello,

I have an issue with importing a python module and am a bit confused.

I installed the module requests with pip and it shows up correctly. I can use it with a regular python .py.

But in the HABapp.log I get this:

$
[2021-03-08 21:09:13,417] [             HABApp.Rules]    ERROR | Error "No module named 'requests'" in load:
[2021-03-08 21:09:13,418] [             HABApp.Rules]    ERROR | Could not load /etc/openhab/habapp/rules/myfirstrule.py!
[2021-03-08 21:09:13,418] [             HABApp.Rules]    ERROR | File "/opt/habapp/lib/python3.7/site-packages/HABApp/rule_manager/rule_file.py", line 79, in load
[2021-03-08 21:09:13,419] [             HABApp.Rules]    ERROR |     self.create_rules(created_rules)
[2021-03-08 21:09:13,421] [             HABApp.Rules]    ERROR | File "/opt/habapp/lib/python3.7/site-packages/HABApp/rule_manager/rule_file.py", line 68, in crea$
[2021-03-08 21:09:13,421] [             HABApp.Rules]    ERROR |     '__HABAPP__RULES': created_rules,
[2021-03-08 21:09:13,422] [             HABApp.Rules]    ERROR | File "/usr/lib/python3.7/runpy.py", line 263, in run_path
[2021-03-08 21:09:13,422] [             HABApp.Rules]    ERROR |     pkg_name=pkg_name, script_name=fname)
[2021-03-08 21:09:13,424] [             HABApp.Rules]    ERROR | File "/usr/lib/python3.7/runpy.py", line 96, in _run_module_code
[2021-03-08 21:09:13,425] [             HABApp.Rules]    ERROR |     mod_name, mod_spec, pkg_name, script_name)
[2021-03-08 21:09:13,426] [             HABApp.Rules]    ERROR | File "/usr/lib/python3.7/runpy.py", line 85, in _run_code
[2021-03-08 21:09:13,427] [             HABApp.Rules]    ERROR |     exec(code, run_globals)
[2021-03-08 21:09:13,427] [             HABApp.Rules]    ERROR | File "/etc/openhab/habapp/rules/myfirstrule.py", line 4, in myfirstrule.py
[2021-03-08 21:09:13,428] [             HABApp.Rules]    ERROR |     1    import datetime
[2021-03-08 21:09:13,428] [             HABApp.Rules]    ERROR |     2    import time
[2021-03-08 21:09:13,429] [             HABApp.Rules]    ERROR |     3
[2021-03-08 21:09:13,429] [             HABApp.Rules]    ERROR | --> 4    import requests
[2021-03-08 21:09:13,430] [             HABApp.Rules]    ERROR |     5    import json
[2021-03-08 21:09:13,431] [             HABApp.Rules]    ERROR |     ..................................................
[2021-03-08 21:09:13,431] [             HABApp.Rules]    ERROR |      datetime = <module 'datetime' from '/usr/lib/python3.7/datetime.py'>
[2021-03-08 21:09:13,431] [             HABApp.Rules]    ERROR |      time = <module 'time' (built-in)>
[2021-03-08 21:09:13,432] [             HABApp.Rules]    ERROR |     ..................................................
[2021-03-08 21:09:13,432] [             HABApp.Rules]    ERROR |
[2021-03-08 21:09:13,432] [             HABApp.Rules]    ERROR | ModuleNotFoundError: No module named 'requests'

What am I doing wrong?

Thanks for any help!

If you install additional packages you have to restart HABApp to pick up the changes in the virtual environment.