Jython and GenericEventTrigger

I would like to catch any item events using Jython. I pieced the code below from various examples on the forum but still cannot get it to catch any events…

scriptExtension.importPreset("RuleSimple")
scriptExtension.importPreset("RuleSupport")
scriptExtension.importPreset("RuleFactories")


from core.rules import rule
from core.triggers import StartupTrigger, CronTrigger, ItemStateUpdateTrigger, GenericEventTrigger
from core.log import logging, LOG_PREFIX


@rule("Any Item Changed")
class AnyItemChanged(object):
    def __init__(self):
        self.triggers = [GenericEventTrigger("","openhab/*","ItemStateChangedEvent").trigger]
    
    def execute(self, module, inputs):
        self.log.warn("Item Changed ***************")

Looking at the JavaDocs, a Rule requires a list of EventTrigger Objects.

Browsing and clicking through the JavaDocs, I can’t even find a GenericEventTrigger Class nor Interface. So I can’t even tell if that’s something from OH at all or if the search is broken.

Looking at the Helper Library, a GenericEventTrigger is indeed something that only exists in the Helper Library. One thing that is telling is the comment

It’s one of the the most powerful triggers, but it is also the most complicated to configure.

openhab-helper-libraries/Core/automation/lib/python/core/triggers.py at ivans-updates · CrazyIvan359/openhab-helper-libraries · GitHub starting at line 614.

I think your best bet will be to study the code and modify the logging on the event bus to log all events and see if you can gather what each of the arguments to the GenericEventTrigger need to be.

In general, I don’t know that it’s possible to trigger a rule on any and all events.

Here is the documentation page from OH 3.1. Mostly I am not sure how wildcards work…

I am going to try logging from the helper library.

@CrazyIvan359 - any thoughts?

For anyone interested in using Jython and the extended triggers, the code below should help.

scriptExtension.importPreset("RuleSimple")
scriptExtension.importPreset("RuleSupport")
scriptExtension.importPreset("RuleFactories")


from core.rules import rule
from core.triggers import StartupTrigger, CronTrigger, ItemStateUpdateTrigger, GenericEventTrigger, ItemEventTrigger, ItemStateUpdateTrigger
from core.log import logging, LOG_PREFIX


@rule("Any Item Changed")
class AnyItemChanged(object):
    def __init__(self):
        self.triggers = [GenericEventTrigger("openhab/items/", "ItemStateEvent", "openhab/items/*", "").trigger]
    
    def execute(self, module, inputs):
        self.log.warn("Item State Event {} ".format (inputs.get('event')))




1 Like