How to create / use scripts

I installed OH3 and also VS-Code extensions successfully.
Now I try to run create a script and run it from a rule defined in paperUI.

The script is there (in the file system - test.script) but I can’t find it via “execute command” in the rule.
Can’t I use scripts defined in the file system via paperUI?

So I simply tried to add a script via paperUI.
I the documentation I found -

if (Temperature.state < 20) {
    Heating.sendCommand(ON)
}

So I made a very simple script - just:
Box.sendCommand(ON)

When I try to run it from paperUI I get no error message but in the log I find this message:
Script execution of rule with UID ‘t1’ failed: ReferenceError: “Box” is not defined in at line number 1

Why “rule” I just wanted to write a script?

I guess I got something wrong. My approach is to write a sripte (pseudocde)
if Box.state==ON {
Box.sendCommand(OFF)
sleep(1000)
}
Box.sendCommand(ON)
sleep(100)
Box.sendCommand(OFF)
sleep(100)
Box.sendCommand(ON)

The reason - box is a “switch dimmed” device. To dimm it you have to turn it on - then off within max a second and on again.

Since the rule editor in paperUI doesn’t offer functions like Thread::sleep I wanted to define a rule (triggered by a switch) which calls a script.

These are different things.

First of all, the NGRE in OH 2.5 does not support Rules DSL. You can write using ECMAScript (i.e. JavaScript) or you can install and use Python or Groovy. You cannot do anything with Rules DSL from PaperUI (NOTE: Rules DSL is fully supported in the the UI in OH 3).

Unfortunately the term “Script” is overloaded. In PaperUI a rule consists of zero or more triggers, zero or more conditions, and zero or more actions. One type of action is a “Script Action”. If you were writing a rule in a .rules file, this would be the code that goes between the then and the end. It has nothing at all to do with Scripts (blocks of Rules DSL code that you can call from rules).

So you either need to write this in a .rules file (Rules | openHAB) or you need to write the rule using JavaScript. Or you need to upgrade to OH 3.

Thank you @rlkoshak ,

I still don’t understand how “scripts” work. But (as a wrote at the beginning of my question) I have installed OH3 and I was able to write a rule in VS-Code.

import java.lang.Threadrule "Dimm Buero"
when
Item BueroSwitch_BueroS3 changed to "ON"
then
BueroSwitch_BueroS3.sendCommand(OFF)
if(BuroSwitch_BuroS1.state == ON){
    BuroSwitch_BuroS1.sendCommand(OFF)
    Thread::sleep(1000)
}
BuroSwitch_BuroS1.sendCommand(ON)
Thread::sleep(800)
BuroSwitch_BuroS1.sendCommand(OFF)
Thread::sleep(800)
BuroSwitch_BuroS1.sendCommand(ON)
end

First of all it works - and much better I can see (read only) this in paperUI like this:

triggers:
  - id: "0"
    configuration:
      itemName: BueroSwitch_BueroS3
      state: ON
    type: core.ItemStateChangeTrigger
conditions: []
actions:
  - inputs: {}
    id: script
    configuration:
      type: application/vnd.openhab.dsl.rule
      script: |
        // context: m1-1
        BueroSwitch_BueroS3.sendCommand(OFF)
        if(BuroSwitch_BuroS1.state == ON){
            BuroSwitch_BuroS1.sendCommand(OFF)
            Thread::sleep(1000)
        }
        BuroSwitch_BuroS1.sendCommand(ON)
        Thread::sleep(800)
        BuroSwitch_BuroS1.sendCommand(OFF)
        Thread::sleep(800)
        BuroSwitch_BuroS1.sendCommand(ON)
    type: script.ScriptAction

So I still have no idea of the scripts folder can be used or if it even has any pratical use.
But last not least the rule solves my problem.

Very little practical use. That’s why the one link is titles “Finally a use for scripts.”

If you did want to use the UI to create a rule like that, you would define the trigger separately under “when”, create a Script Action and choose Rules DSL under the “then” and copy in

        // context: m1-1
        BueroSwitch_BueroS3.sendCommand(OFF)
        if(BuroSwitch_BuroS1.state == ON){
            BuroSwitch_BuroS1.sendCommand(OFF)
            Thread::sleep(1000)
        }
        BuroSwitch_BuroS1.sendCommand(ON)
        Thread::sleep(800)
        BuroSwitch_BuroS1.sendCommand(OFF)
        Thread::sleep(800)
        BuroSwitch_BuroS1.sendCommand(ON)

as the contents of the Script Action.

Note that in OH 3 you will see a “Scripts” section under Settings. These are not the scripts you would see in the scripts folders either. These a a special type of rule that have only one single Script Action (no triggers, no conditions) and are tagged with “Script”. These you can call from other rules through the UI.

2 Likes