Create timer in openHAB3 rule with ECMA

  • Platform information:

    • Hardware: i686 / 1 GB / 130 GB
    • OS: Debian 10.7
    • Java Runtime Environment: openjdk 11.0.9.1
    • openHAB version: openHAB 3.0.0
  • Issue:
    I’d like to create a rule with a timer that fires some fixed time after an item was updated.

I’ve tried doing it by generating a rule with the openHAB3 GUI and adding ECMA script for the timer, with the syntax/example from these docs. I suspect the example isn’t appropriate ECMA syntax - so what would be the correct way of doing this?

Really, I’d be happy with any solution that isn’t too convoluted or a pain to set up and maintain :slight_smile:
While I’d be happy to use the GUI and ECMA for simplicity’s sake, I’m not set on those two choices and would use anything as long as it doesn’t take extensive learning to pick up.

I suppose my limitation is that while I’m OK with programming / console / linux, I don’t have the necessary overview of all openHAB scripting languages, bindings, libraries, etc. to select the straightforward way of doing things and to extract the relevant information from the examples in the docs. Plus, of course, the recent shift from OH2 to OH3…

  • Rule:

      triggers:
        - id: "1"
          configuration:
            itemName: BathroomTemperature
          type: core.ItemStateUpdateTrigger
      conditions: []
      actions:
        - inputs: {}
          id: "2"
          configuration:
            type: application/javascript
            script: >
              var myTimer = createTimer(now.plusMinutes(5), [ | logInfo("rules",
              "Timer activated") ]);
          type: script.ScriptAction
    
  • Log (/var/log/openhab/openhab.log):

      2021-01-08 15:07:48.153 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'BlueSensorAlarm' failed: <eval>:1:48 Expected an operand but found |
      var myTimer = createTimer(now.plusMinutes(5), [ | logInfo("rules", "Timer activated") ]);
                                                      ^ in <eval> at line number 1 at column number 48
    

I’ve got to give it to the ScriptActionHandler: This doesn’t look like a JS lambda as I would expect it.

see here:

I’ve had exactly the same challenge today and this helped

1 Like

Thanks a lot, that works! :smiley:

For the record, here’s what I’d consider equivalent to the example above:

 triggers:
  - id: "1"
    configuration:
      itemName: BathroomTemperature
    type: core.ItemStateUpdateTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/javascript
      script: >
        var ScriptExecution = Java.type("org.openhab.core.model.script.actions.ScriptExecution");
        var ZonedDateTime = Java.type("java.time.ZonedDateTime");  
        var logger = Java.type("org.slf4j.LoggerFactory").getLogger("org.openhab.model.script.Rules.Examples");

        function lambdaSchmamda() {
          logger.info("Timer activated");
        }

        var timer = ScriptExecution.createTimer(ZonedDateTime.now().plusMinutes(5), lambdaSchmamda);
    type: script.ScriptAction

I couldn’t get it to work with the classic x => x+1 lambda notation but then I’m not bothered by the named function :slight_smile:

I like that name, but you also could do:

var timer = ScriptExecution.createTimer(ZonedDateTime.now().plusMinutes(5), function(){
  logger.info("Timer activated");
});