How to create a JS Rule

Hey there, I want to create a rule with the Automation/Scripts function using JS (ECMA Edition 11).

I read the information from the docs and tried to create a sample rule.

I created follwoing rule but it does not trigger.

var email = "my@mail.com"

rules.JSRule({
  name: "testscript",
  description: "none",
  triggers: [triggers.GenericCronTrigger("*/15 * * * * *")],
  execute: (event) => {
    actions.NotificationAction.sendNotification(email, "Test");
  },
});

After creation the console log informs me about a rule update but nothing happns. Tried to use a item to trigger the rule, nothing too…

I’m searching for a tutorial or something similar but I can’t find any information how to get startet with this feature. As I understand, the Main UI Scripts you add to “normal” rules, right?

Start with the basics. Did you install the JS Scripting add-on?

Add logging to the rule so you can see whether or not it is executing and failing silently or indeed not executing.

No.

Please review Rules - Introduction | openHAB and Rules | openHAB, for more on what Scripts you see in MainUI are and their use.

And which openHAB version did you try?

v3.4.2 (Stable)

I have installed the JavaScript Scripting addon. Does this count?

Pic

It does not execute.

I will do so, but I thing I did before. Surely I overlooked something…

Are you trying to add this script using the UI rules? The rule text you’ve pasted is only if you are using file-based js rules. If you are using the UI then you put the trigger information in under the When heading using the add trigger button. And the script that gets added as a script action is only the actual lines you want executed.

No I want to create a script / rules using the “Automation/Scripts” feature.
grafik

OK, there’s your problem.

I will ask you even more strongly to go to those links I posted and read how rules work.

Also notice how all the JSRule docs in the JS Scripting readme are under a section titled “File Based Rules”.

Still the same issue. You do not enter the full file-based syntax in there. Just the lines you want executed.

The “scripts” are just rules without event triggers, not a generic engine for rule-based syntax. Since you clearly show that you want a trigger in your rule, then you use the “rules” section and follow the steps I put above.

This tiny piece did the trick! THANK YOU!

const { rules, triggers, items } = require('openhab');

I have not seen this before or i missed it…

I don’t get the terminology. Scripts inside Rules are called ‘MainUI’? Scripts under the menu Scripts are ‘MainUI’ or ‘FileBased’? And what are Scripts called that are the Actions of Rules that are created inside a Script? Sorry I read the docs the whole day. I just can’t get it…

The script I posted was the first simple attempt to get scripts without the rules menu of OH. The idea is, to create button press patterns with timers so I can use some variables as globals outside of the rules. I want to avoid to create multiple helper rules/items to provide this. Just one script with nearly everything needed inside. Maybe you can suggest a better way? :slight_smile:

No, that’s not it. What you’ve done is create a Script (see below) that when it runs, creates a wholly separate Rule that has a cron trigger. The levels of redirection is asinine. Just create the rule following the instructions in the Getting Started Tutorial.

The tl;dr for how to do that is quite aptly provided by @JustinG above.

  • MainUI: When you go to http://<ipaddress>:8080 you see a user interface. That user interface is called “MainUI”. OH supports many UIs. The one that lets you administer OH (create Items, rules, etc.) is called “MainUI”.

  • Scripts under the menu Scripts are ‘MainUI’ or ‘FileBased’?: MainUI based. File based rules live in text files under $OH_CONF/automation/js in files that have a .js file extension. The Scripts under Settings → Scripts are rules that lack triggers or conditions. There’s a whole section of the Getting Started Tutorial that discusses this: Getting Started - Introduction | openHAB in addition to the l docs referenced above.

  • And what are Scripts called that are the Actions of Rules that are created inside a Script?: The word “script” has multiple uses. There is a section of the Getting Started Tutorial and of the Rules Concepts Page that discusses this. The “Actions of Rules that are created inside a Script” is kind of nonsense. Assuming you mean “Actions of Rules created inside a Rule” than they are called “Script Actions”. They contain the code that runs when that rule is triggered.

I appreciate that but several people spent a lot of time writing those docs. Without more specific questions nothing we write here as an answer will be as well thought out, complete, and comprehensive as what exists there in the docs.

Your questions indicate a lack of reading of the docs, jumping around the reference docs without having understood some of the fundamentals of OH that are covered in the concepts sections of the docs, or a complete misunderstanding of what you’ve read. It’s really hard to help because we don’t know which of these is the case.

It might help if you go to Rules - Introduction | openHAB. Read that in detail and then come back with anything you do not understand. If you don’t have these basic concepts down, it’s going to be really hard to help.

Then go through the complete Getting Started tutorial. You can skim through much of it but at least scan each section of each page. Read the Rules pages in detail. Come back with anything you don’t understand from there.

Maybe then we can be of more help. If we can get some specific questions we can perhaps explain it in a different way or provide more details.

I’m not sure you have an understanding of how rules work and the parts of a rule to have any hope of achieving this right now. But to store variables outside a rule you can use Items (there is nothing wrong with using Items to store state, that’s sort of the whole point of them) or use the cache.

2 Likes

Thank you for your effort. I will do so and come back if any questions are open :pray: