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
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.
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
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.
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.
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!
I canāt get that working.
I want a rule to be triggered on sun set using the openHAB Astro binding. Something that would look like
Channel astro:sun:local:set#event triggered START
in DSL.
So in my rule Rule_ESP32_ChickenDoors_Automation
I call
self.listen_event("astro:sun:local:set#event", self.on_event,
EventFilter(ChannelTriggeredEvent, event="START")
When the rule is instantiated during startup, I get the log message
Item "astro:sun:local:set#event" does not exist (yet)! self.listen_event in "Rule_ESP32_ChickenDoors_Automation" may not work as intended.
The rule isnāt triggered when sunset startsā¦
Any help appreciated.
So you see the event in the HABAppEvents.log
?
Another obvious easy solution is to use the HABApp scheduler to trigger on sunset ā¦
There are other work arounds like adding a rule in openHAB updating an item, Iām awareā¦ But Iād prefer to have the option to listen to channel triggers.
I have checked HABApp_events.log and it shows trigger events. In the sample below is no astro:sun:local:set#event
included - that has been rotated out since yesterday eveningā¦ I have rules on astro:sun:local:rise#event
too - but they didnāt fire either.
[2024-12-07 08:32:00,010] [ HABApp.EventBus] INFO | astro:sun:local:rise#event: <ChannelTriggeredEvent name: astro:sun:local:rise#event, event: START>
[2024-12-07 08:37:00,009] [ HABApp.EventBus] INFO | astro:sun:local:daylight#event: <ChannelTriggeredEvent name: astro:sun:local:daylight#event, event: START>
[2024-12-07 08:37:00,012] [ HABApp.EventBus] INFO | astro:sun:local:rise#event: <ChannelTriggeredEvent name: astro:sun:local:rise#event, event: END>
The HABApp Scheduler is much more powerful so in case of sunrise/sunset using the channel trigger is a crutch and the actual workaround.
It itās in the event log you can trigger on it. There must be something else wrong.
You can also try without an event filter since the name seems unique enough.
Background why Iād like to stay with triggers if possible:
As mentioned in another post, Iām in the process of migrating a big Jython home automation and energy management solution. Rather than jumping on new APIs I prefer to keep the code unchanged and adopt to the new framework (HABApp in this case) by adding helper and wrapper functions. As an example, I have added the well known @rule, @when etc decorators used in the Jython environment and matching DSL directly.
@rule("Rule_Security_System")
@when("System started")
@when("Item Presence changed")
@when("Item Group_Security_EntranceDoorContact changed")
@when("Item Group_Security_DoorContact changed")
@when("Member of Group_Security_Motion received update")
def rule_security_system(event):
info(LOG, "Rule_Security_System: triggered with event {}", event)
My decorators map everything to HABApp event handling and include all standard DSL pattern including most of Time cron
. Nothing against superior frameworks , but as long as channel triggers are supported in HABApp, Iād like to use them. In general, Iām scared with the Python support in openHAB. Currently I can either use the depreciated and unsupported Jython environment or your 3rd party solution. I even considered leaving openHAB altogether because of this.
I will try the channel triggers without filter to check whether this is the culprit. And Iām aware chances are good the problem is in my code.
Do you have instructions how to replace the openhabian HABApp installation by a developer version from GitHub? I assume openhabian has set a specific HABApp configuration when installed with openhabian-config? I assume I need to apply that too to not break the current system. A developer version would allow me to trace what is going onā¦