Yes - I only use the rules because I can easily get the state with self.get_item_state and set it with self.set_item_state.
What I sometimes do is use another rule and its functions to keep small logical units properly abstracted, e.g.:
class RuleA:
def do_sth_a():
pass
MyRuleA = RuleA()
class RuleB:
def do_sth_b():
MyRuleA.do_sth_a()
RuleB()
Another question:
If I have each rule in its own file, can I instantiate each rule in some global file?
I assume that each rule runs in the same context, right?
Would it be possible to use global variable if needed?
It is not clear for me how all my created files are related to each other and related to HapAPP.
Are the rule files (normal Python files?) just collected and concatenated or how does it work?
There is the possibility to get a rule instance by name so there is no need for global variables.
Each python file you create is loaded independently as you can see in the HABApp.log.
So if you want to share Rule instances you can use the function to get it by name or put the rules that require each other in one file as I showed in the example above
This looks like great work! Especially the possibility to use python libraries is great. Will definitely play with it soon. Is it also possible to use OH2 actions?
@Spaceman_Spiff I really like this! I’ve started using HabApp to convert some of my rules from DSL as I am somewhat familiar with python.
Is there any chance you can do some “bridging” between jython and HabApp to make actions work?
Hi @lfs_alp5, I am really glad you like it! I’ve tried to get information how to use actions, but so fare there is no possibility. I’ve also opened an issue on github.
A workaround is to define an item and then send commands to it.
e.g.
Added possibility to process error messages from rules
Added callback when rule gets unloaded
Added possibility to read item definitions from openhab
fixed a bug where created items were not put in any group
added possibility to check if an item already exists
HABApp items can be used in every rule function
new classmethod for getting/creating items in HABApp which provides syntax highlighting for the class functions:
from HABApp.openhab.items import SwitchItem
switch = SwitchItem.get_create_item('MySwitch')
switch.is_on()
Register a callback for errors
This example shows how to create a rule with a function which will be called when any rule throws an error. The rule function then can push the error message to an openhab item or e.g. use Pushover to send the error message to the mobile device.
import HABApp
from HABApp.core import WrappedFunction
class NotifyOnError(HABApp.Rule):
def __init__(self):
super().__init__()
# so it get's unloaded propperly in case we make changes to this rule
self.register_on_unload(WrappedFunction.CLEAR_ERROR_CALLBACK)
# register function
WrappedFunction.REGISTER_ERROR_CALLBACK(self.on_error)
def on_error(self, error_message: str):
print(f'Message:\n{error_message}\n')
NotifyOnError()
# this is a faulty example. Do not create this part!
class FaultyRule(HABApp.Rule):
def __init__(self):
super().__init__()
self.run_soon(self.faulty_function)
def faulty_function(self):
1 / 0
FaultyRule()
will output:
Message:
Error in FaultyRule.faulty_function: division by zero
Traceback (most recent call last):
File "<string>", line 32, in faulty_function
ZeroDivisionError: division by zero
I just tried to run the docker container on my RPi4 to test it out but it doesn’t look like the image is ARM-compatible. Any chance you can offer an image that will run on ARM?