Code: Select all
on = {
timer = {
'between 07:30 and sunrise'
}
}What is the correct (and easy
Moderator: leecollings
Code: Select all
on = {
timer = {
'between 07:30 and sunrise'
}
}Code: Select all
local lamp = 'lamp1'
return {
on = {
timer = {
'07:30',
'at sunrise'
}
},
execute = function( domoticz, device, triggerInfo )
local Time = require('Time')
local currenttime = Time();
if ( triggerInfo.trigger == '07:30' ) then
if ( currentTime < Time('sunrise') ) then
domoticz.devices(lamp).switchOn()
end
else
domoticz.devices(lamp).switchOff()
end
end
}
Code: Select all
local lamp = 'lamp1'
return {
on = {
timer = {
'07:30',
'at sunrise'
}
},
execute = function(domoticz, item)
if (item.trigger == 'at sunrise') then
domoticz.devices(lamp).switchOff()
return -- early exit
end
if domoticz.time.isNightTime then
domoticz.devices(lamp).switchOn()
end
end
}
Code: Select all
-- Specify the name of the 1st dummy switch here.
local SUNUP_NAME = 'Sun up'
-- Specify the name of the 2nd dummy switch here.
local LIGHTSON_NAME = 'Outside lights'
-- 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 LIGHTS = { 'lamp1' }
return {
on = {
devices = {
SUNUP_NAME,
LIGHTSON_NAME
}
},
execute = function(domoticz, device)
domoticz.log('Device ' .. device.name .. ' was changed', domoticz.LOG_INFO)
if device.isDevice and (SUNUP_NAME == device.name or LIGHTSON_NAME == device.name) then
local sunup = domoticz.devices(SUNUP_NAME)
local lightson = domoticz.devices(LIGHTSON_NAME)
if nil == sunup then
domoticz.log( "Missing device " .. SUNUP_NAME .. ".", domoticz.LOG_ERROR)
elseif nil == lightson then
domoticz.log( "Missing device " .. LIGHTSON_NAME .. ".", domoticz.LOG_ERROR)
else
domoticz.devices().filter(LIGHTS).forEach(
function(light)
if not sunup.active and lightson.active then
if not light.active then
light.switchOn()
end
else
if light.active then
light.switchOff()
end
end
end
)
end
end
end
}What do you mean by "individual randomization"? That one light can switch of/on x minutes before the other, where x is a random number from n to m?sciurius wrote: Wednesday 24 November 2021 10:46 Yes, that's another option.
rrozema's script has the advantages mentioned in the posting and I extended it to use two 'sun' periods: sunrise/sunset and sunrise-0:15/sunset+0:15. I use the first when it is raining. It controls multiple lamps, and the next challenge is to add individual randomization to the lamps.
Code: Select all
light.switchOn().withinMin(10)This is one script I use myself, only some names have been changed to fit the poster's question and to make it more clear as an example. I like my coding robust and thus I always try to put in the 'safety checks' on the type of the device and the creation of the devices. This saves myself a lot of time in debugging if something is wrong later, when I try to make changes in the code or as in this case, when I give the code to someone else and they try to run the script but forgets some preparation step or make a change/ an addition to script for their own needs.EddyG wrote: Thursday 25 November 2021 10:09 @rrozema Just curious.
Only 2 devices are used as trigger devices. Why do you check for device.isDevice and why do you check "if nil == sunup then"
Are not those checks redundant? B.t.w. my line would have been "if sunup == nil then"
And why "if not light.active then" could that not be changed in just 1 line "light.switchOn().checkFirst()"? Or leave the checkFirst all together?
Also the function "domoticz.devices().filter(LIGHTS).forEach(" takes a lot more time and memory than just a walk thru a few devices.
The first rule of optimization is: Don’t do it. The second rule of optimization (for experts only) is: Don’t do it yet.rrozema wrote: Thursday 25 November 2021 13:23There is another programming mantra for that: first make sure it works correctly, then optimise it
Users browsing this forum: No registered users and 1 guest