Aller au contenu

Messages recommandés

  • 2 semaines après...
Posté(e)

sebcbien,

J'ai ENFIN pris le temps de tester, sorry pour le délais.

La nouvelle version continue de fonctionner, mais je n'ai pas réussi à  la configurer pour allumer une lampe quand je rentre, c'est-à -dire à  l'arrêt du simulateur.

 

En fait, je ne comprends pas bien l'utilisation des variables suivantes :

local ID_On_After_Simu = 0; -- If next line is commented, no light will turn on after simulation ends
local ID_On_After_Simu = id["LAMPE_HALL"]; -- Only One ID of a light to turn on after simulation ends. Comment this line to turn off this feature
local ID_On_When_Simu_Deactivated = 0; -- If next line is commented, no light will turn on after simulation is stopped (by putting Simu_presence to 0)
local ID_On_When_Simu_Deactivated = id["LAMPE_HALL"]; -- Only One ID of a light to turn on after simulation is stopped. Comment this line to turn off this feature

quelle est la différence entre ID_On_After_Simu et ID_On_When_Simu_Deactivated ?

Posté(e)

J'ai pu également tester et tout semble ok, voici également une nouvelle version avec la possibilité de randomiser la fin de la simulation via la variable rndmaxendtime

Il y a aussi des améliorations du code pour plus de stabilité.

@jojo , Pour tes questions, j'ai amélioré les commentaires dans le code.

En gros, YAPS fait maintenant la différence quand le script est arrété en cours de simulation par le changement de la variable globale simu_presence. et quand le script est arrêté normalement car il a atteint l'heure de fin de simulation. dans ce cas, moi je laisse la lumière du couloir allumée (et je l'éteint via gea le matin).

La lumière du hall (qui s'éteint chez moi de toute façon automatiquement après 5 minutes) est elle allumée quand le script est terminé par changement de VG (quand je rentre chez moi tard le soir)

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

---------------------------------
-- YAPS Presence Simulator V2.6.4
-- SebcBien
-- Avril 2015
---------------------------------
--V2.6.4
-- added random end time + small stability changes and cleaning
--V2.6.3
-- Added array of lights to turn on after simu, ONLY if Simu_presence = 1 (normal ending, not ended by setting Simu_presence to 0)
--V2.6.2
-- Added the possibility to not have an always on lamp
--V2.6.1
-- Added naming of devices in the debug during simulation
--V2.6.0
-- Added the possibility to select always on light during simulation
--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("Scene already active, exiting this new occurence !!"); 
	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,
    LAMPE_COULOIR		= 1316,
	PHONE_SEB			= 1347,
    PHONE_GG			= 1327
	}
  
local stop_hour = "01"; -- Hour when you want simulation to stop 
local stop_minute = "15"; -- 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. (even more with var rndmaxendtime)
-- 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 = 20; -- random time of light change in minutes --> here each device is on maximum 30min 
local rndmaxendtime = 15; -- random time to add at the stop hour+stop minute so the simulation can be more variable (0 to deactivate)
local ID_devices_lights_always_on = {id["LAMPE_BUREAU"],id["LAMPE_COULOIR"]} -- IDs of lights who will always stay on during simulation - leave empty array if none -> {}
local ID_devices_lights = {id["LAMPE_SDB"],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"]} -- Reduced set for test purposes
local activatePush = true; -- activate push when simulation starts and stops 
--local ID_Smartphones = {id["PHONE_SEB"],id["PHONE_GG"]}; 
local ID_Smartphones = {id["PHONE_SEB"]}; -- list of device receiving Push
local ID_On_After_Simu = 0; -- If next line is commented, no light will turn on after simulation ends
local ID_On_After_Simu = id["LAMPE_COULOIR"]; -- ID of a light (Only One) to turn on after simulation ends (at specified stop_hour & stop_minute). Comment this line to turn off this feature
local ID_On_When_Simu_Deactivated = 0; -- If next line is commented, no light will turn on after simulation is stopped (by putting Simu_presence to 0)
local ID_On_When_Simu_Deactivated = id["LAMPE_HALL"]; -- ID of a light (Only One) to turn on after simulation is stopped (Simu_). Comment this line to turn off this feature


--------------------- 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.6.4"; 
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 = {}; 

-- FONCTIONS
Debug = function ( color, message ) 
		--fibaro:debug(string.format('<%s style="color:%s;">%s</%s>', "span", color, message, "span")); 
        fibaro:debug(string.format('<%s style="color:%s;">%s</%s>', "span", color, os.date("%a %d/%m", os.time()).." "..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
-- 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
-- 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 
-- Simulate Presence Main 
function SimulatorPresenceEngine:Launch() 
	-- pushMessage("Lights simulation started, will stop at: "..stop_hour..":"..stop_minute ) 
    pushMessage("Lights simulation started, will stop at: "..stop_hour..":"..stop_minute.."+ random of "..rndmaxendtime)
	-- ExtraDebug("Lights simulation started, will stop at: "..stop_hour..":"..stop_minute );
    ExtraDebug("Lights simulation started, will stop at: "..stop_hour..":"..stop_minute.."+ random of "..rndmaxendtime);	
	if ID_devices_lights_always_on[1] ~= nil then SimulatorPresenceEngine:TurnOn(ID_devices_lights_always_on); end
	endtime = endtime + math.random(rndmaxendtime+(1/10)) -- add random minutes to end hour
	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:'.. fibaro:getName(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() 
	if ID_devices_lights_always_on[1] ~= nil then SimulatorPresenceEngine:TurnOff(ID_devices_lights,ID_devices_lights_always_on); end
	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()
	Debug("red","Presence Simulator is Terminated");
	pushMessage("Presence Simulator is Terminated");
end
-- Switch off devices in the list 
function SimulatorPresenceEngine:TurnOff(group,group2) 
	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 
	local ID_devices_group = group2; 
	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 then
	  fibaro:call(ID_On_After_Simu, "turnOn");
    	name = fibaro:getName(ID_On_After_Simu); 
			if (name == nil or name == string.char(0)) then 
				name = "Unknown" 	
			end 
      Debug("red","Manual Light Settings: Turned On light: " .. name);
	  end
	    if ID_On_When_Simu_Deactivated ~= 0 and Simu_presence == 0 then
	  fibaro:call(ID_On_When_Simu_Deactivated, "turnOn");
    	name = fibaro:getName(ID_On_When_Simu_Deactivated); 
			if (name == nil or name == string.char(0)) then 
				name = "Unknown" 	
			end 
      Debug("red","Manual Light Settings: Turned On light: " .. name);
	  end
	end 
-- Switch on devices in the list 
function SimulatorPresenceEngine:TurnOn(group) 
	Debug("red","Turning On always on 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, "turnOn"); 
	name = fibaro:getName(id2); 
	if (name == nil or name == string.char(0)) then 
		name = "Unknown" 	
	end 
	StandardDebug("Device: " .. name .. " On "); 
	end
	Debug("red","Now randomizing other lights...");
	end 
	
Debug("green", "Presence Simulator | v" .. version ); 
Debug( "green", "--------------------------------");
if tonumber(stop_hour) <= 12 then wait_for_tomorrow = 0 end -- if stop hour is between 00 and 12h then will consider that stop hour is before midnight

------------------------ Main Loop ----------------------------------
if (simu == "0") then -- check before the while loop below... remove ?. 
	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
while true do -- boucle infinie
	SimulatorPresenceEngine:EndTimeCalc(); 
	-- local start_simu = "00:01"  -- uncomment this line when testing to force a start hour (only for the first loop)

	if (os.date("%H:%M") >= start_simu) then -- define if nighttime (sunset = 1)
		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();
			SimulatorPresenceEngine:EndSimulation();
		end 
		if manualOveride == "1" then 
			Debug("grey", "Manual Override Activated -> Simulation ON");
			SimulatorPresenceEngine:Launch();
			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: " .. start_simu .. " -> 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 

	if (simu == "0") then -- Condition to end simulation 
		SimulatorPresenceEngine:ExitSimulation();
		Debug("red","Simu = 0, Exit from scene");
		fibaro:abort(); 
	end
	
	if NotifLoop <= 30 then --a waiting xx times the fibaro sleep below before checking again 
    	if NotifLoop == 30 then NotifLoop = 0  end
		if NotifLoop == 0 then ExtraDebug("Looping to check for changes every 4 min")  end
		NotifLoop = NotifLoop + 1
    end
		
	fibaro:sleep(4*60*1000); -- wait 4 minutes before testing again the global vars below
	simu = fibaro:getGlobal("Simu_presence"); 
	manualOveride = fibaro:getGlobal("overideSimuSunset"); 

end
Posté(e)

Voila voila, j'ai enfin pris le temps de l'installer, je vous tines au jus dès que je suis absent ;-)

 

tu as mis ton git à  jour avec la derniere version que tu as postée ? enfin dans tous les cas, j'ai pris celle que tu as mise dans le post précédent pour @jojo

 

Merci beaucoup pour YAPS.;

Vous n'auriez pas une icone pour la scene svp ?

Posté(e)

Non, git n'a pas la dernière version, je ne sais pas le faire pour le moment :-/

Sent from my SM-N910F using Tapatalk

Posté(e)

L'icône que j'ai mise pour la scène est la même que pour le vd.... Je la posterai bientôt

Sent from my SM-N910F using Tapatalk

Posté(e)

Sebcbien,

La lampe du hall ne veut pas s'allumer à  la fin de la simulation. Ai-je fais qqch de mal ?

voici mes paramètres

local ID_On_When_Simu_Deactivated = 0; -- If next line is commented, no light will turn on after simulation is stopped (by putting Simu_presence to 0)
local ID_On_When_Simu_Deactivated = id["LAMPE_Rez_Hall"]; -- ID of a light (Only One) to turn on after simulation is stopped (Simu_). Comment this line to turn off this feature
 

et le débug


[DEBUG] 23:35:30: Wed 05/08 Presence Simulator | v2.6.4
[DEBUG] 23:35:30: Wed 05/08 --------------------------------
[DEBUG] 23:35:30: Wed 05/08 It's sunset time -> Simulation ON
[DEBUG] 23:35:30: Wed 05/08 Turning On always on lights:
[DEBUG] 23:35:30: Wed 05/08 Device: Lum_SalonTV On
[DEBUG] 23:35:30: Wed 05/08 Now randomizing other lights...
[DEBUG] 23:35:31: Wed 05/08 light ID:Lum_SdBEtage status:1
[DEBUG] 23:35:31: Wed 05/08 entering loop of 10.94 minutes
[DEBUG] 23:35:46: Scene already active, exiting this new occurence !!
[DEBUG] 23:35:51: Wed 05/08 exiting loop of 10.94 minutes
[DEBUG] 23:35:51: Wed 05/08 TurnOff All lights!
[DEBUG] 23:35:51: Wed 05/08 Device: Lum_SdBRez_Douche Off
[DEBUG] 23:35:51: Wed 05/08 Device: Lum_SalleManger Off
[DEBUG] 23:35:51: Wed 05/08 Device: Prise_Bureau Off
[DEBUG] 23:35:51: Wed 05/08 Device: Lum_SdBEtage Off
[DEBUG] 23:35:51: Wed 05/08 Device: Lum_SalonTV Off
[DEBUG] 23:35:51: Wed 05/08 Simulation is deactivated
[DEBUG] 23:35:51: Wed 05/08 Presence Simulator is Terminated
[DEBUG] 23:35:51: Wed 05/08 Simu = 0, Exit from scene

P.S. : j'ai utilisé ton VD pour modifier les valeurs de Simu-presence

P.S.2 : j'ai vérifié 10 fois laid de la lampe du hall

 

Qu'ai-je fais de mal ?

Posté(e)

SAlut messieurs, c'est OK pour moi,

 

Pour info : impossible de mettre rndmaxendtime à  0, ca genere une erreur :

[DEBUG] 15:15:23: Thu 06/08 Device: Eclairage_Halogene On 
[DEBUG] 15:15:23: Thu 06/08 Now randomizing other lights...
[ERROR] 15:15:23: line 155: bad argument #1 to 'random' (interval is empty)

BEsoin d'aide qd meme, je souhaiterias recevoir un SMS plutot que le push qui pour moi ne fonctionne pas lol

j'ai rajoute

 

fibaro:setGlobal("Freesms", "Depart YAPS") juste au-dessus de la ligne qui appelle la fonction pushmesasge ligne 278, mais cela ne fonctionne pas.

 

Je ne recois rien, de même j'aimerais bien recevoir un SMS à  la fin de la SImu

 

@jojo : tu veux voirmon code ? mais rien de special, juste modifier le user settings comme demandé.

Mais je ne laisse pas de lumiere allumee a la fin de la simu.

Posté(e)

@pepite, 

ce que je voulais, c'est qu'à  la fin de la sim, il allume une lampe. 

En fait la fin de la sim correspond à  quand je rentre chez moi et désactive l'alarme, je souhaite que la lampe du Hall s'allume, et en tout cas  qu'elle ne séteigne pas, si elle avait été allumée par la sim.

C'est quand même bien ici qu'il faut entrer laid de la lampe à  laisser allumée : ID_On_When_Simu_Deactivated ?

Posté(e)

oui jojo j'ai compris cela aussi, mais je ne sais pas pourquoi je mettrais un 1 àla place de 0 non ?

local ID_On_When_Simu_Deactivated = 1 ; -- If next line is commented, no light will turn on after simulation is stopped (by putting Simu_presence to 0)
local ID_On_When_Simu_Deactivated = id["LAMPE_Rez_Hall"]; -- ID of a light (Only One) to turn on after simulation is stopped (Simu_). Comment this line to turn off this feature
Posté(e)

@jojo, si tu ne poste pas tes paramètres je vais avoir du mal àt'aider ;-) ceci dit avec ma config (postée) mon installation àexactement le comportement que tu souhaites...

@pepite, le 0 je ne l'ai pas testé effectivement, je met ça dans ma todo. En attendant tu peux mettre 1 ça ne ralongera endtime que de max 1 minute.

Sent from my SM-N910F using Tapatalk

Posté(e)

@ sebcbien,

Je pensais avoir mi ce qui était nécessaire, je mets plus. Merci de ton aide

 
--[[ 
%% autostart
%% properties 
%% globals 
Simu_presence 
--]] 
 
---------------------------------
-- YAPS Presence Simulator V2.6.4
-- SebcBien
-- Avril 2015
---------------------------------
--V2.6.4
-- added random end time + small stability changes and cleaning
 
if (fibaro:countScenes() > 1) then 
	fibaro:debug("Scene already active, exiting this new occurence !!"); 
	fibaro:abort(); 
end 
--------------------- USER SETTINGS --------------------------------
local id = {
	LAMPE_Rez_Salon		  = 48,
    LAMPE_Rez_SalleManger = 550,
	LAMPE_Rez_SdB 		  = 41,
    LAMPE_Rez_Hall        = 51,
	LAMPE_Etage_Bureau	  = 24,
	LAMPE_Etage_SdB		  = 126,
	PHONE_NEXUS_5		  = 357,
	}
 
local stop_hour = "00"; -- Hour when you want simulation to stop 
local stop_minute = "15"; -- 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. (even more with var rndmaxendtime)
-- 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 = 12; -- random time of light change in minutes --> here each device is on maximum 30min 
local rndmaxendtime = 15; -- random time to add at the stop hour+stop minute so the simulation can be more variable (0 to deactivate)
local ID_devices_lights_always_on = {id["LAMPE_Rez_Salon"]} -- IDs of lights who will always stay on during simulation - leave empty array if none -> {}
local ID_devices_lights = {id["LAMPE_Rez_SdB"], id["LAMPE_Rez_SalleManger"], id["LAMPE_Etage_Bureau"], id["LAMPE_Etage_SdB"]} -- IDs of lights to use in simulation 
local activatePush = true; -- activate push when simulation starts and stops 
local ID_Smartphones = {id["PHONE_NEXUS_5"]}; -- list of device receiving Push
local ID_On_After_Simu = 0; -- If next line is commented, no light will turn on after simulation ends
--local ID_On_After_Simu = id["LAMPE_COULOIR"]; -- ID of a light (Only One) to turn on after simulation ends (at specified stop_hour & stop_minute). Comment this line to turn off this feature
local ID_On_When_Simu_Deactivated = 0; -- If next line is commented, no light will turn on after simulation is stopped (by putting Simu_presence to 0)
local ID_On_When_Simu_Deactivated = id["LAMPE_Rez_Hall"]; -- ID of a light (Only One) to turn on after simulation is stopped (Simu_). Comment this line to turn off this feature
  
--------------------- 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 ---------------------- 
--------------------------------------------------------------------

Posté(e)

voili voilou, deux bugs corrigés pour @pepite et @jojo :-)

j'ai mis le showExtraDebugInfo à  true, mettez le à  false si tout va bien

J'ai égalment fixé (j'espère) un bug lorsque le simulateur devait boucler plusieurs jours sans interruption (car les jours diminuent maintenant et le calcul ne le prenait pas en compte)

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

---------------------------------
-- YAPS Presence Simulator V2.6.5
-- SebcBien
-- August 2015
---------------------------------
--V2.6.5
-- Fixed bug when rndmaxendtime = 0
-- Probably fixed endtime bug calculation when looping for days due to days that are shorter now than the previous day
-- Fixed bug not turning on ID_On_After_Simu when exiting simulation
--V2.6.4
-- added random end time + small stability changes and cleaning
--V2.6.3
-- Added array of lights to turn on after simu, ONLY if Simu_presence = 1 (normal ending, not ended by setting Simu_presence to 0)
--V2.6.2
-- Added the possibility to not have an always on lamp
--V2.6.1
-- Added naming of devices in the debug during simulation
--V2.6.0
-- Added the possibility to select always on light during simulation
--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("Scene already active, exiting this new occurence !!"); 
	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,
    LAMPE_COULOIR		= 1316,
	PHONE_SEB			= 1347,
    PHONE_GG			= 1327
	}
  
local stop_hour = "01"; -- Hour when you want simulation to stop 
local stop_minute = "15"; -- 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. (even more with var rndmaxendtime)
-- 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 = 20; -- random time of light change in minutes --> here each device is on maximum 30min 
local rndmaxendtime = 15; -- random time to add at the stop hour+stop minute so the simulation can be more variable (0 to deactivate)
local ID_devices_lights_always_on = {id["LAMPE_BUREAU"],id["LAMPE_COULOIR"]} -- IDs of lights who will always stay on during simulation - leave empty array if none -> {}
local ID_devices_lights = {id["LAMPE_SDB"],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"]} -- Reduced set for test purposes
local activatePush = true; -- activate push when simulation starts and stops 
--local ID_Smartphones = {id["PHONE_SEB"],id["PHONE_GG"]}; 
local ID_Smartphones = {id["PHONE_SEB"]}; -- list of device receiving Push
local ID_On_After_Simu = 0; -- If next line is commented, no light will turn on after simulation ends
local ID_On_After_Simu = id["LAMPE_COULOIR"]; -- ID of a light (Only One) to turn on after simulation ends (at specified stop_hour & stop_minute). Comment this line to turn off this feature
local ID_On_When_Simu_Deactivated = 0; -- If next line is commented, no light will turn on after simulation is stopped (by putting Simu_presence to 0)
local ID_On_When_Simu_Deactivated = id["LAMPE_HALL"]; -- ID of a light (Only One) to turn on after simulation is stopped (Simu_). Comment this line to turn off this feature


--------------------- 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 version = "2.6.5"; 
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 = {}; 

-- FONCTIONS
Debug = function ( color, message ) 
		--fibaro:debug(string.format('<%s style="color:%s;">%s</%s>', "span", color, message, "span")); 
        fibaro:debug(string.format('<%s style="color:%s;">%s</%s>', "span", color, os.date("%a %d/%m", os.time()).." "..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
-- 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
-- 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+(5*60) < os.time())) then -- if endtime (+5 min to avoid sunset shifting) 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+(5*60) < os.time()) and ((now >= sunrise) and (now <= sunset))) then -- if it looping days and endtime (+5 min to avoid sunset shifting) 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 
-- Simulate Presence Main 
function SimulatorPresenceEngine:Launch() 
	-- pushMessage("Lights simulation started, will stop at: "..stop_hour..":"..stop_minute ) 
    pushMessage("Lights simulation started, will stop at: "..stop_hour..":"..stop_minute.."+ random of "..rndmaxendtime)
	-- ExtraDebug("Lights simulation started, will stop at: "..stop_hour..":"..stop_minute );
    ExtraDebug("Lights simulation started, will stop at: "..stop_hour..":"..stop_minute.."+ random of "..rndmaxendtime);	
	if ID_devices_lights_always_on[1] ~= nil then SimulatorPresenceEngine:TurnOn(ID_devices_lights_always_on); end
	--rndmaxendtime = tonumber(rndmaxendtime) + 1
  if rndmaxendtime ~= 0 then   -- add random minutes to end hour 
  		endtime2 = endtime + math.random(rndmaxendtime)
    else
    	endtime2 = endtime
  end
    	ExtraDebug("old endtime : ".. endtime .. " endtime2 (+random): " .. endtime2)
    while ((os.time() <= endtime2) 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:'.. fibaro:getName(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() 
	if ID_devices_lights_always_on[1] ~= nil then SimulatorPresenceEngine:TurnOff(ID_devices_lights,ID_devices_lights_always_on); end
	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()
	Debug("red","Presence Simulator is Terminated");
	pushMessage("Presence Simulator is Terminated");
end
-- Switch off devices in the list 
function SimulatorPresenceEngine:TurnOff(group,group2) 
	Debug("red","TurnOff All Simulation 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 
	Debug("red","TurnOff All Always on lights!");
	local ID_devices_group = group2; 
	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 simu == "1" then
		fibaro:call(ID_On_After_Simu, "turnOn");
		name = fibaro:getName(ID_On_After_Simu); 
			if (name == nil or name == string.char(0)) then 
				name = "Unknown" 	
			end 
	  Debug("red","Turned On light ID_On_After_Simu: " .. name);
	end
	if ID_On_When_Simu_Deactivated ~= 0 and simu == "0" then
		fibaro:call(ID_On_When_Simu_Deactivated, "turnOn");
    	name = fibaro:getName(ID_On_When_Simu_Deactivated); 
			if (name == nil or name == string.char(0)) then 
				name = "Unknown" 	
			end 
      Debug("red","Turned On light ID_On_When_Simu_Deactivated: " .. name);
	end
end 
-- Switch on devices in the list 
function SimulatorPresenceEngine:TurnOn(group) 
	Debug("red","Turning On always on 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, "turnOn"); 
		name = fibaro:getName(id2); 
		if (name == nil or name == string.char(0)) then 
			name = "Unknown" 	
		end 
		StandardDebug("Device: " .. name .. " On "); 
	end
	Debug("red","Now randomizing other lights...");
end 
	
Debug("green", "Presence Simulator | v" .. version ); 
Debug( "green", "--------------------------------");
if tonumber(stop_hour) <= 12 then wait_for_tomorrow = 0 end -- if stop hour is between 00 and 12h then will consider that stop hour is before midnight

------------------------ Main Loop ----------------------------------
if (simu == "0") then -- check before the while loop below... remove ?. 
	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
while true do -- boucle infinie
	SimulatorPresenceEngine:EndTimeCalc(); 
	-- local start_simu = "00:01"  -- uncomment this line when testing to force a start hour (only for the first loop)

	if (os.date("%H:%M") >= start_simu) then -- define if nighttime (sunset = 1)
		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();
			SimulatorPresenceEngine:EndSimulation();
		end 
		if manualOveride == "1" then 
			Debug("grey", "Manual Override Activated -> Simulation ON");
			SimulatorPresenceEngine:Launch();
			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: " .. start_simu .. " -> 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 

	if (simu == "0") then -- Condition to end simulation 
		SimulatorPresenceEngine:ExitSimulation();
		Debug("red","Simu = 0, Exit from scene");
		fibaro:abort(); 
	end
	
	if NotifLoop <= 30 then --a waiting xx times the fibaro sleep below before checking again 
    	if NotifLoop == 30 then NotifLoop = 0  end
		if NotifLoop == 0 then ExtraDebug("Looping to check for changes every 4 min")  end
		NotifLoop = NotifLoop + 1
    end
		
	fibaro:sleep(4*60*1000); -- wait 4 minutes before testing again the global vars below
	simu = fibaro:getGlobal("Simu_presence"); 
	manualOveride = fibaro:getGlobal("overideSimuSunset"); 

end
Posté(e)

Tiens je viens de penser que pour l'extinction en fin "normale" de simulation, ne pas éteindre toutes les lampes de la maison en même temps pourrait ajouter du réalisme.

Par exemple, un paramètre temps_extinction_after_simu, àdiviser par le nombre de lampes ?

Exemple: 15 minutes, àdiviser par 5 lampes = 3 minutes entre chaque extinction.

Des intéressés ? :-)

Sent from my SM-N910F using Tapatalk

Posté(e)

Salut,

 

Merci beaucoup du beau boulot vraiment ;-)

 

Moi je suis interessé, super idée ;-) je prends ;-)

 

Petite question, oui j'abuse ;-),

Je ne epuxpas utiliser les push malheureusement, ou dois-je mettre ma ligne

fibaro:setGlobal("FreeSms", "Depart YAPS") et fibaro:setGlobal("FreeSms", "Arrêt YAPS")

 

Je l'ai mis à  plusieurs endroits, mais pas concluant lol

Posté(e)

@sebcbien,

Merci pour la correction.

Je suis également intéressé par ta proposition d'amélioration. MERCI

Posté(e)

À l'aise :-)

@pepite sorry, je l'avais lu puis j'ai oublié. Je regarderai demain, promis :)

En gros, pour tester, tu peux les mettre dans les fonctions "launch" et "light off" ( pas sûr des noms exacts). Tu peux insérer tes lignes au début de ces fonctions.

C'est basique mais ça devrait marcher

Sent from my SM-N910F using Tapatalk

Posté(e)

le plus simple/propre ne serait-il pas que Pepite rajoute son instruction dans la fonction pushMessage après le for ... end
 

fibaro:setGlobal("FreeSms", sendPush)
Posté(e)

tu parles de là@jojo :

-- 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
Posté(e)

je teste de suite, je ne suis contre rien ;-)

Et je recevrai donc le sms de fin aussi je pense..

 

je vous tiens au jus ;-)

je le mets bien après le 1er END on est daccord ?

Posté(e)

Je n'ai qu'un mot à  dire messieurs, cela fonctionne du tonnerre, je viens de tester, super idée à jojo merci

et encore merci pour le boulot seb.

 

Petite question : le random of 2 recu correspond bien au rndmaxendtime ? je dirais oui, mais je verifie hihi

×
×
  • Créer...