just finished the first version of the "script_time_switch-timer" LUA script.
Its a modified version of "Time Script to Turn a Light Off"
on "https://www.domoticz.com/wiki/Smart_Lua_Script"
With this script:
if you create a "General, Counter incremental" with a name of a switch prefixed by " (TMR)" this will count
when your switch is ON.
Domoticz Installation instructions:
- Save this script as "script_time_switch-timer.lua" on "Domoticz/scripts/lua" folder
- Add a new "General, Counter incremental" with a name of a switch prefixed by " (TMR)" for each switch you want to time.
example:
Timed switch: Timer device: Hope you like it!!
Code: Select all
--
-- Switch Timer for Domoticz via LUA Script
--
-- Ycahome, 2017
-- https://www.domoticz.com/forum
--
--
function timedifference(s)
year = string.sub(s, 1, 4)
month = string.sub(s, 6, 7)
day = string.sub(s, 9, 10)
hour = string.sub(s, 12, 13)
minutes = string.sub(s, 15, 16)
seconds = string.sub(s, 18, 19)
t1 = os.time()
t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
difference = os.difftime (t1, t2)
return difference
end
function trim1(s)
return (s:gsub("^%s*(.-)%s*$", "%1"))
end
commandArray = {}
debuglevel = 0
local function update(idx, value1, value2)
local cmd = idx.."|"..value1.."|"..value2
--local cmd = string.format("%d|0|%f;%f", idx, value1, value2)
table.insert (commandArray, { ['UpdateDevice'] = cmd } )
end
for i, v in pairs(otherdevices) do
timeon = 240
tc = tostring(i)
--v = i:sub(1,4)
v = string.sub(i, -5)
if (v == '(TMR)') then
if (debuglevel > 0) then print("Timer Device Detected:".. i) end
tc2 = string.len(i)
--print("Len:".. tc2)
vname = trim1(tostring(string.sub(i,1,tc2-5)))
if (otherdevices[vname] == 'On') then
if (debuglevel > 0) then print("Timed Device:".. vname) end
print("Timed Device Detected ON:" ..vname)
if (debuglevel > 0) then print("Timer Dev Idx:".. otherdevices_idx[i]) end
if (debuglevel > 0) then print("Timed Dev Idx:".. otherdevices_idx[vname]) end
if (debuglevel > 0) then print("Current Value:".. otherdevices[i]) end
newvalue = otherdevices[i] + 1
--newvalue = 1
if (debuglevel > 0) then print("New Value:".. newvalue) end
--commandArray['UpdateDevice'] = otherdevices_idx[i]..'|1|'..newvalue
update (otherdevices_idx[i], 1, newvalue)
end
end
if (v == 'PIR----') then
difference = timedifference(otherdevices_lastupdate[tc])
if (difference > timeon and difference < (timeon + 60)) then
tempdiff = tostring(difference)
c = i:sub(4)
tempmessage = c.." Light Off - after at least " .. (timeon+1) .. "secs up - actually - " .. tempdiff .. "seconds"
print(tempmessage)
commandArray[c] = 'Off'
end
end
end
return commandArray