Lichtsteuerung funktioniert plötzlich nicht mehr...

Hallo, hatte meine Lichtsteuerung mittels 2 Motionsensoren und 2 Türsensoren ohne Schwierigkeiten am laufen. Sobald entweder Bewegung registriert oder eine Tür geöffnet wurde, ging das Licht bei entsprechender Unterschreitung des Min Lux Wertes an. Habe an dem Script nichts geändert und auch sonst wäre mir nichts bekannt, aber das Ganze funktioniert nun nicht mehr. Weder Bewegung noch Türöffnen aktiviert das Licht, im Debug Fenster der Szene erkenne ich auch nicht das diese registriert wird.

--[[
%% properties
353 value
355 value
281 value
424 value
216 value
426 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
     This code is developed by Control Living. You can use this free of charge.
     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 - 2016 Smart lights by Control Living
     Version 1.7.0
--]]

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

motionSensorID    = {353, 281, 216, 424};  -- change id for your motion sensor.
LuxSensorID       = {355, 426};  -- 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  = 5;
minLuxDay      = 10;
minLuxEvening  = 5;
minLuxNight    = 5;

--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 }
                                                RGBW set: {id=LightID, setValue={brg="20"}, onTime=timeInSeconds }
   --Philips Hue: set: {id=LightID, setValue={brg="100", sat="144", hue="14910"}, onTime=timeInSeconds }
   --brg = brightness from .. to ..
   --sat=Saturation from .. to ..
   --sue is Hue. from .. to ..
  
   --]]
   
lightsSleeping = {{id=349, setValue="turnOn", onTime=180}};
lightsMorning  = {{id=349, setValue="turnOn", onTime=180}};
lightsDay      = {{id=349, setValue="turnOn", onTime=180}};
lightsEvening  = {{id=349, setValue="turnOn", onTime=180}};
lightsNight    = {{id=349, setValue="turnOn", onTime=180}};

--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.7.0";

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

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

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" }

SupportTypes = { Binary="Binary", Dimmer="Dimmer", RGBW="RGBW", PhilipsHue="PhilipsHue" }
OffModes = {None="None", ByScene="ByScene", ManualOverride="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();
  CurrentState.lightArray  = GetTimeOfDayLightArray();
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 ) or  (CurrentState.sleepState ~= SavedState.sleepState ) 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 ManageLights( lightArray )
  local stillLightsOn = 0;
  local currentTime = os.time();
  for i = 1,#lightArray do
  	lightItem = lightArray[i];
    if ( lightItem.onTime ~= nil ) then
      lightIsOn = IsOn(lightItem);
	  lightItem.OffMode = CheckManualOverrideItem( lightItem, lightIsOn );
	  --local 
	  if ( lightItem.OffMode == OffModes.ManualOverride ) then
	      ExtraDebug("Manual override for light: [" .. lightItem.id .. "]" .. lightItem.name .. " active, not turning on");
	  else 
	      --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 ~= OffModes.ByScene) then
              lightItem.OffMode = OffModes.ByScene;
              --Setting the current value so that if the lights turn back on after dimming
              --it will be set back to the last set value by the user.
              lightItem.setValue = lightItem.currentValue;
            end
	        if (timeLeft <= 0) then
              if (lightIsOn) then
		        turnLightOff(lightItem);
              end
            else
              stillLightsOn = stillLightsOn + 1;
              if ( lightItem.lightType == SupportTypes.Dimmer ) then
		        currentValueDiv = roundit(((tonumber(lightItem.setValue) ) / (dimmDownTime)), 0);
		        currentValueNew = (lightItem.currentValue - currentValueDiv);
		        if (currentValueNew <=0 ) then currentValueNew = 0; end --2, otherwise it will not be switched of by above statement
                ActionDebug( "Dim [" .. lightItem.id .. "]'" .. lightItem.name .. "' down to:" .. tostring(currentValueNew));
  		        fibaro:call(lightItem.id, "setValue", tostring(currentValueNew));
              end
		    end
          elseif (CheckLightToBeTurnedOnAgain(lightItem)) then
            --turn light back on after dimming down, or switch off by scene.
            ExtraDebug("Turn light: " .. lightItem.id .. " back on");
            turnLightOn(lightItem);
		    lightItem.OffMode = OffModes.None;
            stillLightsOn = stillLightsOn + 1;  
          else
	        lightItem.OffMode = OffModes.None;
	        stillLightsOn = stillLightsOn + 1;   
          end
        end
    end
  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();
    CheckStatusChanges();
    local stillLightsOn = 0;  
    stillLightsOn = ManageLights( CurrentState.lightArray );
    if (stillLightsOn == 0 ) then
	  keepLightsOn = RunManualOverrideMode( CurrentState.lightArray );
      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, lightIsOn ) 
  --ExtraDebug("Manual override check for: [" .. currentItem.id .. "]" .. currentItem.name);
  if (not (lightIsOn) and currentItem.OffMode ~= OffModes.ByScene ) then
    ExtraDebug( "Manual override for light: [" .. currentItem.id .. "]" .. currentItem.name .. " active" );
	return OffModes.ManualOverride;
  elseif ( lightIsOn and currentItem.OffMode == OffModes.ManualOverride ) then
	ExtraDebug( "Manual override for light: [" .. currentItem.id .. "]" .. currentItem.name .. " cancelled" );
	return OffModes.None;
  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
    lightIsOn = IsOn(currentLightArray[i]);
    if ( CheckManualOverrideItem(currentLightArray[i], lightIsOn) == OffModes.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()
     ActionDebug("Switching off all lights"); -- in current lightarray.
     --TODO: Switch off all lights in room (not yet in all lightarrays)!
     timeOfDayArray = GetTimeOfDayLightArray();
     CallLightArray(timeOfDayArray, "off" );
	return;
end

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

function CallLightArray( lightArray, turnOnOrOff )
  if (CurrentState.sleepState == sleepStateMapping.Sleeping) then
    currentLightArrayString = "Sleeping";
  else
    currentLightArrayString = CurrentState.timeOfDay;
  end

  if (#lightArray == 0 ) then
    StandardDebug( "No lights set for " .. currentLightArrayString );
    return;
  end
  --TODO: time of day is not correct if the sleepstate is sleeping...
  if not ( CheckIfTable(lightArray, currentLightArrayString ) ) then return end
  keepLightsOn = false;
  for i = 1,#lightArray do
    if not ( CheckIfTable(lightArray[i], currentLightArrayString ) ) then break end
    local lightItem = lightArray[i];
    if (turnOnOrOff == "on" ) then
      turnLightOn(lightItem);
      if (lightItem.onTime ~= nil) then keepLightsOn = true; end
    else
	  --turnLightOff(lightItem); --Always turn off, if HC2 light status is not actual.  
      if ( IsOn( lightItem) ) then
        turnLightOff(lightItem); 
        --keepLightsOn = false;      
      end
    end
  end
  StandardDebug( "Lights turned: " .. turnOnOrOff .. " for " .. currentLightArrayString );
end

function turnLightOn( lightItem )
  if (lightItem.lightType == SupportTypes.Dimmer ) then
    fibaro:call(lightItem.id, "setValue", tostring(lightItem.setValue));
    ActionDebug( "Set: [" .. lightItem.id .. "]'" .. lightItem.name .. "' to Value: " .. lightItem.setValue );
  elseif ( lightItem.lightType == SupportTypes.RGBW )then
    ActionDebug( "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 == SupportTypes.Binary) then
    fibaro:call(lightItem.id, "turnOn");
	ActionDebug( "Turn: [" .. lightItem.id .. "]'" .. lightItem.name .. "' On");
  elseif( lightItem.lightType == SupportTypes.PhilipsHue) then
    fibaro:call(lightItem.id, "turnOn");
    if ( lightItem.setValue.brg ~= nil ) then fibaro:call(lightItem.id, "changeBrightness", lightItem.setValue.brg); end
    if ( lightItem.setValue.sat ~= nil ) then fibaro:call(lightItem.id, "changeSaturation", lightItem.setValue.sat); end
    if ( lightItem.setValue.hue ~= nil ) then fibaro:call(lightItem.id, "changeHue", lightItem.setValue.hue); end
    ActionDebug( "Turn: [" .. lightItem.id .. "]'" .. lightItem.name .. "' On");
  end
end

function turnLightOff( lightItem )
  ActionDebug("Switch off light: [" .. lightItem.id .. "]'" .. lightItem.name .."'");
  fibaro:call(lightItem.id, "turnOff"); 
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 turnLightOff(lightItem); end --      if not ( inarray ) then lightItem.OffMode = OffModes.ByScene; turnLightOff(lightItem); 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" );
    end
  else
    ExtraDebug("ExtraLightTriggerChecks failed, so no lights");
  end
  CheckPreviousLights( lightArray );
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   ( not IsOn(lightItem) ) then
          return "off";
        else
          return "on";
        end 
      end
    end
  end
  ErrorDebug("Light status unknown");
  return "Unknown";  
end

function GetTimeOfDayLightArray()
newLights = {}
 if ( CurrentState.sleepState == sleepStateMapping.Sleeping ) then
    newLights = lightsSleeping;
  elseif ( CurrentState.timeOfDay == timeOfDayMapping.Morning ) then
    newLights = lightsMorning;
  elseif ( CurrentState.timeOfDay == timeOfDayMapping.Day ) then
	newLights = lightsDay;
  elseif ( CurrentState.timeOfDay == timeOfDayMapping.Evening ) then
	newLights = lightsEvening;
  elseif ( CurrentState.timeOfDay == timeOfDayMapping.Night ) then
	newLights =  lightsNight;
  end
  SetLightValues(newLights);
  UpdateLightValues(newLights);
  return newLights;
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 IsOn(lightItem)
  if (lightItem.lightType == SupportTypes.Binary) then
    if ( lightItem.currentValue ~= 0 ) then return true; end
  elseif (lightItem.lightType == SupportTypes.Dimmer ) then
    if ( lightItem.currentValue ~= 0 ) then return true; end
  elseif( lightItem.lightType == SupportTypes.RGBW ) then
    cClr = lightItem.currentValue;
    if ( cClr.brg ~= nil and cClr.brg ~= "0" ) or (cClr.R ~= "0" or cClr.G ~= "0" or cClr.B ~= "0" or cClr.W ~= "0" ) then
      return true;
    end
  elseif (lightItem.lightType == SupportTypes.PhilipsHue) then
    return lightItem.currentValue.isOn;
  end
   --light is off. 
  return false;
end

function UpdateLightValues( currentArray )
  for i = 1,#currentArray do
    item = currentArray[i];
	newValue = fibaro:getValue(item.id, "value");
    --Binary and multilevel type
	if ( (item.lightType == SupportTypes.Binary) or (item.lightType == SupportTypes.Dimmer) ) then
      item.currentValue = tonumber(newValue) ~= nil and tonumber(newValue) or ( newValue == "true" and 1 or 0 );
    --RGBW type
    elseif ( item.lightType == SupportTypes.RGBW ) then
      color = fibaro:getValue(item.id, "color");
      c = {R="0", G="0", B="0", W="0", brg=nil }
      if (item.setValue.brg ~= nil) then
        c.brg = fibaro:getValue(item.id, "brightness"); 
      end
      c.R, c.G, c.B, c.W = color:match("([^,]+),([^,]+),([^,]+),([^,]+)");
      item.currentValue = c;
    --Philips Hue type
    elseif (item.lightType == SupportTypes.PhilipsHue) then
      ph = {isOn=false, brg="0", sat="0", hue="0", ct="0"};
      item.currentValue = ph;
      item.currentValue.isOn = fibaro:getValue(item.id, "on");
      item.currentValue.brg  = fibaro:getValue(item.id, "bri");
      item.currentValue.sat  = fibaro:getValue(item.id, "sat");
      item.currentValue.hue  = fibaro:getValue(item.id, "hue");
      item.currentValue.ct   = fibaro:getValue(item.id, "ct");
    end
  end
end

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

function GetDeviceType(itemId)
  deviceType = fibaro:getType(itemId);
  supported = true;
  if     ( idIsInList(deviceType, RGBWDeviceType) )     then deviceType = SupportTypes.RGBW;
  elseif ( idIsInList(deviceType, BinaryDeviceType) )   then deviceType = SupportTypes.Binary;
  elseif ( idIsInList(deviceType, DimmerDeviceType ) )  then deviceType = SupportTypes.Dimmer;
  elseif ( deviceType == "com.fibaro.philipsHueLight" ) then deviceType = SupportTypes.PhilipsHue;
  else
    ErrorDebug("Device type not supported, please report to Control Living: " .. deviceType );
    supported = false;
  end
  if (supported) then ExtraDebug("Supported device type: " .. deviceType); end
    return deviceType;
end

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

function CheckLightToBeTurnedOnAgain(lightItem)
  if (lightItem.OffMode ~= OffModes.ByScene) then return false; end
  if (lightItem.lightType == SupportTypes.Dimmer ) then
    if (lightItem.currentValue) < tonumber(lightItem.setValue) then return true; end
  elseif( lightItem.lightType == SupportTypes.RGBW ) then
    cClr = lightItem.currentValue;
    --TODO: not check with 0 but not equal or higher lower...
    local sClr = lightItem.setValue;
    if ( cClr.brg ~= nil and cClr.brg ~= sClr.brg ) or (cClr.R ~= sClr.R or cClr.G ~= sClr.G or cClr.B ~= sClr.B or cClr.W ~= sClr.W ) then
      return true;
    end
  elseif (lightItem.lightType == SupportTypes.PhilipsHue) then
    return not lightItem.currentValue.isOn;
  elseif (lightItem.lightType == SupportTypes.Binary) then
    return ( lightItem.currentValue == 0 ); --if 0 return true;
  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 ActionDebug( debugMessage )
  if ( showStandardDebugInfo ) then Debug("cyan", debugMessage); end
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
 -- elseif ( idIsInList( startSourceID, walkDirections ) ) then
    --StandardDebug( triggerDebug .. " Walk Direction sensor" );
    --walkDirection( startSourceID );
  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

Funktionieren die einzelnen Komponenten?
Habe das Script nicht im Einsatz aber steht es auf Automatik?

Überprüfe mal alle ID’s ob die noch stimmen.

ich hatte mal Sorgen mit einer Blockszene, die trotz richtiger Einstellungen nicht richtig getriggert wurde. Habe sie komplett identisch neu angelegt, ging dann wieder. Vielleicht mal den kompletten Code 1:1 in eine neue Lua-Szene übernehmen und diese dann löschen?

Viele Grüße
Schnuckman

Und:
HC mal neu gestartet?
Module sind alle noch erreichbar (weder als dead angezeigt, noch Bstterie leer)?

Bei mir haben sich beim Lux Wert alle Sensoren verstellt. Da war bei Dunkelheit plötzlich 84Lux. Da funktioniert das beim Script natürlich nicht mehr. Vielleicht ist ja das bei dir auch passiert.

Hallo,

überprüfe auch mal die aktuellen Werte deiner Globalen Varialblen Sleepstate, timeofday und presentstate. Wenn die nicht stimmen, macht das Skript auch nicht mehr das was es soll.
Gruß Armin

Hallo, danke für die zahlreichen Antworten. Leider hat weder ein Neustart was gebracht. Die Komponenten sind alle aktiv, d.h. einer der Motion Sensoren (Türsensor) zeigt Türe offen/geschlossen und auch Bewegung an. Wenn ich das Script manuell starte, reagiert auch das Licht. Da ich mit dem gleichen Script in weiteren Szenen (und somit den gleichen Variablen) andere Bereiche schalte, sehe ich auch hier nicht das Problem.