Turn on lights at 7.00 unless the sun has risen  [Solved]

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

Moderator: leecollings

Post Reply
Mrtn83
Posts: 11
Joined: Friday 25 October 2019 22:43
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Vlaardingen
Contact:

Turn on lights at 7.00 unless the sun has risen

Post by Mrtn83 »

Hello,

I'am new to the world of dzVents and with some solutions I found on this forum I have made my own little script.
The goal is to turn on the outside lights at 7.00 unless the sun is already up. I have made a group with al the lights that I want to turn on named Buitenverlichting. Could somebody advise me if this script triggers the lights how I would like? The lights are turned off with Homekit at sunrise. So no need to included that in the script.

Code: Select all

return {
	on = {
		timer = {
			function(domoticz)
			    	    return (domoticz.time.matchesRule('at 07:00') and domoticz.time.matchesRule('before sunrise'))
			end
		}
	},
	execute = function(domoticz, triggeredItem, info)
		local d = dz.groups("Buitenverlichting")
		if (d.state ~= 'On') then
		    d.switchOn()
		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 at 7.00 unless the sun has risen

Post by waaren »

Mrtn83 wrote: Monday 09 November 2020 19:13 Could somebody advise me if this script triggers the lights how I would like? The lights are turned off with Homekit at sunrise. So no need to included that in the script.
Allmost..

The only remaining problems are:
You use dz in the body of the execute function but you send domoticz as the first parameter to this function; you should use the same name for both.
'before sunrise' by itself is not a valid timerule. You could use 'between 00:00 and sunrise'

It is easier to use something like.

Code: Select all

return 
{
    on = 
    {
        timer = 
        {
                'at 07:00',
        },
    },

    execute = function(dz)
        if dz.time.matchesRule('between 00:00 and sunrise') then
            local d = dz.groups("Buitenverlichting")
            if (d.state ~= 'On') then
                d.switchOn()
            end
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Mrtn83
Posts: 11
Joined: Friday 25 October 2019 22:43
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Vlaardingen
Contact:

Re: Turn on lights at 7.00 unless the sun has risen

Post by Mrtn83 »

Thank you very much. I will give it a try!
Mrtn83
Posts: 11
Joined: Friday 25 October 2019 22:43
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Vlaardingen
Contact:

Re: Turn on lights at 7.00 unless the sun has risen

Post by Mrtn83 »

It works perfectly. That makes me want te do more.

Would this script work to turn the ligts of at sunrise?

Code: Select all

return 
{
    on = 
    {
        timer = 
        {
                'at 07:00',
        },
    Off = 
    {
        timer = 
        {
                'at sunrise',
        },
    },

    execute = function(dz)
        if dz.time.matchesRule('between 00:00 and sunrise') then
            local d = dz.groups("Buitenverlichting")
            if (d.state ~= 'On') then
                d.switchOn()
            end
            elseif dz.time.matchesRule("at sunrise") then
            local d = dz.groups("Buitenverlichting")
            if (d.state ~= 'Off') then
                d.switchOff()
        end
        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 at 7.00 unless the sun has risen

Post by waaren »

Mrtn83 wrote: Thursday 19 November 2020 10:56 Would this script work to turn the ligts of at sunrise?
Almost but the syntax you used is not what dzVents expects. Try below example.

Code: Select all

return
{
    on =
    {
        timer =
        {
                'at 07:00',
                'at sunrise',
        },
    },

    execute = function(dz)
        local d = dz.groups('Buitenverlichting')

        if dz.time.matchesRule('at sunrise') then
            if d.state ~= 'Off' then d.switchOff() end
        elseif dz.time.matchesRule('between 00:00 and sunrise') and d.state ~= 'On' then
            d.switchOn()
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Mrtn83
Posts: 11
Joined: Friday 25 October 2019 22:43
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Vlaardingen
Contact:

Re: Turn on lights at 7.00 unless the sun has risen

Post by Mrtn83 »

Great, thank you very much. For future entertainment. If I would change for instance sunrise to 12:00 would that work?
That way I could use the base of this script for other autimations.
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 at 7.00 unless the sun has risen

Post by waaren »

Mrtn83 wrote: Thursday 19 November 2020 14:12 Great, thank you very much. For future entertainment. If I would change for instance sunrise to 12:00 would that work?
That way I could use the base of this script for other autimations.
Changing sunrise.. You must have great powers.. very far beyond domoticz :D
But yes. For dzVents sunrise is just a time. The only difference with something like 12:01 is that the value of the sunrise time is set by domoticz at 00:00 every day if the location is set and for some locations it is not filled during midwinter.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Mrtn83
Posts: 11
Joined: Friday 25 October 2019 22:43
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Vlaardingen
Contact:

Re: Turn on lights at 7.00 unless the sun has risen

Post by Mrtn83 »

My superpowers amaze me to from time to time :-).
Thank you very much for the explanation. No Midwinter where we live so that won't be an issue.

Am I correct that if I would change local d = dz.groups('Buitenverlichting') to local light = domoticz.devices('Buitenlamp achter') only the light Buitenlamp achter would go on?
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 at 7.00 unless the sun has risen

Post by waaren »

Mrtn83 wrote: Thursday 19 November 2020 16:25 Am I correct that if I would change local d = dz.groups('Buitenverlichting') to local light = domoticz.devices('Buitenlamp achter') only the light Buitenlamp achter would go on?
No you would have to change some more.. Most important is that you don't mix dz and domoticz. The first (and here the only) parameter of the execute function is the domoticz object.
If you use the name dz for it, you have to keep using that name in the body of this execute function.

Code: Select all

return
{
    on =
    {
        timer =
        {
                'at 07:00',
                'at sunrise',
        },
    },

    execute = function(dz)
        local light = dz.devices('Buitenlamp achter')

        if dz.time.matchesRule('at sunrise') then
            if light.state ~= 'Off' then light.switchOff() end
        elseif dz.time.matchesRule('between 00:00 and sunrise') and light.state ~= 'On' then
            light.switchOn()
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Mrtn83
Posts: 11
Joined: Friday 25 October 2019 22:43
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Vlaardingen
Contact:

Re: Turn on lights at 7.00 unless the sun has risen  [Solved]

Post by Mrtn83 »

Thank you very much!
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest