Additional rule examples

Using the scheduler

from datetime import time, timedelta, datetime
from HABApp import Rule

class MyRule(Rule):

    def __init__(self):
        super().__init__(), 34, 20), weekdays=['Mo'], callback=self.run_mondays), 3, self.run_every_3s, 'arg 1', asdf='kwarg 1'), 00), self.run_workdays), 00), self.run_weekends)

    def run_every_3s(self, arg, asdf = None):
        print(f'run_ever_3s: {} : {arg}, {asdf}')

    def run_mondays(self):
        print('Today is monday!')

    def run_workdays(self):
        print('Today is a workday!')

    def run_weekends(self):
        print('Finally weekend!')


Mirror openHAB events to a MQTT Broker

import HABApp
from import ItemStateEvent
from HABApp.openhab.items import OpenhabItem

class ExampleOpenhabToMQTTRule(HABApp.Rule):
    """This Rule mirrors all updates from OpenHAB to MQTT"""

    def __init__(self):

        for item in self.get_items(OpenhabItem):
            item.listen_event(self.process_update, ItemStateEvent)

    def process_update(self, event):
        assert isinstance(event, ItemStateEvent)

        print(f'/openhab/{} <- {event.value}')
        self.mqtt.publish(f'/openhab/{}', str(event.value))


Trigger an event when an item is constant

Get an even when the item is constant for 5 and for 10 seconds.

import HABApp
from HABApp.core.items import Item
from import ItemNoChangeEvent

class MyRule(HABApp.Rule):
    def __init__(self):
        my_item = Item.get_item('test_watch')

        my_item.watch_change(5)     # Create event when item doesn't change for  5 secs
        my_item.watch_change(10)    # Create event when item doesn't change for 10 secs

        # Listen to these events
        self.listen_event(my_item, self.item_constant, ItemNoChangeEvent)

        # Set the item to a value

    def item_constant(self, event):

<ItemNoChangeEvent name: test_watch, seconds: 5>
<ItemNoChangeEvent name: test_watch, seconds: 10>

Turn something off after movement

Turn a device off 30 seconds after one of the movement sensors in a room signals movement.

import HABApp
from HABApp.core.items import Item
from import ValueUpdateEvent

class MyCountdownRule(HABApp.Rule):
    def __init__(self):

        self.countdown =, self.switch_off)
        self.device = Item.get_item('my_device')

        self.movement1 = Item.get_item('movement_sensor1')
        self.movement1.listen_event(self.movement, ValueUpdateEvent)

        self.movement2 = Item.get_item('movement_sensor2')
        self.movement2.listen_event(self.movement, ValueUpdateEvent)

    def movement(self, event: ValueUpdateEvent):
        if self.device != 'ON':


    def switch_off(self):


Process Errors in Rules

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 (see Avanced Usage for more information).

import HABApp
from import HABAppException

class NotifyOnError(HABApp.Rule):
    def __init__(self):

        # Listen to all errors
        self.listen_event('HABApp.Errors', self.on_error, HABAppException)

    def on_error(self, error_event: HABAppException):
        msg = event.to_str() if isinstance(event, HABAppException) else event


# this is a faulty example. Do not create this part!
class FaultyRule(HABApp.Rule):
    def __init__(self):

    def faulty_function(self):
        1 / 0
Error in FaultyRule.faulty_function: division by zero
File "/home/docs/checkouts/", line 93, in __run
    self._func(*args, **kwargs)
File "<string>", line 30, in faulty_function

ZeroDivisionError: division by zero
Error in NotifyOnError.on_error: name 'event' is not defined
File "/home/docs/checkouts/", line 93, in __run
    self._func(*args, **kwargs)
File "<string>", line 30, in faulty_function

ZeroDivisionError: division by zero

While handling the above exception, another exception occurred:

File "/home/docs/checkouts/", line 93, in __run
    self._func(*args, **kwargs)
File "<string>", line 17, in on_error

NameError: name 'event' is not defined