runRule equivalent in Python scripting

The rules.runRule() function is part of the openHAB JavaScript Scripting library, used to programmatically trigger another rule from within a script or rule.

I am wondering If there ist a similar function in Python Scripting. Any modules to be imported?

Searching the docs and the web was not helpful so far. Thanks.

At the end, all languages are using the java api. I just checked the js source code

Just port this to python and it should work.

If you are not successful, I can provide a more detailed example. Currently I’m afk.

Hi Holger,
thanks a lot. Your feedback was leading me to the following description:

from core import osgi
ruleEngine = osgi.get_service("org.openhab.core.automation.RuleManager")
ruleEngine.runNow(ruleFunction.UID)

In my case the import of osgi fails. I get this error:

ModuleNotFoundError, No module named 'core'
Please, advice. Thanks.

following the javascript example, you must do the following things

  1. Get the rule service
  2. Check the rule
  3. call runNow on the rule manager
from openhab.services import getService

ruleManager = getService('org.openhab.core.automation.RuleManager')

status = ruleManager.getStatus("<myruleid>")

if not status:
     print("your rule does not exists")

if status == "UNINITIALIZED":
     print("your rule is not initialized")

ruleManager.runNow("<myruleid>", True, {});

btw. the example on openhab scripters are outdatet since years and not maintained by any openhab dev.

Thanks a lot. Your example is working as expected and is solving my problem.
Where do I find related information in the docs? I don’t want to bother you in the future.

I improved the documentation