[OH3] How to 'create new' Rule DSL in OH3 UI?

Hello everyone
I am playing around with the new OH3 snapshots. I am not at all adept at writing JS rules. But i also know that the new OH3 UI supports Rule DSL. I am hoping it supports both - creating new and reading from the .rules files.

Could some bodybe kind to guide me on how to start writing Rules DSL scripts using the OH3 UI?
I read somewhere on Github that these need to start with //context: <something> and indeed a .rules file appears so in the UI. But what about creating a new one? How do i start? Whats the value after context?

My simple newrule.rules file:

rule "New rule for testing"
when
Item HallTV received command On
then
logInfo("TV","Received command On")
end

how it looks in OH3 UI:

// context: newrule-1
logInfo("TV","Received command On")

I see many things all mashed together here.

  1. JS rules use the NGRE Rule engine or manuual install in OH 2 & 3.

  2. Rules DSL. In OH 2 these were generally file based. I understand OH3 accepts filr based rules in addition to allowing creation through the UI.I have created a couple of rules through the UI.

Are you wanting to write rules files or use the UI??

This does not use the UI though.

This is extremely confusing.

ok sorry, i will try to re state.

OH3 UI allows creation of rule scripts using ECMA - 262 5.1 (which i assume is the Nashorn / JS223 implementation)

My simple objective is to create a Rule DSL script using this UI (–>edit script). What do i write here? You mentioned you have done this, can you share some example?

The example i posted above.
newrule.rules file - created from the command prompt and saved in
When seen from the UI:
image

Another example where it did not work for me. I created this Rule DSL script - i just get loads of errors.
image

2020-09-20 02:48:23.803 [WARN ] [e.automation.internal.RuleEngineImpl] - Fail to execute action: 1
org.eclipse.xtext.parser.ParseException: java.lang.IllegalStateException: Unresolved proxy https://openhab.org/model/Script#//Script. Make sure the EPackage has been registered.
        at org.eclipse.xtext.parser.antlr.AbstractAntlrParser.doParse(AbstractAntlrParser.java:105) ~[?:?]
...

Hope it makes sense what i am trying to accomplish.

I have not seen that menu option. How did you get that?? I only see Rules mentioned in the UI, not Scripts. I am running Build #1928.

I just when to the Rules menu on the left & clocked the + sign, starting a new rule. No choice of rule type.

On the screen with the rules list, click on the blue “+” button in the lower-right corner, give an ID and a name, then click “Add action”, pick “execute a given script”, choose “Rule DSL” as the language then you can write your script the same way you would in the “then” section of a rule in .rules file.

Add a trigger if needed and click Save. Currently this will get back to the rules list (but it will probably be changed).

If you need to continue editing the rule again, click on it then on the little icon on the action item - it’s a shortcut to edit the script, saves a click.
Tip: while you’re editing you can use Ctrl+S to save it and Ctrl+R to run it immediately (no matter what the triggers are).

If you write your rules in a .rules file, they will appear in the UI but you can’t edit them (that’s why you have a lock icon after the name).

1 Like

Thanks but this doesn’t work. See screenshot above where I just put a logInfo("abcd","abcd") and it gives an error

No, I am also using #1928:

2 Likes

Hm, you’re right, I can reproduce this, pretty sure it worked before. Maybe @Kai can comment - otherwise please open an issue here: https://github.com/openhab/openhab-core/issues to have to looked at.

1 Like

Would have to dig into it as well - so yes, please open an issue for it.

1 Like

Okay, its weird but it started working. On its own. I dont know how. The only thing i did was (1) update by raspberry pi OS and (2) link the rule to trigger from an item instead of directly triggering from UI. And now it executs every time; even if i create a new scipt from UI.

Initially I thought it could be related to these (the JSR223 scripts still suffer from this) -


But I just cant reproduce the issue now. Rule DSL scripts created from the UI are executing fine. I restarted the OS, re-created rules, wrote a new script etc., but it just seems to be working fine now. @ysc please check the behaviour at your end too. Maybe by creating a new Rule DSL from UI, triggering it from an item and then triggering again from UI.

1 Like

I am experiencing a similar issue with OH3 build 1945.
When I write a simple rule and save this in conf/rules this rule is properly executed.
When I create the same rule in the UI, the rule does not execute. I get the following in the log:

[ERROR] [internal.handler.ScriptActionHandler] - Script execution failed: An error occurred during the script execution: null

My file based rule, when looking in the UI is shown as:

triggers:

  • id: “0”
    configuration:
    cronExpression: 0 0 0 * * ?
    type: timer.GenericCronTrigger
    conditions: []
    actions:
  • inputs: {}
    id: script
    configuration:
    type: application/vnd.openhab.dsl.rule
    script: |
    // context: system-2
    counter = Uptime.state as DecimalType
    counter = counter + 1
    postUpdate(Uptime, counter)
    watchdog = 0
    postUpdate(Watchdog, watchdog)
    type: script.ScriptAction

The rule created in UI:

triggers:

  • id: “0”
    configuration:
    cronExpression: 0 0 0 * * ?
    type: timer.GenericCronTrigger
    conditions: []
    actions:
  • inputs: {}
    id: script
    configuration:
    type: application/vnd.openhab.dsl.rule
    script: |
    var Number counter = Uptime.state as DecimalType
    counter = counter + 1
    postUpdate(Uptime, counter)
    var Number watchdog = 0
    postUpdate(Watchdog, watchdog)
    type: script.ScriptAction

The main difference is that in my file based rules, I have declared the variables globally. In the UI based rule I declared these in the rule.

When I copy the line with // context to the UI based rule, it does function.

I have submitted an issue for this:

1 Like