The script works with more than one device and totally independently; it is possible to turn on a device by setting, for example, a 30-minute timer and turn on others by setting different times.
For use it, you must first create a virtual selector (I called it "TV Timer") in which we will give each level a numerical value (Ex. 30; 60; 90; 120) and will be the sleep minutes.
Then, in the script we will enter the names of the devices we want to control and the name of the selector just created (which will be the same for all devices).
Once the script has been installed in Domoticz, to use it just select a timing level and then turn on a device among those we have inserted in the script; the selector will return to off automatically and the device will turn off after the set time.
Code: Select all
local devicesToControl = {
{
name = 'TV kitchen', -- First device name
timer = 'Timer TV' -- Unique timer selector name for all devices
},
{
name = 'TV living', -- Second device name
timer = 'Timer TV' -- Unique timer selector name for all devices
},
{
name = 'TV attic', -- Third device name
timer = 'Timer TV' -- Unique timer selector name for all devices
},
{
name = 'TV bedroom', -- Fourth device name
timer = 'Timer TV' -- Unique timer selector name for all devices
},
-- You can add more devices with the unique timer selector
}
return {
on = {
devices = {
-- Add the names of the devices to monitor the status
'TV kitchen',
'TV living',
'TV attic',
'TV bedroom', -- Add more devices if needed
'Timer TV', -- Add unique timer selector name
}
},
execute = function(domoticz, device)
-- Function to set independent sleep timer for specified device
local function setTimerToSwitchOff(deviceName, timerValue)
local selectedDevice = domoticz.devices(deviceName)
selectedDevice.switchOff().afterMin(timerValue)
domoticz.log('Device ' .. deviceName .. ' will turn off after ' .. timerValue .. ' minutes.')
domoticz.notify('Scheduled turn off', 'Device ' .. deviceName .. ' will turn off after ' .. timerValue .. ' minutes.', domoticz.PRIORITY_LOW)
--Reset the selector after power on
--domoticz.devices('Timer TV').switchOff().afterSec(10)
domoticz.devices('Timer TV').switchOff()
end
-- Check if the device belongs to the list of monitored devices and if it has been turned on
local deviceToSwitchOff = nil
for _, deviceInfo in ipairs(devicesToControl) do
if deviceInfo.name == device.name and device.state == 'On' then
deviceToSwitchOff = deviceInfo
break
end
end
-- If the device belongs to the list of monitored devices and is turned on, set the sleep timer
if deviceToSwitchOff then
local timerValue = tonumber(domoticz.devices(deviceToSwitchOff.timer).state)
if timerValue and timerValue > 0 then
setTimerToSwitchOff(deviceToSwitchOff.name, timerValue)
else
--domoticz.log('Invalid timer selector value for device ' .. deviceToSwitchOff.name)
--domoticz.notify('Invalid time', 'The selected time is not valid for the device ' .. deviceToSwitchOff.name .. '. Make sure you select a number greater than 0.', domoticz.PRIORITY_HIGH)
end
end
end
}