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?