Very simple DSL rule throwing bunch of errors

openHAB 4.1.3 Release Build

I’m trying to learn DSL rules. I wrote a simpliest one (aka hello world) but the logs tell me that I miss something obvious.
The rule:

configuration: {}
triggers:
  - id: "1"
    configuration:
      cronExpression: 0/15 * * * * ? *
    type: timer.GenericCronTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/vnd.openhab.dsl.rule
      script: |-
        rule "AC_Available_Power_W changed"
          when
                Item AC_Available_Power_W changed
          then
                logInfo("KOSTAL","AC_Available_Power_W changed")
          end
    type: script.ScriptAction


the log

11:22:01.039 [ERROR] [.internal.handler.ScriptActionHandler] - Script execution of rule with UID '78f9850186' failed:  ___ rule  ___ "AC_Available_Power_W changed"
  when
        Item AC_Available_Power_W changed
  then
        logInfo("KOSTAL","AC_Available_Power_W changed")
  end
   1. The method or field rule is undefined; line 1, column 0, length 4
   2. The method or field when is undefined; line 2, column 38, length 4
   3. The method or field changed is undefined; line 3, column 77, length 7
   4. The method or field then is undefined; line 4, column 87, length 4
   5. The method or field end is undefined; line 6, column 151, length 3
   6. This expression is not allowed in this context, since it doesn't cause any side effects.; line 1, column 5, length 30
   7. This expression is not allowed in this context, since it doesn't cause any side effects.; line 3, column 51, length 4
   8. This expression is not allowed in this context, since it doesn't cause any side effects.; line 3, column 56, length 20

The error seems to tell that OH doesn’t even get the “when” line… What I am missing?

If you’re not already familiar and comfortable with RulesDSL, don’t start learning it now. Instead, use JSScripting, Blockly, or JRuby.

Anyway, the solution is this:

configuration: {}
triggers:
  - id: "1"
    configuration:
      cronExpression: 0/15 * * * * ? *
    type: timer.GenericCronTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/vnd.openhab.dsl.rule
      script: |-
        logInfo("KOSTAL","AC_Available_Power_W changed")
    type: script.ScriptAction

In other words, you just write whatever that’s inside the then block into your UI Rule.

What you wrote was meant for file-based rules.

2 Likes

Thank you! That doc page send me into the ditch: Textual Rules | openHAB
It says about textual rules, then shows screenshots how to add a DSL rule and says " Choose Rule DSL and enter a rule like it is described below in this article."

Thank you for the advice about the rules, I’m OK with Blocky, but I need to use averageSince and I’m not sure how to get it in Blocky. I need to check JSScripting or JRuby..

AFAIK averageSince is available in Blockly

Thanks for pointing that out - I was not searching hard enough… It seems to be there: Rules Blockly - Persistence | openHAB