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: