LUA commands

From Domoticz
Jump to: navigation, search

LUA commands and variables list for use in Domoticz

This page is created to give an overview of all the possible commands and variables in Lua that can be used in Domoticz. There is already a lot of information available on the forum, but this is scattered throughout. Next to that, the syntax is not always clear.

THIS PAGE IS STILL UNDER CONSTRUCTION


General

commandArray = {}
if (devicechanged['MyDeviceName'] == 'On' and otherdevices['MyOtherDeviceName'] == 'Off') then
   commandArray['MyOtherDeviceName1']='On'
   commandArray['MyOtherDeviceName2']='On'
   commandArray['MyOtherDeviceName3']='Off'
   commandArray['MyOtherDeviceName4']='On FOR 10' -- minutes
   commandArray['MyOtherDeviceName5']='Off RANDOM 30' -- random within x minutes
   commandArray['Scene:MyScene']='On'
   commandArray['Group:MyGroup']='Off AFTER 30' -- seconds
   commandArray['SendNotification']='subject#body#extraData#priority#sound'
   commandArray['OpenURL']='www.yourdomain.com/api/movecamtopreset.cgi' 
   commandArray['SendEmail']='subject#body#to'
   commandArray['Variable:MyVar']='Some value'
   commandArray['SetSetPoint:MySetPointIdx']='20.5'
   commandArray['UpdateDevice']='idx|nValue|sValue' -- for updating Dummy devices e.g. '96|0|Hello World'
end
return commandArray

Update multiple devices in one pass

commandArray = {}
local function update(idx, value1, value2)
    local cmd = string.format("%d|0|%.2f;%.2f", idx, value1, value2)
    table.insert (commandArray, { ['UpdateDevice'] = cmd } )
end
 
update (16, 320, 0)
update (19, 4, 25)
return commandArray

Debugging

Function to dump all variables supplied to the script

function LogVariables(x,depth,name)
    for k,v in pairs(x) do
        if (depth>0) or ((string.find(k,'device')~=nil) or (string.find(k,'variable')~=nil) or 
                         (string.sub(k,1,4)=='time') or (string.sub(k,1,8)=='security')) then
            if type(v)=="string" then print(name.."['"..k.."'] = '"..v.."'") end
            if type(v)=="number" then print(name.."['"..k.."'] = "..v) end
            if type(v)=="boolean" then print(name.."['"..k.."'] = "..tostring(v)) end
            if type(v)=="table" then LogVariables(v,depth+1,k); end
        end
    end
end
-- call with this command
LogVariables(_G,0,'');

Date and Time

Funtion to calculate the time difference

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

Time of day

Check if it is daytime or nighttime

Example:

if (timeofday['Daytime']) then ...
 
or
 
if (timeofday['Nighttime']) then ...

Devices