Script LUA que recoge si un dispositivo está en uso y almacena ese dato en un contador que se llame igual que el dimmer acabado en (TMR), si existe

@damian_rguez del grupo «Domoticz a lo spain» de telegram comparte con nosotros este script de LUA que recoge si un dispositivo está en uso y almacena ese dato en un contador que se llame igual que el dimmer acabado en (TMR), si existe.

  • Creamos un dummy tipo COUNTER con el mismo nombre que el dimmer acabado en (TMR):
  • Recordar que cuando abramos el editor del script, escogemos el apartado LUA y, a continuación, seleccionamos la opción Time en el trigger que hay a la derecha para este script en concreto!
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

De este modo, quedaría un dummy del siguiente tipo:

Debéis tener en cuenta que este único script actualiza todos los dummys TMR que tengáis creados de una sola vez, sin necesidad de actualizar el script o crear más para este cometido.

Para cualquier duda o consulta tienes disponible el grupo de telegram «Domoticz a lo Spain» para entrar, ponerte cómodo y participar cuanto quieras.

Clica aquí para entrar en él

Cualquier duda, sugerencia o comentario será bien recibida en la web!