I’m exploring some of the new stuff that is possible with JSR223 and have stumbled upon the OSGI Event Rule trigger. In particular I’m interested in ItemUpdatedTrigger. Unfortunately the Helper Library Docs and Extensions examples are not very clear about when this event triggers and I can’t figure out how to create a Rule that actually uses this trigger to try and experiment and answer some of these questions on my own.
- When any part of the Item is updated, e.g. metadata, Group membership, label, etc?
- What get’s put into the event Object passed to the Rule?
- Can I use the @when decorator? If so what is the syntax? “Item Blah updated” doesn’t appear to work.
- Can I use “Member of Item BlahGroup updated”?
- Are the docs up to date for creating a Rule using Extensions? I can’t even get as far as creating a Rule with as I keep getting
Error during evaluation of script 'file:/openhab/conf/automation/jsr223/python/personal/dp/timeofday.py': TypeError: __init__() takes exactly 1 argument (2 given) in <script> at line number 31
The code
from core.rules import rule
from core.triggers import when, ItemUpdatedTrigger
from org.joda.time import DateTime
from core.utils import sendCommandCheckFirst
from core.metadata import get_key_value
from time import sleep
from core.actions import ScriptExecution
from configuration import tod_group, tod_item
from core.log import log_traceback, logging, LOG_PREFIX
scriptExtension.importPreset("RuleSupport")
scriptExtension.importPreset("RuleSimple")
class ToDUpdatesRule(object):
def __init__(self):
self.triggers = [ItemUpdatedTrigger("vDay_Time").trigger]
self.name = "Update Items when they change"
self.description = "Triggers when an Item changes it's definition."
self.tags = set("ToD")
self.log = logging.getLogger("{}.ToDUpdatesRule".format(LOG_PREFIX))
def execute(self, module, inputs):
self.log.info("vDay_Time updated")
automationManager.addRule(ToDUpdatesRule())
Note that there are lots of extra imports because I have the rest of my ToD Rule in this file. I’m looking at ways to update Item when the Item’s metadata changes, for instance.
The error points to the automationManager line or, when I try to use the @rule decorator it points at the @rule line. As you can see, I’m not passing in two arguments to any constructor as far as I can tell.
I’m using the latest snapshot of OH with the latest master from the helper libraries.