How to start developing on top of openHab + Developing My Own Rule Engine

I would like to develop my own rule engine on top of openHab. Thus i need to figure out how to interact with openHab System Directly. So any advice would be appreicated on how to get started

This begs the question what do the Rules DSL, Next Gen Rules Engine, JSR 223 rules in Jython, JavaScript, or Groovy, NodeRed, Prend, or HABApp do not provide that you would create yet another rules engine?

You best bet is to look at the OH core and OH automation repos for how they do it.

Will basically i am trying to integrate semantic rule languages such as prolog with openhab

You need an OSGi bundle deployed to karaf, openhabs OSGi environment.

Get the event bus service via reference. The event bus allows you to get notified about item changes and updates (and everything else) and allows you to change items as well.

This is all in the developer docs.

Dear david,
I appreciate your answer i am still a newbie in understanding the OSGi concepts. And actually having trouble on how to develop on top of openhab.

It sounds like you are looking more to create something like a script engine rather than a complete rule engine, so specifically look at org.openhab.core.modules.script. Although, if you built something that worked with javax.script, OH could use it.

If you are this interested why not develop with openHAB and share your contribution to everyone?..

1 Like

@Thedannymullen i don’t know what you actually mean but i am looking to develop with openHab. I am trying to build an engine on openHab that instead of using the default rule engine it goes to a software called protege that uses a reasoner to evaluate things and then take the response and apply it on openhab

@5iver what does the script bundle do??

JSR223 (spec) is a standard scripting API for Java Virtual Machine (JVM) languages. The JVM languages provide varying levels of support for the JSR223 API and interoperability with the Java runtime. Currently the following languages are known to work well for openHAB scripting: Jython (Python on the JVM), Nashorn Javascript (ECMAScript implementation included in JDK8 through 10, deprecated in 11), and Apache Groovy (JVM scripting language).

Although JSR223 scripts can be used as a general-purpose extension language for openHAB, it is most commonly used for the creation of rules, and within scripted Actions or Conditions. Currently, openHAB allows JSR223 scripting to access all packages, which may not be included in the official APIs. This provides great flexibility for the users of JSR223, but is also use at your own risk , since changes outside of the offical APIs occur frequently, and are not considered to be breaking changes . New APIs are planned to be implemented in the future, which will provide standardized interfaces for interacting with openHAB through scripted automation.

Taken out of:

So this could be an existing api you could already use to biuld your rule engine on top of it.

1 Like

Ummm… read it? :wink:

It utilizes javax.script for scripted automation. With it you can use the automation API (in the new rule engine) to create rules using any JVM language that supports JSR223 (Jython, JavaScript, Groovy, etc.).

There are several interfaces for building rules with the new engine, and the documentation is evolving.

1 Like

Protege is java based. So it actually makes sense to stay at the java level for performance reasons in this case.

The tl;dr with OSGi and protege is something like this (based on

public class ProtegeBridge implements EventSubscriber {
    private final Set<String> subscribedEventTypes = ImmutableSet.of(ItemStateEvent.TYPE, ItemCommandEvent.TYPE);
    private final EventFilter eventFiter = new TopicEventFilter("smarthome/items/.*");
    private Protege protege; // PSEUDO CODE

    public Set<String> getSubscribedEventTypes() {
        return subscribedEventTypes;

    public EventFilter getEventFilter() {
        return eventFilter;

    public void receive(Event event) {
        String topic = event.getTopic();
        String type = event.getType();
        String payload = event.getPayload();
        if (event instanceof ItemCommandEvent) {
            ItemCommandEvent itemCommandEvent = (ItemCommandEvent) event;
            String itemName = itemCommandEvent.getItemName();
            Command command = itemCommandEvent.getItemCommand();
        } else if (event instanceof ItemStateEvent) {
            ItemStateEvent itemStateEvent = (ItemStateEvent) event;


    void activated() {
       protege = new Protege(); // PSEUDE CODE: Init protege

    void deactivated() {
       if (protege!=null) protege.dispose(); // PSEUDE CODE: free protege

Not complete, it doesn’t handle sending item changes or reacting to anything else than item changes.
But it’s that easy to integrate into openHAB. Just create an OSGi service like above, create your protege objects and interact with them.

You would just compile the above with the openhab Core dependencies and OSGi annotation dependencies declared in your pom file and then drop the generated jar file into the addons directory together with the protege stuff. This assumes that you want to run protege inside of the openHAB OSGi container.

Cheers, David