Re: Timer script to turn off/on device [Solved]
Posted: Sunday 01 November 2020 9:29
Hi Waaren,
I have tested the Cat Fence script for a few days now, and it is solid working.
Thanx for the help!
This is the final script:
I have tested the Cat Fence script for a few days now, and it is solid working.
Thanx for the help!
This is the final script:
Code: Select all
-- Script to turn OFF the cat electric fence (Stopcontacten) between 02:00-06:30
-- Or if All Phones (iDetect) are Offline for 20 minutes
return
{
on =
{
timer =
{
'at 02:00',
'at 06:30',
},
devices =
{
['iDetect-Anyone'] =
{
'at 06:30-02:00',
},
'Stopcontacten',
},
},
logging =
{
level = domoticz.LOG_ERROR, -- change to domoticz.LOG_ERROR when all OK
marker = 'Time/Phones controlled switch',
},
execute = function(dz, item)
local switch = dz.devices('Stopcontacten') -- change to name of switch
local iDetect = dz.devices('iDetect-Anyone')
local function notify(targetState)
local msg = 'Cat fence (' .. switch.name .. ') has been switched to ' .. targetState ..'!'
dz.notify(switch.name,msg, dz.PRIORITY_NORMAL,'','',dz.NSS_TELEGRAM)
dz.log(msg, dz.LOG_INFO)
end
dz.log(switch.name .. ' state: ' .. switch.state , dz.LOG_DEBUG)
dz.log(iDetect.name .. ' state: ' .. iDetect.state , dz.LOG_DEBUG)
if item == switch then
notify(switch.state)
elseif dz.time.matchesRule('at 02:00-06:29') then
if switch.state ~= 'Off' then
switch.switchOff().silent()
notify('Off')
end
elseif iDetect.state == 'Off' and switch.state ~= 'Off' then
switch.switchOff().afterMin(20)
elseif iDetect.state == 'On' then
switch.cancelQueuedCommands()
if dz.time.matchesRule('at 06:30-01:59') and switch.state ~= 'On' then
switch.switchOn().silent()
notify('On')
end
end
end
}