How to run a rule from a rule

I am trying to run a rule from within a rule using this:

    if event.itemName == 'TestTimeField':
        rule_registry = get_service("org.openhab.core.automation.RuleRegistry") or get_service("org.eclipse.smarthome.automation.RuleRegistry")
        for rule in [rule for rule in rules.getAll() if rule.name == 'Test']:
            rule_registry.runNow(rule.UID)

but I get an error:

AttributeError: 'org.openhab.core.automation.internal.RuleRegistryI' object has no attribute 'runNow'

Does anyone know what I’m doing wrong?

First, what is the name of the function defined for the rule? If the rule was created with the decorators, then the UID was added as an attribute, so no need to search for it if in the same script… my_rule_function.UID.

https://openhab-scripters.github.io/openhab-helper-libraries/Guides/Rules.html?highlight=uid#rule

Once you have the UID, you’ll need to use RuleManager (not RuleRegistry) to execute it…

https://openhab-scripters.github.io/openhab-helper-libraries/Guides/But%20How%20Do%20I.html#run-a-rule-by-uid

I was using this as my guide
https://openhab-scripters.github.io/openhab-helper-libraries/Examples/Examples.html
I must admit to not fully understanding what I’m doing. Just copy pasting…

To answer your question, yes, the rule was created using decorators, but it is in another file, so I think I still have to search.

Using RuleManager made it work. Thank you.

That example is not right! Thank you… fixed (after I merge the commit :wink:).

Yep

:+1:

If you have any other questions, just shout!

1 Like