LUA命令

来自Domoticz
Dt27讨论 | 贡献2017年7月1日 (六) 11:27的版本 →‎用于计算时差的方法
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)

Domoticz中的Lua命令及变量列表

创建此页面以概述可在Domoticz中使用的Lua中的所有可能的命令和变量。 论坛上已经有很多资料可供参考,但是都是分散的而且语法并不是很清楚。

THIS PAGE IS STILL UNDER CONSTRUCTION


基本

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

在一次提交中更新多个设备

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

调试

用于列出所有可用变量的方法

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,'');

日期和时间

用于计算时差的方法

--timedifference("2017-05-27 14:43:54")
function timedifference(timestamp)
  y, m, d, H, M, S = timestamp:match("(%d+)-(%d+)-(%d+) (%d+):(%d+):(%d+)")
  difference = os.difftime(os.time(), os.time{year=y, month=m, day=d, hour=H, min=M, sec=S})
  return difference
end

用法

s = otherdevices_lastupdate['mydevice']
if(otherdevices['mydevice']=='On' and timedifference(s) > 600) then
    commandArray['mydevice']='Off'
end

白天还是晚上

检查当前时间是白天还是晚上

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

或者

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

设备