Random timer rule not working

I’m trying to create an random element to turn lights on in my openhab setup.

I’ve installed Astro binding which successfully tigers a Sunset event once per day. Once that Sunset event triggers, I want turn a light on at some point in the next 30 mins.

As the first part (Sunset event) is working correctly I’m testing the random timer element but without much luck.

I’ve created this sample rule that runs every 10 mins via cron and then should write a random offset timer to the log file. However I’m not seeing any entries written to the /var/log/openhab/openhab.log as expected.

What’s going wrong?

var Timer tRandomLights = null

rule "Random time test"
        when
                Time cron "0 */10 * * * *"
        then

                        // Create a timer with a random value
                        var int randomTime = (new java.util.Random).nextInt(1800)
                        logInfo("org.openhab","Setting random lights timer to " + randomTime + " seconds.")
                        tRandomLights = createTimer(now.plusSeconds(randomTime)) [
                                logInfo("org.openhab","Switching light state")
                        ]
end

Are you not seeing either log statement or just not seeing the “Switching Light State”?

When I do every x minutes I use:

cron "0 0/10 * * ? *"

I’m not seeing either of the two logInfo statements being written to the log file.

If I run the following two lines via SSH I get no output. At minimum I’d expect to see something along the lines of “Setting random lights timer to XX seconds” in the /var/log/openhab/openhab.log.

$ grep -i 'Setting' /var/log/openhab/openhab.log
$ grep -i 'Random' /var/log/openhab/openhab.log

As I understand it the use of ? instead of * is that ? causes cron to run at every point from when it was first loaded at the same recurring point. Where as * just means on all possible occasions.

The big and important change is the “0/10” verses “/10". The library the Rules uses for cron is not exactly like Unix/Linux cron. I’ve never seen a rule that uses "/10”.

From quartz cron documentaion:

Note the subtlety that “/35” does *not mean “every 35 minutes” - it mean
“every 35th minute of the hour, starting at minute zero” - or in other words the same as specifying ‘0,35’.

So /10 and 0/10 is the same, at least in quartz cron. But for */10 there is no distinct value or list of values, so I think it’s not allowed.

In question of question mark ? :

The ‘?’ character is allowed for the day-of-month and day-of-week fields. It is used to specify “no specific value”. This is useful when you need to specify something in one of the two fields, but not the other. See the examples below (and CronTrigger JavaDoc) for clarification.

I’ve updated my rules code with your suggestions but I’m still not seeing any output in the log file.

var Timer tRandomLights = null

rule "Random time test"
        when
                Time cron "0 0/10 * * ? *"
        then

                        // Create a timer with a random value
                        var int randomTime = (new java.util.Random).nextInt(1800)
                        logInfo("org.openhab","Setting random lights timer to " + randomTime + " seconds.")
                        tRandomLights = createTimer(now.plusSeconds(randomTime)) [
                                logInfo("org.openhab","Switching light state")
                        ]
end

Are you seeing errors?

Better use

logInfo("org.openhab","Setting random lights timer to {} seconds.", randomTime)

or use randomTime.toString, as you are using string composition. I’m pretty sure the rule stops at this point.
Even more, in the createTimer line, you forgot a | at the very end of the line - just after the bracket [.

I’ve updated my rules to take into account Udo’s suggestions. I think this is right however I’m still not seeing anything that I would expect to see outputted to any of the openhab log files.

var Timer tRandomLights = null

rule "Random time test"
        when
                Time cron "0 0/2 * * ? *"
        then

                        // Create a timer with a random value
                        var int randomTime = (new java.util.Random).nextInt(1800)
                        logInfo("org.openhab","Setting random lights timer to {} seconds.", randomTime)
                        tRandomLights = createTimer(now.plusSeconds(randomTime)) [ |
                                logInfo("org.openhab","Switching light state")
                        ]
end

The following command still produces no output as I’d expect. I’ve also tried running a tail -f command to watch the openhab.log file. With the adjusted cron time to every 2 mins, I’d expect something to appear with the tail command.

grep -i 'random' /var/log/openhab/*.log

Argh… didn’t see this at first sight. The Time cron expression is wrong.
Try

Time cron "0 0/2 * * * ?" 

From left to right:
0 -> full minute
0/2 -> every 2nd minute, biginning at 0

  • -> every hour
  • -> every day of month (you have to use ? here if you set weekdays)
  • -> every month (? isn’t allowed here)
    ? -> every day of week (* isn’t allowed here)
    optional, you could also set the year (is omitted here)
    See the complete documentation for Quartz cron.

That makes sense. I re-read the quartz cron reference in the openhab wiki.

However this still doesn’t seem to be working.

So I’ve broken it down even further to try and isolate the issue. So I’d expect to see something in the log file for the following rule, but still I see nothing in events.log or openhab.log

rule "test cron"
        when
                Time cron "0 0/2 * * * ?"
        then
                logInfo("testing cron again", "cron fired")
end

Have you made any modifications to logback.xml?

You wouldn’t see anything in events.log but you should be seeing these log statements in openhab.log unless there is either a problem or logback has been configured to silence the messages. Possible errors can be unclosed brackets in some code above this rule or another massive syntax error somewhere, the rule file is failing to be loaded, etc.

For comparison here is one of my working cron triggers:

Time cron "0 0/30 * * * ?"

Notice how it is essentially identical to yours and I know this one is working.

I took some time to play around with the locations of the rules in my configurations/rules/home.rules file and have identified what might have been causing the error.

I’m able to get this to fire and appear in the log:

rule "Random time test"
        when
                Time cron "0 0/1 * * * ?"
        then
                logInfo("org.openhab","cron test")
end

However if I place the same lines of code after this line, which I was using in the Randomiser rule original posted.

var Timer tRandomLights = null

It just wont run.

To your question rlkoshak, the only change I’ve made to logback.xml, that I’m aware of, is to setup verbose DEBUG to help me find errors whilst configuring.

So is var Timer tRandomLights = null a global (i.e. not in a rule)? If so, that has to go at the top of the file. You cannot intersperse globals with rules.

var Timer tRandomLights = null sits between two rules. Which sounds like a bad idea.

I can post the full home.rules file if need be…

No need. Just put all your globals at the top and that problem at least will be solved.