Zeit Zufallsgenerator

Hallo,

ich habe folgendes:

--[[
%% autostart
%% properties
%% events
%% globals
--]]

local sourceTrigger = fibaro:getSourceTrigger();
function tempFunc()
local currentDate = os.date("*t");
local startSource = fibaro:getSourceTrigger();
if (
 ( ((currentDate.wday == 1 or currentDate.wday == 2 or currentDate.wday == 3 or currentDate.wday == 4 or currentDate.wday == 5 or currentDate.wday == 6 or currentDate.wday == 7) and os.date("%H:%M", os.time()-15*60) == fibaro:getValue(1, "sunsetHour")) )
)
then
	fibaro:call(33, "close");
	fibaro:call(29, "close");
	setTimeout(function()
		fibaro:call(5, "close");
	end, 8000)
	setTimeout(function()
		fibaro:call(25, "close");
	end, 10000)
	setTimeout(function()
		fibaro:call(31, "close");
	end, 12000)
	setTimeout(function()
		fibaro:call(35, "close");
	end, 15000)
	setTimeout(function()
		fibaro:call(27, "close");
	end, 16000)
end

setTimeout(tempFunc, 60*1000)
end
if (sourceTrigger["type"] == "autostart") then
tempFunc()
else

local currentDate = os.date("*t");
local startSource = fibaro:getSourceTrigger();
if (
startSource["type"] == "other"
)
then
	fibaro:call(33, "close");
	fibaro:call(29, "close");
	setTimeout(function()
		fibaro:call(5, "close");
	end, 8000)
	setTimeout(function()
		fibaro:call(25, "close");
	end, 10000)
	setTimeout(function()
		fibaro:call(31, "close");
	end, 12000)
	setTimeout(function()
		fibaro:call(35, "close");
	end, 15000)
	setTimeout(function()
		fibaro:call(27, "close");
	end, 16000)
end

end


Jetzt möchte ich gerne, dass wenn die Szene startet, die einzelnen IDs die Verzögerungen Zufällig in einem Bereich (z.B: 5 - 10 Sekunden) gestartet werden. Kann mir da einer einen Tipp geben wie ich das einbaue?

Hi,

hiermit bekommst du einen zufälligen Wert zwischen 5 und 10 zurückgeliefert:

math.randomseed(os.time())
local random = math.random(5,10)

Gruß

Ich habs bei mir ähnlich gelöst

math.randomseed(os.time());
fibaro:sleep(math.random(5000,10000));

Bin kein Lua Experte, denke das die setTimeout Function auch nur sleep macht.

danke… aber wie muss ich das einbinden??? :frowning:

Habe ich jetzt schon gefunden.

--[[
%% autostart
%% properties
%% events
%% globals
--]]

local sourceTrigger = fibaro:getSourceTrigger();
math.randomseed(os.time())
local random = math.random(10000,20000)
function tempFunc()
local currentDate = os.date("*t");
local startSource = fibaro:getSourceTrigger();
if (
 ( ((currentDate.wday == 1 or currentDate.wday == 2 or currentDate.wday == 3 or currentDate.wday == 4 or currentDate.wday == 5 or currentDate.wday == 6 or currentDate.wday == 7) and string.format("%02d", currentDate.hour) .. ":" .. string.format("%02d", currentDate.min) == fibaro:getValue(1, "sunsetHour")) )
)
then
	setTimeout(function()
		fibaro:call(5, "open");
	end, 11000)
end

setTimeout(tempFunc, 60*1000)
end
if (sourceTrigger["type"] == "autostart") then
tempFunc()
else

local currentDate = os.date("*t");
local startSource = fibaro:getSourceTrigger();
if (
startSource["type"] == "other"
)
then
	setTimeout(function()
		fibaro:call(5, "open");
	end, random
    )
end

end


Hier die abgespeckte Szene. Oben den Random einbinden, unten dann anstatt den Sekunden “random” eingeben. Aber darauf achten, dass 1 Sek = 1000 ist.

Ja das geht schon, aber du hast im ersten script eine Menge an devices angegeben, deshalb hätte ich der Einfachheit halber folgendes gemacht:


	fibaro:call(29, "close");
        fibaro:sleep(math.random(5000,10000));
	fibaro:call(5, "close");
        fibaro:sleep(math.random(5000,10000));
	fibaro:call(25, "close");
        fibaro:sleep(math.random(5000,10000));
	...

perfekt danke :slight_smile:

bekomme jetzt den fehler:

[DEBUG] 19:38:56: line 65: attempt to call global ‘random’ (a nil value)

habe das etwas geändert:

--[[
%% autostart
%% properties
%% events
%% globals
--]]

math.randomseed(os.time())
local random1 = math.random(5000,9000)
local random2 = math.random(5000,7000)
local random3 = math.random(9000,12000)
local random4 = math.random(15000,19000)
local random5 = math.random(20000,24000)
local random6 = math.random(1,5)

local sourceTrigger = fibaro:getSourceTrigger();
function tempFunc()
local currentDate = os.date("*t");
local startSource = fibaro:getSourceTrigger();
if (
 ( ((currentDate.wday == 1 or currentDate.wday == 2 or currentDate.wday == 3 or currentDate.wday == 4 or currentDate.wday == 5 or currentDate.wday == 6 or currentDate.wday == 7) and os.date("%H:%M", os.time()-15*60) == fibaro:getValue(1, "sunsetHour")) )
)
then
        setTimeout(function()
		fibaro:call(33, "close");
	end, random(random6))
        setTimeout(function()
		fibaro:call(25, "close");
	end, random(random6))
	setTimeout(function()
		fibaro:call(5, "close");
	end, random(random6))
	setTimeout(function()
		fibaro:call(29, "close");
	end, random(random6))
	setTimeout(function()
		fibaro:call(31, "close");
	end, random(random6))
	setTimeout(function()
		fibaro:call(35, "close");
	end, random(random6))
	setTimeout(function()
		fibaro:call(27, "close");
	end, random(random6))
end

setTimeout(tempFunc, 60*1000)
end
if (sourceTrigger["type"] == "autostart") then
tempFunc()
else

local currentDate = os.date("*t");
local startSource = fibaro:getSourceTrigger();
if (
startSource["type"] == "other"
)
then
    setTimeout(function()
		fibaro:call(33, "close");
	end, random(random6))
    setTimeout(function()
		fibaro:call(29, "close");
	end, random(random6))
	setTimeout(function()
		fibaro:call(5, "close");
	end, random(random6))
	setTimeout(function()
		fibaro:call(25, "close");
	end, random(random6))
	setTimeout(function()
		fibaro:call(31, "close");
	end, random(random6))
	setTimeout(function()
		fibaro:call(35, "close");
	end, random(random6))
	setTimeout(function()
		fibaro:call(27, "close");
	end, random(random6))
end

end

ich will mit “random6” halt zufällig einer der Random 1-5 auswählen.

Mit nur random 1-5 war irgendwie immer noch nicht “zufällig” genügend das verschließen / öffnen (2 Szenen)

Hi,
das kann so nicht funktionieren, denn random ist keine deklarierte Funktion, deshalb der Fehler. Aber selbst wenn Du es durch math.random ersetzt bringt das nichts, denn das wäre ein neuer random und hätte einen Parameter zu wenig. Und wenn Du nur random6 in der setTimeout Funktion übrig läst, hast Du dann eine 1 bis 5 drin stehen. Das ist nicht was Du willst.
Habe eventuell am WE Zeit darüber nachzudenken.