[SOLVED] Rule not loaded correctly

I’m using the openhab2-jython scripts for rule support.

Rules using the rule decorators work fine, however, for a special use case I have my own decorators, so I want to directly register at at automationManager.

It seems that the rule is not loaded correctly. I can see the rule in PaperUI, but there are no triggers? However, the trigger is set in the rule.

The rule worked fine with openhab 2.3 and the old version of the openhab2-jyton scripts.

Idea is that there is a String item and when a string is written to it a function with that name is executed.
Therefore, the decorator task = lambda f: tasks.setdefault(f.__name__, f) adds the function name and the function itself to a dictionary.
This mechanism works fine (the log.debug shows the dictionary), however, there is no trigger for the rule, altough a trigger is set with self.triggers = [ItemStateChangeTrigger("global_command")]??

Am I missing some mandatory field for the SimpleRule class? Or what else could cause this problem?


from core.triggers import ItemStateChangeTrigger

from core.log import logging, LOG_PREFIX

rlog = logging.getLogger("RULES.CMD.Global")

tasks = {}
task = lambda f: tasks.setdefault(f.__name__, f)

class GlobalCmdHandler(SimpleRule):
    def __init__(self):
        self.triggers = [ItemStateChangeTrigger("global_command")]
        rlog.debug(u"Loading global command handler")
        self.__name__ = "Global Command handler"

    def idle():
        return False

    def all_off():
        rlog.info(u"Alles AUS!")   
        # details omitted
        return True       

    def chill():
        rlog.info(u"LED-Mode chill")
        # details omitted
        return true

    # execute method maps global_command to a method with the same name.
    def execute(self, module, input):       
        rlog.debug(u"Key: " + unicode(input))
        rc = tasks.get(str(items.global_command), lambda: rlog.error("Wrong key"))()
        if (rc):
            rlog.debug(u"Updateding global_command back to idle state")
            events.postUpdate("global_command", "idle")

gcmd = GlobalCmdHandler()

Rule in PaperUI (–> no trigger)

Debug output when rule is loaded:

2019-02-03 23:25:45.071 [DEBUG] [RULES.CMD.Global      ] - Loading global command handler
2019-02-03 23:25:45.156 [DEBUG] [RULES.CMD.Global      ] - {'idle': <function idle at 0x2d>, 'chill': <function chill at 0x2e>, 'all_off': <function all_off at 0x2f>, 'panic_mode': <function panic_mode at 0x30>}
2019-02-03 23:25:45.214 [DEBUG] [RULES.CMD.Global      ] - [org.python.proxies.core.triggers$ItemStateChangeTrigger$19@18a76c2]

–> Rule is loaded and there is “some” ItemStateChangeTrigger.

Debug output when rule is ran manually:

23:28:17.827 [DEBUG] [RULES.CMD.Global     ] - Key: {}
23:28:17.860 [ERROR] [RULES.CMD.Global     ] - Wrong key

–> Rule can be executed. The error is as expected, because the rule expects a parameter as key for its internal dictionary.

Ah… I found the issue.

Instead of

it must be

self.triggers = [ItemStateChangeTrigger("global_command").trigger]

Seems to be somehing that has changed in openhab2-jython.

There was a change in the automation API, introducing TriggerBuilder. The modules were updated in order to be compatible.