You're trying to fix the wrong problem. You're trying to keep the light off, whereas you better should avoid that it gets switched on.
One trick I apply often is to create a dummy switch and have a script react to the state of that switch instead of to a timer. Timers are hard to program: they fire every minute again and when they fire, you still need to check for all your conditions. Here is a script I use to switch one or more outside lights. They go on when the evening starts and off when I go to sleep. Because the script uses 2 dummy switches: "Time for Avond" and "slaaptijd" the lights are only switched when either of those switches changes state. If you choose to switch a light off after it was switched on, it will remain off until the next day (or someone switches it on). And also, when it gets dark after I go to sleep, the lights will not go on at all. Plus, it is easy to test your script because all you need to do is click the dummy switch and the script will think it just got dark, or you just went to sleep. And also, it is easy to re-use the same dummy switches in other scripts as well. Think of all the other things you can do when Domoticz knows when it gets dark or when you go to sleep.
All you need to do to use this script is: create two dummy switches:
1 - 'Time for Avond' and
2 - 'Slaaptijd'.
You can of course call them whatever you want, just change the names in the script too.
You can use timers to change state of either or both of the dummy switches, or you can use another script. For example, you could create a script to have 'Time for Avond' switch on and off by some sensor's lux level. Any way, next time when you decide to change something, you won't have to go through all of your scripts anymore: all of your scripts react to the dummy switch, and they don't know about the actual conditions when it goes off or on.
Code: Select all
-- This script can be used to control garden or porch lights: These
-- lights should go on when it gets dark and off when the sun comes
-- up. But, if you're anything like me, you think it's a waste to
-- leave them on when nobody's there to see them anyway. So you want
-- them to go off while you're asleep. This sounds simple, but your
-- sleep time may vary, plus the time the sun sets and rises varies
-- throughout the year. So much even that in some periods the sun does
-- not set before you go to sleep or it is up already before you wake
-- up, in both situations the lights never need to switch on. This
-- simple script accomplishes this task with ease. Plus it makes it
-- easy to adapt the timers to you personal life style.
--
-- How does it work? 1st you create 2 dummy On/Off switches. The names
-- of these switches you later put in the settings below this comment.
-- On one of the switches you define 2 timers:
-- 1 - On at 30 minutes before sunset
-- 2 - Off at 30 minutes after sunrise
-- On the other switch you define 4 timers:
-- 1 - On at 23:00 on work days
-- 2 - Off at 7:00 on work days
-- 3 - On at 0:00 on weekend days
-- 4 - Off at 9:00 on weekend days
-- Of course you can vary the timers on the 2nd switch depending on
-- your personal life style.
-- Now you paste this script code into a DzVents script, fill in the
-- names of the both dummy switches you created. And you fill in the
-- names of the lights you want to switch. If you want to switch more
-- than one light, simply enter multiple names, separate them with a
-- comma. Then give your script a name and click the Save button.
-- To see if it works, you don't need to wait for the evening, you
-- can simply push the buttons yourself :-). This is also a nice way
-- to override the timers if you're one day expecting someone to
-- arrive late or early.
-- Instead of using a timer for the darkness switch, you can also opt
-- to use a lux meter to actually measure darkness and feed this into
-- the script. I don't have an outdoor lux sensor, so I leave that to
-- your own imagination.
-- Specify the name of the 1st dummy switch here.
local DARK_TIMER_NAME = 'Time for Avond'
-- Specify the name of the 2nd dummy switch here.
local SLEEP_TIMER_NAME = 'Slaaptijd'
-- Specify the names of the switches for your garden or porch lights
-- here. If you want to switch multiple lights specify them like
-- { 'light1', 'light2', 'light3' }
local BUITENLAMPEN = { 'Hal: Buitenlamp' }
return {
on = {
devices = {
DARK_TIMER_NAME,
SLEEP_TIMER_NAME
}
},
execute = function(domoticz, device)
domoticz.log('Device ' .. device.name .. ' was changed', domoticz.LOG_INFO)
if device.isDevice and (DARK_TIMER_NAME == device.name or SLEEP_TIMER_NAME == device.name) then
local dark = domoticz.devices(DARK_TIMER_NAME)
local sleep = domoticz.devices(SLEEP_TIMER_NAME)
if nil == dark then
domoticz.log( "Missing device " .. DARK_TIMER_NAME .. ".", domoticz.LOG_ERROR)
elseif nil == sleep then
domoticz.log( "Missing device " .. SLEEP_TIMER_NAME .. ".", domoticz.LOG_ERROR)
else
domoticz.devices().filter(BUITENLAMPEN).forEach(
function(lamp)
if dark.bState and not sleep.bState then
if not lamp.bState then
lamp.switchOn()
end
else
if lamp.bState then
lamp.switchOff()
end
end
end
)
end
end
end
}