分页: 1 / 1

请教如何在LUA内执行多段开关内的选项 (LUA多段开关设置及时间判断)

发表于 : 周二 8月 08, 2017 20:38
ylfzl
我想请教一下,如何在LUA内实现用小米网关+小米门窗感应实现,门打开10分钟未关上报警的功能,我参照了WIKI内的10分钟未关车库报警的范例:

不知道语句要怎么调用小米网关内的报警。
以下是报警开关
2.png
2.png (25.83 KiB) 查看 43796 次

以下代码是测试通过的,当门窗打开超为10分钟可以开灯,二个原句我注释不。

先感谢大神指教如何调用多段开关内的选项。谢谢

代码: 全选

t1 = os.time()
s = otherdevices_lastupdate['小米门窗传感器']
-- returns a date time like 2013-07-11 17:23:12

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)

commandArray = {}

t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
difference = (os.difftime (t1, t2))
if (otherdevices['小米门窗传感器'] == 'Open' and difference > 600) then
  --  commandArray['SendNotification']='Garage door alert#The garage door has been open for more than 10 minutes!'
  --  commandArray['小米门铃'] ='Knock on door #The garage door has been open for more than 10 minutes!'
commandArray['客厅灯4号'] = "On"
end
return commandArray

Re: 请教如何在LUA内执行多段开关内的选项

发表于 : 周二 8月 08, 2017 20:57
ylfzl
自问自答:用 “ ='Set Level 20' 就可以实现了。

想要再加一条,在晚上22点以后才执行这个提醒要如何写判断?

Re: 请教如何在LUA内执行多段开关内的选项

发表于 : 周三 8月 09, 2017 10:44
DT27
ylfzl 写了: 周二 8月 08, 2017 20:57 自问自答:用 “ ='Set Level 20' 就可以实现了。

想要再加一条,在晚上22点以后才执行这个提醒要如何写判断?
Lua里获取当前时间(时间戳格式,例如1502246047):

代码: 全选

t1 = os.time()
获取当前日期及时间(输出:Wed Aug 9 10:37:16 2017):

代码: 全选

d1 = os.date()
获取当前是几点(输出:10):

代码: 全选

h1 = os.date("%H")
所以你想要22点以后执行,只需要加入时间条件(时间获取后是字符串类型,与数字进行对比时要用tonumber转为数字):

代码: 全选

if (otherdevices['小米门窗传感器'] == 'Open' and difference > 600 and tonumber(h1) >= 22) then

具体Lua日期时间格式请参考:lua Date和Time

Re: 请教如何在LUA内执行多段开关内的选项 (LUA多段开关设置及时间判断)

发表于 : 周四 8月 10, 2017 00:08
ylfzl
感谢如此详尽的回复。我试试看。

Re: 请教如何在LUA内执行多段开关内的选项 (LUA多段开关设置及时间判断)

发表于 : 周二 10月 17, 2017 07:40
chengka3463
s = otherdevices_lastupdate['小米门窗传感器']
我需要赋值多个设备怎么弄?还是一定要另开个同样lua

Re: 请教如何在LUA内执行多段开关内的选项 (LUA多段开关设置及时间判断)

发表于 : 周二 10月 17, 2017 07:48
chengka3463
时间范围怎么写?比如晚上19点到22点之间。

Re: 请教如何在LUA内执行多段开关内的选项 (LUA多段开关设置及时间判断)

发表于 : 周二 10月 17, 2017 11:05
Admin
chengka3463 写了: 周二 10月 17, 2017 07:48 时间范围怎么写?比如晚上19点到22点之间。

代码: 全选

h1 = otherdevices_lastupdate['mydevice']
if (otherdevices['小米门窗传感器'] == 'Open' and tonumber(h1) >= 17 and tonumber(h1) < 22) then
多个设备直接

代码: 全选

commandArray['客厅灯1号'] = "On"
commandArray['客厅灯2号'] = "Off"
commandArray['客厅灯3号'] = "On"

Re: 请教如何在LUA内执行多段开关内的选项 (LUA多段开关设置及时间判断)

发表于 : 周二 10月 17, 2017 11:30
chengka3463
多个设备是需要时间判断的设备,S赋值。我是主要是判断设备在不在线,domo的ping苹果设备支持不好,加个时间判断,超过10分钟持续离线判断为不在线。感谢。就是将s = otherdevices_lastupdate['小米门窗传感器'],s指定多个设备,在一个lua里同时处理判断时间差。小白问题多,理解差,多包涵 :lol:

Re: 请教如何在LUA内执行多段开关内的选项 (LUA多段开关设置及时间判断)

发表于 : 周二 10月 17, 2017 18:13
blindlight
s随便你取啊
a= otherdevices_lastupdate['其他设备1']
b= otherdevices_lastupdate['其他设备2']
c= otherdevices_lastupdate['其他设备3']

Re: 请教如何在LUA内执行多段开关内的选项 (LUA多段开关设置及时间判断)

发表于 : 周三 10月 18, 2017 19:16
chengka3463
-- 获取设备上次更新时间
s1 = otherdevices_lastupdate['电视IP']
s2 = otherdevices_lastupdate['yinde-iPhone']
s3 = otherdevices_lastupdate['chengkadeiPhone']

-- 判断时间差
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

-- 关
if(otherdevices['电视IP']=='Off' and otherdevices['TV'] == 'On' and timedifference(s1) > 300 ) then
commandArray['TV']='Off'
print('电视机离线啦')
end

if(otherdevices['yinde-iPhone']=='Off' and otherdevices['女主'] == 'On' and timedifference(s2) > 3000 ) then
commandArray['女主']='Off'
print('女主不在家')
end

if(otherdevices['chengkadeiPhone']=='Off' and otherdevices['男主'] == 'On' and timedifference(s3) > 3000 ) then
commandArray['男主']='Off'
print('男主不在家')
end

帮忙看看这样写对不对

Re: 请教如何在LUA内执行多段开关内的选项 (LUA多段开关设置及时间判断)

发表于 : 周三 10月 18, 2017 20:11
blindlight
return后面还要有end?
确定是这样的?

Re: 请教如何在LUA内执行多段开关内的选项 (LUA多段开关设置及时间判断)

发表于 : 周三 10月 18, 2017 20:47
chengka3463
https://www.domoticz.com/wiki/Smart_Lua_Scripts
抄的官方wiki 应该没问题吧,就是后面的时间判断 多个设备的有没问题?

Re: 请教如何在LUA内执行多段开关内的选项 (LUA多段开关设置及时间判断)

发表于 : 周四 10月 19, 2017 09:33
blindlight
chengka3463 写了: 周三 10月 18, 2017 20:47 https://www.domoticz.com/wiki/Smart_Lua_Scripts
抄的官方wiki 应该没问题吧,就是后面的时间判断 多个设备的有没问题?
不知道 边调边试吧 调程序很少一次成功的