DSL rule timer for less than one second

Hopefully a quick one. I want to space out a command and have the spacing set a 1 second,

timer1 = createTimer(now.plusSeconds(1), [ |

but would like to reduce the delay to 250 milliseconds. I tried

timer1 = createTimer(now.plusMillis(250), [ |

but got an error in the log

script execution of rule with UID 'Kitchen_motion-2' failed: 'plusMillis' is not a member of 'java.time.ZonedDateTime';

After a lot of google, but I can’t seem to find an example of the ZonedDateTime member I need.

As a side note I have tried Thread :: sleep(250), but also must not be doing something right.
Script execution of rule with UID 'Kitchen_motion-1' failed: The name 'thread' cannot be resolved to an item or type;

I have the timers setup and working, so prefer sticking with them, if possible, before finding the problem with the thread idea.

Bob

It might be Nanos not Millis: Link also here (for full documentation)

Thanks ! Let me try that tomorrow.

Bob

I have been meaning to update this: Nanos is a solution in the sense that it does not cause an error. However, at least with DSL, it does not appear to be recognized. I reverted to Thread::sleep( ).

I created this rule

rule "Test of Nanos vs seconds"
when
    Time cron "0 43 15 ? * * *"
then
    var Timer timer1 = null
    var Timer timer2 = null
    logWarn("events", "Pretimer")    
    if (timer1 === null) {
        timer1 = createTimer(now.plusSeconds(1), [ |
        logWarn("events", "One second timer")
        ])
        timer1 = null
    }
    if (timer2 === null) {
        timer2 = createTimer(now.plusNanos(2000000), [ |
        logWarn("events", "two million nanos timer")  
        ])
        timer2 = null
    }    
end

With the following output

2022-01-18 15:43:00.407 [WARN ] [org.openhab.core.model.script.events] - Pretimer
2022-01-18 15:43:00.513 [WARN ] [org.openhab.core.model.script.events] - two million nanos timer
2022-01-18 15:43:01.410 [WARN ] [org.openhab.core.model.script.events] - One second timer

I know folks are migrating to other rule languages and the Thread::sleep() is an alternative, so it is not a big deal, but looks to be a bug.

Bob

2 million nanos is a short time, isn’t it? Try 2 billion?

1 Like

My bad.

2022-01-18 17:19:00.766 [WARN ] [org.openhab.core.model.script.events] - Pretimer
2022-01-18 17:19:01.772 [WARN ] [org.openhab.core.model.script.events] - One second timer
2022-01-18 17:19:02.774 [WARN ] [org.openhab.core.model.script.events] - two billion nanos timer

Sorry,

Bob

1 Like

Odd. I use createTimer(now.plusMillis(x)) without any issues.

However, I’m just using it directly in the rule, not in a variable.

createTimer(now.plusMillis(500)) { item.sendCommand(ON) }

Maybe that makes a difference?