Calling an external file-based script from an openHAB 3 UI-based rule using callScript()

I’ve setup VS Code with the openHAB extension, linked it to my openHAB server and have access to the $OPENHAB_CONF/scripts directory. Bearing in mind the workaround for the bug related to calling external scripts (Scripts in OH3) I created a test.script.script file and am able to call it successfully using callScript(‘test.script’). I am currently doing this by defining a GUI-based rule that “executes a given script” and therein I simply have callScript(‘test.script’).

The way I’m currently achieving this functionality (using a rule to execute a script to call an external script) seems a little convoluted to me. I’ve seen references to being able to add callScript(‘test.script’) directly to a file-based rule but I’m not sure of the format for a UI-based rule. This leads me to my question. Is there a preferred method of calling callScript(‘test.script’) either from the rule configuration in the UI, or in the code window of the UI-based rule, or am I currently doing it the preferred way?

If you are use Rules DSL as the Script Action language, it should work as it always has per Actions | openHAB.

That’s what a Script is. It’s a bit of code that you call from a rule’s “script”. Honestly, Scripts have always been not terribly useful because you can’t pass any values to it and you can’t get any values back from it. Most people put the code they want to run in the Script Action itself instead of putting the code in a .script.

Note the “execute a given script” is a Script Action. It’s part of the rule. It’s not the same thing as the code snippets you’d put in .script files in the script folder.

If you have a need for a library type capability, you should use ECMAScript which can import a personal library of functions you can call and be able to pass values to them and get values back. See OH 3 Examples: Writing and using JavaScript Libraries in MainUI created Rules

Anyway, yes, you are doing it about the only way that it can be done. If it feels convoluted I think that stems more from a lack of understanding on how rules work and what .script files can do and are for. The only time you’d really use a .script file is if you had a block of code that does the exact same thing every time it’s called, and you need to call that from more than one Rule. About the only use case I’ve seen that makes sense is to set up Scenes where you have two or more rules that can enable a scene (i.e. set of states for a number of lights and other stuff).

Gotcha. I left out that the main reason I’m using external scripts is a preference for the functionality of the external code editor (structure wireframing, syntax validation, design patterns, code completion, etc.) since I am new to learning the openHAB ecosystem. This is in contrast to expecting extra functionality or code reuse from external scripts.

When I said convoluted, it wasn’t meant as a criticism of the system by any means, more of a lack of understanding on my part as you mentioned. I wanted to make sure there weren’t performance implications related to the way I am doing things that wouldn’t scale well. Now I have a better understanding that the “execute a given script” simply embeds the script inline into the rule definition rather than calling from an external source…

  - id: "1"
      itemName: SonoffTH16_SonoffTH16Temperature
    type: core.ItemStateChangeTrigger
conditions: []
  - inputs: {}
    id: "2"
      type: application/vnd.openhab.dsl.rule
      script: |-
        logInfo("testRule", "debugging from test rule")
        // more code here
    type: script.ScriptAction

Then just write your rules in text files and skip the UI. If you want to use Rules DSL, use .rules files in the rules folder.

Given the limitations of .script files, you’re not going to get much code reuse from there.

It’s a lot more than that. The Script Action is going to have a lot of information that a .script will never have. See Rules | openHAB. None of those exist in the .script file. They only exist in the rule.

All things considered, it is just not feasible to code everything in .script files.

Ok, it’s starting to click now. Thanks a bunch!