Trigger actions on random basis between certain time frame

Hi all,

I’m looking to a sollution for the following requirement.
I’ve a set of rullershutters which are controlled by Zwave devices. those rullershutters are opend in the morning at 8:00 during week days and at 9:00 during weekend days. In the evening the rollershutters are closed triggered by the sunset time. For security reasons I want to open the rollershutters during the weekdays at 8 o’clock within a deviation of 10 min. meaning the rollershutter can be trigger between 7:50 and 8:10.
Is there a sollution in Openhab to fullfil this requirement?

Thanks in advance.

OpenHAB is designed to be a very flexible framework. I am sure that using one of the rule languages using a random number generator that this could be done.
To answer your question there is nothing built in to OpenHAB for this as far as I am aware.

I think I’d run a rule at a fixed time 07:49, and set up a timer for a random 1 - 20 minutes.

1 Like

Bruce,

Thanks for the reply.

Is there in the rule language a way to calculate a random number {rand(0,20)} like we have in other languages. I’ve been looking for that but without success.

Thanks

Thanks for your reply.

Do you have an idea how to setup the random timer between 1-20 minutes in the rule language?

Thanks

I was looking around in java and there is a possibility to create random numbers. the code below is working and is created for testing purposes. I’ve put it here perhaps it could be interesting for others.
It is just triggered every minute and then a random timer is triggered to send a command to a wall switch toggling on off wich is controlled by the boolean onoff.

import java.util.Random;

var boolean onoff = false

rule "TestRandomOn"
    when
        Time cron "0 0/1 * 1/1 * ? *"  // every minute
    then {
        logInfo("TestRandomOn",'Time cron triggered')
        // Obtain a number between [0 - 59].
        if (onoff == false) {
            var Random rand= new Random();
            var int randnum = rand.nextInt(60)
            logInfo("TestRandomOn","randnum initialised :" + randnum)
                createTimer(now.plusSeconds(randnum),[|
                    logInfo("TestRandomOn","Timer expired :" + randnum + " and onoff = false")
                    WallPlug.sendCommand(ON)
                    logInfo("TestRandomOn","Wallplug switched on")
                    onoff = true 
                ])
        } 
    }
end

rule "TestRandomOff"
    when
        Time cron "0 0/1 * 1/1 * ? *"  // every minute
    then {
        logInfo("TestRandomOff",'Time cron triggered')
        // Obtain a number between [0 - 59].
        if (onoff == true) {
            var Random rand= new Random();
            var int randnum = rand.nextInt(60)
            logInfo("TestRandomOff","randnum initialised :" + randnum)
                createTimer(now.plusSeconds(randnum),[|
                    logInfo("TestRandomOff","Timer expired :" + randnum + " and onoff = true")
                    WallPlug.sendCommand(OFF)
                    logInfo("TestRandomOff","Wallplug switched off")
                    onoff = false 
                ])
        } 
    }
end

Hope it is useful… :slightly_smiling_face:

When doing the same approach but with the math.random class this is giving an error ‘An error occurred during the script execution: Could not invoke method: org.joda.time.DateTime.plusSeconds(int) on instance’.
Below the code:

var boolean onoff = false
var int rand


rule "TestRandomOn"
    when
        Time cron "0 0/1 * 1/1 * ? *"  // every minute
    then {
        logInfo("TestRandomOn",'Time cron triggered')
        // Obtain a number between [0 - 59].
        if (onoff == false) {
            rand= Math.round(Math.random() * 60)
            logInfo("TestRandomOn","randnum initialised :" + rand)
                createTimer(now.plusSeconds(rand),[|
                    logInfo("TestRandomOn","Timer expired :" + randnum + " and onoff = false")
                    WallPlug.sendCommand(ON)
                    logInfo("TestRandomOn","Wallplug switched on")
                    onoff = true 
                ])
        } 
    }
end

rule "TestRandomOff"
    when
        Time cron "0 0/1 * 1/1 * ? *"  // every minute
    then {
        logInfo("TestRandomOff",'Time cron triggered')
        // Obtain a number between [0 - 59].
        if (onoff == true) {
            rand= Math.round(Math.random() * 60)
            logInfo("TestRandomOff","randnum initialised :" + rand)
                createTimer(now.plusSeconds(rand),[|
                    logInfo("TestRandomOff","Timer expired :" + randnum + " and onoff = true")
                    WallPlug.sendCommand(OFF)
                    logInfo("TestRandomOff","Wallplug switched off")
                    onoff = false 
                ])
        } 
    }
end

Looking further the Math.random is using more processing power as java.util.Random so it would be nice to have an explanation why the math.random is generating an error. Nevertheless I’m using the first java.util.Random and it’s working fine.

Thanks anyone for the feedback.

As pseudo code this looks ok to me, but it is not correct Rules DSL to create a timer.

Personally, I don’t know if a random opening time is more secure than a fixed opening time. If the rollershutters always open at the same time and a potential burglar has figured this out then based on that knowledge he still can’t judge whether someone is at home or not.
I guess it’s a bit like the wasp in the box problem. As long as the box is closed you can be certain that the wasp is in the box, however when the box is opened, would you stick your hand in the box if you didn’t see the wasp fly out of the box? Then again, may be a burglar is that stupid :slight_smile:

Marcel,

You’re right but besides the rollershutters there will be other activities in the house which is aiming that somebody is in the house. Nevertheless it will not be an ensurance that a burglare will be kept out. :wink:

1 Like