Page 1 of 1

Turn on lights when it is dark between 6:30 and 7:00

Posted: Friday 18 October 2019 7:38
by dutchronnie
I use this script to switch on and off some lights in a group. But nothing happens.
Maybe some one can help me to get it working.

What i want is this:
- Turn on the lights in the evening when it becomes dark and turn them off on 10:30.

- Turn on the lights on 6:30 in the morning, but only when it is dark.

I think is is dark from 12 minutes before sunset to 10 minutes after sunrise

In my opinion this should work, but nothing happens in the morning.
I hope someone can tell me what i am doing wrong.

Code: Select all

return { on = 
			{ timer = {   
				"12 minutes before sunset", 
                            	"10 minutes after sunrise",  
                            	"at 22:30",
                            	"at 06:30",
                        }},

    execute = function(dz)
        local ochtend_verlichting   = dz.groups("Ochtend verlichting")       	-- group used to switch multiple lights/switches
        local checkTimeInMinutes    = 6 * 60 + 30                           		-- 6:30   
        
        if dz.time.matchesRule("12 minutes before sunset") then
            ochtend_verlichting.switchOn()
        elseif dz.time.matchesRule("at 6:30") then
            if dz.time.sunriseInMinutes < checkTimeInMinutes then
                ochtend_verlichting.switchOn()
            end    
        elseif dz.time.matchesRule("10 minutes after sunrise") or dz.time.matchesRule("at 22:30") then
            ochtend_verlichting.switchOff()
        end
    end
}

Re: Turn on lights when it is dark between 6:30 and 7:00

Posted: Friday 18 October 2019 8:55
by waaren
dutchronnie wrote: Friday 18 October 2019 7:38 I use this script to switch on and off some lights in a group. But nothing happens.
I hope someone can tell me what i am doing wrong.
In my timezone dz.time.sunriseInMinutes = 490. Because checkTimeInMinutes = 390 the ochtend_verlichting.switchOn() will not be executed.

Re: Turn on lights when it is dark between 6:30 and 7:00

Posted: Friday 18 October 2019 9:34
by dutchronnie
Oke,

So i have to change:
if dz.time.sunriseInMinutes < checkTimeInMinutes then

in
if dz.time.sunriseInMinutes > checkTimeInMinutes then

Then it should work.
Thanks, i wil try tommorow

Re: Turn on lights when it is dark between 6:30 and 7:00

Posted: Friday 18 October 2019 13:58
by waaren
dutchronnie wrote: Friday 18 October 2019 9:34 So i have to change:
if dz.time.sunriseInMinutes < checkTimeInMinutes then
in
if dz.time.sunriseInMinutes > checkTimeInMinutes then
You could do that but why not change

Code: Select all

        elseif dz.time.matchesRule("at 6:30") then
            if dz.time.sunriseInMinutes > checkTimeInMinutes then
                ochtend_verlichting.switchOn()
            end  
to

Code: Select all

        elseif dz.time.matchesRule("at 6:30 at nighttime") then
            ochtend_verlichting.switchOn()

Re: Turn on lights when it is dark between 6:30 and 7:00

Posted: Wednesday 23 October 2019 23:18
by BOverdevest
Hi, below my solution for managing an outside light.
Innitially, i used a script running every minute using programmed time blocks and a LUX number from a Weather station. (when LUX < certain value it is getting dark, much better then using sunset or sunrise).

Because i also want to turn on the light when the door opens (only when it is dark) or when a PIR triggers (only when it is dark), i moved to the code below
The trick is in the order of the "if .. then". Took some time to get it perfect...

**edit: code updated post Waaren's review, see next posting**

Code: Select all

    local DeviceName    = "Voordeur Lamp"
    local TijdAan       = "6:30" -- Lamp aan in de ochtend
    local TijdUit       = "21:15" -- Lamp uit in de avond
    local Timer         = 15 -- hoe lang het licht nog aan blijft, bij afschakeling
    local Actie = "Niks"

return {
        on =    { timer = {'at ' .. TijdAan, 'at ' .. TijdUit, "at sunrise", "at sunset" }},
        
        logging = { level = domoticz.LOG_ERROR  ,marker = "Verlichting Voordeur"},
        
        execute = function(dz, Trigger, triggerInfo)

        local SubSystem   =   dz.NSS_TELEGRAM
        local Priority      = dz.PRIORITY_NORMAL
        local Sound         = dz.SOUND_DEFAULT
        local Tittle        = triggerInfo.scriptName
	      
    if (dz.time.matchesRule('between ' .. TijdUit ..' and 23:59')) then
         Actie = "Licht Uit"
         print("Matchrule =" .. 'between ' .. TijdUit ..' and 23:59')
         
    elseif (dz.time.matchesRule('between sunrise and 12:00')) then
         Actie = "Licht Uit"
        print("Matchrule =" ..'between sunrise and 12:00')
        
    else -- Tijd gelijk aan TijdAan or sunset
         Actie = 'Licht Aan'
         print("Matchrule =" .. TijdAan)
    end

    dz.notify(Tittle, Tittle ..  " - " .. Trigger.trigger .. " - " .. Actie .. " [" .. dz.devices('Voordeur Lamp') .state.. "]", Priority, Sound, '' , SubSystem)

    if Actie == 'Licht Aan' then
        dz.devices(DeviceName).switchOn()
    else -- "Licht Uit"
        dz.devices(DeviceName).switchOff().afterMin(Timer)
    end
end
}   

Re: Turn on lights when it is dark between 6:30 and 7:00

Posted: Thursday 24 October 2019 0:06
by waaren
BOverdevest wrote: Wednesday 23 October 2019 23:18 Hi, below my solution for managing an outside light.
Hi, does sundown work or should it be sunset ? The string sundown is not part of dzVents.

Re: Turn on lights when it is dark between 6:30 and 7:00  [Solved]

Posted: Wednesday 06 November 2019 21:50
by BOverdevest
waaren wrote: Thursday 24 October 2019 0:06
BOverdevest wrote: Wednesday 23 October 2019 23:18 Hi, below my solution for managing an outside light.
Hi, does sundown work or should it be sunset ? The string sundown is not part of dzVents.
Hi Waaren,
Thanks for reviewing the code. But yes, the code is working, however, it is more luck than wisdom...
At sundown the script kicks in as per On statement, then the logic roles to the 'else' statement and turns on the light as it should be.
So, the lines can be removed...

I updated the script...

Bart