JSR233 jython rule dynamic triggers

  • Platform information:
    • Hardware: RPi3
    • OS: openhabian latest
    • Java Runtime Environment:
    • openHAB version: 2.1 stable

I’m trying to write a set of rules that handles turning GPIO Pins on my raspi on and off based on configuration and environment values (MHT22).

So, for instance, i would like to create a rule that turns a pin on and off based on a timer. BUT: I would like to define the hours and minutes dynamically - i have already defined some items for hours and minutes (maybe weekdays and such lateron), and i used a Selection on the sitemap to change these values - so far, so … ok.

Now, with the nice item_triggered decorator in jython (thank you @steve1), reacting to a changed minute or hour value seems easy enough.

But how would i go ahead and change a trigger on the current rule? Is there any way to modify triggers/rules dynamically and make OH react on the changes?

Best regards,

I know i could use a threading.Timer object, but it is more for situations that are relative to now (i.e. “in half an hour, turn off the lights in the kitchen” or “5 minutes after noone is home, turn on the alarm”), but i’m looking for exactly what the CronTrigger does, only dynamically.
Plus, my timer objects won’t survive a restart, i want a trigger in openHABs workings to make it durable.

Can anyone point me in the right direction, please?

Ok, my progress so far:

@steve1 hints in openhab.osgi.events on accessing triggers. I’m guessing now that it must somehow be possible to change the Configuration of a trigger so that it corresponds to the new values.
But i can’t guess on how to access the collection of triggers for a rule within openhab. Maybe @smerschjo can point me in the right (documentation) direction? I’m afraid i’m not fit enough to find what i need by just reading the code :cold_sweat:

It would be much easier to just deregister the previous rule and register a new one with the updated triggers. OH1 used Quartz as the scheduler for time-based rule triggers. I think OH2/ESH uses a custom-written scheduler. In either case, it wouldn’t be easy to directly deregister and re-register the trigger itself with those internal schedulers.

Ok, that sounds feasible. I’m guessing that changing the contents of the array of triggers for the given rule (self.triggers) will be enough?
As for removal and addition: I’m using your extensions like the rule decorator and the addRule function - but i can’t find a removeRule - should that be in automation_manager?

I’m not able to try this at the moment, but there is a ruleRegistry object in the script scope with a remove function. The argument to the function is the rule UID (rule.getUID()). I’d try that. The type of the ruleRegistry is RuleSupportRuleRegistryDelegate, which you could look up in the GitHub SmartHome repository if you need more details.

1 Like

Thank you very much, that sounds very promissing!

Hi @steve1 ,

removing and adding the rule works fine, the dynamic CronTriggers fire too - but even though i set the triggerName, there’s nothing to recognize the fired trigger once it hits the execute Method - i even tried to use setLabel() to put my triggername there - but to no avail.

Is there any other method to differentiate between the CronTriggers fired? input seems to be empty, only the module parameter is filled, but nothing specific to the actual trigger, it seems.

Are you saying you have multiple CronTriggers in a single rule and you want to know which one fired?

Exactly, i’m constructing those triggers based on times set through items in my OH config. Once they fire, the execute method gets called for this one big rule, and i can’t tell which of the crontriggers fired.
For the other triggers it’s easy (all ItemStateUpdateTriggers), they have input['module'] set to their respective triggerNames, so i can take it from there - but the CronTriggers don’t have that, so i don’t know which it is.

Looking at the ESH trigger definition, it looks consistent with the missing input data you saw. Is it necessary to have one rule versus several or many single trigger rules with some rule instance information to identify the trigger? They could all call the same function with the trigger identifier, if that’s needed.

These crontriggers turn three different relais on and off - so i would at least need to differentiate between a CronTrigger that’s supposed to turn a relais on vs. one that’s supposed to turn a relais off.
If i can’t make a difference between the triggers, i need 6 different rules, one for each possible trigger, maybe tie them together with an Observer.

I can see in your CronTrigger definition you’re supplying the triggerName to the ESH Trigger as its ID, but nevertheless, this doesn’t come back as expected - is that by design or is it worth to open a bug?

I don’t get it… the RuleEngineCallbackImpl does know the triggerName (or id), because it outputs it in the debug log at public void triggered(Trigger trigger, Map<String, ?> outputs), but the execute method of my rule doesn’t get an input like the other triggers do - where the difference? Can anyone enlighten me?