Got it working on my system. Can you check what your result is on yours ?
Code: Select all
--[[
Motion sensor "PIR chodba" Idx 27
Yeelight ceiling "Světlo chodba" Idx 14 and "Světlo hala" Idx 19
I need to be in the time period between 23:00 and 6:30 they turned on the lights, but only if not already switched on in some other way. Five minutes without moving to turn off again. But I want them to shine through the night mode. Which in the LUA script is set as follows:
local runcommandbaby = "sudo echo -ne '{\\"id\\":1,\\"method\\":\\"set_scene\\", \\"params\\":[\\"bright\\", 1, 500]}\\r\\n"
]]--
local nightTime = {"at 21:00-06:30" }
return {
on = {
devices = {
["PIR chodba"] = nightTime , -- PIR need to react during nightTime
["XButton-1"] = nightTime ,
"Světlo chodba","Světlo hala" , -- controlling selector devices
},
timer = {
"every 5 minutes between 23:00 and 06:30",
"at 06:32",
},
},
logging = {
level = domoticz.LOG_DEBUG,
marker = "PIRTest"
},
execute = function(dz, item)
local function logWrite(str,level) -- Support function for shorthand debug log statements
dz.log(tostring(str),level or dz.LOG_DEBUG)
end
local chodba = dz.devices("Světlo chodba")
local hala = dz.devices("Světlo hala")
local PIR = dz.devices("PIR chodba")
local function getLevel( levels, search ) -- support function to get level (0,10,20 etc.. based on levelNames)
for index,levelName in ipairs(levels) do
logWrite("getLevel, index: " .. index .. " ==>> " .. levelName .. " " .. search )
if search == levelName then return ( ( index - 1 ) * 10 ) end
end
return nil
end
local function setLight(t,key) -- This function does the actual work
if not t[key] then
logWrite("No such defined setting (" .. key .. ")" ,dz.LOG_ERROR)
return
else
logWrite("Found " .. key .." ==> " .. t[key] )
local osCommand = "sudo echo -ne " .. t[key] .. "\\r\\\\n | " .. t.executable .. t.parm .. t.address .. t.port ..";"
logWrite("Sending command: \r\n" .. osCommand)
os.execute(osCommand)
local osCommand = "sudo echo -ne " .. t[key] .. "\\r\\\\n | " .. t.executable .. t.parm .. t.address .. t.port .. "; > fff.log"
os.execute(osCommand)
logWrite("Calculated level: " .. getLevel(t.levels,key))
t.device.switchSelector(getLevel(t.levels,key)).silent() -- update the selector to reflect the new level but prevent loop
end
end
local chodbaControls = -- Table with all relevant settings
{
device = chodba,
Off = '{\\"id\\":1,\\"method\\":\\"set_power\\", \\"params\\":[\\"off\\", \\"smooth\\", 500]}\\',
["Bright 25%"] = '{\\"id\\":1,\\"method\\":\\"set_scene\\", \\"params\\":[\\"bright\\", 25, 500]}\\',
["Bright 50%"] = '{\\"id\\":1,\\"method\\":\\"set_scene\\", \\"params\\":[\\"bright\\", 50, 500]}\\',
["Bright 75%"] = '{\\"id\\":1,\\"method\\":\\"set_scene\\", \\"params\\":[\\"bright\\", 75, 500]}\\',
Sunrise = '{\\"id\\":1,\\"method\\":\\"set_scene\\", \\"params\\":[\\"cf\\",2,2,\\"50,2,4000,75,900000,2,4000,1\\"]}\\',
Sunset = '{\\"id\\":1,\\"method\\":\\"set_scene\\", \\"params\\":[\\"cf\\",24,2,\\"4000,2,4000,30,7000,2,4000,10,8000,2,4000,1\\"]}\\',
["4-7-8"] = '{\\"id\\":1,\\"method\\":\\"set_scene\\", \\"params\\":[\\"cf\\",24,2,\\"4000,2,400[0,30,7000,2,4000,10,8000,2,4000,1\\"]}\\',
Baby = '{\\"id\\":1,\\"method\\":\\"set_scene\\", \\"params\\":[\\"bright\\", 1, 500]}\\',
levels = {"Off","Bright 25%","Bright 50%","Bright 75%","Sunrise","Sunset","4-7-8","Baby"},
executable = "/opt/bin/ncat",
parm = " -w2 ",
address = "192.168.0.112 ",
port = " 55443",
background = " &"
}
local halaControls =
{
device = hala,
Off = '{\\"id\\":1,\\"method\\":\\"set_power\\", \\"params\\":[\\"off\\", \\"smooth\\", 500]}\\',
["Bright 25%"] = '{\\"id\\":1,\\"method\\":\\"set_scene\\", \\"params\\":[\\"bright\\", 25, 500]}\\',
["Bright 50%"] = '{\\"id\\":1,\\"method\\":\\"set_scene\\", \\"params\\":[\\"bright\\", 50, 500]}\\',
["Bright 75%"] = '{\\"id\\":1,\\"method\\":\\"set_scene\\", \\"params\\":[\\"bright\\", 75, 500]}\\',
Sunrise = '{\\"id\\":1,\\"method\\":\\"set_scene\\", \\"params\\":[\\"cf\\",2,1,\\"50,2,4000,1,900000,2,4000,100\\"]}\\',
Sunset = '{\\"id\\":1,\\"method\\":\\"set_scene\\", \\"params\\":[\\"cf\\",2,2,\\"50,2,4000,75,900000,2,4000,1\\"]}\\',
["4-7-8"] = '{\\"id\\":1,\\"method\\":\\"set_scene\\",\\"params\\":[\\"cf\\",24,2,\\"4000,2,4000,30,7000,2,4000,10,8000,2,4000,1\\"]}\\',
Baby = '{\\"id\\":1,\\"method\\":\\"set_scene\\", \\"params\\":[\\"bright\\", 1, 500]}\\',
levels = {"Off","Bright 25%","Bright 50%","Bright 75%","Sunrise","Sunset","4-7-8","Baby"},
executable = "/opt/bin/ncat",
parm = " -w2 ",
address = "192.168.0.105 ",
port = " 55443",
background = " &"
}
if item.isTimer and dz.time.matchesRule("at 06:32") then -- switch Off in the morning
setLight(chodbaControls,"Off")
setLight(halaControls,"Off")
elseif item.isTimer and PIR.lastUpdate.minutesAgo > 5 and PIR.lastUpdate.minutesAgo < 15 then -- No activity during the night ?
if hala.level ~= halaControls.BabyLevel then setLight(halaControls,"Baby") end -- switch to baby mode if not already there
if chodba.level ~= chodbaControls.BabyLevel then setLight(chodbaControls,"Baby") end
elseif item.name == "Světlo chodba" then -- triggered by manual selector change
setLight(chodbaControls,item.levelName)
elseif item.name == "Světlo hala" then
setLight(halaControls,item.levelName) -- triggered by manual selector change
elseif item.name == "PIR chodba" and item.isActive then
setLight(halaControls,"Baby") -- PIR see something
setLight(chodbaControls,"Baby")
else
setLight(halaControls,"Off")
end
-- test / debug only
if _G.logLevel == dz.LOG_DEBUG then
for key, value in pairs(halaControls) do
-- logWrite(key .." => " .. tostring(value))
end
end
end
}