Can i create rules via the REST-API?

Hi i just recently started playing with openhab2 and one thing that i’m missing is an easy way to do scenes.
So i was looking into creating a little web-app to help with that. So what i want to do is this:

  • get a list of all Items filtered by group and/or type
  • let the user select multiple items
  • let the user enter a name for the scene
  • create a Switch Item for the scene
  • create a rule that sets the state of all selected items to their current states

Now i have been playing with the REST-API and the first 4 points seem pretty straight forward, but i can’t figure out how to create a rule. I installed the rule engine in PaperUI and it’s not working at all. I doesn’t show me any trigger types action types or whatever.
Is this thing just broken atm or am i doing something wrong?

Also if there is a simpler way to do what i want that i don’t know about please tell me.

Thanks

Have you refreshed paper ui? I have no issue viewing the rule engine. Also, you may want to look into rules via node-red…

Personally I have no experience with it, but…i have used mit’s app inventor which uses a similar language. Being a visual language may help, plus it is very capable of handling even the most difficult rules.

1 Like

What exactly do you mean by refreshed? reloaded in the browser? yes i did that several times on several browsers.
I havent tried restarting openhab yet but it seems like that shouldn’t be necessary.
I know of Node-RED, but that would require quite a bit of work to make everything talk via mqtt. Also being a visual language makes it rather useless for my needs. I want to programatically create rules.

If you want to do it programatically you’ll want to use the rules text files http://docs.openhab.org/configuration/rules-dsl.html

Yes i am aware of the rules files. But then i need a way to get the file i created into the proper folder. I mean writing a Node.js App that takes the file and puts it there shouldn’t be hard, but I kinda wanted to do it without putting another one-trick-pony web-server on my rPi.
Also i want to do this in html/js because that allows me to embed the thing in HABPanel or potentionally turn it into a widget.

Your past my pay grade on that one…

1 Like

This is something I’ve been thinking about as well and haven’t really thought of a good way to implement it yet. It would be nice to have a way to dynamically create a scene, chose what members are part of the scene, and chose the setting for each device from a panel and have that persist.

I’m still trying to work out how to do that only I don’t have any java script experience yet so that’s my first hurdle. If I come up with a way I will post it to this forum. And I hope others will do the same.

With the default Rules Engine there no way to add rules through the REST API.

I do not know what is or is not possible using the Experimental Rules Engine or the JSR233 Add On.

I suspect the Experimental Rules Engine does or will support adding rules through the REST API.

That’s what i figured. The experimental rules engine does expose a REST API, but it’s not all that intuitive and since i couldn’t create a rule with paperUI and see what it looks like, that was a problem. However after a restart of openhab PaperUI works so it shouldn’t be to hard now to figure out how to do it now. Once i got something that’s working i’ll post it here.

One possibility is to communicate the scene definitions from the UI as a JSON structure (a list of item/command pairs) assigned to an item state (via ReST). I’d likely create a new item for each of the defined scenes, but it would be possible to put all the scene definitions into one item state. I’d have a single rule that would be triggered by a scene name item command. The triggered rule would “look up” the scene definition by querying the state of the scene definition item (generating the definition item name dynamically based on the scene name in the case of separate definition items). The rule would then parse the associated JSON and send the item commands. I’d be using a Jython rule rather than XText because it would be trivial to parse the JSON data and iterate over it.

I was perhaps interpreting OP’s original question a little more literally than you are.

Of course, it is possible to set up what amounts to a rule interpreter on top of the existing Rules Engine, which is what you are describing.

It is also possible to do something similar in a number of other ways and browsing through the forum will show several other approaches (state machines, group manipulation, etc).

Personally, before I went to all that trouble I would answer the following questions:

  • would something like NodeRed or some other external rules execution approach do what I need with less work?
  • is there some other approach I can use that would work better or be less work?

Usually, the answer to one or both of those questions is yes. But I’m more interested in the end product and getting there as quickly and cleanly as possible than I am in finding a clever way to do something. I’m far more likely to re-examine and modify my initial requirements than I am to spend a ton of effort building up what would amount to a whole new language.

Iterating over a list of of name/command pairs and sending the command to the named item is not a “rule interpreter” by most people’s definition of that term. It could probably be implemented in 1-3 lines of Jython code.

In which case I’m not sure you are answering OP’s original question which was being able to add a new Rules through the REST API.

Agreed. I’m suggesting a way to implement the scene editor functionality the OP wants to implement. Dynamically generated rules through the official ReST API (vs. a custom ReST endpoint exposed by JSR223 specifically for defining new rules) might be one way to it, but obviously that way is not well supported by openHAB, if at all.

Well actually, now that i can create rules in PaperUI and look at the generated JSON the whole thing lokks not that bad.
I created a simple rule in PaperUI and the JSON looks like this:

"triggers": [
      {
        "id": "1",
        "label": "an item state changes",
        "description": "This triggers the rule if an item state has changed.",
        "configuration": {
          "itemName": "swAllOff",
          "state": "ON"
        },
        "type": "core.ItemStateChangeTrigger"
      }
    ],
    "conditions": [],
    "actions": [
      {
        "inputs": {},
        "id": "2",
        "label": "send a command",
        "description": "Sends a command to a specified item.",
        "configuration": {
          "itemName": "WZ_LED_Rechts_Color",
          "command": "0,100,80"
        },
        "type": "core.ItemCommandAction"
      }
    ],
    "configuration": {},
    "configDescriptions": [],
    "uid": "rule_1",
    "tags": [],
    "visibility": "VISIBLE"
  }

So all i need to do is:

  • create a Switch Item to trigger the rule.
  • create the rule object
  • add a trigger object for the switch
  • create an action replicating the current state for each selected item

As for the JSR223 solution, i haven’t really looked into that, but judging from a quick google that seems about as experimental in ESH as the ngRule engine.

That’s true since JSR223 is just a way to script the creation of the ngRule engine components. In other words, it’s the same rule engine. The difference is that with one approach you will write code to generate relatively complex JSON that is then used to create relatively simple rules instead of creating the rules directly using a JSR223 scripting language like Javascript or Jython.

You are looking for the result of http://SERVERADDRESS:8080/rest/module-types (replace [SERVERADDRESS] with your local OpenHab ip address)

One parameter is filtering the query by action, condition or trigger.

Hi,

@hrc
Even I am trying to created some thing similar, as u have tried say

  1. get a list of all Items filtered by group and/or type
    2.let the user select multiple items
    3.let the user enter a name for the scene
    4.create a Switch Item for the scene
    5.create a rule that sets the state of all selected items to their current states

Now, I am getting the list of item , but not by filtering by group. Can u please share how exactly u have got it working.