Page 1 of 1

random on / random duration

Posted: Wednesday 18 December 2019 18:08
by mash47
dzVents allows randomised switch times using withinMin(minutes) etc but I've been unable to find anything in dzVents for a randomised duration. Here's the code to operate a switch with random on times and random duration ...

Code: Select all

return {
	on = {
	    timer = {'every 30 minutes'}
	},
	execute = function(dz)
	    local n = tostring(math.random(5, 15))
	    if( dz.time.matchesRule('between sunset and 23:00') ) then
	        dz.devices(15).switchOn().withinMin(15).forMin(n)
	    end
  	end
}

Re: random on / random duration

Posted: Thursday 19 December 2019 11:38
by imdos
Well; as you have already found. The randomised duration is within! There is: withinMin(minutes); this enables to switch off or on within a random time.

But I guess it might influence too much with your current idea.

If you have:

Code: Select all

dz.devices(15).switchOn().withinMin(15)
dz.devices(15).switchOff().withinMin(15)
This might already switch off the light before the switch on.

Re: random on / random duration

Posted: Thursday 19 December 2019 17:13
by mash47
imdos wrote: Thursday 19 December 2019 11:38 If you have:

Code: Select all

dz.devices(15).switchOn().withinMin(15)
dz.devices(15).switchOff().withinMin(15)
This might already switch off the light before the switch on.
Yes I agree, that is why I used the math.random(n, m) function to ensure that the randomised duration was applied after the switch had been turned on. It also has the advantage of being able to specify a minimum duration.