local temp1 = tonumber(fibaro:getValue(85, "value")) 
local temp2 = tonumber(fibaro:getValue(86, "value"))
local temp3 = tonumber(fibaro:getValue(87, "value"))
local tempOUT = tonumber(fibaro:getValue(3, "Temperature"))
local IDCooler = 99   
local IDCond = 100 
local IDCondHeat = 102 
local IDFloor = 23 
local IDHeat = 24 
local tempset = fibaro:getGlobalValue("tempSet")
local workmode = fibaro:getGlobalValue("Workmode")
local tempInside = (temp1 + temp2 + temp3) / 3
local date = os.date("!*t", now)
local currtime = date.hour*60 + date.min 
local d = tempset - tempInside
local w = tempset - tempOUT
local cHeating = "on"
local winStatus = "closed"
print("Starting Climate Control")
print("t   = "..tempInside..",  t  = "..tempset)
print("Workmode: "..workmode..",  Windows: "..winStatus,",  Central Heating: "..cHeating)
if (fibaro:getGlobalValue("Workmode1") == "At work") 
then 
    
	if (tempInside < 18) 
    then 
    	if (cHeating == "on")
    	then 
    		print(" ")
      		fibaro:call(IDHeat, "turnOn")
      		if(Isventnow == "no")
        	then
      			fibaro:call(IDCooler, "turnOff")
      		end
            fibaro:call(IDCondHeat, "turnOff")
      		fibaro:call(IDCond, "turnOff")
      		fibaro:call(IDFloor, "turnOff")
      	elseif (windows == "closed")
      	then 
     		print("  ")
      		fibaro:call(IDCondHeat, "turnOn")
      		fibaro:call(IDCond, "turnOn")
      		fibaro:call(IDFloor, "turnOff")
     		if(Isventnow == "no")
        	then
      			fibaro:call(IDCooler, "turnOff")
      		end      
      		fibaro:call(IDHeat, "turnOff")
      	end
    
    elseif (tempInside > 21)
    then 
		if(fibaro:getValue(IDHeat, value) >0)
		then
    		print(" ")
    		fibaro:call(IDHeat, "turnOff")
		else
        	fibaro:call(IDCondHeat, "turnOn")
      		fibaro:call(IDCond, "turnOn")
      		fibaro:call(IDFloor, "turnOff")
            if(Isventnow == "no")
        	then
      			fibaro:call(IDCooler, "turnOff")
      		end
		end
    end 
elseif d < 1 and d > -1
then
  	print(" ")
    
	fibaro:call(IDCondHeat, "turnOff")
    fibaro:call(IDCond, "turnOff")
	
    if(Isventnow == "no")
    then
		fibaro:call(IDCooler, "turnOff")
    end
	
	fibaro:call(IDHeat, "turnOff")
	
	fibaro:call(IDFloor, "turnOff")
elseif (cHeating == "on")
then 
    if (d >= 1)
    then  
        if (d <= 3)
        then 
    		print("  ")
            
            fibaro:call(IDFloor, "turnOn")		
            fibaro:call(IDCondHeat, "turnOff")
            fibaro:call(IDCond, "turnOff")
            fibaro:call(IDHeat, "turnOff")
            fibaro:call(IDCooler, "turnOff")
        elseif (d > 3)
        then
    		print("    ")
			
    		fibaro:call(IDFloor, "turnOn")
            
    		fibaro:call(IDHeat, "turnOn")
			fibaro:call(IDCondHeat, "turnOff")
      		fibaro:call(IDCond, "turnOff")
      	    if(Isventnow == "no")
    		then
				fibaro:call(IDCooler, "turnOff")
    		end
        end
    elseif (d <= -1)
    then 
  		print(" ")
        
  		if(Isventnow == "no")
   	 	then
			fibaro:call(IDCooler, "turnOn")
    	end
        lastCondition = currTime
    end
elseif (cHeating == "off")
then 
    if (d >= 1)
    then
        if (w <= 0)
        then
    		print(" ")
            
    		if(Isventnow == "no")
      		then
				fibaro:call(IDCooler, "turnOn")
    		end
    		fibaro:call(IDCondHeat, "turnOff")    
			lastCondition = currTime
        elseif (d < 4)
        then 
    		print(" ")
			
    		fibaro:call(IDCondHeat, "turnOff")
      		if(Isventnow == "no")
    		then
				fibaro:call(IDCooler, "turnOn")
    		end
        	lastCondition = currTime
        elseif (winStatus == "closed")
        then
    		print("   ")
			
    		fibaro:call(IDCondHeat, "turnOn")
      		fibaro:call(IDCond, "turnOn")    
      		if(Isventnow == "no")
    		then
				fibaro:call(IDCooler, "turnOff")
    		end
        end
    elseif (d <= -1)
    then 
		if(w >= 0)
		then
	    	if(d > -4)
	    	then
     			print(" ")
	        	
                if(Isventnow == "no")
                then
                    fibaro:call(IDCooler, "turnOn")
                end
    			fibaro:call(IDCondHeat, "turnOff")
      			fibaro:call(IDCond, "turnOff")
	    	elseif (winStatus == "closed")
        	then
      			print(" ")
	        	
      			fibaro:call(IDCond, "turnOn")
      			fibaro:call(IDCondHeat, "turnOff")
                if(Isventnow == "no")
                then
                    fibaro:call(IDCooler, "turnOff")
                end
	    	end
		elseif (winStatus == "closed")
      	then
    		print(" ")
	    	
   			fibaro:call(IDCond, "turnOn")
      		fibaro:call(IDCondHeat, "turnOff")    
            if(Isventnow == "no")
            then
                fibaro:call(IDCooler, "turnOff")
            end
		end
    end
end