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

文档:
https://en.domoticz.cn/wiki/DzVents:_ne ... _scripting
回复
ylfzl
帖子: 10
注册时间: 周日 7月 30, 2017 14:38

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

帖子 ylfzl »

我想请教一下,如何在LUA内实现用小米网关+小米门窗感应实现,门打开10分钟未关上报警的功能,我参照了WIKI内的10分钟未关车库报警的范例:

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

以下代码是测试通过的,当门窗打开超为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
ylfzl
帖子: 10
注册时间: 周日 7月 30, 2017 14:38

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

帖子 ylfzl »

自问自答:用 “ ='Set Level 20' 就可以实现了。

想要再加一条,在晚上22点以后才执行这个提醒要如何写判断?
头像
DT27
帖子: 345
注册时间: 周四 3月 30, 2017 08:54
Gender:

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

帖子 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
ylfzl
帖子: 10
注册时间: 周日 7月 30, 2017 14:38

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

帖子 ylfzl »

感谢如此详尽的回复。我试试看。
chengka3463
帖子: 37
注册时间: 周四 6月 01, 2017 19:32

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

帖子 chengka3463 »

s = otherdevices_lastupdate['小米门窗传感器']
我需要赋值多个设备怎么弄?还是一定要另开个同样lua
chengka3463
帖子: 37
注册时间: 周四 6月 01, 2017 19:32

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

帖子 chengka3463 »

时间范围怎么写?比如晚上19点到22点之间。
头像
Admin
网站管理员
帖子: 118
注册时间: 周六 2月 25, 2017 12:47
Gender:

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

帖子 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"
chengka3463
帖子: 37
注册时间: 周四 6月 01, 2017 19:32

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

帖子 chengka3463 »

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

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

帖子 blindlight »

s随便你取啊
a= otherdevices_lastupdate['其他设备1']
b= otherdevices_lastupdate['其他设备2']
c= otherdevices_lastupdate['其他设备3']
chengka3463
帖子: 37
注册时间: 周四 6月 01, 2017 19:32

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

帖子 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

帮忙看看这样写对不对
blindlight
帖子: 98
注册时间: 周四 3月 30, 2017 00:03

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

帖子 blindlight »

return后面还要有end?
确定是这样的?
chengka3463
帖子: 37
注册时间: 周四 6月 01, 2017 19:32

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

帖子 chengka3463 »

https://www.domoticz.com/wiki/Smart_Lua_Scripts
抄的官方wiki 应该没问题吧,就是后面的时间判断 多个设备的有没问题?
blindlight
帖子: 98
注册时间: 周四 3月 30, 2017 00:03

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

帖子 blindlight »

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