HABApp - Mirror openHAB events to a MQTT Broker

Hi,

I want to try HABApp with openHAB 2 on Ubuntu 20.04. I copied this code to /etc/openhab2/habapp/rules/mqtt.py:

import HABApp
from HABApp.openhab.events import ItemStateEvent
from HABApp.openhab.items import Thing
from HABApp.mqtt.items import MqttItem


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

    def __init__(self):
        super().__init__()

        for item in HABApp.core.Items.get_all_items():
            if isinstance(item, (Thing, MqttItem)):
                continue
            item.listen_event(self.process_update, ItemStateEvent)

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

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


ExampleOpenhabToMQTTRule()

You can find it here: Additional rule examples — HABApp beta documentation

My HABApp.log gives me following output/errors:

[2021-10-06 08:54:57,367] [                   HABApp]     INFO | HABApp Version 0.30.3
[2021-10-06 08:54:57,371] [   HABApp.mqtt.connection]     INFO | Connecting to <ip>:1883
[2021-10-06 08:54:57,372] [     HABApp.openhab.thing]     INFO | Config folder does not exist - textual thing config disabled!
[2021-10-06 08:54:57,373] [    HABApp.RuleParameters]     INFO | Parameter files disabled: Folder /etc/openhab2/habapp/params does not exist!
[2021-10-06 08:54:57,377] [   HABApp.mqtt.connection]     INFO | Connection Accepted.
[2021-10-06 08:54:57,556] [HABApp.openhab.connection]     INFO | Connected to OpenHAB instance <removed>
[2021-10-06 08:55:00,772] [     HABApp.openhab.items]     INFO | Updated 3818 Items
[2021-10-06 08:55:00,997] [HABApp.openhab.connection]  WARNING | Status 404 for PUT http://<ip>:8080/rest/items/HABApp_Ping/state/ NULL
[2021-10-06 08:55:02,627] [     HABApp.openhab.items]     INFO | Updated 247 Things
[2021-10-06 08:55:04,883] [             HABApp.Rules]    ERROR | Error "name 'OpenhabItem' is not defined" in load:
[2021-10-06 08:55:04,883] [             HABApp.Rules]    ERROR | Could not load /etc/openhab2/habapp/rules/mqtt.py!
[2021-10-06 08:55:04,883] [             HABApp.Rules]    ERROR | File "/opt/habapp/lib/python3.8/site-packages/HABApp/rule_manager/rule_file.py", line 80, in load
[2021-10-06 08:55:04,883] [             HABApp.Rules]    ERROR |     self.create_rules(created_rules)
[2021-10-06 08:55:04,883] [             HABApp.Rules]    ERROR | File "/opt/habapp/lib/python3.8/site-packages/HABApp/rule_manager/rule_file.py", line 66, in create_rules
[2021-10-06 08:55:04,883] [             HABApp.Rules]    ERROR |     runpy.run_path(str(self.path), run_name=str(self.path), init_globals={
[2021-10-06 08:55:04,883] [             HABApp.Rules]    ERROR | File "/usr/lib/python3.8/runpy.py", line 265, in run_path
[2021-10-06 08:55:04,884] [             HABApp.Rules]    ERROR |     return _run_module_code(code, init_globals, run_name,
[2021-10-06 08:55:04,884] [             HABApp.Rules]    ERROR | File "/usr/lib/python3.8/runpy.py", line 97, in _run_module_code
[2021-10-06 08:55:04,884] [             HABApp.Rules]    ERROR |     _run_code(code, mod_globals, init_globals,
[2021-10-06 08:55:04,884] [             HABApp.Rules]    ERROR | File "/usr/lib/python3.8/runpy.py", line 87, in _run_code
[2021-10-06 08:55:04,884] [             HABApp.Rules]    ERROR |     exec(code, run_globals)
[2021-10-06 08:55:04,884] [             HABApp.Rules]    ERROR | File "/etc/openhab2/habapp/rules/mqtt.py", line 26, in mqtt.py
[2021-10-06 08:55:04,884] [             HABApp.Rules]    ERROR |     22           print(f'/openhab/{event.name} <- {event.value}')
[2021-10-06 08:55:04,884] [             HABApp.Rules]    ERROR |     23           self.mqtt.publish(f'/openhab/{event.name}', str(event.value))
[2021-10-06 08:55:04,884] [             HABApp.Rules]    ERROR |     24
[2021-10-06 08:55:04,884] [             HABApp.Rules]    ERROR |     25
[2021-10-06 08:55:04,884] [             HABApp.Rules]    ERROR | --> 26   ExampleOpenhabToMQTTRule()
[2021-10-06 08:55:04,884] [             HABApp.Rules]    ERROR |     ..................................................
[2021-10-06 08:55:04,884] [             HABApp.Rules]    ERROR |      ExampleOpenhabToMQTTRule = <class '/etc/openhab2/habapp/rules/mqtt.py.ExampleOpenhabToMQTTRule'>
[2021-10-06 08:55:04,884] [             HABApp.Rules]    ERROR |     ..................................................
[2021-10-06 08:55:04,884] [             HABApp.Rules]    ERROR |
[2021-10-06 08:55:04,885] [             HABApp.Rules]    ERROR | File "/etc/openhab2/habapp/rules/mqtt.py", line 15, in __init__
[2021-10-06 08:55:04,885] [             HABApp.Rules]    ERROR |     10   def __init__(self):
[2021-10-06 08:55:04,885] [             HABApp.Rules]    ERROR |     11       super().__init__()
[2021-10-06 08:55:04,885] [             HABApp.Rules]    ERROR |     12
[2021-10-06 08:55:04,885] [             HABApp.Rules]    ERROR |     13       for item in HABApp.core.Items.get_all_items():
[2021-10-06 08:55:04,885] [             HABApp.Rules]    ERROR |     14           #if isinstance(item, (Thing, MqttItem)):
[2021-10-06 08:55:04,885] [             HABApp.Rules]    ERROR | --> 15           if not isinstance(item, OpenhabItem):
[2021-10-06 08:55:04,885] [             HABApp.Rules]    ERROR |     16               continue

As you can see I changed one line from if isinstance(item, (Thing, MqttItem)): to if not isinstance(item, OpenhabItem):. Both does not work.

The configuration is in /etc/openhab2/habapp and habapp is installed in /opt/habapp.

Here is my config.yml:

directories:
  logging: log  # Folder where the logs will be written to
  rules: rules # Folder from which the rule files will be loaded
  param: params # Folder from which the parameter files will be loaded
  config: config # Folder from which configuration files (e.g. for textual thing configuration) will be loaded
  lib: lib # Folder where additional libraries can be placed
location:
  latitude: 0.0
  longitude: 0.0
  elevation: 0.0
mqtt:
  connection:
    client_id: HABApp
    host: <ip>
    port: 1883
    user: ''
    password: ''
    tls: false
    tls_ca_cert: ''  # Path to a CA certificate that will be treated as trusted
    tls_insecure: true
  general:
    listen_only: false  # If True HABApp will not publish any value to the broker
  publish:
    qos: 0  # Default QoS when publishing values
    retain: false # Default retain flag when publishing values
  subscribe:
    qos: 0  # Default QoS for subscribing
    topics:
    - '#'
    - 0
openhab:
  connection:
    host: <ip>
    port: 8080
    user: ''
    password: ''
  general:
    listen_only: false  # If True HABApp will not change anything on the openHAB instance.
    wait_for_openhab: true # If True HABApp will wait for items from the openHAB instance before loading any rules on startup
  ping:
    enabled: true  # If enabled the configured item will show how long it takes to send an update from HABApp and get the updated value back from openhabin milliseconds
    item: HABApp_Ping # Name of the Numberitem
    interval: 10 # Seconds between two pings

I hope you can help me. Thanks in advance.

Okay, my fault, sorry. I have to import from HABApp.openhab.items import OpenhabItem.