Aller au contenu
Ez3kiel

Smart Lights - Gestion Auto De Votre Eclairage

Recommended Posts

Du coup le DIM n'est pas possible, le OFF garde les leds légèrement allumé, et en plein feu ça clignote un peut.

Il faut que je baisse a 10% pour arreter le clignotement...mais ça ne dim pas.

Partager ce message


Lien à poster
Partager sur d’autres sites

Pour info, ce type de led dispo en blanc chaud ou froid donne rendu vraiment correct.

Je dirais l'équivalent de 120w halogène. Je ne constate que 2% (à  l'oeil) de différence avec les halogènes.

Par contre j'ai du modifier les appliques.

 

160206105046188135.jpg

Partager ce message


Lien à poster
Partager sur d’autres sites

Bonjour,

 

Suite à  mon passage en 4.07 et mes problèmes d'instances en trop pour le controle de la lumiere, j'ai trouvé ce post et ce smartlight. je suis equipé de detecteur de présence fibaro et d'ampoule limitless/milight. Je n'ai donc pas d'interupteur manuel. 

 

Comme ce smartlight fait exactement ce que je recherche en une seule scene j'ai tenté de l'intégrer chez moi.

les commandes d'allumage et d'extinction de la lampe sont ajoutées dans les parties extraXXXXXXXFunc et extraOffChecks et de fait je n'utilise que la partie detection et conpteur.

 

Malheureusement si la lumiere s'allume bien l'extinction ne se produit jamais. Dans le log je tombe dans l'override (comme angelz) et à  la fin de l'override il ne fait plus rien. Mais comme je n'ai pas compris sa correction je suis bloqué. Quelqu'un aurait il une solution?

 

Merci

Partager ce message


Lien à poster
Partager sur d’autres sites

Mis en place ce soir pour commander 2 lampes via 2 détecteurs de mvt et 1 capteur de luminosité

Partager ce message


Lien à poster
Partager sur d’autres sites

Salut a vous utilisateurs de Smart lights !

 

Depuis la maj 4.071 j'ai que des merdes avec le script.

Soit les lumières ne s'éteignent plus après 5 minutes.

Soit elles ne s'allument pas bien.. un coup oui un coup non..

 

Je n'arrive pas à  comprendre, juste que c'est depuis la maj.

 

Fibaro... je pense que dès que je trouve une béta stable ou stable non béta, je ne TOUCHE PLUS A RIEN, je n'upgrade plus ! fini basta.

Partager ce message


Lien à poster
Partager sur d’autres sites

Salut

 

Je viens d'installer le script, sur ma HC2 qui est en V4.080.

Le problème est que lorsque je démarre la scene, elle fonctionne une seul est unique fois...

Une idée ?

Partager ce message


Lien à poster
Partager sur d’autres sites

bonjour,

 

voila j'utilise ce script depuis quelques semaines et c'est vrai qu'il est pas mal :-)

 

par contre je ne vois pas comment (et si c'est possible) que la détection se fasse sur X sec et pas a chaque détection

 

en fait pour ma monté d'escalier les lumière s'allume trop souvent j'aimerais dire au script si détection de mouvement et que 2 sec plus tard il y a toujours une détection la lampe s'allume 

 

voici mon script :

--[[
%% properties
65 value
79 value
%% globals
TimeOfDay
PresentState
--]]

--[[ Change - XXX value - above. One for motion sensor, one for Light sensor.
     TimeOfDay global variable and lux make sure that the scene
     will be triggered when you are already in the room and something changes.

     Set your settings below, lights and add extra functions
     Feel free to suggest changes or contact when having problems.
	 It is not allowed to sell or distribute this software and earlier versions without the approval of Control Living
     © 2014 - 2015 Smart lights by Control Living
     Version 1.6.7
--]]

--------------------------------------------------------------------
-----------------------YOUR LIGHT SETTINGS--------------------------

motionSensorID    = {65};  -- change id for your motion sensor.
LuxSensorID       = {79};  -- change id for your light sensor.
actionDevice      = 100;  -- change id for your light sensor.

--Enter the name of your Global variable. WITHOUT IT, THE SCENE DOES NOT WORK. Capital sensitive!
sleepState   = "SleepState"; --Sleep globalstate variable.
timeOfDay    = "TimeOfDay"; --Time of Day Global variable.
presentState = "PresentState"; --Present state Global variable.

--Enter the values of your global variables stated above. If not using, copy the name in front of the "YourValueName"
sleepStateMapping   = {Sleeping="Sleeping", Awake="Awake"};
timeOfDayMapping    = {Morning="Morning", Day="Day", Evening="Evening", Night="Night"};
presentStateMapping = {Home="Home", Away="Away", Holiday="Holiday"};

-- Set the lux value for which the lights have to be turned on.
-- If you don't want to use the MinLux then just set it to: 65535
minLuxMorning  = 2500;
minLuxDay      = 2500;
minLuxEvening  = 2500;
minLuxNight    = 2500;

--If the current lux value is above the maxlux, the lights will turn off after their set onTime.
maxLuxMorning = 6000;
maxLuxDay = 6000;

--[[
 Now the most important part:
   Here you can enter what light, for how long and at what value has to be turned on.
   Leave empty brackets for no lights.
   To set the light value without timer use 2 parameters: {id=LightID, setValue="VALUE"}
   To set the light value with timer    use 3 parameters: {id=LightID, setValue="VALUE", onTime=timeInSeconds }
   To set the light value for RGBW:  use the above, where setValue is: setValue={R="255",G="255",B="255",W="255"}
   --                                           RGBW set: {id=LightID, setValue={R="255",G="255",B="255",W="255"}, onTime=timeInSeconds }
   --]]
   

lightsSleeping = {}; --lights that are triggered when Sleepstate is sleeping. 
lightsMorning  = {{id=actionDevice, setValue="turnOn", onTime=20}};
lightsDay      = {{id=actionDevice, setValue="turnOn", onTime=20}};
lightsEvening  = {{id=actionDevice, setValue="turnOn", onTime=20}};
lightsNight    = {{id=actionDevice, setValue="turnOn", onTime=20}};


--Manual Override, the time that lights will not be turned ON again after manually turning them off.
OverrideFor  = 90;
dimmDownTime = 10;

--------------------------------------------------------------------
-------------------------EXTRA FEATURES-----------------------------

extraMorningFunc = function()
  -- Add your extra code here. If you want some checks or maybe run a virtual device button.
  --This code is always triggered if there is motion.
  -- movieLights =  {{180, "10"},{181, "10"} };
  --if (xbmc == "playing" ) then CallLightArray(movieLights);
  ExtraDebug("Extra morning function called");
end

extraDayFunc = function()
  -- Add your extra code here. If you want some checks or maybe run a virtual device button. 
  --This code is always triggered if there is motion.
  ExtraDebug("Extra day function called");
end

extraEveningFunc = function()
  -- Add your extra code here. If you want some checks or maybe run a virtual device button.
  --This code is always triggered if there is motion.
  ExtraDebug("Extra evening function called");
end

extraNightFunc = function()
  -- Add your extra code here. If you want some checks or maybe run a virtual device button.
  --This code is always triggered if there is motion.
  ExtraDebug("Extra night function called");
end

extraLightTriggerChecks = function()
--add extra checks here. and return the total true or false value.
--if returning false the lights will not be triggered.
-- for instance:  return ( (pcTurnedOff == true ) and (xbmc ~= "Empty") );
  -- return true to enable lights to turn on
  return true;
end

extraOffChecks = function()
  --return true to keep lights on.
  return false;
end

--------------------------------------------------------------------
----------------------ADVANCES SETTINGS-----------------------------
local showStandardDebugInfo = true; -- Debug shown in white
local showExtraDebugInfo    = false; -- Debug shown in orange

--------------------------------------------------------------------
--------------------------------------------------------------------
--               DO NOT CHANGE THE CODE BELOW                     --
--------------------------------------------------------------------
--UPDATE FROM HERE

--private variables
startSource        = fibaro:getSourceTrigger();
keepLightsOn       = false;
timerRunning       = false;
previousLights     = nil;
version = "1.6.7";

SavedState = {
  homeStatus = "",
  sleepState = 0,
  timeOfDay  = "",
  lux        = 0,
  motion     = 0,
  startTime  = 0
}

CurrentState = {
  homeStatus  = "",
  sleepState  = "",
  timeOfDay   = "",
  lux         = 0,
  motionState = 0,
  currentLightArray = {}
}

RGBWDeviceType = { "com.fibaro.RGBW", "com.fibaro.colorController", "com.fibaro.FGRGBW441M"  }
BinaryDeviceType = { "com.fibaro.binarySwitch","com.fibaro.FGWP101", "com.fibaro.developer.angelz.SwitchLumiereFhem", "turnOn"  }
DimmerDeviceType = { "com.fibaro.multilevelSwitch" }

OffModes = {None, ByScene, ManualOverride }

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

--Making sure that only one instance of the scene is running.
fibaro:sleep(50); --sleep to prevent all instances being killed.
if (fibaro:countScenes() > 1) then
  if (showExtraDebugInfo) then
    Debug( "grey", "Abort, Scene count = " .. fibaro:countScenes());
  end
  fibaro:abort();
end

--------------------------EXECUTION----------------------------------

function SetCurrentStatus()
  ExtraDebug("Updating current variable statuses");
  CurrentState.homeStatus  = GetPresentState();
  CurrentState.timeOfDay   = GetTimeOfDay();
  CurrentState.sleepState  = GetSleepState();
  CurrentState.lux         = GetAverageLuxValue();
  CurrentState.motionState = GetCurrentMotionStatus();
end

function SaveCurrentStatus()
  ExtraDebug("Saving current variable statuses");
  SavedState.homeStatus = CurrentState.homeStatus;
  SavedState.timeOfDay  = CurrentState.timeOfDay;
  SavedState.sleepState = CurrentState.sleepState;
  SavedState.lux        = CurrentState.lux;
  SavedState.motion     = CurrentState.motionState;
end

function CheckStatusChanges()
  ExtraDebug("Status change check");
  if (CurrentState.homeStatus ~= SavedState.homeStatus ) or (CurrentState.timeOfDay ~= SavedState.timeOfDay ) then
    if (CurrentState.homeStatus ~= presentStateMapping.Home ) then
      TurnOffLights();
    else
      SceneTriggered();
	  --reset time.
    end;
  end
  SaveCurrentStatus();
    -- if we still have a motion then reset timer.
  if ( (CurrentState.motionState ~= 0 or extraOffChecks() ) and not IsMaxLuxExceeded() )  then 
	ExtraDebug( "Resetting time" );
    SavedState.startTime = os.time(); 
  else
    ExtraDebug("Timer is not reset.");
  end
  --any other case, we are not resetting the timer.
end

function IsMaxLuxExceeded()
  if (CurrentState.timeOfDay == timeOfDayMapping.Morning and CurrentState.lux > maxLuxMorning ) then
    ExtraDebug( "Current lux value: " .. CurrentState.lux .. " higher then max Lux Morning: " .. maxLuxMorning .. ".");
	return true;
  elseif (CurrentState.timeOfDay == timeOfDayMapping.Day and CurrentState.lux > maxLuxDay ) then
    ExtraDebug( "Current lux value: " .. CurrentState.lux .. " higher then max Lux Day: " .. maxLuxDay .. ".");
	return true;
   end
   return false;
end

function LightsOff( lightArray )
  local stillLightsOn = 0;
  local currentTime = os.time();
  for i = 1,#lightArray do
    if ( lightArray[i].onTime ~= nil) then
	  lightItem = lightArray[i];
	  lightItem.OffMode = CheckManualOverrideItem( lightItem );
	  --local 
	  if ( lightItem.OffMode == "ManualOverride" ) then
	      ExtraDebug("Manual override for light: [" .. lightItem.id .. "]" .. lightItem.name .. " active, not turning on");
		  goto continue;
	  end
	  --On till:
      local timeL = SavedState.startTime + (lightItem.onTime);
	  local timeLeft =  timeL -  currentTime;
 	  if (timeLeft >= 0 ) then ExtraDebug("Time left for: [" .. lightItem.id .. "]" .. lightItem.name .. ": " .. timeLeft .. " seconds" );   end
	  if ( timeLeft < dimmDownTime ) then
        if ( lightItem.OffMode ~= "ManualOverride" ) then
			lightItem.OffMode = "ByScene";
		end
	    if ( (timeLeft <= 0) and (lightItem.currentValue ~= 0) ) then
			fibaro:call(lightItem.id, "turnOff");
			StandardDebug("Switch off light: [" .. lightItem.id .. "]'" .. lightItem.name .."'");
        else
            if ( item.lightType == "com.fibaro.multilevelSwitch" ) then
			  currentValueDiv = roundit(((lightItem.currentValue) / (dimmDownTime)), 0);
			  currentValueNew = (lightItem.currentValue - currentValueDiv);
			  if (currentValueNew <=0 ) then currentValueNew = 0; end
			  fibaro:call(lightItem.id, "setValue", tostring(currentValueNew));
            end
			stillLightsOn = stillLightsOn + 1;
		end
      elseif (CheckLightToBeTurnedOnAgain(lightItem)) then
        ExtraDebug("Turn light: " .. lightItem.id .. " back on");
        turnLightOn(lightItem);
		lightItem.OffMode = "";
        stillLightsOn = stillLightsOn + 1;  
      else
	    lightItem.OffMode = "NoOverride";
	    stillLightsOn = stillLightsOn + 1;   
      end
    end
	::continue::
  end
  return stillLightsOn;
end

function KeepLightsOnTimer()
  ExtraDebug("--------------- Timer running ---------------");
  ExtraDebug("starting with while loop, to keep lights on");
  SavedState.startTime = os.time();
  SaveCurrentStatus();
  sleepTime = 1000;
  while ( keepLightsOn ) do
    ExtraDebug("--------------- next timer run ---------------");
    fibaro:sleep(sleepTime);
	start_time = os.clock();
    SetCurrentStatus();
    currentLightArray = GetTimeOfDayLightArray();
    UpdateLightValues(currentLightArray);
    CheckStatusChanges();
    local stillLightsOn = 0;  
    stillLightsOn = LightsOff( currentLightArray );
    if (stillLightsOn == 0 ) then
	  keepLightsOn = RunManualOverrideMode( currentLightArray );
      end_time = 0; --if lights turned back on after override.
    else
      end_time = os.clock() 
    end
	elapsed_time = (end_time - start_time) * 1000;
	sleepTime = 1000 - elapsed_time;
  end
end

function CheckManualOverrideItem( currentItem ) 
  --ExtraDebug("Manual override check for: [" .. currentItem.id .. "]" .. currentItem.name);
  --fibaro:debug(currentItem.currentValue)
  --fibaro:debug(currentItem.OffMode)
  if (currentItem.currentValue == 0 and currentItem.OffMode ~= "ByScene" ) then
    ExtraDebug( "Manual override for light: [" .. currentItem.id .. "]" .. currentItem.name .. " active" );
	return "ManualOverride";
  elseif (currentItem.currentValue ~= 0 and currentItem.OffMode == "ManualOverride" ) then
	ExtraDebug( "Manual override for light: [" .. currentItem.id .. "]" .. currentItem.name .. " cancelled" );
	return "NoOverride";
  end
 
  return currentItem.OffMode; --returning current mode;
end

function RunManualOverrideMode( currentLightArray )
  OverrideForAll = CheckIfAllInOverrideMode(currentLightArray);
  if ( OverrideForAll ) then
    ExtraDebug("-----------------Override Mode---------------");
    OverrideTimer = os.time();
    while ( OverrideForAll and (OverrideTimer + OverrideFor ) - os.time() > 0 ) do
      ExtraDebug("Still in override for: " .. (OverrideTimer + OverrideFor ) - os.time() .. " seconds" );
      fibaro:sleep(1000);
	  UpdateLightValues(currentLightArray);
	  OverrideForAll = CheckIfAllInOverrideMode(currentLightArray);
	  motion = GetCurrentMotionStatus();
	  if ( motion ~= 0 ) then
	    OverrideTimer = os.time();
	  end
    end
	else return false;
  end
 -- if  ( (OverrideTimer + OverrideFor ) - os.time() <= 0 ) then
  --time has end. So not continue to run lights loop 
 --   return false;
 -- end
  if (OverrideForAll) then
    return false; --run lights 
  else return true;
  end
end

function CheckIfAllInOverrideMode(currentLightArray)
  OverrideForAll = 0;
  for i = 1,#currentLightArray do
    if ( CheckManualOverrideItem(currentLightArray[i]) == "ManualOverride" ) then
      OverrideForAll = OverrideForAll +1;
    else
      return false;
    end
  end
  if (  OverrideForAll ~= 0 and OverrideForAll == #currentLightArray ) then
    return true;
  end
  return false;
end

function RunTimer()
if ( keepLightsOn and not timerRunning ) then
    ExtraDebug("Starting timer, not yet running");
    timerRunning = true;
    KeepLightsOnTimer();
  else
	ExtraDebug("Timer already running, returning");
  end
end

function TurnOffLights()
     local timeOfDayArray = GetTimeOfDayLightArray();
     SetLightValues(timeOfDayArray);
	 UpdateLightValues(timeOfDayArray);
     CallLightArray(timeOfDayArray, "off" );
	return;
end

----------------------Turn lights on functions------------------------

function CallLightArray( lightArray, turnOnOrOff )
  if (#lightArray == 0 ) then
    if (CurrentState.sleepState == sleepStateMapping.Sleeping) then
	  noLightsSetText = "lightsSleeping";
	else
      noLightsSetText = CurrentState.timeOfDay;
	end
    StandardDebug( "No lights set for " .. noLightsSetText );
    return;
  end
  if not ( CheckIfTable(lightArray, "lights" .. CurrentState.timeOfDay ) ) then return end
  for i = 1,#lightArray do
    if not ( CheckIfTable(lightArray[i], "lights" .. CurrentState.timeOfDay ) ) then break end
    lightItem = lightArray[i];
    if (turnOnOrOff == "on" ) then
      turnLightOn(lightItem);
      if (lightItem.onTime ~= nil) then keepLightsOn = true; end
    else
	  fibaro:call(lightItem.id, "turnOff"); --Always turn off, if HC2 light status is not actual.  
      if ( lightItem.currentValue ~= 0) then
        keepLightsOn = false;
        StandardDebug("Switch off light: [" .. lightItem.id .. "]'" .. lightItem.name .."'");
      end
    end
  end
  StandardDebug( "Lights turned: " .. turnOnOrOff .. " for " .. CurrentState.timeOfDay );
end

function turnLightOn( lightItem )
  if (lightItem.lightType == "com.fibaro.multilevelSwitch" ) then
    fibaro:call(lightItem.id, "setValue", tostring(lightItem.setValue));
    StandardDebug( "Set: [" .. lightItem.id .. "]'" .. lightItem.name .. "' to Value: " .. lightItem.setValue );
  elseif ( lightItem.lightType == "com.fibaro.RGBW" )then
    StandardDebug( "Turn: [" .. lightItem.id .. "]'" .. lightItem.name .. "' On"); 
    if ( lightItem.setValue.brg~= nil ) then
      fibaro:call(lightItem.id, "setBrightness", tostring(lightItem.setValue.brg));
    else
	  local clrvalues =lightItem.setValue;
      fibaro:call(lightItem.id, "setColor", clrvalues.R,  clrvalues.G,  clrvalues.B,  clrvalues.W)	
    end
  elseif (lightItem.lightType == "com.fibaro.binarySwitch") then
    fibaro:call(lightItem.id, "turnOn");
	StandardDebug( "Turn: [" .. lightItem.id .. "]'" .. lightItem.name .. "' On"); 
  end
end

function CheckPreviousLights( NewLightArray )
  if ( previousLights ~= nil ) then
    for i = 1,#previousLights do
      local lightItem = previousLights[i];
      for i = 1,#NewLightArray do
        local lightItem1 = NewLightArray[i];
        inarray = false;
        if ( lightItem.id == lightItem1.id ) then
          inarray= true;
          break 
        end
      end
      if not ( inarray ) then fibaro:call(lightItem.id, "turnOff"); end
    end
  end
  previousLights = NewLightArray;	
end

function LightperDayPart( minLux, lightArray )
  local newLuxValue = CurrentState.lux;
  if ( extraLightTriggerChecks() ) then
    if ( newLuxValue  >  minLux )  then
      StandardDebug( "Sensor lux: " .. newLuxValue .. " higher then minValue: " .. minLux .. " : no action");
    else
      StandardDebug("Sensor lux: " .. newLuxValue .. " is lower then minValue: " .. minLux);
	  SetLightValues(lightArray);
	  CallLightArray( lightArray, "on" );
	  CheckPreviousLights( lightArray );
    end
  else
    ExtraDebug("ExtraLightTriggerChecks failed, so no lights");
  end
end

function SceneTriggered()
  if ( CurrentState.homeStatus ~= presentStateMapping.Home ) then
	ExtraDebug("Presentstate = not at home, so no lights");
  elseif  ( CurrentState.sleepState == sleepStateMapping.Sleeping ) then
    LightperDayPart( 65535, lightsSleeping );	
  elseif ( CurrentState.timeOfDay == timeOfDayMapping.Morning ) then
    extraMorningFunc();
    LightperDayPart( minLuxMorning, lightsMorning );
  elseif ( CurrentState.timeOfDay == timeOfDayMapping.Day ) then
    extraDayFunc();
	LightperDayPart( minLuxDay, lightsDay );
  elseif ( CurrentState.timeOfDay == timeOfDayMapping.Evening ) then
    extraEveningFunc();
    LightperDayPart( minLuxEvening, lightsEvening );
  elseif ( CurrentState.timeOfDay == timeOfDayMapping.Night  ) then
    extraNightFunc();
    LightperDayPart( minLuxNight, lightsNight );
  else
	ErrorDebug( "No lights: " .. CurrentState.timeOfDay );
  end
  if (keepLightsOn == true) then
    RunTimer(); 
  end
end

function SceneTriggeredByLights( st )
  if (st == "off") then
    ExtraDebug( "light turned off, sleep 4 sec" );
    fibaro:sleep(4000);
  elseif (st == "on") then
    ExtraDebug( "light turned on, activating timer" );
	fibaro:sleep(4000);
	--TODO: add light to current array. 
	--keepLightsOn = true;
	--RunTimer();
	end
end

------------------------STATUS functions------------------------------

function GetTimeOfDay()
  return LookupGlobal( timeOfDay, "TimeOfDay", "Day");
end

function GetSleepState()
  return LookupGlobal( sleepState, "sleepState", "Awake");
end

function GetPresentState()
  return LookupGlobal( presentState, "presentState", "Home");
end

function LookupGlobal( name, stateName, default )
  local ps = fibaro:getGlobalValue( name );
  if ( (ps ~= "") and (ps ~= nil ) ) then
    ExtraDebug("returned " .. stateName .. ": " .. ps );
    return ps;
  else 
    ErrorDebug( stateName .. " variable not found");
    return default;
  end
end

function lightsStatus( id )
  --check if lights are already on.
  allLights = {lightsSleeping, lightsMorning, lightsDay, lightsEvening, lightsNight };
  for i = 1,#allLights do
    for iL = 1, #allLights[i] do 
	  local lightItem =  allLights[i][iL];
      if ( lightItem.id == tonumber(id) ) then
        if   ( lightItem.currentValue == 0 ) then
          return "off";
        else
          return "on";
        end 
      end
    end
  end
  ErrorDebug("Light status unknown");
  return "Unknown";  
end

function GetTimeOfDayLightArray()
 if ( CurrentState.sleepState == sleepStateMapping.Sleeping ) then
    return lightsSleeping;
  elseif ( CurrentState.timeOfDay == timeOfDayMapping.Morning ) then
    return lightsMorning;
  elseif ( CurrentState.timeOfDay == timeOfDayMapping.Day ) then
	return lightsDay;
  elseif ( CurrentState.timeOfDay == timeOfDayMapping.Evening ) then
	return lightsEvening;
  elseif ( CurrentState.timeOfDay == timeOfDayMapping.Night ) then
	return lightsNight;
  end
end

function GetCurrentMotionStatus()
  local sensorbreached = 0;
  if (CheckIfTable(motionSensorID, "motionSensorID") ) then
    for i = 1,#motionSensorID do
      if ( tonumber(fibaro:getValue(motionSensorID[i], "value")) > 0 ) then
        sensorbreached = 1;
      end
    end
   else 
    --if not a table, just return the value of the containing ID
	sensorbreached = tonumber(fibaro:getValue(motionSensorID, "value"))
  end
  return sensorbreached;
end

function GetAverageLuxValue()
  local luxAverage = 0;
  if (CheckIfTable(LuxSensorID, "LuxSensorID") ) then
    if (#LuxSensorID == 1) then 
      return tonumber(fibaro:getValue(LuxSensorID[1], "value"));
    end
    for i = 1,#LuxSensorID do 
      luxAverage = luxAverage + tonumber(fibaro:getValue(LuxSensorID[i], "value"));
    end
      luxAverage = roundit( (luxAverage / #LuxSensorID), 0 );
  else 
    --if not a table, just return the value of the containing ID
    luxAverage = tonumber(fibaro:getValue(LuxSensorID, "value"));
  end
  return luxAverage;
end

function UpdateLightValues( currentArray )
  for i = 1,#currentArray do
    item = currentArray[i];
	newValue = fibaro:getValue(item.id, "value");
	item.currentValue = tonumber(newValue) ~= nil and tonumber(newValue) or ( newValue == "true" and 1 or 0 );
    if ( item.lightType == "com.fibaro.RGBW" )then
         color = fibaro:getValue(item.id, "color");
         c = {R="0", G="0", B="0", W="0", brg="0" }
         c.R, c.G, c.B, c.W = color:match("([^,]+),([^,]+),([^,]+),([^,]+)");
         c.brg = fibaro:getValue(item.id, "brightness"); 
         item.currentValue = c;
       end
  end
end

function SetLightValues(currentArray)
  for i = 1,#currentArray do
    item = currentArray[i];
    item.name = tostring(fibaro:getName(item.id));
	item.OffMode = "NoOverride";
	if (item.name == nil) then item.name = "Unknown"; end
    item.lightType = GetDeviceType( item.id );
  end
end

function GetDeviceType(itemId)
  deviceType = fibaro:getType(item.id);
  if ( idIsInList(deviceType, RGBWDeviceType) ) then
      deviceType ="com.fibaro.RGBW";
  elseif ( idIsInList(deviceType, BinaryDeviceType) ) then
       deviceType = "com.fibaro.binarySwitch";
  elseif ( idIsInList(deviceType, DimmerDeviceType ) ) then
       deviceType = "com.fibaro.multilevelSwitch";
 --elseif( ) then
 --deviceType = "Philips.Hue";
  end
    return deviceType;
end

--------------------Helper functions--------------------------------

function CheckLightToBeTurnedOnAgain(lightItem)
 if not (lightItem.OffMode=="ByScene") then return; end
 	   
 if (lightItem.lightType == "com.fibaro.multilevelSwitch" ) then
   if (lightItem.currentValue) < tonumber(lightItem.setValue) then return true; end
 elseif( lightItem.lightType == "com.fibaro.RGBW" )then
   c = lightItem.currentValue;
   if (c.R == "0" and c.G == "0" and c.B == "0" and c.W == "0" ) then
     return true;
   end    
 elseif (lightItem.lightType == "com.fibaro.binarySwitch") then
   if (lightItem.setValue == "turnOn" and lightItem.currentValue == 0 ) then return true; end
  end
  return false;
end

function CheckIfTable( array, arrayname )
  local tableCheck = tostring( type( array ) ); 
  if ( tableCheck ~= "table" ) then
    ErrorDebug("Missing brackets for variable: '" .. arrayname .. "', please place extra brackts: { } around: " .. array .. "."); 
	return false;
  end
  return true;
end

function roundit(num, idp)
  local mult = 10^(idp or 0)
  return math.floor(num * mult + 0.5) / mult
end

function idIsInList( startSourceId, sensorList )
  for i = 1,#sensorList do
    if ( startSourceId == sensorList[i] ) then
      return true;
    end
  end
  return false;
end

--------------------Debugging Functions-----------------------------

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

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

function ErrorDebug( debugMessage )
    Debug( "red", "Error: " .. debugMessage);
    Debug( "red", "");
end

function TestDebug(debugMessage )
   Debug( "blue", "Testing: " .. debugMessage );
end

----------------------START OF THE SCENE----------------------------
SetCurrentStatus();
StandardDebug("Home status: " .. CurrentState.homeStatus );
StandardDebug("Motion status: " .. ( CurrentState.motionState == 0 and "No movement" or "movement")); 
  
if (startSource["type"] == "property") then
  startSourceID = tonumber(startSource['deviceID']);
  triggerDebug  = "Triggered by: " .. startSourceID;
  if ( idIsInList( startSourceID, motionSensorID ) ) then
    StandardDebug( triggerDebug .. " Motion sensor" );
    if ( CurrentState.motionState > 0 ) then
      SceneTriggered();
    end
  elseif ( idIsInList( startSourceID, LuxSensorID ) ) then
    StandardDebug( triggerDebug .. " Lux sensor" );
    ExtraDebug( "Lux value changed to: " .. CurrentState.lux );
    if ( CurrentState.motionState > 0 ) then
      SceneTriggered();
    end
  else
    StandardDebug( triggerDebug .. " Light switch" );
    st = lightsStatus( startSourceID );
    if (st == "Unknown") then
      ErrorDebug( "Unknown light trigger" );
	else
	  SceneTriggeredByLights( st );
	end
    -- Maybe we can change the light preset to make it more intelligent.
    -- Maybe we can change the Lux preset to make it more intelligent.
  end
elseif ( startSource["type"] == "global" ) then
  StandardDebug( "Triggered by: " .. "global variable" );
  -- If home status away, we trigger so the current array will be turned off. If motion, turn them on.
  if (CurrentState.homeStatus ~= presentStateMapping.Home) then
    TurnOffLights();
  elseif (CurrentState.motionState > 0 ) then
    SceneTriggered();
  end
else 
  StandardDebug( "Triggered by: " .. startSource["type"] .. " , Not checking the motion state." );
  --Just run the Light Code, not checking for motion. Probably triggered manually.
  if ( startSource["type"] == "other" ) then
    SceneTriggered();
  end
end

Debug( "green", "© Smart Lights V" .. version .. " | by Control Living, Finished" );
Debug( "green", "-------------------------------------------------------" );
--fibaro:abort(); --otherwise scene will stay alive to long.

--UPDATE TILL HERE

si quelqu'un a une idée ?  

 

merci de votre aide

Partager ce message


Lien à poster
Partager sur d’autres sites

Bonjour,

 

j'utilise ce script depuis quelque semaine et ca marche bien sauf que j'ai des fausse detection

 

j'aimerais dire au script :

 

si une detection de mouvement, attendre 1 seconde ou plus et reverifier si il y a toujours une detection et seulement la allumer la lampe

 

mais je ne vois pas comment faire.

 

voici mon code :

--[[
%% properties
65 value
79 value
%% globals
TimeOfDay
PresentState
--]]

--[[ Change - XXX value - above. One for motion sensor, one for Light sensor.
     TimeOfDay global variable and lux make sure that the scene
     will be triggered when you are already in the room and something changes.

     Set your settings below, lights and add extra functions
     Feel free to suggest changes or contact when having problems.
	 It is not allowed to sell or distribute this software and earlier versions without the approval of Control Living
     © 2014 - 2015 Smart lights by Control Living
     Version 1.6.7
--]]

--------------------------------------------------------------------
-----------------------YOUR LIGHT SETTINGS--------------------------

motionSensorID    = {65};  -- change id for your motion sensor.
LuxSensorID       = {79};  -- change id for your light sensor.
actionDevice      = 100;  -- change id for your light sensor.

--Enter the name of your Global variable. WITHOUT IT, THE SCENE DOES NOT WORK. Capital sensitive!
sleepState   = "SleepState"; --Sleep globalstate variable.
timeOfDay    = "TimeOfDay"; --Time of Day Global variable.
presentState = "PresentState"; --Present state Global variable.

--Enter the values of your global variables stated above. If not using, copy the name in front of the "YourValueName"
sleepStateMapping   = {Sleeping="Sleeping", Awake="Awake"};
timeOfDayMapping    = {Morning="Morning", Day="Day", Evening="Evening", Night="Night"};
presentStateMapping = {Home="Home", Away="Away", Holiday="Holiday"};

-- Set the lux value for which the lights have to be turned on.
-- If you don't want to use the MinLux then just set it to: 65535
minLuxMorning  = 2500;
minLuxDay      = 2500;
minLuxEvening  = 2500;
minLuxNight    = 2500;

--If the current lux value is above the maxlux, the lights will turn off after their set onTime.
maxLuxMorning = 6000;
maxLuxDay = 6000;

--[[
 Now the most important part:
   Here you can enter what light, for how long and at what value has to be turned on.
   Leave empty brackets for no lights.
   To set the light value without timer use 2 parameters: {id=LightID, setValue="VALUE"}
   To set the light value with timer    use 3 parameters: {id=LightID, setValue="VALUE", onTime=timeInSeconds }
   To set the light value for RGBW:  use the above, where setValue is: setValue={R="255",G="255",B="255",W="255"}
   --                                           RGBW set: {id=LightID, setValue={R="255",G="255",B="255",W="255"}, onTime=timeInSeconds }
   --]]
   

lightsSleeping = {}; --lights that are triggered when Sleepstate is sleeping. 
lightsMorning  = {{id=actionDevice, setValue="turnOn", onTime=20}};
lightsDay      = {{id=actionDevice, setValue="turnOn", onTime=20}};
lightsEvening  = {{id=actionDevice, setValue="turnOn", onTime=20}};
lightsNight    = {{id=actionDevice, setValue="turnOn", onTime=20}};


--Manual Override, the time that lights will not be turned ON again after manually turning them off.
OverrideFor  = 90;
dimmDownTime = 10;

--------------------------------------------------------------------
-------------------------EXTRA FEATURES-----------------------------

extraMorningFunc = function()
  -- Add your extra code here. If you want some checks or maybe run a virtual device button.
  --This code is always triggered if there is motion.
  -- movieLights =  {{180, "10"},{181, "10"} };
  --if (xbmc == "playing" ) then CallLightArray(movieLights);
  ExtraDebug("Extra morning function called");
end

extraDayFunc = function()
  -- Add your extra code here. If you want some checks or maybe run a virtual device button. 
  --This code is always triggered if there is motion.
  ExtraDebug("Extra day function called");
end

extraEveningFunc = function()
  -- Add your extra code here. If you want some checks or maybe run a virtual device button.
  --This code is always triggered if there is motion.
  ExtraDebug("Extra evening function called");
end

extraNightFunc = function()
  -- Add your extra code here. If you want some checks or maybe run a virtual device button.
  --This code is always triggered if there is motion.
  ExtraDebug("Extra night function called");
end

extraLightTriggerChecks = function()
--add extra checks here. and return the total true or false value.
--if returning false the lights will not be triggered.
-- for instance:  return ( (pcTurnedOff == true ) and (xbmc ~= "Empty") );
  -- return true to enable lights to turn on
  return true;
end

extraOffChecks = function()
  --return true to keep lights on.
  return false;
end

--------------------------------------------------------------------
----------------------ADVANCES SETTINGS-----------------------------
local showStandardDebugInfo = true; -- Debug shown in white
local showExtraDebugInfo    = false; -- Debug shown in orange

--------------------------------------------------------------------
--------------------------------------------------------------------
--               DO NOT CHANGE THE CODE BELOW                     --
--------------------------------------------------------------------
--UPDATE FROM HERE

--private variables
startSource        = fibaro:getSourceTrigger();
keepLightsOn       = false;
timerRunning       = false;
previousLights     = nil;
version = "1.6.7";

SavedState = {
  homeStatus = "",
  sleepState = 0,
  timeOfDay  = "",
  lux        = 0,
  motion     = 0,
  startTime  = 0
}

CurrentState = {
  homeStatus  = "",
  sleepState  = "",
  timeOfDay   = "",
  lux         = 0,
  motionState = 0,
  currentLightArray = {}
}

RGBWDeviceType = { "com.fibaro.RGBW", "com.fibaro.colorController", "com.fibaro.FGRGBW441M"  }
BinaryDeviceType = { "com.fibaro.binarySwitch","com.fibaro.FGWP101", "com.fibaro.developer.angelz.SwitchLumiereFhem", "turnOn"  }
DimmerDeviceType = { "com.fibaro.multilevelSwitch" }

OffModes = {None, ByScene, ManualOverride }

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

--Making sure that only one instance of the scene is running.
fibaro:sleep(50); --sleep to prevent all instances being killed.
if (fibaro:countScenes() > 1) then
  if (showExtraDebugInfo) then
    Debug( "grey", "Abort, Scene count = " .. fibaro:countScenes());
  end
  fibaro:abort();
end

--------------------------EXECUTION----------------------------------

function SetCurrentStatus()
  ExtraDebug("Updating current variable statuses");
  CurrentState.homeStatus  = GetPresentState();
  CurrentState.timeOfDay   = GetTimeOfDay();
  CurrentState.sleepState  = GetSleepState();
  CurrentState.lux         = GetAverageLuxValue();
  CurrentState.motionState = GetCurrentMotionStatus();
end

function SaveCurrentStatus()
  ExtraDebug("Saving current variable statuses");
  SavedState.homeStatus = CurrentState.homeStatus;
  SavedState.timeOfDay  = CurrentState.timeOfDay;
  SavedState.sleepState = CurrentState.sleepState;
  SavedState.lux        = CurrentState.lux;
  SavedState.motion     = CurrentState.motionState;
end

function CheckStatusChanges()
  ExtraDebug("Status change check");
  if (CurrentState.homeStatus ~= SavedState.homeStatus ) or (CurrentState.timeOfDay ~= SavedState.timeOfDay ) then
    if (CurrentState.homeStatus ~= presentStateMapping.Home ) then
      TurnOffLights();
    else
      SceneTriggered();
	  --reset time.
    end;
  end
  SaveCurrentStatus();
    -- if we still have a motion then reset timer.
  if ( (CurrentState.motionState ~= 0 or extraOffChecks() ) and not IsMaxLuxExceeded() )  then 
	ExtraDebug( "Resetting time" );
    SavedState.startTime = os.time(); 
  else
    ExtraDebug("Timer is not reset.");
  end
  --any other case, we are not resetting the timer.
end

function IsMaxLuxExceeded()
  if (CurrentState.timeOfDay == timeOfDayMapping.Morning and CurrentState.lux > maxLuxMorning ) then
    ExtraDebug( "Current lux value: " .. CurrentState.lux .. " higher then max Lux Morning: " .. maxLuxMorning .. ".");
	return true;
  elseif (CurrentState.timeOfDay == timeOfDayMapping.Day and CurrentState.lux > maxLuxDay ) then
    ExtraDebug( "Current lux value: " .. CurrentState.lux .. " higher then max Lux Day: " .. maxLuxDay .. ".");
	return true;
   end
   return false;
end

function LightsOff( lightArray )
  local stillLightsOn = 0;
  local currentTime = os.time();
  for i = 1,#lightArray do
    if ( lightArray[i].onTime ~= nil) then
	  lightItem = lightArray[i];
	  lightItem.OffMode = CheckManualOverrideItem( lightItem );
	  --local 
	  if ( lightItem.OffMode == "ManualOverride" ) then
	      ExtraDebug("Manual override for light: [" .. lightItem.id .. "]" .. lightItem.name .. " active, not turning on");
		  goto continue;
	  end
	  --On till:
      local timeL = SavedState.startTime + (lightItem.onTime);
	  local timeLeft =  timeL -  currentTime;
 	  if (timeLeft >= 0 ) then ExtraDebug("Time left for: [" .. lightItem.id .. "]" .. lightItem.name .. ": " .. timeLeft .. " seconds" );   end
	  if ( timeLeft < dimmDownTime ) then
        if ( lightItem.OffMode ~= "ManualOverride" ) then
			lightItem.OffMode = "ByScene";
		end
	    if ( (timeLeft <= 0) and (lightItem.currentValue ~= 0) ) then
			fibaro:call(lightItem.id, "turnOff");
			StandardDebug("Switch off light: [" .. lightItem.id .. "]'" .. lightItem.name .."'");
        else
            if ( item.lightType == "com.fibaro.multilevelSwitch" ) then
			  currentValueDiv = roundit(((lightItem.currentValue) / (dimmDownTime)), 0);
			  currentValueNew = (lightItem.currentValue - currentValueDiv);
			  if (currentValueNew <=0 ) then currentValueNew = 0; end
			  fibaro:call(lightItem.id, "setValue", tostring(currentValueNew));
            end
			stillLightsOn = stillLightsOn + 1;
		end
      elseif (CheckLightToBeTurnedOnAgain(lightItem)) then
        ExtraDebug("Turn light: " .. lightItem.id .. " back on");
        turnLightOn(lightItem);
		lightItem.OffMode = "";
        stillLightsOn = stillLightsOn + 1;  
      else
	    lightItem.OffMode = "NoOverride";
	    stillLightsOn = stillLightsOn + 1;   
      end
    end
	::continue::
  end
  return stillLightsOn;
end

function KeepLightsOnTimer()
  ExtraDebug("--------------- Timer running ---------------");
  ExtraDebug("starting with while loop, to keep lights on");
  SavedState.startTime = os.time();
  SaveCurrentStatus();
  sleepTime = 1000;
  while ( keepLightsOn ) do
    ExtraDebug("--------------- next timer run ---------------");
    fibaro:sleep(sleepTime);
	start_time = os.clock();
    SetCurrentStatus();
    currentLightArray = GetTimeOfDayLightArray();
    UpdateLightValues(currentLightArray);
    CheckStatusChanges();
    local stillLightsOn = 0;  
    stillLightsOn = LightsOff( currentLightArray );
    if (stillLightsOn == 0 ) then
	  keepLightsOn = RunManualOverrideMode( currentLightArray );
      end_time = 0; --if lights turned back on after override.
    else
      end_time = os.clock() 
    end
	elapsed_time = (end_time - start_time) * 1000;
	sleepTime = 1000 - elapsed_time;
  end
end

function CheckManualOverrideItem( currentItem ) 
  --ExtraDebug("Manual override check for: [" .. currentItem.id .. "]" .. currentItem.name);
  --fibaro:debug(currentItem.currentValue)
  --fibaro:debug(currentItem.OffMode)
  if (currentItem.currentValue == 0 and currentItem.OffMode ~= "ByScene" ) then
    ExtraDebug( "Manual override for light: [" .. currentItem.id .. "]" .. currentItem.name .. " active" );
	return "ManualOverride";
  elseif (currentItem.currentValue ~= 0 and currentItem.OffMode == "ManualOverride" ) then
	ExtraDebug( "Manual override for light: [" .. currentItem.id .. "]" .. currentItem.name .. " cancelled" );
	return "NoOverride";
  end
 
  return currentItem.OffMode; --returning current mode;
end

function RunManualOverrideMode( currentLightArray )
  OverrideForAll = CheckIfAllInOverrideMode(currentLightArray);
  if ( OverrideForAll ) then
    ExtraDebug("-----------------Override Mode---------------");
    OverrideTimer = os.time();
    while ( OverrideForAll and (OverrideTimer + OverrideFor ) - os.time() > 0 ) do
      ExtraDebug("Still in override for: " .. (OverrideTimer + OverrideFor ) - os.time() .. " seconds" );
      fibaro:sleep(1000);
	  UpdateLightValues(currentLightArray);
	  OverrideForAll = CheckIfAllInOverrideMode(currentLightArray);
	  motion = GetCurrentMotionStatus();
	  if ( motion ~= 0 ) then
	    OverrideTimer = os.time();
	  end
    end
	else return false;
  end
 -- if  ( (OverrideTimer + OverrideFor ) - os.time() <= 0 ) then
  --time has end. So not continue to run lights loop 
 --   return false;
 -- end
  if (OverrideForAll) then
    return false; --run lights 
  else return true;
  end
end

function CheckIfAllInOverrideMode(currentLightArray)
  OverrideForAll = 0;
  for i = 1,#currentLightArray do
    if ( CheckManualOverrideItem(currentLightArray[i]) == "ManualOverride" ) then
      OverrideForAll = OverrideForAll +1;
    else
      return false;
    end
  end
  if (  OverrideForAll ~= 0 and OverrideForAll == #currentLightArray ) then
    return true;
  end
  return false;
end

function RunTimer()
if ( keepLightsOn and not timerRunning ) then
    ExtraDebug("Starting timer, not yet running");
    timerRunning = true;
    KeepLightsOnTimer();
  else
	ExtraDebug("Timer already running, returning");
  end
end

function TurnOffLights()
     local timeOfDayArray = GetTimeOfDayLightArray();
     SetLightValues(timeOfDayArray);
	 UpdateLightValues(timeOfDayArray);
     CallLightArray(timeOfDayArray, "off" );
	return;
end

----------------------Turn lights on functions------------------------

function CallLightArray( lightArray, turnOnOrOff )
  if (#lightArray == 0 ) then
    if (CurrentState.sleepState == sleepStateMapping.Sleeping) then
	  noLightsSetText = "lightsSleeping";
	else
      noLightsSetText = CurrentState.timeOfDay;
	end
    StandardDebug( "No lights set for " .. noLightsSetText );
    return;
  end
  if not ( CheckIfTable(lightArray, "lights" .. CurrentState.timeOfDay ) ) then return end
  for i = 1,#lightArray do
    if not ( CheckIfTable(lightArray[i], "lights" .. CurrentState.timeOfDay ) ) then break end
    lightItem = lightArray[i];
    if (turnOnOrOff == "on" ) then
      turnLightOn(lightItem);
      if (lightItem.onTime ~= nil) then keepLightsOn = true; end
    else
	  fibaro:call(lightItem.id, "turnOff"); --Always turn off, if HC2 light status is not actual.  
      if ( lightItem.currentValue ~= 0) then
        keepLightsOn = false;
        StandardDebug("Switch off light: [" .. lightItem.id .. "]'" .. lightItem.name .."'");
      end
    end
  end
  StandardDebug( "Lights turned: " .. turnOnOrOff .. " for " .. CurrentState.timeOfDay );
end

function turnLightOn( lightItem )
  if (lightItem.lightType == "com.fibaro.multilevelSwitch" ) then
    fibaro:call(lightItem.id, "setValue", tostring(lightItem.setValue));
    StandardDebug( "Set: [" .. lightItem.id .. "]'" .. lightItem.name .. "' to Value: " .. lightItem.setValue );
  elseif ( lightItem.lightType == "com.fibaro.RGBW" )then
    StandardDebug( "Turn: [" .. lightItem.id .. "]'" .. lightItem.name .. "' On"); 
    if ( lightItem.setValue.brg~= nil ) then
      fibaro:call(lightItem.id, "setBrightness", tostring(lightItem.setValue.brg));
    else
	  local clrvalues =lightItem.setValue;
      fibaro:call(lightItem.id, "setColor", clrvalues.R,  clrvalues.G,  clrvalues.B,  clrvalues.W)	
    end
  elseif (lightItem.lightType == "com.fibaro.binarySwitch") then
    fibaro:call(lightItem.id, "turnOn");
	StandardDebug( "Turn: [" .. lightItem.id .. "]'" .. lightItem.name .. "' On"); 
  end
end

function CheckPreviousLights( NewLightArray )
  if ( previousLights ~= nil ) then
    for i = 1,#previousLights do
      local lightItem = previousLights[i];
      for i = 1,#NewLightArray do
        local lightItem1 = NewLightArray[i];
        inarray = false;
        if ( lightItem.id == lightItem1.id ) then
          inarray= true;
          break 
        end
      end
      if not ( inarray ) then fibaro:call(lightItem.id, "turnOff"); end
    end
  end
  previousLights = NewLightArray;	
end

function LightperDayPart( minLux, lightArray )
  local newLuxValue = CurrentState.lux;
  if ( extraLightTriggerChecks() ) then
    if ( newLuxValue  >  minLux )  then
      StandardDebug( "Sensor lux: " .. newLuxValue .. " higher then minValue: " .. minLux .. " : no action");
    else
      StandardDebug("Sensor lux: " .. newLuxValue .. " is lower then minValue: " .. minLux);
	  SetLightValues(lightArray);
	  CallLightArray( lightArray, "on" );
	  CheckPreviousLights( lightArray );
    end
  else
    ExtraDebug("ExtraLightTriggerChecks failed, so no lights");
  end
end

function SceneTriggered()
  if ( CurrentState.homeStatus ~= presentStateMapping.Home ) then
	ExtraDebug("Presentstate = not at home, so no lights");
  elseif  ( CurrentState.sleepState == sleepStateMapping.Sleeping ) then
    LightperDayPart( 65535, lightsSleeping );	
  elseif ( CurrentState.timeOfDay == timeOfDayMapping.Morning ) then
    extraMorningFunc();
    LightperDayPart( minLuxMorning, lightsMorning );
  elseif ( CurrentState.timeOfDay == timeOfDayMapping.Day ) then
    extraDayFunc();
	LightperDayPart( minLuxDay, lightsDay );
  elseif ( CurrentState.timeOfDay == timeOfDayMapping.Evening ) then
    extraEveningFunc();
    LightperDayPart( minLuxEvening, lightsEvening );
  elseif ( CurrentState.timeOfDay == timeOfDayMapping.Night  ) then
    extraNightFunc();
    LightperDayPart( minLuxNight, lightsNight );
  else
	ErrorDebug( "No lights: " .. CurrentState.timeOfDay );
  end
  if (keepLightsOn == true) then
    RunTimer(); 
  end
end

function SceneTriggeredByLights( st )
  if (st == "off") then
    ExtraDebug( "light turned off, sleep 4 sec" );
    fibaro:sleep(4000);
  elseif (st == "on") then
    ExtraDebug( "light turned on, activating timer" );
	fibaro:sleep(4000);
	--TODO: add light to current array. 
	--keepLightsOn = true;
	--RunTimer();
	end
end

------------------------STATUS functions------------------------------

function GetTimeOfDay()
  return LookupGlobal( timeOfDay, "TimeOfDay", "Day");
end

function GetSleepState()
  return LookupGlobal( sleepState, "sleepState", "Awake");
end

function GetPresentState()
  return LookupGlobal( presentState, "presentState", "Home");
end

function LookupGlobal( name, stateName, default )
  local ps = fibaro:getGlobalValue( name );
  if ( (ps ~= "") and (ps ~= nil ) ) then
    ExtraDebug("returned " .. stateName .. ": " .. ps );
    return ps;
  else 
    ErrorDebug( stateName .. " variable not found");
    return default;
  end
end

function lightsStatus( id )
  --check if lights are already on.
  allLights = {lightsSleeping, lightsMorning, lightsDay, lightsEvening, lightsNight };
  for i = 1,#allLights do
    for iL = 1, #allLights[i] do 
	  local lightItem =  allLights[i][iL];
      if ( lightItem.id == tonumber(id) ) then
        if   ( lightItem.currentValue == 0 ) then
          return "off";
        else
          return "on";
        end 
      end
    end
  end
  ErrorDebug("Light status unknown");
  return "Unknown";  
end

function GetTimeOfDayLightArray()
 if ( CurrentState.sleepState == sleepStateMapping.Sleeping ) then
    return lightsSleeping;
  elseif ( CurrentState.timeOfDay == timeOfDayMapping.Morning ) then
    return lightsMorning;
  elseif ( CurrentState.timeOfDay == timeOfDayMapping.Day ) then
	return lightsDay;
  elseif ( CurrentState.timeOfDay == timeOfDayMapping.Evening ) then
	return lightsEvening;
  elseif ( CurrentState.timeOfDay == timeOfDayMapping.Night ) then
	return lightsNight;
  end
end

function GetCurrentMotionStatus()
  local sensorbreached = 0;
  if (CheckIfTable(motionSensorID, "motionSensorID") ) then
    for i = 1,#motionSensorID do
      if ( tonumber(fibaro:getValue(motionSensorID[i], "value")) > 0 ) then
        sensorbreached = 1;
      end
    end
   else 
    --if not a table, just return the value of the containing ID
	sensorbreached = tonumber(fibaro:getValue(motionSensorID, "value"))
  end
  return sensorbreached;
end

function GetAverageLuxValue()
  local luxAverage = 0;
  if (CheckIfTable(LuxSensorID, "LuxSensorID") ) then
    if (#LuxSensorID == 1) then 
      return tonumber(fibaro:getValue(LuxSensorID[1], "value"));
    end
    for i = 1,#LuxSensorID do 
      luxAverage = luxAverage + tonumber(fibaro:getValue(LuxSensorID[i], "value"));
    end
      luxAverage = roundit( (luxAverage / #LuxSensorID), 0 );
  else 
    --if not a table, just return the value of the containing ID
    luxAverage = tonumber(fibaro:getValue(LuxSensorID, "value"));
  end
  return luxAverage;
end

function UpdateLightValues( currentArray )
  for i = 1,#currentArray do
    item = currentArray[i];
	newValue = fibaro:getValue(item.id, "value");
	item.currentValue = tonumber(newValue) ~= nil and tonumber(newValue) or ( newValue == "true" and 1 or 0 );
    if ( item.lightType == "com.fibaro.RGBW" )then
         color = fibaro:getValue(item.id, "color");
         c = {R="0", G="0", B="0", W="0", brg="0" }
         c.R, c.G, c.B, c.W = color:match("([^,]+),([^,]+),([^,]+),([^,]+)");
         c.brg = fibaro:getValue(item.id, "brightness"); 
         item.currentValue = c;
       end
  end
end

function SetLightValues(currentArray)
  for i = 1,#currentArray do
    item = currentArray[i];
    item.name = tostring(fibaro:getName(item.id));
	item.OffMode = "NoOverride";
	if (item.name == nil) then item.name = "Unknown"; end
    item.lightType = GetDeviceType( item.id );
  end
end

function GetDeviceType(itemId)
  deviceType = fibaro:getType(item.id);
  if ( idIsInList(deviceType, RGBWDeviceType) ) then
      deviceType ="com.fibaro.RGBW";
  elseif ( idIsInList(deviceType, BinaryDeviceType) ) then
       deviceType = "com.fibaro.binarySwitch";
  elseif ( idIsInList(deviceType, DimmerDeviceType ) ) then
       deviceType = "com.fibaro.multilevelSwitch";
 --elseif( ) then
 --deviceType = "Philips.Hue";
  end
    return deviceType;
end

--------------------Helper functions--------------------------------

function CheckLightToBeTurnedOnAgain(lightItem)
 if not (lightItem.OffMode=="ByScene") then return; end
 	   
 if (lightItem.lightType == "com.fibaro.multilevelSwitch" ) then
   if (lightItem.currentValue) < tonumber(lightItem.setValue) then return true; end
 elseif( lightItem.lightType == "com.fibaro.RGBW" )then
   c = lightItem.currentValue;
   if (c.R == "0" and c.G == "0" and c.B == "0" and c.W == "0" ) then
     return true;
   end    
 elseif (lightItem.lightType == "com.fibaro.binarySwitch") then
   if (lightItem.setValue == "turnOn" and lightItem.currentValue == 0 ) then return true; end
  end
  return false;
end

function CheckIfTable( array, arrayname )
  local tableCheck = tostring( type( array ) ); 
  if ( tableCheck ~= "table" ) then
    ErrorDebug("Missing brackets for variable: '" .. arrayname .. "', please place extra brackts: { } around: " .. array .. "."); 
	return false;
  end
  return true;
end

function roundit(num, idp)
  local mult = 10^(idp or 0)
  return math.floor(num * mult + 0.5) / mult
end

function idIsInList( startSourceId, sensorList )
  for i = 1,#sensorList do
    if ( startSourceId == sensorList[i] ) then
      return true;
    end
  end
  return false;
end

--------------------Debugging Functions-----------------------------

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

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

function ErrorDebug( debugMessage )
    Debug( "red", "Error: " .. debugMessage);
    Debug( "red", "");
end

function TestDebug(debugMessage )
   Debug( "blue", "Testing: " .. debugMessage );
end

----------------------START OF THE SCENE----------------------------
SetCurrentStatus();
StandardDebug("Home status: " .. CurrentState.homeStatus );
StandardDebug("Motion status: " .. ( CurrentState.motionState == 0 and "No movement" or "movement")); 
  
if (startSource["type"] == "property") then
  startSourceID = tonumber(startSource['deviceID']);
  triggerDebug  = "Triggered by: " .. startSourceID;
  if ( idIsInList( startSourceID, motionSensorID ) ) then
    StandardDebug( triggerDebug .. " Motion sensor" );
    if ( CurrentState.motionState > 0 ) then
      SceneTriggered();
    end
  elseif ( idIsInList( startSourceID, LuxSensorID ) ) then
    StandardDebug( triggerDebug .. " Lux sensor" );
    ExtraDebug( "Lux value changed to: " .. CurrentState.lux );
    if ( CurrentState.motionState > 0 ) then
      SceneTriggered();
    end
  else
    StandardDebug( triggerDebug .. " Light switch" );
    st = lightsStatus( startSourceID );
    if (st == "Unknown") then
      ErrorDebug( "Unknown light trigger" );
	else
	  SceneTriggeredByLights( st );
	end
    -- Maybe we can change the light preset to make it more intelligent.
    -- Maybe we can change the Lux preset to make it more intelligent.
  end
elseif ( startSource["type"] == "global" ) then
  StandardDebug( "Triggered by: " .. "global variable" );
  -- If home status away, we trigger so the current array will be turned off. If motion, turn them on.
  if (CurrentState.homeStatus ~= presentStateMapping.Home) then
    TurnOffLights();
  elseif (CurrentState.motionState > 0 ) then
    SceneTriggered();
  end
else 
  StandardDebug( "Triggered by: " .. startSource["type"] .. " , Not checking the motion state." );
  --Just run the Light Code, not checking for motion. Probably triggered manually.
  if ( startSource["type"] == "other" ) then
    SceneTriggered();
  end
end

Debug( "green", "© Smart Lights V" .. version .. " | by Control Living, Finished" );
Debug( "green", "-------------------------------------------------------" );
--fibaro:abort(); --otherwise scene will stay alive to long.

--UPDATE TILL HERE

si quelqu'un a une idée ?

 

merci

Partager ce message


Lien à poster
Partager sur d’autres sites

Bonsoir à tous,

 

J'ai perdu mon script (je ne sais comment) il n'est plus dans ma HC2, impossible de le retrouver.

J'ai voulu le désactiver (car mon oeil sans pile) et puis j'ai du le supprimer à la place :D

 

En le cherchant chez l'auteur je vois que dans sa dernière version il est payant, par contre je ne sais pas trop ce qu'il a de nouveau...

Modifié par Domodial

Partager ce message


Lien à poster
Partager sur d’autres sites

A gauche, dans l'onglet visibilité, tu as "show all".

La tu verras tous les modules

Sinon ....

Partager ce message


Lien à poster
Partager sur d’autres sites

non mais je l'avais vraiment supprimé :D

heureusement j'avais une sauvegarde grace au HC2 Toolkit :)

Partager ce message


Lien à poster
Partager sur d’autres sites

Salut à tous,

 

Pour ceux qui utilise ce script, est-ce qu'il fonctionne encore chez vous en 4.110 ?

Car j'ai tout repris de zéro, et il ne fonctionne plus.

Dans le log la détection se passe bien, il inscrit "On" mais pas de lumière allumé.

Il dit aussi souvent qu'il a détecté à 412lux alors qu'il fait 18lux sur le moment...

 

Je ne comprends plus rien en fait.

 

Juste si ceux qui l'utilise peuvent me dire s'il est fonctionnel.

 

Merci

Partager ce message


Lien à poster
Partager sur d’autres sites

bon, le script fonctionne si je bypass les paramètres de Lux.

Je vais revoir tout cela ce soir...

 

C'est suite a Sauron qui n'avait plus de pile depuis xxx mois :D 

J'ai remis une pile et ça fonctionne pas trop bien...

J'ai pas encore controlé les paramètres, mais si c'est perdu parce que plus de pile (je ne pense pas car le réglage est dans la HC2) alors je comprendrais mieux...

  • Upvote 1

Partager ce message


Lien à poster
Partager sur d’autres sites

Hello pepite

 

HS Disons que finalement je ne veux pas perdre mon ancienneté et donc je recherche en interne mais ça ne se passe pas bien :D /HS

Partager ce message


Lien à poster
Partager sur d’autres sites

Salut Domodial,

pas de soucis chez moi avec le script.Tu as bien gardé les memes ID pour l'oeil de sauron?

La valeur de Lux évolue ou elle reste fixe à 418 ? (debug script)

Partager ce message


Lien à poster
Partager sur d’autres sites

Merci de répondre Ez3kiel :)

 

Je galère ^^ 

 

Je jongle entre ici et le forum US, mais autant dire que je ne trouve pas la panne.

 

Si je met la valeur lux (matin, soir ou nuit) à 65535 ça fonctionne très bien.

Si je met 30 Lux pour minLuxMorning et que ma valeur lux de Sauron est à 0 et bien ça me dit ça dans le log (ce qui est totalement dingue) :

 

[DEBUG] 18:28:01: Home status: Present
[DEBUG] 18:28:01: Motion status: movement
[DEBUG] 18:28:01: Triggered by: 460 Motion sensor
[DEBUG] 18:28:01: Off
[DEBUG] 18:28:01: Sensor lux: 0 is lower then minValue: 30
[DEBUG] 18:28:01: No lights set for Matin
[DEBUG] 18:28:01: © Smart Lights V1.7.0 | by Control Living, Finished
[DEBUG] 18:28:01: -------------------------------------------------------
[DEBUG] 18:28:08: Home status: Present
[DEBUG] 18:28:08: Motion status: No movement
[DEBUG] 18:28:08: Triggered by: 460 Motion sensor
[DEBUG] 18:28:08: © Smart Lights V1.7.0 | by Control Living, Finished
[DEBUG] 18:28:08: -------------------------------------------------------

Autrement voici la commande de la lampe du matin (rien de violent) :

-- Matin : Spots plafond uniquement
lightsMorning = {id=305, setValue="turnOn", onTime=300};

 

EDIT : je crois avoir un problème de syntaxe, je viens de faire le test avec un RGBW et ça fonctionne... :(

Modifié par Domodial

Partager ce message


Lien à poster
Partager sur d’autres sites

La syntaxe parait bonne à 1ere vue. Ta lampe du matin est pas sur un FGD par hasard ?

Je crois me souvenir qu'il y avait eu une modif dans le script sur les types de modules (vers le passage en V4 ou juste avant ), tu es repartis d'un vieux backup ? 


Ci dessous mon backup qui logiquement est fonctionnel :

Révélation



--[[
%% properties
33 value
35 value
491 value
%% globals
--]]

--[[ Change - XXX value - above. One for motion sensor, one for Light sensor.
     TimeOfDay global variable and lux make sure that the scene
     will be triggered when you are already in the room and something changes.

     Set your settings below, lights and add extra functions
     Feel free to suggest changes or contact when having problems.
	 It is not allowed to sell or distribute this software and earlier versions without the approval of Control Living
     © 2014 - 2015 Smart lights by Control Living
     Version 1.6.7
--]]

--------------------------------------------------------------------
-----------------------YOUR LIGHT SETTINGS--------------------------

motionSensorID    = {33};  -- change id for your motion sensor.
LuxSensorID       = {35};  -- change id for your light sensor.

--Enter the name of your Global variable. WITHOUT IT, THE SCENE DOES NOT WORK. Capital sensitive!
sleepState   = "SleepState"; --Sleep globalstate variable.
timeOfDay    = "TimeOfDay"; --Time of Day Global variable.
presentState = "Presence"; --Present state Global variable.

--Enter the values of your global variables stated above. If not using, copy the name in front of the "YourValueName"
sleepStateMapping   = {Sleeping="Sleeping", Awake="Awake"};
timeOfDayMapping    = {Morning="Morning", Day="Day", Evening="Evening", Night="Night"};
presentStateMapping = {Home="Present", Away="Absent", Holiday="Holiday"};

-- Set the lux value for which the lights have to be turned on.
-- If you don't want to use the MinLux then just set it to: 65535
minLuxMorning  = 30;
minLuxDay      = 30;
minLuxEvening  = 30;
minLuxNight    = 30;

--If the current lux value is above the maxlux, the lights will turn off after their set onTime.
maxLuxMorning = 300;
maxLuxDay = 300;

--[[
 Now the most important part:
   Here you can enter what light, for how long and at what value has to be turned on.
   Leave empty brackets for no lights.
   To set the light value without timer use 2 parameters: {id=LightID, setValue="VALUE"}
   To set the light value with timer    use 3 parameters: {id=LightID, setValue="VALUE", onTime=timeInSeconds }
   To set the light value for RGBW:  use the above, where setValue is: setValue={R="255",G="255",B="255",W="255"}
   --                                           RGBW set: {id=LightID, setValue={R="255",G="255",B="255",W="255"}, onTime=timeInSeconds }
   --]]
   
lightsSleeping = {}; --lights that are triggered when Sleepstate is sleeping. 
lightsMorning  = {{id=111, setValue="turnOn", onTime=1800},{id=128, setValue={R="255",G="255",B="136",W="255"}, onTime=1800}};
lightsDay      = {{id=111, setValue="turnOn", onTime=3600},{id=128, setValue={R="255",G="255",B="136",W="255"}, onTime=3600}};
lightsEvening  = {{id=111, setValue="turnOn", onTime=3600},{id=128, setValue={R="255",G="255",B="136",W="255"}, onTime=3600}};
lightsNight    = {{id=111, setValue="turnOn", onTime=3600},{id=128, setValue={R="77",G="77",B="41",W="0"}, onTime=3600}};

--Manual Override, the time that lights will not be turned ON again after manually turning them off.
OverrideFor  = 90;
dimmDownTime = 10;

--------------------------------------------------------------------
-------------------------EXTRA FEATURES-----------------------------

extraMorningFunc = function()
  -- Add your extra code here. If you want some checks or maybe run a virtual device button.
  --This code is always triggered if there is motion.
  -- movieLights =  {{180, "10"},{181, "10"} };
  --if (xbmc == "playing" ) then CallLightArray(movieLights);
  ExtraDebug("Extra morning function called");
end

extraDayFunc = function()
  -- Add your extra code here. If you want some checks or maybe run a virtual device button. 
  --This code is always triggered if there is motion.
  ExtraDebug("Extra day function called");
end

extraEveningFunc = function()
  -- Add your extra code here. If you want some checks or maybe run a virtual device button.
  --This code is always triggered if there is motion.
  ExtraDebug("Extra evening function called");
end

extraNightFunc = function()
  -- Add your extra code here. If you want some checks or maybe run a virtual device button.
  --This code is always triggered if there is motion.
  ExtraDebug("Extra night function called");
end

extraLightTriggerChecks = function()
--add extra checks here. and return the total true or false value.
--if returning false the lights will not be triggered.
-- for instance:  return ( (pcTurnedOff == true ) and (xbmc ~= "Empty") );
  -- return true to enable lights to turn on
  local xbmc= fibaro:getGlobalValue("Xbmc_salon");
  return (xbmc == "Empty");
  --return true;
end

extraOffChecks = function()
  --return true to keep lights on.
  return false;
end

--------------------------------------------------------------------
----------------------ADVANCES SETTINGS-----------------------------
local showStandardDebugInfo = true; -- Debug shown in white
local showExtraDebugInfo    = false; -- Debug shown in orange

--------------------------------------------------------------------
--------------------------------------------------------------------
--               DO NOT CHANGE THE CODE BELOW                     --
--------------------------------------------------------------------
--UPDATE FROM HERE

--private variables
startSource        = fibaro:getSourceTrigger();
keepLightsOn       = false;
timerRunning       = false;
previousLights     = nil;
version = "1.6.7";

SavedState = {
  homeStatus = "",
  sleepState = 0,
  timeOfDay  = "",
  lux        = 0,
  motion     = 0,
  startTime  = 0
}

CurrentState = {
  homeStatus  = "",
  sleepState  = "",
  timeOfDay   = "",
  lux         = 0,
  motionState = 0,
  currentLightArray = {}
}

RGBWDeviceType = { "com.fibaro.RGBW", "com.fibaro.colorController", "com.fibaro.FGRGBW441M"  }
BinaryDeviceType = { "com.fibaro.binarySwitch","com.fibaro.FGWP101", "turnOn"  }
DimmerDeviceType = { "com.fibaro.multilevelSwitch" }

OffModes = {None, ByScene, ManualOverride }

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

--Making sure that only one instance of the scene is running.
fibaro:sleep(50); --sleep to prevent all instances being killed.
if (fibaro:countScenes() > 1) then
  if (showExtraDebugInfo) then
    Debug( "grey", "Abort, Scene count = " .. fibaro:countScenes());
  end
  fibaro:abort();
end

--------------------------EXECUTION----------------------------------

function SetCurrentStatus()
  ExtraDebug("Updating current variable statuses");
  CurrentState.homeStatus  = GetPresentState();
  CurrentState.timeOfDay   = GetTimeOfDay();
  CurrentState.sleepState  = GetSleepState();
  CurrentState.lux         = GetAverageLuxValue();
  CurrentState.motionState = GetCurrentMotionStatus();
end

function SaveCurrentStatus()
  ExtraDebug("Saving current variable statuses");
  SavedState.homeStatus = CurrentState.homeStatus;
  SavedState.timeOfDay  = CurrentState.timeOfDay;
  SavedState.sleepState = CurrentState.sleepState;
  SavedState.lux        = CurrentState.lux;
  SavedState.motion     = CurrentState.motionState;
end

function CheckStatusChanges()
  ExtraDebug("Status change check");
  if (CurrentState.homeStatus ~= SavedState.homeStatus ) or (CurrentState.timeOfDay ~= SavedState.timeOfDay ) then
    if (CurrentState.homeStatus ~= presentStateMapping.Home ) then
      TurnOffLights();
    else
      SceneTriggered();
	  --reset time.
    end;
  end
  SaveCurrentStatus();
    -- if we still have a motion then reset timer.
  if ( (CurrentState.motionState ~= 0 or extraOffChecks() ) and not IsMaxLuxExceeded() )  then 
	ExtraDebug( "Resetting time" );
    SavedState.startTime = os.time(); 
  else
    ExtraDebug("Timer is not reset.");
  end
  --any other case, we are not resetting the timer.
end

function IsMaxLuxExceeded()
  if (CurrentState.timeOfDay == timeOfDayMapping.Morning and CurrentState.lux > maxLuxMorning ) then
    ExtraDebug( "Current lux value: " .. CurrentState.lux .. " higher then max Lux Morning: " .. maxLuxMorning .. ".");
	return true;
  elseif (CurrentState.timeOfDay == timeOfDayMapping.Day and CurrentState.lux > maxLuxDay ) then
    ExtraDebug( "Current lux value: " .. CurrentState.lux .. " higher then max Lux Day: " .. maxLuxDay .. ".");
	return true;
   end
   return false;
end

function LightsOff( lightArray )
  local stillLightsOn = 0;
  local currentTime = os.time();
  for i = 1,#lightArray do
    if ( lightArray[i].onTime ~= nil) then
	  lightItem = lightArray[i];
	  lightItem.OffMode = CheckManualOverrideItem( lightItem );
	  --local 
	  if ( lightItem.OffMode == "ManualOverride" ) then
	      ExtraDebug("Manual override for light: [" .. lightItem.id .. "]" .. lightItem.name .. " active, not turning on");
		  goto continue;
	  end
	  --On till:
      local timeL = SavedState.startTime + (lightItem.onTime);
	  local timeLeft =  timeL -  currentTime;
 	  if (timeLeft >= 0 ) then ExtraDebug("Time left for: [" .. lightItem.id .. "]" .. lightItem.name .. ": " .. timeLeft .. " seconds" );   end
	  if ( timeLeft < dimmDownTime ) then
        if ( lightItem.OffMode ~= "ManualOverride" ) then
			lightItem.OffMode = "ByScene";
		end
	    if ( (timeLeft <= 0) and (lightItem.currentValue ~= 0) ) then
			fibaro:call(lightItem.id, "turnOff");
			StandardDebug("Switch off light: [" .. lightItem.id .. "]'" .. lightItem.name .."'");
        else
            if ( item.lightType == "com.fibaro.multilevelSwitch" ) then
			  currentValueDiv = roundit(((lightItem.currentValue) / (dimmDownTime)), 0);
			  currentValueNew = (lightItem.currentValue - currentValueDiv);
			  if (currentValueNew <=0 ) then currentValueNew = 0; end
			  fibaro:call(lightItem.id, "setValue", tostring(currentValueNew));
            end
			stillLightsOn = stillLightsOn + 1;
		end
      elseif (CheckLightToBeTurnedOnAgain(lightItem)) then
        ExtraDebug("Turn light: " .. lightItem.id .. " back on");
        turnLightOn(lightItem);
		lightItem.OffMode = "";
        stillLightsOn = stillLightsOn + 1;  
      else
	    lightItem.OffMode = "NoOverride";
	    stillLightsOn = stillLightsOn + 1;   
      end
    end
	::continue::
  end
  return stillLightsOn;
end

function KeepLightsOnTimer()
  ExtraDebug("--------------- Timer running ---------------");
  ExtraDebug("starting with while loop, to keep lights on");
  SavedState.startTime = os.time();
  SaveCurrentStatus();
  sleepTime = 1000;
  while ( keepLightsOn ) do
    ExtraDebug("--------------- next timer run ---------------");
    fibaro:sleep(sleepTime);
	start_time = os.clock();
    SetCurrentStatus();
    currentLightArray = GetTimeOfDayLightArray();
    UpdateLightValues(currentLightArray);
    CheckStatusChanges();
    local stillLightsOn = 0;  
    stillLightsOn = LightsOff( currentLightArray );
    if (stillLightsOn == 0 ) then
	  keepLightsOn = RunManualOverrideMode( currentLightArray );
      end_time = 0; --if lights turned back on after override.
    else
      end_time = os.clock() 
    end
	elapsed_time = (end_time - start_time) * 1000;
	sleepTime = 1000 - elapsed_time;
  end
end

function CheckManualOverrideItem( currentItem ) 
  --ExtraDebug("Manual override check for: [" .. currentItem.id .. "]" .. currentItem.name);
  if (currentItem.currentValue == 0 and currentItem.OffMode ~= "ByScene" ) then
    ExtraDebug( "Manual override for light: [" .. currentItem.id .. "]" .. currentItem.name .. " active" );
	return "ManualOverride";
  elseif (currentItem.currentValue ~= 0 and currentItem.OffMode == "ManualOverride" ) then
	ExtraDebug( "Manual override for light: [" .. currentItem.id .. "]" .. currentItem.name .. " cancelled" );
	return "NoOverride";
  end
  return currentItem.OffMode; --returning current mode;
end

function RunManualOverrideMode( currentLightArray )
  OverrideForAll = CheckIfAllInOverrideMode(currentLightArray);
  if ( OverrideForAll ) then
    ExtraDebug("-----------------Override Mode---------------");
    OverrideTimer = os.time();
    while ( OverrideForAll and (OverrideTimer + OverrideFor ) - os.time() > 0 ) do
      ExtraDebug("Still in override for: " .. (OverrideTimer + OverrideFor ) - os.time() .. " seconds" );
      fibaro:sleep(1000);
	  UpdateLightValues(currentLightArray);
	  OverrideForAll = CheckIfAllInOverrideMode(currentLightArray);
	  motion = GetCurrentMotionStatus();
	  if ( motion ~= 0 ) then
	    OverrideTimer = os.time();
	  end
    end
	else return false;
  end
 -- if  ( (OverrideTimer + OverrideFor ) - os.time() <= 0 ) then
  --time has end. So not continue to run lights loop 
 --   return false;
 -- end
  if (OverrideForAll) then
    return false; --run lights 
  else return true;
  end
end

function CheckIfAllInOverrideMode(currentLightArray)
  OverrideForAll = 0;
  for i = 1,#currentLightArray do
    if ( CheckManualOverrideItem(currentLightArray[i]) == "ManualOverride" ) then
      OverrideForAll = OverrideForAll +1;
    else
      return false;
    end
  end
  if (  OverrideForAll ~= 0 and OverrideForAll == #currentLightArray ) then
    return true;
  end
  return false;
end

function RunTimer()
if ( keepLightsOn and not timerRunning ) then
    ExtraDebug("Starting timer, not yet running");
    timerRunning = true;
    KeepLightsOnTimer();
  else
	ExtraDebug("Timer already running, returning");
  end
end

function TurnOffLights()
     local timeOfDayArray = GetTimeOfDayLightArray();
     SetLightValues(timeOfDayArray);
	 UpdateLightValues(timeOfDayArray);
     CallLightArray(timeOfDayArray, "off" );
	return;
end

----------------------Turn lights on functions------------------------

function CallLightArray( lightArray, turnOnOrOff )
  if (#lightArray == 0 ) then
    if (CurrentState.sleepState == sleepStateMapping.Sleeping) then
	  noLightsSetText = "lightsSleeping";
	else
      noLightsSetText = CurrentState.timeOfDay;
	end
    StandardDebug( "No lights set for " .. noLightsSetText );
    return;
  end
  if not ( CheckIfTable(lightArray, "lights" .. CurrentState.timeOfDay ) ) then return end
  for i = 1,#lightArray do
    if not ( CheckIfTable(lightArray[i], "lights" .. CurrentState.timeOfDay ) ) then break end
    lightItem = lightArray[i];
    if (turnOnOrOff == "on" ) then
      turnLightOn(lightItem);
      if (lightItem.onTime ~= nil) then keepLightsOn = true; end
    else
	  fibaro:call(lightItem.id, "turnOff"); --Always turn off, if HC2 light status is not actual.  
      if ( lightItem.currentValue ~= 0) then
        keepLightsOn = false;
        StandardDebug("Switch off light: [" .. lightItem.id .. "]'" .. lightItem.name .."'");
      end
    end
  end
  StandardDebug( "Lights turned: " .. turnOnOrOff .. " for " .. CurrentState.timeOfDay );
end

function turnLightOn( lightItem )
  if (lightItem.lightType == "com.fibaro.multilevelSwitch" ) then
    fibaro:call(lightItem.id, "setValue", tostring(lightItem.setValue));
    StandardDebug( "Set: [" .. lightItem.id .. "]'" .. lightItem.name .. "' to Value: " .. lightItem.setValue );
  elseif ( lightItem.lightType == "com.fibaro.RGBW" )then
    StandardDebug( "Turn: [" .. lightItem.id .. "]'" .. lightItem.name .. "' On"); 
    if ( lightItem.setValue.brg~= nil ) then
      fibaro:call(lightItem.id, "setBrightness", tostring(lightItem.setValue.brg));
    else
	  local clrvalues =lightItem.setValue;
      fibaro:call(lightItem.id, "setColor", clrvalues.R,  clrvalues.G,  clrvalues.B,  clrvalues.W)	
    end
  elseif (lightItem.lightType == "com.fibaro.binarySwitch") then
    fibaro:call(lightItem.id, "turnOn");
	StandardDebug( "Turn: [" .. lightItem.id .. "]'" .. lightItem.name .. "' On"); 
  end
end

function CheckPreviousLights( NewLightArray )
  if ( previousLights ~= nil ) then
    for i = 1,#previousLights do
      local lightItem = previousLights[i];
      for i = 1,#NewLightArray do
        local lightItem1 = NewLightArray[i];
        inarray = false;
        if ( lightItem.id == lightItem1.id ) then
          inarray= true;
          break 
        end
      end
      if not ( inarray ) then fibaro:call(lightItem.id, "turnOff"); end
    end
  end
  previousLights = NewLightArray;	
end

function LightperDayPart( minLux, lightArray )
  local newLuxValue = CurrentState.lux;
  if ( extraLightTriggerChecks() ) then
    if ( newLuxValue  >  minLux )  then
      StandardDebug( "Sensor lux: " .. newLuxValue .. " higher then minValue: " .. minLux .. " : no action");
    else
      StandardDebug("Sensor lux: " .. newLuxValue .. " is lower then minValue: " .. minLux);
	  SetLightValues(lightArray);
	  CallLightArray( lightArray, "on" );
	  CheckPreviousLights( lightArray );
    end
  else
    ExtraDebug("ExtraLightTriggerChecks failed, so no lights");
  end
end

function SceneTriggered()
  if ( CurrentState.homeStatus ~= presentStateMapping.Home ) then
	ExtraDebug("Presentstate = not at home, so no lights");
  elseif  ( CurrentState.sleepState == sleepStateMapping.Sleeping ) then
    LightperDayPart( 65535, lightsSleeping );	
  elseif ( CurrentState.timeOfDay == timeOfDayMapping.Morning ) then
    extraMorningFunc();
    LightperDayPart( minLuxMorning, lightsMorning );
  elseif ( CurrentState.timeOfDay == timeOfDayMapping.Day ) then
    extraDayFunc();
	LightperDayPart( minLuxDay, lightsDay );
  elseif ( CurrentState.timeOfDay == timeOfDayMapping.Evening ) then
    extraEveningFunc();
    LightperDayPart( minLuxEvening, lightsEvening );
  elseif ( CurrentState.timeOfDay == timeOfDayMapping.Night  ) then
    extraNightFunc();
    LightperDayPart( minLuxNight, lightsNight );
  else
	ErrorDebug( "No lights: " .. CurrentState.timeOfDay );
  end
  if (keepLightsOn == true) then
    RunTimer(); 
  end
end

function SceneTriggeredByLights( st )
  if (st == "off") then
    ExtraDebug( "light turned off, sleep 4 sec" );
    fibaro:sleep(4000);
  elseif (st == "on") then
    ExtraDebug( "light turned on, activating timer" );
	fibaro:sleep(4000);
	--TODO: add light to current array. 
	--keepLightsOn = true;
	--RunTimer();
	end
end

------------------------STATUS functions------------------------------

function GetTimeOfDay()
  return LookupGlobal( timeOfDay, "TimeOfDay", "Day");
end

function GetSleepState()
  return LookupGlobal( sleepState, "sleepState", "Awake");
end

function GetPresentState()
  return LookupGlobal( presentState, "presentState", "Home");
end

function LookupGlobal( name, stateName, default )
  local ps = fibaro:getGlobalValue( name );
  if ( (ps ~= "") and (ps ~= nil ) ) then
    ExtraDebug("returned " .. stateName .. ": " .. ps );
    return ps;
  else 
    ErrorDebug( stateName .. " variable not found");
    return default;
  end
end

function lightsStatus( id )
  --check if lights are already on.
  allLights = {lightsSleeping, lightsMorning, lightsDay, lightsEvening, lightsNight };
  for i = 1,#allLights do
    for iL = 1, #allLights[i] do 
	  local lightItem =  allLights[i][iL];
      if ( lightItem.id == tonumber(id) ) then
        if   ( lightItem.currentValue == 0 ) then
          return "off";
        else
          return "on";
        end 
      end
    end
  end
  ErrorDebug("Light status unknown");
  return "Unknown";  
end

function GetTimeOfDayLightArray()
 if ( CurrentState.sleepState == sleepStateMapping.Sleeping ) then
    return lightsSleeping;
  elseif ( CurrentState.timeOfDay == timeOfDayMapping.Morning ) then
    return lightsMorning;
  elseif ( CurrentState.timeOfDay == timeOfDayMapping.Day ) then
	return lightsDay;
  elseif ( CurrentState.timeOfDay == timeOfDayMapping.Evening ) then
	return lightsEvening;
  elseif ( CurrentState.timeOfDay == timeOfDayMapping.Night ) then
	return lightsNight;
  end
end

function GetCurrentMotionStatus()
  local sensorbreached = 0;
  if (CheckIfTable(motionSensorID, "motionSensorID") ) then
    for i = 1,#motionSensorID do
      if ( tonumber(fibaro:getValue(motionSensorID[i], "value")) > 0 ) then
        sensorbreached = 1;
      end
    end
   else 
    --if not a table, just return the value of the containing ID
	sensorbreached = tonumber(fibaro:getValue(motionSensorID, "value"))
  end
  return sensorbreached;
end

function GetAverageLuxValue()
  local luxAverage = 0;
  if (CheckIfTable(LuxSensorID, "LuxSensorID") ) then
    if (#LuxSensorID == 1) then 
      return tonumber(fibaro:getValue(LuxSensorID[1], "value"));
    end
    for i = 1,#LuxSensorID do 
      luxAverage = luxAverage + tonumber(fibaro:getValue(LuxSensorID[i], "value"));
    end
      luxAverage = roundit( (luxAverage / #LuxSensorID), 0 );
  else 
    --if not a table, just return the value of the containing ID
    luxAverage = tonumber(fibaro:getValue(LuxSensorID, "value"));
  end
  return luxAverage;
end

function UpdateLightValues( currentArray )
  for i = 1,#currentArray do
    item = currentArray[i];
	newValue = fibaro:getValue(item.id, "value");
	item.currentValue = tonumber(newValue) ~= nil and tonumber(newValue) or ( newValue == "true" and 1 or 0 );
    if ( item.lightType == "com.fibaro.RGBW" )then
         color = fibaro:getValue(item.id, "color");
         c = {R="0", G="0", B="0", W="0", brg="0" }
         c.R, c.G, c.B, c.W = color:match("([^,]+),([^,]+),([^,]+),([^,]+)");
         c.brg = fibaro:getValue(item.id, "brightness"); 
         item.currentValue = c;
       end
  end
end

function SetLightValues(currentArray)
  for i = 1,#currentArray do
    item = currentArray[i];
    item.name = tostring(fibaro:getName(item.id));
	item.OffMode = "NoOverride";
	if (item.name == nil) then item.name = "Unknown"; end
    item.lightType = GetDeviceType( item.id );
  end
end

function GetDeviceType(itemId)
  deviceType = fibaro:getType(item.id);
  if ( idIsInList(deviceType, RGBWDeviceType) ) then
      deviceType ="com.fibaro.RGBW";
  elseif ( idIsInList(deviceType, BinaryDeviceType) ) then
       deviceType = "com.fibaro.binarySwitch";
  elseif ( idIsInList(deviceType, DimmerDeviceType ) ) then
       deviceType = "com.fibaro.multilevelSwitch";
 --elseif( ) then
 --deviceType = "Philips.Hue";
  end
    return deviceType;
end

--------------------Helper functions--------------------------------

function CheckLightToBeTurnedOnAgain(lightItem)
 if not (lightItem.OffMode=="ByScene") then return; end
 	   
 if (lightItem.lightType == "com.fibaro.multilevelSwitch" ) then
   if (lightItem.currentValue) < tonumber(lightItem.setValue) then return true; end
 elseif( lightItem.lightType == "com.fibaro.RGBW" )then
   c = lightItem.currentValue;
   if (c.R == "0" and c.G == "0" and c.B == "0" and c.W == "0" ) then
     return true;
   end    
 elseif (lightItem.lightType == "com.fibaro.binarySwitch") then
   if (lightItem.setValue == "turnOn" and lightItem.currentValue == 0 ) then return true; end
  end
  return false;
end

function CheckIfTable( array, arrayname )
  local tableCheck = tostring( type( array ) ); 
  if ( tableCheck ~= "table" ) then
    ErrorDebug("Missing brackets for variable: '" .. arrayname .. "', please place extra brackts: { } around: " .. array .. "."); 
	return false;
  end
  return true;
end

function roundit(num, idp)
  local mult = 10^(idp or 0)
  return math.floor(num * mult + 0.5) / mult
end

function idIsInList( startSourceId, sensorList )
  for i = 1,#sensorList do
    if ( startSourceId == sensorList[i] ) then
      return true;
    end
  end
  return false;
end

--------------------Debugging Functions-----------------------------

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

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

function ErrorDebug( debugMessage )
    Debug( "red", "Error: " .. debugMessage);
    Debug( "red", "");
end

function TestDebug(debugMessage )
   Debug( "blue", "Testing: " .. debugMessage );
end

----------------------START OF THE SCENE----------------------------
SetCurrentStatus();
StandardDebug("Home status: " .. CurrentState.homeStatus );
StandardDebug("Motion status: " .. ( CurrentState.motionState == 0 and "No movement" or "movement")); 
  
if (startSource["type"] == "property") then
  startSourceID = tonumber(startSource['deviceID']);
  triggerDebug  = "Triggered by: " .. startSourceID;
  if ( idIsInList( startSourceID, motionSensorID ) ) then
    StandardDebug( triggerDebug .. " Motion sensor" );
    if ( CurrentState.motionState > 0 ) then
      SceneTriggered();
    end
  elseif ( idIsInList( startSourceID, LuxSensorID ) ) then
    StandardDebug( triggerDebug .. " Lux sensor" );
    ExtraDebug( "Lux value changed to: " .. CurrentState.lux );
    if ( CurrentState.motionState > 0 ) then
      SceneTriggered();
    end
  else
    StandardDebug( triggerDebug .. " Light switch" );
    st = lightsStatus( startSourceID );
    if (st == "Unknown") then
      ErrorDebug( "Unknown light trigger" );
	else
	  SceneTriggeredByLights( st );
	end
    -- Maybe we can change the light preset to make it more intelligent.
    -- Maybe we can change the Lux preset to make it more intelligent.
  end
elseif ( startSource["type"] == "global" ) then
  StandardDebug( "Triggered by: " .. "global variable" );
  -- If home status away, we trigger so the current array will be turned off. If motion, turn them on.
  if (CurrentState.homeStatus ~= presentStateMapping.Home) then
    TurnOffLights();
  elseif (CurrentState.motionState > 0 ) then
    SceneTriggered();
  end
else 
  StandardDebug( "Triggered by: " .. startSource["type"] .. " , Not checking the motion state." );
  --Just run the Light Code, not checking for motion. Probably triggered manually.
  if ( startSource["type"] == "other" ) then
    SceneTriggered();
  end
end

Debug( "green", "© Smart Lights V" .. version .. " | by Control Living, Finished" );
Debug( "green", "-------------------------------------------------------" );
--fibaro:abort(); --otherwise scene will stay alive to long.

--UPDATE TILL HERE


 

 

Modifié par Ez3kiel

Partager ce message


Lien à poster
Partager sur d’autres sites

Hello,

 

Non en fait je suis repartit du nouveau script en 1.7 et j'ai remis mes lignes.

Mais hier soir, pour une raison encore que j'ignore et que je pense j'ignorerais encore :D ça c'est mis à refonctionner correctement !

 

Truc de dingue en fait...je ne sais pas si c'est la reconfiguration douce du Sauron ... ^^

  • Upvote 1

Partager ce message


Lien à poster
Partager sur d’autres sites

Juste pour dire que ça fonctionne toujours !

Sauron ne devait pas remonter les Lux correctement.

Modifié par Domodial
  • Upvote 1

Partager ce message


Lien à poster
Partager sur d’autres sites

Hello,

 

J'ai quand même un problème (et oui ça serait trop beau).

Et du coup, c'est à rien ne comprendre.

 

Si j'écrit cette ligne ça fonctionne ! 

lightsMorning = {{id=544, setValue={R="132",G="0",B="255",W="255"}, onTime=300}, {id=305, setValue="turnOn", onTime=300}};

 

Si j'écrit cette ligne le module ne fonctionne pas (alors qu'il fonctionne sur la ligne plus haut)

lightsMorning = {id=305, setValue="turnOn", onTime=300};

 

Ce module est un simple On/Off et cette même ligne fonctionnait dans la version 1.6 de smart Light.

 

J'ai posé la question sur le sujet de l'auteur sur le forum officiel, c'est assez déconcertant comme bug.

 

Partager ce message


Lien à poster
Partager sur d’autres sites

Salut Domodial,

Afin de tester , je te propose de reprendre le code que j'ai posté plus haut et de remplacer dans ton script toute la partie sous le :

--------------------------------------------------------------------
--               DO NOT CHANGE THE CODE BELOW                     --
--------------------------------------------------------------------
--UPDATE FROM HERE

Par contre ,fais le dans une nouvelle scene car c'est pas le script 1.7 , il y a peut etre eu un changement dans la nouvelle version

 

Une autre idée , ca marche avec les doubles accolades car tu as 2 devices

Mais as-tu essayé avec cette syntaxe  ? :

 

lightsMorning = {{id=305, setValue="turnOn", onTime=300}};

 

 

Partager ce message


Lien à poster
Partager sur d’autres sites

Hello,

 

Avec les double accolades j'avait essayé direct, mais non ça ne fonctionne pas :(

Partager ce message


Lien à poster
Partager sur d’autres sites

Si quelqu'un peux m'envoyer le 1.6.8 ou 1.7, je suis preneur pour tester avec mes oeils ;)

Modifié par schwinny
ajout version

Partager ce message


Lien à poster
Partager sur d’autres sites

soucis désolé.

Le 20/02/2017 à 18:42, Domodial a dit :

Merci de répondre Ez3kiel :)

 

Je galère ^^ 

 

Je jongle entre ici et le forum US, mais autant dire que je ne trouve pas la panne.

 

Si je met la valeur lux (matin, soir ou nuit) à 65535 ça fonctionne très bien.

Si je met 30 Lux pour minLuxMorning et que ma valeur lux de Sauron est à 0 et bien ça me dit ça dans le log (ce qui est totalement dingue) :

 


[DEBUG] 18:28:01: Home status: Present
[DEBUG] 18:28:01: Motion status: movement
[DEBUG] 18:28:01: Triggered by: 460 Motion sensor
[DEBUG] 18:28:01: Off
[DEBUG] 18:28:01: Sensor lux: 0 is lower then minValue: 30
[DEBUG] 18:28:01: No lights set for Matin
[DEBUG] 18:28:01: © Smart Lights V1.7.0 | by Control Living, Finished
[DEBUG] 18:28:01: -------------------------------------------------------
[DEBUG] 18:28:08: Home status: Present
[DEBUG] 18:28:08: Motion status: No movement
[DEBUG] 18:28:08: Triggered by: 460 Motion sensor
[DEBUG] 18:28:08: © Smart Lights V1.7.0 | by Control Living, Finished
[DEBUG] 18:28:08: -------------------------------------------------------

Autrement voici la commande de la lampe du matin (rien de violent) :


-- Matin : Spots plafond uniquement
lightsMorning = {id=305, setValue="turnOn", onTime=300};

 

EDIT : je crois avoir un problème de syntaxe, je viens de faire le test avec un RGBW et ça fonctionne... :(

 

@Domodial

 

j'ai eu le meme soucis est en fait il faut absolument mettre des double accolades pour un dimmer..

 

Autour de la ligne 45.

RGBWDeviceType = { "com.fibaro.RGBW", "com.fibaro.colorController", "com.fibaro.FGRGBW441M"  }
BinaryDeviceType = { "com.fibaro.binarySwitch","com.fibaro.FGWP101", "turnOn"  }
DimmerDeviceType = { "com.fibaro.multilevelSwitch","com.fibaro.FGD212" }

et autour de la 140

 

lightsSleeping = {}; --lights that are triggered when Sleepstate is sleeping. 
lightsMorning  = {{id=98, setValue=60, onTime=60}};
lightsDay      = {{id=98, setValue=60, onTime=60}};
lightsEvening  = {{id=98, setValue=60, onTime=60}};
lightsNight    = {{id=98, setValue=20, onTime=60}};

 

Partager ce message


Lien à poster
Partager sur d’autres sites

×