DateTimeTrigger JS Rule Offset issue

Dear community,

I want to use a DateTimeTrigger with an offset. I have the following rule in place:

rules.JSRule({
    id: "outdoor_sunset_on",
    name: "outdoor_sunset_on",
    description: "Sonnenuntergang",
    triggers: [triggers.DateTimeTrigger('Sunset_Time', false, 3600)], // 30min offset
    execute: (event) => {
        console.log("Sonnenuntergang")
        
    },
    tags: []
});  

but unfortunately as soon as I provide an offset value (as integer) I get the following error message:

2024-11-01 14:27:30.434 [ERROR] [omation.script.javascript.outdoor.js] - Failed to execute script: TypeError: invokeMember (withId) on org.openhab.core.automation.util.TriggerBuilder@20b0d76 failed due to: Cannot convert '3600'(language: Java, type: java.lang.Integer) to Java type 'java.lang.String': Invalid or lossy primitive coercion.
        at <js>.u(@openhab-globals.js:2)
        at <js>.DateTimeTrigger(@openhab-globals.js:2)
        at <js>.:program(outdoor.js:5)
        at org.graalvm.polyglot.Context.eval(Context.java:399)
        at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.eval(GraalJSScriptEngine.java:458)
        ... 21 more
2024-11-01 14:27:30.434 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script '/openhab/conf/automation/js/outdoor.js'

Do I miss something? Within the source code at triggers.js - Documentation line 271 I can find the additional parameter :-/

Any help would be awsome :slight_smile:

All the best!
Dennis

I don’t see anything wrong with what you’ve done.

What happens if you try using "3600" instead of 3600?

I assume then the parameter will be taken as triggerName :frowning: - but I give it a try right now

const DateTimeTrigger = (itemOrName, timeOnly = false, offset = 0, triggerName) => {
  // backward compatibility for (itemOrName, timeOnly, triggerName) signature:
  if (typeof offset === 'string') {
    triggerName = offset;
    offset = 0;
    console.warn('The signature DateTimeTrigger(itemOrName, timeOnly, triggerName) is deprecated. Please use DateTimeTrigger(itemOrName, timeOnly, offset, triggerName) instead.');
  }
  return _createTrigger('timer.DateTimeTrigger', triggerName, {
    itemName: _isItem(itemOrName) ? itemOrName.name : itemOrName,
    timeOnly,
    offset
  });
};

As expected - the 3 parameter will not be interpreted as offset.

rules.JSRule({
    id: "outdoor_sunset_on",
    name: "outdoor_sunset_on",
    description: "Sonnenuntergang",
    triggers: [triggers.DateTimeTrigger('Sunset_Time', false, "-3600")], // 30min offset
    execute: (event) => {
        console.log("Sonnenuntergang")
    },
    tags: []
});  


rules.JSRule({
    id: "outdoor_sunset_on_two",
    name: "outdoor_sunset_on_two",
    description: "Sonnenuntergang2",
    triggers: [triggers.DateTimeTrigger('Sunset_Time', false, "3600")], // 30min offset
    execute: (event) => {
        console.log("Sonnenuntergang2")
    },
    tags: []
});  

results in:

2024-11-01 17:02:00.672 [INFO ] [ab.automation.script.file.outdoor.js] - Sonnenuntergang
2024-11-01 17:02:00.678 [INFO ] [ab.automation.script.file.outdoor.js] - Sonnenuntergang2

:frowning: any further ideas/suggestions?

What is your openhab-js version?

Run console.log(utils.OPENHAB_JS_VERSION) from a JS script and check the logs.

Hi Florian,

utils.OPENHAB_JS_VERSION is set to 5.3.1.

Cheers
Dennis

Hi Dennis,

thanks for your answer.

DateTimeTrigger offset is only supported since version 5.6.0 of openhab-js and openHAB 4.3.0.M2, so you need at least openHAB 4.3.0.M2, which is the latest milestone release at the moment.

1 Like