createTimer is an Action so the library to load will be actions.js. createTimer lives in the ScriptExecution class.
Almost all the interactions with openHAB itself (calling actions, working with Items, etc.) is achieve through the same Java Classes and Objects no matter what language you are using. So for the most part, with a little practice, you should be able to look at a jython block of code and convert it to JavaScript without too much trouble. Rules after all are mainly about interacting with openHAB and the rest of the rule is either proforma or stringing together those interactions.
So using the Helper Library you’d create a Timer
var logger = Java.type("org.slf4j.LoggerFactory").getLogger("org.openhab.model.script.Test");
var ZDT = Java.type("java.time.ZonedDateTime");
var OPENHAB_CONF = java.lang.System.getenv("OPENHAB_CONF");
load(OPENHAB_CONF + "/automation/lib/javascript/core/actions.js");
var runme = function(){
logger.info("Timer expired...");'
// timer code goes here
}
var timer = ScriptExecution.createTimer(ZDT.now().plusSeconds(1), runme);
But you’re not asking about a Timer there but a cron triggered rule. As I only use UI created rules with JavaScript I don’t know what it should look like in .js files.
As with createTimer, logInfo is one of the core openHAB Actions. In this case it’s on the Log class. @CrazyIvan359, the Helper Library seems to be trying to import the wrong class for the logger Actions, using org.openhab.core.model.script.actions.LogAction which is what is was before OH 3. I thought someone submitted a PR for that. I can do so if you like. Only the first block need to be updated I think. However that will make it not work for OH 2.5 so I don’t know how you are handling that.
@LordLiverpool, this appears to be one of the places in that 20% not yet done. So you can either create the logger directly like I showed in the createTimer example above, or import the Log class manually.
var Log = Java.type("org.openhab.core.model.script.actions.Log");
Log.logError("Experiments", "This is an OH error log");
Log.logWarn("Experiments", "This is an OH warn log");
Log.logInfo("Experiments", "This is an OH info log");
Log.logDebug("Experiments", "This is an OH debug log");
// There is no trace
Not a file, but a logger name. The logger names are hierarchical. By default, there are configs in log3jk2.xml to log out everything under org.openhab.model.script to openhab.log. You’ve added some customizations to move those to another file. The openHAB Log actions create new loggers under that path. If you use slf4j directly instead, you have to specify the full path.
@rlkoshak I can’t remember if I did JS but the Jython libs are fully OH2/3 agnostic. You can use either Log or LogAction in both version and you’ll get the available object.
For JS, yes they are the same.
For Jython, no. getLogger returns a Python logger class instance, though functionally they are almost identical.
getLogger makes your life easier so you don’t have to remember or know any of the Java, you just give it the log path you want and you get a logger back.
It looks like the JS only refers to LogAction with no reference to Log anywhere in actions.js, at least in the ivans-updates branch. Should I be looking at a different brach?
The new ones receive “event”. I don’t understand the Javascript well enough to work out how this maps to the code in the libraries.
Previously I was able to obtain the trigger item from “input”. You could extract an event string from input, split it and get at the various parts of the trigger condition:
var ev = input.get("event")+"";
// Splits into: 'gRecordLastUpdate,changed,from,7.96612745098039237,to,7.95362745098039237,through,BalconyAeotec_SensorTemperature'
var evArr = ev.split("'").join("").split("Item ").join("").split(" ");
What would be the equivalent of this now? What sort of object is event?