求大神帮忙写一个lua脚本,小米人体感应器关联书房灯。

文档:
https://en.domoticz.cn/wiki/DzVents:_ne ... _scripting
回复
pizixiaotian
帖子: 29
注册时间: 周三 4月 05, 2017 22:15

求大神帮忙写一个lua脚本,小米人体感应器关联书房灯。

帖子 pizixiaotian »

萌新一个,只会用简单的block,我现在用小米的人体感应器关联了书房灯。因为block的if条件里没有一个状态保持一段时间的条件。所以想请大神帮我写个lua脚本。如果人体感应器=on并且书房灯=off,则设置书房灯=on,如果人体感应区=off并且off状态保持10分钟并且书房灯=on,则设置书房灯=off。用block没法设off状态保持10分钟的条件。求大神帮忙。
头像
DT27
帖子: 345
注册时间: 周四 3月 30, 2017 08:54
Gender:

Re: 求大神帮忙写一个lua脚本。

帖子 DT27 »

可以参考wiki 当车库门打开超过10分钟时发出警告

在domoticz/scripts/lua目录中新建lua文件,文件名 script_time_xxxxxx.lua,以此规则命名的lua脚本Domoticz会每分钟自动运行一次。
具体代码我写了一个,没测试过,你试试,有问题直接回复:

代码: 全选

-- script_time_studylight.lua
print('开始执行书房灯控制脚本')

commandArray = {}

-- 开灯
if (otherdevices['人体感应器'] == 'On' and otherdevices['书房灯'] == 'Off') then
	commandArray['书房灯']='On'
	print('人体感应器On,打开书房灯')
end

-- 关灯逻辑

-- 获取设备上次更新时间,类似:2017-05-27 08:50:51
s = otherdevices_lastupdate['人体感应器']

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

if (otherdevices['人体感应器'] == 'Off' and otherdevices['书房灯'] == 'On' and timedifference(s) > 600 and timedifference(s) < 700) then
	commandArray['书房灯']='Off'
	print('人体感应器Off超过十分钟,关闭书房灯')
end

return commandArray
pizixiaotian
帖子: 29
注册时间: 周三 4月 05, 2017 22:15

Re: 求大神帮忙写一个lua脚本。

帖子 pizixiaotian »

谢谢DT27,如果在条件里面再加个时间段呢,比如下午17:00到第二天早上8:00。条件里的代码应该怎么写
头像
DT27
帖子: 345
注册时间: 周四 3月 30, 2017 08:54
Gender:

Re: 求大神帮忙写一个lua脚本。

帖子 DT27 »

pizixiaotian 写了: 周六 5月 27, 2017 14:01 谢谢DT27,如果在条件里面再加个时间段呢,比如下午17:00到第二天早上8:00。条件里的代码应该怎么写
照明的时间控制建议直接用Daytime跟Nighttime,Domoticz根据日出日落时间自动判断的:LUA命令:白天还是晚上

就是直接把

代码: 全选

if (otherdevices['人体感应器'] == 'On' and otherdevices['书房灯'] == 'Off') then
改为

代码: 全选

if (timeofday['Nighttime'] and otherdevices['人体感应器'] == 'On' and otherdevices['书房灯'] == 'Off') then
这样太阳落山后才会开灯,关灯逻辑不变。
pizixiaotian
帖子: 29
注册时间: 周三 4月 05, 2017 22:15

Re: 求大神帮忙写一个lua脚本。

帖子 pizixiaotian »

谢谢!我晚上回家试试。
回复