-- Domoticz devices, both switch device and power device for each lamp lamps = { SWITCH_Entre_SwitchA = "SWITCH_Entre_PowerA", SWITCH_Tvatt_SwitchA = "SWITCH_Tvatt_PowerA", SWITCH_Altan_SwitchA = "SWITCH_Altan_PowerA", SWITCH_Forrad_SwitchA = "SWITCH_Forrad_PowerA" } --integer converts a float into an integer function integer(x) return x<0 and math.ceil(x) or math.floor(x) end -- deviceToTime returns a device time string in a format which can be used by the os.difftime function function deviceToTime(time) return os.time { year = string.sub(time, 1, 4), month = string.sub(time, 6, 7), day = string.sub(time, 9, 10), hour = string.sub(time, 12, 13), min = string.sub(time, 15, 16), sec = string.sub(time, 18, 19) } end -- secsToClock return a string representing seconds as HH:MM:SS function secsToClock(seconds) if ((seconds == 0) or (seconds == nil) ) then return "00:00:00" else days = integer(seconds / (60 * 60 * 24)) hours = integer((seconds - days * 60 * 60 * 24) / (60 * 60)) mins = integer((seconds - days * 60 * 60 * 24 - hours * 60 * 60) / 60) secs = seconds - days * 60 * 60 * 24 - hours * 60 * 60 - mins * 60; if (days > 99) then return string.format("%03d:%02d:%02d:%02d (DDD:HH:MM:SS)", days, hours, mins, secs) elseif (days > 0) then return string.format("%02d:%02d:%02d:%02d (DD:HH:MM:SS)", days, hours, mins, secs) else return string.format("%02d:%02d:%02d (HH:MM:SS)", hours, mins, secs) end end end now = os.time() temp = os.date("*t", now) -- Calculate timestamp for this day mid night today = os.time{year=temp['year'], month=temp['month'], day=temp['day'], hour=0, min=0} sunRise = today + tonumber(timeofday['SunriseInMinutes']) * 60 sunSet = today + tonumber(timeofday['SunsetInMinutes']) * 60 if (now < sunRise) then sunSet = sunSet - 24 * 60 * 60 elseif (now > sunSet) then sunRise = sunRise + 24 * 60 * 60 end -- Get lamp state which is a dummy device switch if (otherdevices['Switch_Utebelysning'] ~= nil) then lampState = otherdevices['Switch_Utebelysning'] else lampState = 'Unknown' end -- Get latest update for dummy device switch if (otherdevices_lastupdate['Switch_Utebelysning'] ~= nil) then lampLastestUpdated = deviceToTime(otherdevices_lastupdate['Switch_Utebelysning']) else lampLatestUpdated = now end commandArray = {} if ((lampState == "Off") and (now > sunSet)) then commandArray['Switch_Utebelysning'] = "On" print ("Turning outdoor lamps on") elseif ((lampState == "On") and (now > sunRise)) then commandArray['Switch_Utebelysning'] = "Off" print ("Turning outdoor lamps off") elseif (lampState == "On") then -- Check if the lamps are on, comsuming power for deviceSwitch, DevicePower in pairs(lamps) do if (otherdevices_svalues[DevicePower] ~= nil) then watts = tonumber(otherdevices_svalues[DevicePower]) if (watts < 1) then print("Turning on "..deviceSwitch) commandArray[deviceSwitch] = "On" else print(string.format("Device %s power is %d watts", deviceSwitch, watts)) end else print("Cannot read power from device "..deviceSwitch) end end elseif (lampState == "Off") then -- Check if the lamps are off, not comsuming power for deviceSwitch, DevicePower in pairs(lamps) do if (otherdevices_svalues[DevicePower] ~= nil) then watts = tonumber(otherdevices_svalues[DevicePower]) if (watts > 1) then print("Turning off "..deviceSwitch) commandArray[deviceSwitch] = "Off" end else print("Cannot read power from device "..deviceSwitch) end end end return commandArray