OH3 Scripts & Rules

I’m newish to home automation and have after investingating OH2 and HA for a few weeks decided on the OH2 route but then came along OH3. As I had only just started writing proper rules for devices I decided to move to OH3 before building up a system using OH2 and having to face the prospect of migrating it at sometime in the future, all be it maybe the distant future.

The inital stages, setting up the Model etc, all went well, where I ran into some challenges is when I started to write rules. In OH2 I created my own rules by coding them in .rules files whithout any real problems. However in OH3 using the rules menu option I have come across a few issues/anomolies:

  • When selecting “Run Script” to add an action it comes up with the options of building a script using Blockly, ECMAScript, or Rule DSL. If, however I click on “Show All” and then select “execute a given script” it offers me the choice of ECMAScript, or Rule DSL, but not Blockly.

  • When developing a rule with “Run Script” it does not allow you to select a Script from those developed under the “Scripts” menu option, instead it forces you to write a new script each time and appears to store it locally within the rule. I would have expected all scripts to be stored in one common location rather than some being stored under the “Scripts” menu option and others being stored within individual rules. I may just be me being a bit dense, or not yet fully understanding how OH3 intend scripts to be used, but I would have thought/hoped that one could write a script once and then select to use it in multiple places, rather than having to have a different copy for each usage.

2 Likes

Experiencing the same thing right now. Did we misunderstand Scripts & Rules in UI?

What good it the Scripts TAB to store scripts when we cannot choose them in our Rules?

I don’t get it. Is this a bug?

2 Likes

I must admit that I do not understand this naming convetion between rules/scripts either. If I create a rule as a trigger for a script, why cant I then select a script (for the trigger action) that I created separately?
However, if I choose to “run these rule(s)” from from the triggering rule, I can select a separately created script.
What am I missing here?

There are three categories of rules:

Category Description
Generic Found under “Rules”, contain zero or more triggers, zero or more conditions, and zero or more actions.
Schedule These are Generic rules that have some sort of time based trigger and optional time based conditions and are tagged with “Schedule”. These rules will appear on the calendar under “Schedule” when they are scheduled to run.
Script This is a Generic rule that only consists of a single Script Action. There are no triggers and there are no conditions. These rules are tagged with “Script”

Thus, a Script is a rule. To call it from another rule, one needs to call it like the rule that it is. For example, selecting “run these rule(s)” as the Script action. There is no separate action for Scripts because Scripts are just a special case of rules.

Note that this distinction between Generic, Schedule, and Script exists only in MainUI.

Unfortunately there is an overlap in terminology with the meanings of the word “script”. If anyone has ideas for alternative terms to use I’m sure an issue would be welcome. The term can mean:

  • a special type of UI created rule consisting of a single Script Action with no triggers and no conditions

  • when used with Action or Condition (i.e. Script Action/Script Condition) this is a block of code that is associated with a Rule; several different programming languages are supported

  • .script files in /etc/openhab/scripts are a block of Rules DSL code that can be called from Rules DSL rules using the callScript action (see Actions | openHAB); honestly these were never all that useful and are kept around as legacy support

  • shell scripts which are blocks of code that execute in a command line shell like bash and which are called using the Exec binding or executeCommandLine.

Therefore, if you’ve created a Script under the Scripts section of MainUI, you are creating a rule. To call that “Script” from another rule, you need to call it as another rule.

If you’ve created a rule and want to write some custom code to execute as part of the rule, you will select Script Action or Script Condition as appropriate.

3 Likes

Many thanks for the clarification. I am as always amazed how much knowledge you posses and share here at the forum.

1 Like

I only know as much as I do because I do share it here on the forum. :slight_smile: I have to constantly be learning in order to answer questions like these.

2 Likes