Aller au contenu

Messages recommandés

Posté(e)

@Sebcbien

Merci beaucoup !!!

J'ai mis en place, et comme je suis encore en déplacement, c'est déjàen fonctionnement.

Je surveillerai ce soir, pour savoir si la simulation s'arrête bien àj'heure prévue et si elle redémarre bien demain soir.

Je te tiens informé.

En tout cas nickel ! Merci encore

Posté(e)

Nickel, j'attends le retour !

De mon côté j'améliore encore un peu mais ce sont surtout les notifications, debug etc, le fonctionnement n'est pas modifié (pour le moment)

Posté(e)

merci pour la réponse rapide.

mais par contre désolé, mais j'aurai bien vu plutôt une durée de simulation.

je ne trouve pas cohérent en sachant que l'heure de début n'est pas la même...

 

en plus une heure de fin fixe, ca se trace facilement ;)

 

 

enfin merci quand même, j'utilisais l'ancienne version avant...

Si ça peut t'aider, la variable

local rndmaxtime = 30

Définit aussi la latitude possible de l'heure de fin.

En effet, le script ne se termine qu'a près la fin du temps aléatoire qui a été donné à  la dernière lampe allumée ou éteinte.

Donc certains jours la simulation se terminera à  l'heure désirée + un random time de 2 min, et un autre jour, il faudra ajouter 28 minutes.

Posté(e)

bon y'a un bug :-/

le script se relance tout seul 5 minutes après la simulation..

Je vais voir ce que je peux faire...

Posté(e)

quel cauchemar ces gestions d'heures  :o

Je pense que je devrais prendre une autre approche... la nuit porte conseil  B)

Posté(e)

@sebcbien

 

Cela tourne chez moi.

Arrêt hier soir à  l'heure prévue.

 

Push vers Smartphone pour avertir le début de la Simu vers 17h30 avec l'heure de fin à  22h30.

controle du tout, ce à  l'air de bien fonctionner..

[DEBUG] 17:23:42: sleeping 5 min before re-check
[DEBUG] 17:28:42: Waiting for next Sunset -> Simulation OFF
[DEBUG] 17:28:42: sleeping 5 min before re-check
[DEBUG] 17:33:42: Waiting for next Sunset -> Simulation OFF
[DEBUG] 17:33:42: sleeping 5 min before re-check
[DEBUG] 17:38:42: It's sunset time -> Simulation ON
[DEBUG] 17:38:42: Push message (Lights simulation started, will stop at: 22:30) sent to mobile: 116
[DEBUG] 17:38:42: Lights simulation started, will stop at: 22:30
[DEBUG] 17:38:43: light ID:74 status:99
[DEBUG] 17:38:43: entering loop of 0.97minutes
[DEBUG] 17:39:41: exiting loop of 0.97minutes
[DEBUG] 17:39:41: sleeptime:0.97
[DEBUG] 17:39:42: light ID:57 status:1
[DEBUG] 17:39:42: entering loop of 7.6931833333333minutes
[DEBUG] 17:47:24: exiting loop of 7.6931833333333minutes
[DEBUG] 17:47:24: sleeptime:7.6931833333333
[DEBUG] 17:47:25: light ID:192 status:29
[DEBUG] 17:47:25: entering loop of 9.04245minutes
[DEBUG] 17:56:27: exiting loop of 9.04245minutes
[DEBUG] 17:56:27: sleeptime:9.04245
[DEBUG] 17:56:28: light ID:65 status:1
[DEBUG] 17:56:28: entering loop of 8.2615666666667minutes
[DEBUG] 18:04:44: exiting loop of 8.2615666666667minutes
[DEBUG] 18:04:44: sleeptime:8.2615666666667
[DEBUG] 18:04:45: light ID:88 status:12
[DEBUG] 18:04:45: entering loop of 8.1353333333333minutes
[DEBUG] 18:12:53: exiting loop of 8.1353333333333minutes

A voir peut être sur plusieurs jours.

De mon coté c'est Ok.

 

Je n'ai pas eu le problème tel que tu le décris...

 

 

sebcbien

Posté hier, 21:12 

bon y'a un bug :-/

le script se relance tout seul 5 minutes après la simulation..

Je vais voir ce que je peux faire...

Posté(e)

Ok, merci du retour, mais je pense que je testais avec une heure de fin après minuit. (1h10 du matin) et làle script ne savais pas le gérer.

Je fais encore quelques tests avec la dernière version qui jusqu'ici fonctionne bien et puis je la poste.

Posté(e)

Ah oui, en effet.. pas essayé avec une heure de fin après 00h00.

Sur cette dernière version, vous avez réécrit le code ou fait des motifs ?

Posté(e)

J'ai ajouté des vérifications pour que l'ajout de 24h pour le prochain endtime ne se fasse qu'après la nuit.

j'ai aussi peaufiné un peu plus les notifications et debug pour que ça prenne moins de place

Posté(e)

voici la V2.4.2

--[[ 
%% autostart
%% properties 
%% globals 
Simu_presence 
--]] 

--------------------------------
-- YAPS Presence Simulator V2.4.2
-- SebcBien
-- Janvier 2015
--------------------------------
--v2.4.2
-- Fine tuning
--v2.4.1
-- fixed big bug simulator restarting after end time
--V2.3.1
-- small notification and debug changes
--V2.3
-- Rewriting the engine
-- now relaunch automatically the next day, even if Simu_presence has not changed
--V2.2
-- Added Manual Stop variable
-- added list of mobiles

if (fibaro:countScenes() > 1) then 
	fibaro:debug("More than one scene active, exiting!"); 
	fibaro:abort(); 
end 
		
--------------------- USER SETTINGS -------------------------------- 
local id = {
	LAMPE_SDB		= 16,
	LAMPE_CH_AMIS		= 24,
	LAMPE_SALON		= 45,
	LAMPE_BUREAU		= 49,
	LAMPE_HALL		= 52,
	LAMPE_CELLIER		= 56,
	LAMPE_CH_EMILIEN	= 58,
	PHONE_NEXUS_5		= 53,
	PHONE_NEXUS_4		= 104
	}
  
local stop_hour = "01"; -- Hour when you want simulation to stop 
local stop_minute = "10"; -- Minute of the hour you want simulation to stop 
-- note 1: the script will not exit while waiting the random time of the last light turned on. So end time can be longer than specified end time
-- note 2: if the global variable changes during the same wait time as above, it will exit immediately (when back home while simulation runs)
local rndmaxtime = 15 -- random time of light change in minutes --> here each device is on maximum 30min 
local ID_devices_lights = {id["LAMPE_SDB"],id["LAMPE_BUREAU"],id["LAMPE_HALL"],id["LAMPE_CELLIER"],id["LAMPE_CH_AMIS"]} -- IDs of lights to use in simulation 
--local ID_devices_lights = {id["LAMPE_BUREAU"],id["LAMPE_CELLIER"]} -- IDs of lights to use in simulation
local activatePush = true; -- activate push when simulation starts and stops 
--local ID_Smartphone = 53; -- ID of your smartphone 
--local ID_Smartphones = {id["PHONE_NEXUS_5"],id["PHONE_NEXUS_4"]}; 
local ID_Smartphones = {id["PHONE_NEXUS_5"]}; 
local ID_On_After_Simu = id["LAMPE_HALL"] -- Only One ID of a lamp to turn on after simulation ends (set 0 to disable)
local Manual_Stop = 1 -- 0 will not turn on the lamp "ID_On_After_Simu" at the end of the script. Replace this variable by a global value if you want to automate
--------------------- USER SETTINGS END ---------------------------- 
----------------------ADVANCED SETTINGS----------------------------- 
local showStandardDebugInfo = true; -- Debug shown in white 
local showExtraDebugInfo = true; -- Debug shown in orange 
local numbers_lights = #ID_devices_lights -- numbers of light devices listed above 
local manualOveride = fibaro:getGlobal("overideSimuSunset"); -- if = 1 then the simulation is forced
-------------------------------------------------------------------- 
----------------------------------- 
----- Do not change code below ---- 
----------------------------------- 
local simu = fibaro:getGlobal("Simu_presence"); --value of the global value: simulation is on or off 
local start_simu = fibaro:getValue(1, "sunsetHour"); --Start simulation when sunset 
local endtime 
local wait_for_tomorrow = 0
local NotifLoop = 30
version = "2.4.2" 

SimulatorPresenceEngine = {}; 

-- debug function 
Debug = function ( color, message ) 
		fibaro:debug(string.format('<%s style="color:%s;">%s</%s>', "span", color, message, "span")); 
	end 

ExtraDebug = function (debugMessage) 
	if ( showExtraDebugInfo ) then 
		Debug( "orange", debugMessage); 
	end 
	end 

StandardDebug = function (debugMessage) 
	if ( showStandardDebugInfo ) then 
		Debug( "white", debugMessage); 
	end 
	end 
-- function push message to mobile 
pushMessage = function (sendPush) 
	if (activatePush) then 
    for i=1, #ID_Smartphones do 
      fibaro:call(tonumber(ID_Smartphones[i]), 'sendPush', sendPush); 
      ExtraDebug("Push message ("..sendPush..") sent to mobile: "..tonumber(ID_Smartphones[i])); 
    end 
	end 
	end
-- function to calculate endtime 
function SimulatorPresenceEngine:EndTimeCalc() 
	local start = os.date("%H:%M") 
	local time = os.time() 
	local date = os.date("*t", time) 
	local year = date.year 
	local month = date.month 
	local day = date.day 
	endtime = os.time{year=year, month=month, day=day, hour=stop_hour, min=stop_minute, sec=sec}
	-- to calculate when it's daytime
	local currentHour = os.date("*t")
	local sunrise = tonumber(string.sub (fibaro:getValue(1,'sunriseHour'), 1 , 2) ) * 60 + tonumber(string.sub(fibaro:getValue(1,'sunriseHour'), 4) )
	local sunset = tonumber(string.sub (fibaro:getValue(1,'sunsetHour'), 1 , 2) ) * 60 + tonumber(string.sub(fibaro:getValue(1,'sunsetHour'), 4) )
	local now = currentHour.hour * 60 + currentHour.min;
	--ExtraDebug ("debug info: Sunrise : " .. sunrise .. " Sunset : "..sunset .. " Now : " ..now);
	--ExtraDebug ("debug info: Current OS Time" ..os.time()) 
	--ExtraDebug ("debug info: Original planed EndTime " ..endtime) 
	--ExtraDebug ("debug info: os.date: "..os.date("%H:%M").. " sunrisehour: "..fibaro:getValue(1, "sunriseHour"))
	if ((wait_for_tomorrow == 0) and (endtime < os.time())) then -- if endtime is gone and it's the first launch of simulator
		endtime = endtime + 24*60*60 -- add 24h at endtime after the night is gone
		start_simu = fibaro:getValue(1, "sunsetHour"); -- recalculate for next day
		ExtraDebug ("Added 24H to Endtime (first start ending after midnignt)");
		ExtraDebug ("Recalculated Simulation StartHour (Sunset): " .. start_simu); 		
		wait_for_tomorrow = 1	
	end 
	if (wait_for_tomorrow == 1 and (endtime < os.time()) and ((now >= sunrise) and (now <= sunset))) then -- if it looping days and endtime is gone and we are daytime
		endtime = endtime + 24*60*60 -- add 24h at endtime after the night is gone
		start_simu = fibaro:getValue(1, "sunsetHour"); -- recalculate for next day
		ExtraDebug ("Added One Day to Endtime: " .. endtime);
		ExtraDebug ("Recalculated Simulation StartHour (Sunset): " .. start_simu); 	
	end 
	--ExtraDebug ("debug info: Recalculated planed EndTime " ..endtime) 
	end 
-- function to simulate a presence 
function SimulatorPresenceEngine:Launch() 
	pushMessage("Lights simulation started, will stop at: "..stop_hour..":"..stop_minute) 
	ExtraDebug("Lights simulation started, will stop at: "..stop_hour..":"..stop_minute ); 
	while ((os.time() <= endtime) and (simu == "1")) or ((manualOveride == "1")) do 
		-- original code: while ((os.time() <= endtime) and (simu == "1")) or ((os.time() <= endtime) and (simu == "1") and (manualOveride == "1")) do 
		if time == endtime then StandardDebug("time and endtime same value -> end") end 
		local random_light = tonumber(ID_devices_lights[math.random(numbers_lights)]) --choose a random light in the list 
		local lightstatus = fibaro:getValue(random_light, 'value') --get the value of the random light in the list 
		-- turn on the light if off or turn off if on 
		if tonumber(lightstatus) == 0 then fibaro:call(random_light, 'turnOn') else fibaro:call(random_light, 'turnOff') end 
		fibaro:sleep(1000); --necessary to get back the new status, because HC2 is too fast :-) 
		lightstatus = fibaro:getValue(random_light, 'value') --get the value of the random light after his update 
		StandardDebug('light ID:'..random_light..' status:'..lightstatus);
		local sleeptime = math.random(rndmaxtime*60000) --random sleep 
		fibaro:debug("entering loop of " .. sleeptime/60000 .. "minutes");
		-- This modification allows to exit the scene if the Simu_presence global var changes to 0 during the random  sleep
			local counterexitsimu = 200
				while (counterexitsimu > 0) do
					counterexitsimu = counterexitsimu - 1;
					test_presence_state = fibaro:getGlobal("Simu_presence");
					simu = tonumber(test_presence_state); --verify the global value, if the virtual device is deactivated, the scene stops. 
					--fibaro:debug("simu var state : " .. simu);
					if simu == 0 then
						counterexitsimu = 0
					end
				fibaro:sleep(sleeptime/200);
				end
			fibaro:debug("exiting loop of " .. sleeptime/60000 .. "minutes");
		local sleeptimemin = math.abs(sleeptime/60000) 
		StandardDebug('sleeptime:'..sleeptimemin);
		simu = fibaro:getGlobal("Simu_presence"); --verify the global value, if the virtual device is deactivated, the scene stops. 
		manualOveride = fibaro:getGlobalValue("overideSimuSunset");
	end 
	end 
	
function SimulatorPresenceEngine:EndSimulation() 
	SimulatorPresenceEngine:TurnOff(ID_devices_lights); 
	Debug("red","Simulation is deactivated");
	if (simu == "1") then
		Debug("grey", "Presence Simulator will Restart tomorrow around ".. fibaro:getValue(1, "sunsetHour"));
		pushMessage("Presence Simulator will Restart tomorrow around ".. fibaro:getValue(1, "sunsetHour"));
		wait_for_tomorrow = 1 -- will make EndTimeCalc add 24h to endtime during daytime
	end
end

function SimulatorPresenceEngine:ExitSimulation()
	--SimulatorPresenceEngine:TurnOff(ID_devices_lights); 
	Debug("red","Presence Simulator is Terminated");
	pushMessage("Presence Simulator is Terminated");
end
-- function to switch off devices in the list 
function SimulatorPresenceEngine:TurnOff(group) 
	Debug("red","TurnOff All lights!");
	local name, id2; 
	local ID_devices_group = group; 
	for i=1, #ID_devices_group do 
	id2 = tonumber(ID_devices_group[i]); 
	fibaro:call(id2, "turnOff"); 
	name = fibaro:getName(id2); 
	if (name == nil or name == string.char(0)) then 
		name = "Unknown" 	
	end 
	StandardDebug("Device:" .. name .. " Off "); 
	end 
	  if (ID_On_After_Simu ~= 0 and Manual_Stop == 1) then
	  fibaro:call(ID_On_After_Simu, "turnOn"); 
	  end
	end 
-- tester startup type et si autostart ou simu = 0 ne pas push et exit
Debug("green", "Presence Simulator | v" .. version ); 
Debug( "green", "--------------------------------");

-- Main Loop ---------------------------------------
if (simu == "0") then 
	Debug("red","Not starting Simulation (Simu_presence = 0)");
	SimulatorPresenceEngine:ExitSimulation();
	fibaro:abort(); 
end

pushMessage("Scheduled Simulation starting time: " .. start_simu);
ExtraDebug("Today's sunset is at "..fibaro:getValue(1, "sunsetHour").." - End of Simulation at "..stop_hour..":"..stop_minute);

while (simu=="1" or simu=="0" ) do
	SimulatorPresenceEngine:EndTimeCalc(); 
	-- local start_simu = "00:01"  -- uncomment this line when testing to force a start hour. ex: 1 min after saving the scene.
	-- define if nighttime (sunset)
	if (os.date("%H:%M") >= start_simu) then 
		sunset = 1 
	else 
		sunset = 0 
	end 
	if (simu == "1") then 
		if sunset == 1 and (os.time() <= endtime) then 
			Debug("grey", "It's sunset time -> Simulation ON");
			SimulatorPresenceEngine:Launch(); --launch the simulation. 
			SimulatorPresenceEngine:EndSimulation();
		end 

		if manualOveride == "1" then 
			Debug("grey", "Manual Override Activated -> Simulation ON");
			SimulatorPresenceEngine:Launch(); --launch the simulation. 
			SimulatorPresenceEngine:EndSimulation();
		end
			--fibaro:debug("sunset: "..sunset .. "endtime: " .. endtime .. "ostime: " .. os.time());
		if manualOveride == "0" and sunset == 0 and NotifLoop == 30 then 
			Debug("grey", "Waiting for next Sunset -> Simulation OFF."); 
		end
	end 
	if sunset == 1 and (os.time() >= endtime) and (os.time() <= (endtime + 60)) then 
		Debug("grey","Simulation ended for this night.");
	end 
		-- Condition to end simulation 
	if (simu == "0") then 
		SimulatorPresenceEngine:ExitSimulation();
		Debug("red","Simu = 0, Exit from scene");
		fibaro:abort(); 
	end
		if NotifLoop <= 30 then
    		if NotifLoop == 30 then NotifLoop = 0
			end
			if NotifLoop == 0 then ExtraDebug("Looping to check for changes every 2min")
			end
			NotifLoop = NotifLoop + 1
    	end
	fibaro:sleep(2*60*1000);
	simu = fibaro:getGlobal("Simu_presence"); 
	manualOveride = fibaro:getGlobal("overideSimuSunset"); 

end
Posté(e)

Installé !!!

Par contre, je ne pourrais pas tester sur plus de 24h, je reviens demain soir.

Mais de nouveau en voyage la semaine suivante. 

Je donnerais un retour milieu de semaine prochaine.

Merci !

Posté(e)

A propos, J'ai mis le repo git de YAPS dans ma signature (uniquement code de la scène)

Sent From my Vic20

Posté(e)

Pour le moment impeccable.

Extinction à  l'heure, redémarrage automatique à  l'heure... 

Parfait quoi !

 

Bon, je chipotte...

en termes de debug, ce log ne fait pas si propre....  :P

[DEBUG] 20:31:15: light ID:44 status:0
[DEBUG] 20:31:15: entering loop of 14.952766666667minutes

- 14.952766666667minutes, peux pas le raccourcir....à  14.5 ou 15.0 ?

Posté(e)

@sebcbien

Il y a un truc qui ne joue pas...

Hormis le fait que ma box était planté.

Lors du redémarrage, la Simu s'est remis en route...

Voici les loggs

[DEBUG] 23:53:03: Presence Simulator | v2.4.2
[DEBUG] 23:53:03: --------------------------------
[DEBUG] 23:53:03: Push message (Scheduled Simulation starting time: 17:37) sent to mobile: 116
[DEBUG] 23:53:03: Today's sunset is at 17:37 - End of Simulation at 23:05
[DEBUG] 23:53:03: Added 24H to Endtime (first start ending after midnignt)
[DEBUG] 23:53:03: Recalculated Simulation StartHour (Sunset): 17:37
[DEBUG] 23:53:03: It's sunset time -> Simulation ON
[DEBUG] 23:53:03: Push message (Lights simulation started, will stop at: 23:05) sent to mobile: 116
[DEBUG] 23:53:03: Lights simulation started, will stop at: 23:05
[DEBUG] 23:53:04: light ID:44 status:0
[DEBUG] 23:53:04: entering loop of 7.8876666666667minutes
[DEBUG] 23:59:56: exiting loop of 7.8876666666667minutes
[DEBUG] 23:59:56: sleeptime:7.8876666666667
[DEBUG] 23:59:57: light ID:57 status:0
[DEBUG] 23:59:57: entering loop of 15.968816666667minutes
[DEBUG] 00:15:55: exiting loop of 15.968816666667minutes
[DEBUG] 00:15:55: sleeptime:15.968816666667
[DEBUG] 00:15:56: light ID:44 status:1
[DEBUG] 00:15:56: entering loop of 3.9510333333333minutes
[DEBUG] 00:17:17: More than one scene active, exiting!
[DEBUG] 00:17:19: exiting loop of 3.9510333333333minutes
[DEBUG] 00:17:19: sleeptime:3.9510333333333
[DEBUG] 00:17:19: TurnOff All lights!
[DEBUG] 00:17:19: Device:Lampes Alcôve Off
[DEBUG] 00:17:19: Device:Eclair Plafond Cui Off
[DEBUG] 00:17:19: Device:Eclair Plafond Ami Off
[DEBUG] 00:17:19: Device:Lampes TV Off
[DEBUG] 00:17:19: Device:Lampe Bureau Ami Off
[DEBUG] 00:17:19: Device:Lampe Plafond Off
[DEBUG] 00:17:19: Device:Eclair Plafond SaM Off
[DEBUG] 00:17:19: Device:Eclair Plafond Off
[DEBUG] 00:17:19: Device:LampePlafond Off
[DEBUG] 00:17:19: Device:Eclair Applique Cui Off
[DEBUG] 00:17:19: Device:Eclair Lustre Off
[DEBUG] 00:17:19: Device:Eclair Applique Off
[DEBUG] 00:17:19: Simulation is deactivated
[DEBUG] 00:17:19: Presence Simulator is Terminated
[DEBUG] 00:17:19: Push message (Presence Simulator is Terminated) sent to mobile: 116
[DEBUG] 00:17:19: Simu = 0, Exit from scene

je regarderais de nouveau demain, j'ai déjà  assez de mal pour ce soir avec la neige...  :)

Posté(e)

Je ne sais pas regarder ça maintenant, pour que je puisse regarder, sois sûr que j'aie toutes les infos (heures démarrage et arrêt. Heures programmées et heure ou tu as interrompu etc.

Merci

Sent From my Vic20

Posté(e)

Oui bien sur, il n'y a aucune urgence, ce n'est pas si grave...

Voici les infos....

Heures de départ de la Simu: Sunset.

Heure de fin: 23:05

Redémarrage de la box: vers 23h40

Je pense, mais je n'ai pas regardé, que la variable Simu était sur 1 (On)

De mon point de vue, ce quoi ne va pas c'est:

 

  1. [DEBUG] 23:53:03: Today's sunset is at 17:37 - End of Simulation at 23:05
  2. [DEBUG] 23:53:03: Added 24H to Endtime (first start ending after midnignt)
  3. [DEBUG] 23:53:03: Recalculated Simulation StartHour (Sunset): 17:37
  4. [DEBUG] 23:53:03: It's sunset time -> Simulation ON
  5. [DEBUG] 23:53:03: Push message (Lights simulation started, will stop at: 23:05) sent to mobile: 116
  6. [DEBUG] 23:53:03: Lights simulation started, will stop at: 23:05

 

Encore une fois, merci pour ton aide

Posté(e)

Peux-tu faire ce test:

Aller sur la scène et faire un save après 23h05 mais avant minuit

Si la simulation démarre, je pense que j'ai une piste...

Désolé mais pour le moment, j'ai une to-do de ouf ;-)

Posté(e)

Alors, oui en effet, après un save, la simu redémarre.

Pour être franc, ce n'est pas trop gênant dans l'immédiat. En fait si tu laisses tourner la scène toute seule, la simu fonctionne bien. C'est uniquement si tu redémarre le tout après l'heure d'arrêt, si je ne m'endors pas avant de vérifierais jusqu'a qu'elle heure ce comportement existe, je ne répond plus de rien après minuit... :-)

merci pour ton support

Posté(e)

J'ai trouvé une demie heure et je viens de terminer le débug ;-)

j'ai même accédé à  la demande de l'arrondi de l'affichage des minutes ;-)

Note: maintenant par défaut j'ai mis le extradebug à  false. en cas de problème donc, pour avoir un debug plus bavard, remettre cette variable à  true.

 

here it is:

--[[ 
%% autostart
%% properties 
%% globals 
Simu_presence 
--]] 

--------------------------------
-- YAPS Presence Simulator V2.5.0
-- SebcBien
-- Février 2015
--------------------------------
--V2.5.0
-- fixed simulation starting if restarted between endtime & midnight
--v2.4.1
-- fixed big bug simulator restarting after end time
--V2.3.1
-- small notification and debug changes
--V2.3
-- Rewriting the engine
-- now relaunch automatically the next day, even if Simu_presence has not changed
--V2.2
-- Added Manual Stop variable
-- added list of mobiles

if (fibaro:countScenes() > 1) then 
	fibaro:debug("More than one scene active, exiting!"); 
	fibaro:abort(); 
end 
		
--------------------- USER SETTINGS -------------------------------- 
local id = {
	LAMPE_SDB		= 16,
	LAMPE_CH_AMIS		= 24,
	LAMPE_SALON		= 45,
	LAMPE_BUREAU		= 49,
	LAMPE_HALL		= 52,
	LAMPE_CELLIER		= 56,
	LAMPE_CH_EMILIEN	= 58,
	PHONE_NEXUS_5		= 53,
	PHONE_NEXUS_4		= 104
	}
  
local stop_hour = "01"; -- Hour when you want simulation to stop 
local stop_minute = "10"; -- Minute of the hour you want simulation to stop 
-- note 1: the script will not exit while waiting the random time of the last light turned on. So end time can be longer than specified end time
-- note 2: if the global variable changes during the same wait time as above, it will exit immediately (when back home while simulation runs)
local rndmaxtime = 15; -- random time of light change in minutes --> here each device is on maximum 30min 
local ID_devices_lights = {id["LAMPE_SDB"],id["LAMPE_BUREAU"],id["LAMPE_HALL"],id["LAMPE_CELLIER"],id["LAMPE_CH_AMIS"]} -- IDs of lights to use in simulation 
--local ID_devices_lights = {id["LAMPE_BUREAU"],id["LAMPE_CELLIER"]} -- IDs of lights to use in simulation
local activatePush = true; -- activate push when simulation starts and stops 
--local ID_Smartphone = 53; -- ID of your smartphone 
--local ID_Smartphones = {id["PHONE_NEXUS_5"],id["PHONE_NEXUS_4"]}; 
local ID_Smartphones = {id["PHONE_NEXUS_5"]}; 
local ID_On_After_Simu = id["LAMPE_HALL"]; -- Only One ID of a lamp to turn on after simulation ends (set 0 to disable)
local Manual_Stop = 1; -- 0 will not turn on the lamp "ID_On_After_Simu" at the end of the script. Replace this variable by a global value if you want to automate
--------------------- USER SETTINGS END ---------------------------- 
----------------------ADVANCED SETTINGS----------------------------- 
local showStandardDebugInfo = true; -- Debug shown in white 
local showExtraDebugInfo = false; -- Debug shown in orange 
local numbers_lights = #ID_devices_lights; -- numbers of light devices listed above 
local manualOveride = fibaro:getGlobal("overideSimuSunset"); -- if = 1 then the simulation is forced
-------------------------------------------------------------------- 
----------------------------------- 
----- Do not change code below ---- 
----------------------------------- 
local version = "2.5.0"; 
local simu = fibaro:getGlobal("Simu_presence"); --value of the global value: simulation is on or off 
local start_simu = fibaro:getValue(1, "sunsetHour"); --Start simulation when sunset 
local endtime;
local wait_for_tomorrow = 1;
local NotifLoop = 30;


SimulatorPresenceEngine = {}; 

-- debug function 
Debug = function ( color, message ) 
		fibaro:debug(string.format('<%s style="color:%s;">%s</%s>', "span", color, message, "span")); 
	end 

ExtraDebug = function (debugMessage) 
	if ( showExtraDebugInfo ) then 
		Debug( "orange", debugMessage); 
	end 
	end 

StandardDebug = function (debugMessage) 
	if ( showStandardDebugInfo ) then 
		Debug( "white", debugMessage); 
	end 
	end 

round = function (num, idp)
  local mult = 10^(idp or 0)
  return math.floor(num * mult + 0.5) / mult
end
-- function push message to mobile 
pushMessage = function (sendPush) 
	if (activatePush) then 
    for i=1, #ID_Smartphones do 
      fibaro:call(tonumber(ID_Smartphones[i]), 'sendPush', sendPush); 
      ExtraDebug("Push message ("..sendPush..") sent to mobile: "..tonumber(ID_Smartphones[i])); 
    end 
	end 
	end
-- function to calculate endtime 
function SimulatorPresenceEngine:EndTimeCalc() 
	local start = os.date("%H:%M") 
	local time = os.time() 
	local date = os.date("*t", time) 
	local year = date.year 
	local month = date.month 
	local day = date.day 
	endtime = os.time{year=year, month=month, day=day, hour=stop_hour, min=stop_minute, sec=sec}
	-- to calculate when it's daytime
	local currentHour = os.date("*t")
	local sunrise = tonumber(string.sub (fibaro:getValue(1,'sunriseHour'), 1 , 2) ) * 60 + tonumber(string.sub(fibaro:getValue(1,'sunriseHour'), 4) )
	local sunset = tonumber(string.sub (fibaro:getValue(1,'sunsetHour'), 1 , 2) ) * 60 + tonumber(string.sub(fibaro:getValue(1,'sunsetHour'), 4) )
	local now = currentHour.hour * 60 + currentHour.min;
	--ExtraDebug ("debug info: Sunrise : " .. sunrise .. " Sunset : "..sunset .. " Now : " ..now);
	--ExtraDebug ("debug info: Current OS Time" ..os.time()) 
	--ExtraDebug ("debug info: Original planed EndTime " ..endtime) 
	--ExtraDebug ("debug info: os.date: "..os.date("%H:%M").. " sunrisehour: "..fibaro:getValue(1, "sunriseHour"))
	if ((wait_for_tomorrow == 0) and (endtime < os.time())) then -- if endtime is gone and it's the first launch of simulator
		endtime = endtime + 24*60*60 -- add 24h at endtime after the night is gone
		start_simu = fibaro:getValue(1, "sunsetHour"); -- recalculate for next day
		ExtraDebug ("Added 24H to Endtime (first start ending after midnignt)");
		ExtraDebug ("Recalculated Simulation StartHour (Sunset): " .. start_simu); 		
		wait_for_tomorrow = 1	
	end 
	if (wait_for_tomorrow == 1 and (endtime < os.time()) and ((now >= sunrise) and (now <= sunset))) then -- if it looping days and endtime is gone and we are daytime
		endtime = endtime + 24*60*60 -- add 24h at endtime after the night is gone
		start_simu = fibaro:getValue(1, "sunsetHour"); -- recalculate for next day
		ExtraDebug ("Added One Day to Endtime: " .. endtime);
		ExtraDebug ("Recalculated Simulation StartHour (Sunset): " .. start_simu); 	
	end 
	--ExtraDebug ("debug info: Recalculated planed EndTime " ..endtime) 
	end 
-- function to simulate a presence 
function SimulatorPresenceEngine:Launch() 
	pushMessage("Lights simulation started, will stop at: "..stop_hour..":"..stop_minute) 
	ExtraDebug("Lights simulation started, will stop at: "..stop_hour..":"..stop_minute ); 
	while ((os.time() <= endtime) and (simu == "1")) or ((manualOveride == "1")) do 
		-- original code: while ((os.time() <= endtime) and (simu == "1")) or ((os.time() <= endtime) and (simu == "1") and (manualOveride == "1")) do 
		if time == endtime then StandardDebug("time and endtime same value -> end") end 
		local random_light = tonumber(ID_devices_lights[math.random(numbers_lights)]) --choose a random light in the list 
		local lightstatus = fibaro:getValue(random_light, 'value') --get the value of the random light in the list 
		-- turn on the light if off or turn off if on 
		if tonumber(lightstatus) == 0 then fibaro:call(random_light, 'turnOn') else fibaro:call(random_light, 'turnOff') end 
		fibaro:sleep(1000); --necessary to get back the new status, because HC2 is too fast :-) 
		lightstatus = fibaro:getValue(random_light, 'value') --get the value of the random light after his update 
		StandardDebug('light ID:'..random_light..' status:'..lightstatus);
		local sleeptime = math.random(rndmaxtime*60000) --random sleep 
		StandardDebug("entering loop of " .. round(sleeptime/60000,2) .. " minutes");
		-- This modification allows to exit the scene if the Simu_presence global var changes to 0 during the random  sleep
			local counterexitsimu = 200
				while (counterexitsimu > 0) do
					counterexitsimu = counterexitsimu - 1;
					test_presence_state = fibaro:getGlobal("Simu_presence");
					simu = tonumber(test_presence_state); --verify the global value, if the virtual device is deactivated, the scene stops. 
					--fibaro:debug("simu var state : " .. simu);
					if simu == 0 then
						counterexitsimu = 0
					end
				fibaro:sleep(sleeptime/200);
				end
			StandardDebug("exiting loop of " .. round(sleeptime/60000,2) .. " minutes");
		local sleeptimemin = math.abs(sleeptime/60000) 
		--StandardDebug('sleeptime:'..sleeptimemin);
		simu = fibaro:getGlobal("Simu_presence"); --verify the global value, if the virtual device is deactivated, the scene stops. 
		manualOveride = fibaro:getGlobalValue("overideSimuSunset");
	end 
	end 
	
function SimulatorPresenceEngine:EndSimulation() 
	SimulatorPresenceEngine:TurnOff(ID_devices_lights); 
	Debug("red","Simulation is deactivated");
	if (simu == "1") then
		Debug("grey", "Presence Simulator will Restart tomorrow around ".. fibaro:getValue(1, "sunsetHour"));
		pushMessage("Presence Simulator will Restart tomorrow around ".. fibaro:getValue(1, "sunsetHour"));
		wait_for_tomorrow = 1 -- will make EndTimeCalc add 24h to endtime during daytime
	end
end

function SimulatorPresenceEngine:ExitSimulation()
	--SimulatorPresenceEngine:TurnOff(ID_devices_lights); 
	Debug("red","Presence Simulator is Terminated");
	pushMessage("Presence Simulator is Terminated");
end
-- function to switch off devices in the list 
function SimulatorPresenceEngine:TurnOff(group) 
	Debug("red","TurnOff All lights!");
	local name, id2; 
	local ID_devices_group = group; 
	for i=1, #ID_devices_group do 
	id2 = tonumber(ID_devices_group[i]); 
	fibaro:call(id2, "turnOff"); 
	name = fibaro:getName(id2); 
	if (name == nil or name == string.char(0)) then 
		name = "Unknown" 	
	end 
	StandardDebug("Device:" .. name .. " Off "); 
	end 
	  if (ID_On_After_Simu ~= 0 and Manual_Stop == 1) then
	  fibaro:call(ID_On_After_Simu, "turnOn"); 
	  end
	end 
-- tester startup type et si autostart ou simu = 0 ne pas push et exit
Debug("green", "Presence Simulator | v" .. version ); 
Debug( "green", "--------------------------------");
-- if stop hour is between 00 and 12h then will consider that stop hour is before midnight
if tonumber(stop_hour) <= 12 then wait_for_tomorrow = 0 end
-- Main Loop ---------------------------------------
if (simu == "0") then 
	Debug("red","Not starting Simulation (Simu_presence = 0)");
	SimulatorPresenceEngine:ExitSimulation();
	fibaro:abort(); 
end
pushMessage("Scheduled Simulation starting time: " .. start_simu);
ExtraDebug("Today's sunset is at "..fibaro:getValue(1, "sunsetHour").." - End of Simulation at "..stop_hour..":"..stop_minute);

while (simu=="1" or simu=="0" ) do
	SimulatorPresenceEngine:EndTimeCalc(); 
	-- local start_simu = "00:01"  -- uncomment this line when testing to force a start hour. ex: 1 min after saving the scene.
	-- define if nighttime (sunset)
	if (os.date("%H:%M") >= start_simu) then 
		sunset = 1 
	else 
		sunset = 0 
	end 
	if (simu == "1") then 
		if sunset == 1 and (os.time() <= endtime) then 
			Debug("grey", "It's sunset time -> Simulation ON");
			SimulatorPresenceEngine:Launch(); --launch the simulation. 
			SimulatorPresenceEngine:EndSimulation();
		end 
		if manualOveride == "1" then 
			Debug("grey", "Manual Override Activated -> Simulation ON");
			SimulatorPresenceEngine:Launch(); --launch the simulation. 
			SimulatorPresenceEngine:EndSimulation();
		end
			--fibaro:debug("sunset: "..sunset .. "endtime: " .. endtime .. "ostime: " .. os.time());
		if manualOveride == "0" and sunset == 0 and NotifLoop == 30 then 
			Debug("grey", "Waiting for next Sunset -> Simulation OFF."); 
		end
	end 
	if sunset == 1 and (os.time() >= endtime) and (os.time() <= (endtime + 60)) then 
		Debug("grey","Simulation ended for this night.");
	end 
		-- Condition to end simulation 
	if (simu == "0") then 
		SimulatorPresenceEngine:ExitSimulation();
		Debug("red","Simu = 0, Exit from scene");
		fibaro:abort(); 
	end
		if NotifLoop <= 30 then
    		if NotifLoop == 30 then NotifLoop = 0
			end
			if NotifLoop == 0 then ExtraDebug("Looping to check for changes every 2min")
			end
			NotifLoop = NotifLoop + 1
    	end
	fibaro:sleep(2*60*1000);
	simu = fibaro:getGlobal("Simu_presence"); 
	manualOveride = fibaro:getGlobal("overideSimuSunset"); 
end
Posté(e)

En fait si tu laisses tourner la scène toute seule, la simu fonctionne bien.

 

Donc tu confirme que ça tourne bien sur plusieurs jours d'affilée ?

J'espère que je n'aurai pas tout cassé avec la dernière mise à  jour  :P

Posté(e)

Cool, chargé !!!

La simu n'a pas redémarré : Parfait

Push pour prévenir du prochain démarrage : Parfait

 

Je confirme que sur plusieurs jours, cela fonctionne.

Je laisse tourner et te redis.

:13: merci beaucoup !!!

×
×
  • Créer...