HABApp - listen to to ChannelTriggeredEvent?

Although there exists a ChannelTriggeredEvent in HABApp I could not figure our how to create an event listener for a channel triggered event.
Has anyone an example?

Thanks,
Dominik

Do you see the corresponding event in the HABApp_events.log?
Then itā€™s
self.listen_event('name', callback, ChannelTriggeredEvent)

when defining it this way I get:

Expected type 'Optional[Any]' (matched generic type 'Optional[HINT_EVENT_FILTER_OBJ]'), got 'Type[ChannelTriggeredEvent]' instead

It seems the interface expects a filter. I cannot find any ChannelTriggeredEventFilter anywhere.

here my code sniplet:

from HABApp import Rule
from HABApp.openhab.events.channel_events import ChannelTriggeredEvent
import logging

log = logging.getLogger('MyRule') 


class MyChannelRule(Rule):
    def __init__(self):
        super().__init__()
        self.listen_event('deconz:switch:00212E00C488:04cd15fffe6f9d60011000', self.on_channel, ChannelTriggeredEvent)


    def on_channel(self, event: ChannelTriggeredEvent):
        log.info('Channel Event: {}'.format(event))

MyChannelRule()

The syntax changed and is now EventFilter(ChannelTriggeredEvent) instead of ChannelTriggeredEvent

Now the rule is added, though not triggering as expected, I did something wrong.
here is the event log I want to trigger on:

[2023-07-28 09:09:48,595] [          HABApp.EventBus]     INFO | deconz:switch:00212E00C488:04cd15fffe6f9d60011000:buttonevent: <ChannelTriggeredEvent name: deconz:switch:00212E00C488:04cd15fffe6f9d60011000:buttonevent, event: 3002>

please help!

How to define to trigger on a particular event

Your event name in the listener does not match the name from the event bus

True!

Now it works, thanks

While unsuccessfully trying to filter on channel event (e.g. ā€œ1002ā€):

  
self.listen_event('deconz:switch:00212E00C488:04cd15fffe6f9d60011000:buttonevent', self.on_channel, EventFilter(ChannelTriggeredEvent(event="1002")))

instead of

self.listen_event('deconz:switch:00212E00C488:04cd15fffe6f9d60011000:buttonevent', self.on_channel, EventFilter(ChannelTriggeredEvent, event="1002"))

I got into a situation where HABApp started logging errors on a millisecond level. I had to stop the container.

Here are the log entries which are always repeated in a loop:

[2023-07-28 14:11:42,418] [              HABApp.Rule]    ERROR | File "/usr/local/lib/python3.10/site-packages/HABApp/rule_ctx/rule_ctx.py", line 44 in unload_rule
[2023-07-28 14:11:42,418] [              HABApp.Rule]    ERROR | --------------------------------------------------------------------------------
[2023-07-28 14:11:42,418] [              HABApp.Rule]    ERROR |      32 | def unload_rule(self):
[2023-07-28 14:11:42,418] [              HABApp.Rule]    ERROR |       (...)
[2023-07-28 14:11:42,418] [              HABApp.Rule]    ERROR |      41 |         while self.objs:
[2023-07-28 14:11:42,418] [              HABApp.Rule]    ERROR |      42 |             with HABApp.core.wrapper.ExceptionToHABApp(log):
[2023-07-28 14:11:42,418] [              HABApp.Rule]    ERROR |      43 |                 to_cancel = next(iter(self.objs))
[2023-07-28 14:11:42,419] [              HABApp.Rule]    ERROR | -->  44 |                 to_cancel.cancel()
[2023-07-28 14:11:42,419] [              HABApp.Rule]    ERROR |      45 |         self.objs = None    # Set to None so we crash if we want to schedule new stuff
[2023-07-28 14:11:42,419] [              HABApp.Rule]    ERROR |    ------------------------------------------------------------
[2023-07-28 14:11:42,419] [              HABApp.Rule]    ERROR |      rule = <MyChannelRule>
[2023-07-28 14:11:42,419] [              HABApp.Rule]    ERROR |      rule.run = <HABApp.rule.scheduler.habappschedulerview.HABAppSchedulerView object at 0xb04f5508>
[2023-07-28 14:11:42,419] [              HABApp.Rule]    ERROR |      rule.run._habapp_ctx = None
[2023-07-28 14:11:42,419] [              HABApp.Rule]    ERROR |      rule.run._scheduler = <HABApp.rule.scheduler.scheduler.HABAppScheduler object at 0xb04f5fe8>
[2023-07-28 14:11:42,419] [              HABApp.Rule]    ERROR |      self = <HABApp.rule_ctx.rule_ctx.HABAppRuleContext object at 0xb04f5ef8>
[2023-07-28 14:11:42,419] [              HABApp.Rule]    ERROR |      self.objs = {<HABApp.core.internals.event_bus_listener.ContextBoundEventBusListener object at 0xb04f5268>}
[2023-07-28 14:11:42,420] [              HABApp.Rule]    ERROR |      self.rule = <MyChannelRule>
[2023-07-28 14:11:42,420] [              HABApp.Rule]    ERROR |      log = <Logger HABApp.Rule (INFO)>
[2023-07-28 14:11:42,420] [              HABApp.Rule]    ERROR |      to_cancel = <HABApp.core.internals.event_bus_listener.ContextBoundEventBusListener object at 0xb04f5268>
[2023-07-28 14:11:42,420] [              HABApp.Rule]    ERROR |    ------------------------------------------------------------
[2023-07-28 14:11:42,420] [              HABApp.Rule]    ERROR | 
[2023-07-28 14:11:42,420] [              HABApp.Rule]    ERROR | File "/usr/local/lib/python3.10/site-packages/HABApp/core/internals/event_bus_listener.py", line 57 in cancel
[2023-07-28 14:11:42,420] [              HABApp.Rule]    ERROR | --------------------------------------------------------------------------------
[2023-07-28 14:11:42,420] [              HABApp.Rule]    ERROR |      55 | def cancel(self):
[2023-07-28 14:11:42,420] [              HABApp.Rule]    ERROR |      56 |     """Stop listening on the event bus"""
[2023-07-28 14:11:42,421] [              HABApp.Rule]    ERROR | -->  57 |     self._ctx_unlink()
[2023-07-28 14:11:42,421] [              HABApp.Rule]    ERROR |    ------------------------------------------------------------
[2023-07-28 14:11:42,421] [              HABApp.Rule]    ERROR |      self = <HABApp.core.internals.event_bus_listener.ContextBoundEventBusListener object at 0xb04f5268>
[2023-07-28 14:11:42,421] [              HABApp.Rule]    ERROR |    ------------------------------------------------------------
[2023-07-28 14:11:42,421] [              HABApp.Rule]    ERROR | 
[2023-07-28 14:11:42,421] [              HABApp.Rule]    ERROR | File "/usr/local/lib/python3.10/site-packages/HABApp/core/internals/event_bus_listener.py", line 52 in _ctx_unlink
[2023-07-28 14:11:42,421] [              HABApp.Rule]    ERROR | --------------------------------------------------------------------------------
[2023-07-28 14:11:42,421] [              HABApp.Rule]    ERROR |      51 | def _ctx_unlink(self):
[2023-07-28 14:11:42,421] [              HABApp.Rule]    ERROR | -->  52 |     event_bus.remove_listener(self)
[2023-07-28 14:11:42,422] [              HABApp.Rule]    ERROR |      53 |     return super()._ctx_unlink()
[2023-07-28 14:11:42,422] [              HABApp.Rule]    ERROR |    ------------------------------------------------------------
[2023-07-28 14:11:42,422] [              HABApp.Rule]    ERROR |      event_bus = <HABApp.core.internals.event_bus.event_bus.EventBus object at 0xb54e9148>
[2023-07-28 14:11:42,422] [              HABApp.Rule]    ERROR |      self = <HABApp.core.internals.event_bus_listener.ContextBoundEventBusListener object at 0xb04f5268>
[2023-07-28 14:11:42,422] [              HABApp.Rule]    ERROR |    ------------------------------------------------------------
[2023-07-28 14:11:42,422] [              HABApp.Rule]    ERROR | 
[2023-07-28 14:11:42,422] [              HABApp.Rule]    ERROR | File "/usr/local/lib/python3.10/site-packages/HABApp/core/internals/event_bus/event_bus.py", line 76 in remove_listener
[2023-07-28 14:11:42,422] [              HABApp.Rule]    ERROR | --------------------------------------------------------------------------------
[2023-07-28 14:11:42,422] [              HABApp.Rule]    ERROR |      67 | def remove_listener(self, listener: _TYPE_LISTENER):
[2023-07-28 14:11:42,423] [              HABApp.Rule]    ERROR |       (...)
[2023-07-28 14:11:42,423] [              HABApp.Rule]    ERROR |      72 |         item_listeners = self._listeners.get(listener.topic, [])
[2023-07-28 14:11:42,423] [              HABApp.Rule]    ERROR |      74 |         # print warning if we try to remove it twice
[2023-07-28 14:11:42,423] [              HABApp.Rule]    ERROR |      75 |         if listener not in item_listeners:
[2023-07-28 14:11:42,423] [              HABApp.Rule]    ERROR | -->  76 |             habapp_log.warning(f'Event listener for {listener.describe()} has already been removed!')
[2023-07-28 14:11:42,423] [              HABApp.Rule]    ERROR |      77 |             return None
[2023-07-28 14:11:42,423] [              HABApp.Rule]    ERROR |    ------------------------------------------------------------
[2023-07-28 14:11:42,423] [              HABApp.Rule]    ERROR |      listener = <HABApp.core.internals.event_bus_listener.ContextBoundEventBusListener object at 0xb04f5268>
[2023-07-28 14:11:42,424] [              HABApp.Rule]    ERROR |      listener.topic = 'deconz:switch:00212E00C488:04cd15fffe6f9d60011000:buttonevent'
[2023-07-28 14:11:42,424] [              HABApp.Rule]    ERROR |      self = <HABApp.core.internals.event_bus.event_bus.EventBus object at 0xb54e9148>
[2023-07-28 14:11:42,424] [              HABApp.Rule]    ERROR |      self._listeners = {'HABApp.Files': [<HABApp.core.internals.event_bus_listener.EventBusListener object at 0xb5696628>, <HABApp.core.internals.event_bus_listener.EventBusListener object at 0xb5664f58>], 'HABApp_Ping': [<HABApp.core.internals.event_bus_listener.EventBusListener object at 0xb5613178>], 'deconz:switch:00212E00C488:04cd15fffe6f9d60011000:buttonevent': []}
[2023-07-28 14:11:42,424] [              HABApp.Rule]    ERROR |      self._lock = <unlocked _thread.lock object at 0xb5542628>
[2023-07-28 14:11:42,424] [              HABApp.Rule]    ERROR |      habapp_log = <Logger HABApp (INFO)>
[2023-07-28 14:11:42,424] [              HABApp.Rule]    ERROR |      item_listeners = []
[2023-07-28 14:11:42,424] [              HABApp.Rule]    ERROR |    ------------------------------------------------------------
[2023-07-28 14:11:42,424] [              HABApp.Rule]    ERROR | 
[2023-07-28 14:11:42,424] [              HABApp.Rule]    ERROR | File "/usr/local/lib/python3.10/site-packages/HABApp/core/internals/event_bus_listener.py", line 49 in describe
[2023-07-28 14:11:42,425] [              HABApp.Rule]    ERROR | --------------------------------------------------------------------------------
[2023-07-28 14:11:42,425] [              HABApp.Rule]    ERROR |      48 | def describe(self) -> str:
[2023-07-28 14:11:42,425] [              HABApp.Rule]    ERROR | -->  49 |     return f'"{self.topic}" (filter={self.filter.describe()})'
[2023-07-28 14:11:42,425] [              HABApp.Rule]    ERROR |    ------------------------------------------------------------
[2023-07-28 14:11:42,425] [              HABApp.Rule]    ERROR |      self = <HABApp.core.internals.event_bus_listener.ContextBoundEventBusListener object at 0xb04f5268>
[2023-07-28 14:11:42,448] [              HABApp.Rule]    ERROR | Error "'ChannelTriggeredEvent' object has no attribute '__name__'" in unload_rule:
[2023-07-28 14:11:42,449] [              HABApp.Rule]    ERROR | Error while formatting traceback: 'ChannelTriggeredEvent' object has no attribute '__name__'
[2023-07-28 14:11:42,449] [              HABApp.Rule]    ERROR | Traceback (most recent call last):
[2023-07-28 14:11:42,449] [              HABApp.Rule]    ERROR |   File "/usr/local/lib/python3.10/site-packages/HABApp/rule_ctx/rule_ctx.py", line 44, in unload_rule
[2023-07-28 14:11:42,449] [              HABApp.Rule]    ERROR |     to_cancel.cancel()
[2023-07-28 14:11:42,449] [              HABApp.Rule]    ERROR |   File "/usr/local/lib/python3.10/site-packages/HABApp/core/internals/event_bus_listener.py", line 57, in cancel
[2023-07-28 14:11:42,450] [              HABApp.Rule]    ERROR |     self._ctx_unlink()
[2023-07-28 14:11:42,450] [              HABApp.Rule]    ERROR |   File "/usr/local/lib/python3.10/site-packages/HABApp/core/internals/event_bus_listener.py", line 52, in _ctx_unlink
[2023-07-28 14:11:42,450] [              HABApp.Rule]    ERROR |     event_bus.remove_listener(self)
[2023-07-28 14:11:42,450] [              HABApp.Rule]    ERROR |   File "/usr/local/lib/python3.10/site-packages/HABApp/core/internals/event_bus/event_bus.py", line 76, in remove_listener
[2023-07-28 14:11:42,450] [              HABApp.Rule]    ERROR |     habapp_log.warning(f'Event listener for {listener.describe()} has already been removed!')
[2023-07-28 14:11:42,450] [              HABApp.Rule]    ERROR |   File "/usr/local/lib/python3.10/site-packages/HABApp/core/internals/event_bus_listener.py", line 49, in describe
[2023-07-28 14:11:42,450] [              HABApp.Rule]    ERROR |     return f'"{self.topic}" (filter={self.filter.describe()})'
[2023-07-28 14:11:42,450] [              HABApp.Rule]    ERROR |   File "/usr/local/lib/python3.10/site-packages/HABApp/core/events/filter/event.py", line 64, in describe
[2023-07-28 14:11:42,450] [              HABApp.Rule]    ERROR |     return f'{self.__class__.__name__}(type={self.event_class.__name__}{values})'
[2023-07-28 14:11:42,451] [              HABApp.Rule]    ERROR | AttributeError: 'ChannelTriggeredEvent' object has no attribute '__name__'
[2023-07-28 14:11:42,451] [              HABApp.Rule]    ERROR | 
[2023-07-28 14:11:42,451] [              HABApp.Rule]    ERROR | During handling of the above exception, another exception occurred:
[2023-07-28 14:11:42,451] [              HABApp.Rule]    ERROR | 
[2023-07-28 14:11:42,451] [              HABApp.Rule]    ERROR | Traceback (most recent call last):
[2023-07-28 14:11:42,451] [              HABApp.Rule]    ERROR |   File "/usr/local/lib/python3.10/site-packages/HABApp/core/lib/exceptions/format.py", line 46, in format_exception
[2023-07-28 14:11:42,451] [              HABApp.Rule]    ERROR |     added = format_frame_info(tb, frame_info, is_last=i == last_frame)
[2023-07-28 14:11:42,451] [              HABApp.Rule]    ERROR |   File "/usr/local/lib/python3.10/site-packages/HABApp/core/lib/exceptions/format_frame.py", line 75, in format_frame_info
[2023-07-28 14:11:42,451] [              HABApp.Rule]    ERROR |     format_frame_variables(tb, frame_info.variables)
[2023-07-28 14:11:42,452] [              HABApp.Rule]    ERROR |   File "/usr/local/lib/python3.10/site-packages/HABApp/core/lib/exceptions/format_frame_vars.py", line 108, in format_frame_variables
[2023-07-28 14:11:42,452] [              HABApp.Rule]    ERROR |     tb.append(f'{" " * (PRE_INDENT + 1)}{name} = {repr(value)}')
[2023-07-28 14:11:42,452] [              HABApp.Rule]    ERROR |   File "/usr/local/lib/python3.10/site-packages/HABApp/core/internals/event_filter.py", line 12, in __repr__
[2023-07-28 14:11:42,452] [              HABApp.Rule]    ERROR |     return f'<{self.describe()} at 0x{id(self):X}>'
[2023-07-28 14:11:42,452] [              HABApp.Rule]    ERROR |   File "/usr/local/lib/python3.10/site-packages/HABApp/core/events/filter/event.py", line 64, in describe
[2023-07-28 14:11:42,452] [              HABApp.Rule]    ERROR |     return f'{self.__class__.__name__}(type={self.event_class.__name__}{values})'
[2023-07-28 14:11:42,452] [              HABApp.Rule]    ERROR | AttributeError: 'ChannelTriggeredEvent' object has no attribute '__name__'
[2023-07-28 14:11:42,452] [              HABApp.Rule]    ERROR | 
[2023-07-28 14:11:42,453] [              HABApp.Rule]    ERROR | --------------------------------------------------------------------------------
[2023-07-28 14:11:42,453] [              HABApp.Rule]    ERROR | Partial Traceback:
[2023-07-28 14:11:42,453] [              HABApp.Rule]    ERROR | --------------------------------------------------------------------------------
[2023-07-28 14:11:42,453] [              HABApp.Rule]    ERROR | File "/usr/local/lib/python3.10/site-packages/HABApp/rule_ctx/rule_ctx.py", line 44 in unload_rule
[2023-07-28 14:11:42,453] [              HABApp.Rule]    ERROR | --------------------------------------------------------------------------------
[2023-07-28 14:11:42,453] [              HABApp.Rule]    ERROR |      32 | def unload_rule(self):
[2023-07-28 14:11:42,453] [              HABApp.Rule]    ERROR |       (...)
[2023-07-28 14:11:42,453] [              HABApp.Rule]    ERROR |      41 |         while self.objs:
[2023-07-28 14:11:42,453] [              HABApp.Rule]    ERROR |      42 |             with HABApp.core.wrapper.ExceptionToHABApp(log):
[2023-07-28 14:11:42,454] [              HABApp.Rule]    ERROR |      43 |                 to_cancel = next(iter(self.objs))
[2023-07-28 14:11:42,454] [              HABApp.Rule]    ERROR | -->  44 |                 to_cancel.cancel()
[2023-07-28 14:11:42,454] [              HABApp.Rule]    ERROR |      45 |         self.objs = None    # Set to None so we crash if we want to schedule new stuff
[2023-07-28 14:11:42,454] [              HABApp.Rule]    ERROR |    ------------------------------------------------------------

Your IDE should be red and screaming since you pass the wrong objects.

This is the correct way:

self.listen_event('deconz:switch:00212E00C488:04cd15fffe6f9d60011000:buttonevent', self.on_channel, EventFilter(ChannelTriggeredEvent, event="1002"))

Is it possible to somehow prevent HabApp from getting into an infinite logging loop?

Fix your code and restart your container. :wink:

Itā€™s always possible to find issues which could lead to an infinite loop.
Thatā€™s why I provide type hints so these issues donā€™t arise.
Iā€™ll add an additional check which prevents this specific issue in future versions but you should listen to your IDE non the less.

I stumbled upon this thread when trying to make use of the astro:sun channels in my OpenHAB installation. From what Iā€™ve read the code should be something like

self.listen_event('astro:sun:local:noon#event', self.log_event, EventFilter(ChannelTriggeredEvent, event="START"))

However, even without the event="START" it seems that there is an underlying issue I cannot get my head around:

WARNING | Item "astro:sun:local:noon#event" does not exist (yet)! self.listen_event in "DummyRule" may not work as intended.

Playing around with those events is a bit cumbersome as they donā€™t trigger often :wink:

i beleive it should work.

i also trigger many channels, and i also have this warnings in the log but the triggers are executed anyway. for example:

2024-03-15 14:04:45.222 [WARN ] [HABApp.Rule                         ] - Item "openwebnet:bus_cen_scenario_control:bticino:EWoK_Cen_Lamellen:button#2" does not exist (yet)! self.listen_event in "Shutters" may not work as intended.
2024-03-15 14:04:45.223 [WARN ] [HABApp.Rule                         ] - Item "openwebnet:bus_cen_scenario_control:bticino:Hwr_Cen:button#1" does not exist (yet)! self.listen_event in "Shutters" may not work as intended.
2024-03-15 14:04:45.223 [WARN ] [HABApp.Rule                         ] - Item "openwebnet:bus_cen_scenario_control:bticino:EWoK_Cen_Ja:button#17" does not exist (yet)! self.listen_event in "Shutters" may not work as intended.
1 Like

When you create an event listener HABApp checks if there is e.g. an item or a retained mqtt topics so you can catch typos. Normally this is not an issue because you create the filters through the item objects.
This is the corresponding warning that the check was not successful and that there might be a typo.
Of course in your case itā€™s ok to ignore this warning.

If you want to get rid of it you can create a string item, link it to the event and use the system:trigger-event-string profile to pass the event to the item. That way youā€™ll also have the last event saved in the item.
This is what Iā€™m doing and what I would recommend.

2 Likes

Indeed, it worked. I was just confused by those warnings. I tried before finding this thread but discarded the idea as I thought channels are not supported.
Thank you for the explanation and the guidance!