Example using JS library functions in rules via textfile

I’m looking for an example of how to use functions of an own JS library-file in rules. All I found were several examples of how to use these in rules generated by UI, but my rules are completely created via VS Code and it seems the way differs?!

Or is the current way using python? If so, the questions stays nearly the same, just the langeuage is another :wink:

Thanks in advance!

It should be the same. Use a load operator to load the contents of a file into your current file.

Here is the constructor for my Timer Manager class which loads my Time Utils library.

var TimerMgr = function() {
  'use strict';
  var OPENHAB_CONF = java.lang.System.getenv("OPENHAB_CONF");
  this.log = Java.type("org.slf4j.LoggerFactory").getLogger("org.openhab.model.script.Rules.TimerMgr");
  this.log.debug("Building timerMgr instance.");
  this.timers = {};
  this.log.debug("Loading timeUtils");
  load(OPENHAB_CONF+'/automation/lib/javascript/community/timeUtils.js');
  this.ScriptExecution = Java.type("org.openhab.core.model.script.actions.ScriptExecution");
  this.log.debug("Timer Mgr is ready to operate");
}

In Python you use import.

I’ve move this to a more appropriate category. Tutorials and Solutions are for posting those, not requesting help.

In GraalVM there are alternative ways to load a library but I’m not expert in that yet.

Maybe I don’t get it, but the snippet you posted is used in a js-file, isn’t it?
I’m looking for a way to make use of js-functions in a rules-file, eg:

rule dummy
when
    Time cron "0/10 * * * * ?"
then
    <call myFunction>
end

I’m looking for an example of how to replace to use functions written in a library-file by my own!

Thanks again!

Oh, you can’t mix and match rule languages. If you are using Rules DSL, whether it be in a .rules file or in the UI, you can’t use anything from any of the other languages. And Rules DSL doesn’t support libraries at all. If you want to use your own libraries, you have to switch to another language. And both the rule and the library have to be written in the same language.

I see - thought I might use JS-libs in Rules DSL, which were ported from old OH-version :frowning:

Since I think you are quite familiar with OH, what language do you suggest to use?
Do I have disadvantages going away from Rules DSL? ( Disregarding time needed for recoding :wink: )
I have the feeling that some rule-executions took quite long atm (not the running time, but the time needed until rule starts at all)…

TIA

It’s really hard to answer that question because things are in flux so there isn’t a good answer right now. At least not yet.

I would say that if you have a need to use something from another language, then use that language. You can have rules written in different languages in OH at the same time. You just can’t mix languages in the same script or file. So you can leave your .rules and write new stuff in Python or JavaScript or even use Rules DSL but write them in the UI instead of files.

You don’t have to change anything unless you want to. You could keep all your existing rules as is and write new rules in a new language. Or switch between languages if one works better in a case than the other. I’ve a mix of pure UI rules, JavaScript UI rules with JavaScript libraries, and a couple of Rules DSL UI rules.

Also, don’t miss the new Marketplace. Over time there will be a growing collection of rule templates you can just install and instantiate. In the not too near future you may not even need to write most of your rules at all, just install them. There is already a small collection of useful stuff posted and more will be written over time.

There was an unidentified ‘something’ in OH2 that caused first-time rule execution to be delayed by some seconds. (Some compilation process presumably)
Many users also found the same effect with rules not triggered for a long time, e.g. overnight.

In OH3, DSL rules run under a different engine, no-one has reported these effects so far.

Hm - I’m running OH3 atm and some rules have a delay - especially the ones regarding a motion-sensor which triggers lightning. And I already tried this “take-this-GC!-and-execute-a-dummy-rule-to-make-file-stay-in-memory”-thing, but I don’t want to switch from OP to a different matter in this posting! I’ll try to port those rules to next-gen-rules and will see if delay still occurs.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.