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

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
dutchronnie
Posts: 46
Joined: Thursday 15 August 2013 22:01
Target OS: -
Domoticz version:
Contact:

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

Post 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
}
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

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

Post 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.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
dutchronnie
Posts: 46
Joined: Thursday 15 August 2013 22:01
Target OS: -
Domoticz version:
Contact:

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

Post 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
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

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

Post 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()
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
BOverdevest
Posts: 23
Joined: Wednesday 28 March 2018 20:56
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.11307
Location: Nederland
Contact:

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

Post 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
}   
Last edited by BOverdevest on Wednesday 06 November 2019 21:52, edited 1 time in total.
HUE, Tradfri, Zwave, RFXCOM, rooted TOON, PS4
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

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

Post 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.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
BOverdevest
Posts: 23
Joined: Wednesday 28 March 2018 20:56
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.11307
Location: Nederland
Contact:

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

Post 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
HUE, Tradfri, Zwave, RFXCOM, rooted TOON, PS4
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest