Switch lights on when dark  [Solved]

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

Moderator: leecollings

Post Reply
User avatar
DRB2019
Posts: 24
Joined: Saturday 06 July 2019 13:59
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: Netherlands
Contact:

Switch lights on when dark

Post by DRB2019 »

I'am struggling with the (simple) scipt below.
I do not have much experience with dzvents, but i want to learn it.
I have a dusk sensor, and when it switches to "on" i need to switch on some light in our room.
(depending on the state of another light and between a time limit)

Any help would be appreciated!

Code: Select all

return {
	on = {
		devices = {
			'Schemerschakelaar1',
			'Staande_lamp'
		}
	},
	execute = function(domoticz,devices)
	    if  Schemerschakelaar1.state == 'On' and 
	        Staande_lamp.state == 'Off' and  
	        (domoticz.time =='Between 11:30 and 22:20')
        then
	        domoticz.devices('Staande_lamp').switchOn()
	        domoticz.devices('Dressoir').switchOn()
	        domoticz.log('Device ' .. device.name .. ' was changed', domoticz.LOG_INFO)
	    end
end
}
Last edited by DRB2019 on Tuesday 30 June 2020 21:17, edited 1 time in total.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Sitch lights on when dark

Post by waaren »

DRB2019 wrote: Tuesday 30 June 2020 12:43 I have a dusk sensor, and when it switches to "On" i need to switch on some light in our room.
(depending on the state of another light and between a time limit)
Assuming the name of your dusk-sensor switch is Schemerschakelaar1 it could look like

Code: Select all

return
{
    on =
    {
        devices =
        {
            'Schemerschakelaar1',
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'dusk sensor',
    },

    execute = function(domoticz, item)
        if  item.state == 'On' and domoticz.devices('Staande_lamp').state == 'Off' and domoticz.time.matchesRule('at 11:30-22:20') then
            domoticz.devices('Staande_lamp').switchOn()
            domoticz.devices('Dressoir').switchOn()
            domoticz.log('Device ' .. item.name .. ' was updated', domoticz.LOG_INFO)
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
DRB2019
Posts: 24
Joined: Saturday 06 July 2019 13:59
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: Netherlands
Contact:

Re: Sitch lights on when dark

Post by DRB2019 »

waaren wrote: Tuesday 30 June 2020 14:02
Assuming the name of your dusk-sensor switch is Schemerschakelaar1 it could look like
Your script works perfect!
I modified it a bit, so when the dusk sensor goes to 'Off' the lights also switch off. (fluctuation of light in the house).
Is there also a possibility to send a telegram message when the lights go on?

Code: Select all

return
{
    on = {
        timer   = {'every 10 minutes'},   
        devices = {'Schemerschakelaar1'}
    },

    logging =
    {
        level = domoticz.LOG_ERROR,  -- change to LOG_ERROR when ok (was LOG_DEBUG)
        marker = 'dusk sensor',
    },

    execute = function(domoticz, item)
        if  item.state == 'On' and domoticz.devices('Staande_lamp').state == 'Off' and domoticz.time.matchesRule('at 15:30-22:20')
        then
            domoticz.devices('Staande_lamp').switchOn()
            domoticz.devices('Dressoir').switchOn()
            domoticz.log('Device ' .. item.name .. ' was updated', domoticz.LOG_INFO)
        else
            if item.state == 'Off' and domoticz.devices('Staande_lamp').state == 'On' and domoticz.time.matchesRule('at 15:30-22:20')
            then
            domoticz.devices('Staande_lamp').switchOff()
            domoticz.devices('Dressoir').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: Sitch lights on when dark

Post by waaren »

DRB2019 wrote: Tuesday 30 June 2020 21:13 Is there also a possibility to send a telegram message when the lights go on?
Yes.

please have a look at the dzVents wiki

notify(subject, message, priority, sound, extra, subsystem): Function. Send a notification (like Prowl). Priority can be like domoticz.PRIORITY_LOW, PRIORITY_MODERATE, PRIORITY_NORMAL, PRIORITY_HIGH, PRIORITY_EMERGENCY. For sound see the SOUND constants below. subsystem can be a table containing one or more notification subsystems. See domoticz.NSS_subsystem types.
why did you add?

Code: Select all

timer   = {'every 10 minutes'},  
It will not do anything in this script.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
DRB2019
Posts: 24
Joined: Saturday 06 July 2019 13:59
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: Netherlands
Contact:

Re: Sitch lights on when dark

Post by DRB2019 »

why did you add?

Code: Select all

timer   = {'every 10 minutes'},  
It will not do anything in this script.
I only want to run the script every 10 minutes.
Just to avoid the lights switching on and off quickly
How can you realize this?

I will dig in the notify function and try it out!
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Sitch lights on when dark  [Solved]

Post by waaren »

DRB2019 wrote: Tuesday 30 June 2020 23:12 I only want to run the script every 10 minutes. Just to avoid the lights switching on and off quickly
How can you realize this?
Could look like

Code: Select all

return
{
    on = {
        timer   = {'every 10 minutes between 15:30 and 22:20'},   
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,  -- change to LOG_ERROR when ok (was LOG_DEBUG)
        marker = 'dusk sensor',
    },

    execute = function(domoticz)
        local dusk = domoticz.devices('Schemerschakelaar1')

        if  dusk.state == 'On' and domoticz.devices('Staande_lamp').state == 'Off' then
            domoticz.devices('Staande_lamp').switchOn()
            domoticz.devices('Dressoir').switchOn()
            domoticz.log('Device ' .. item.name .. ' was updated', domoticz.LOG_INFO)
        elseif dusk.state == 'Off' and domoticz.devices('Staande_lamp').state == 'On' then
            domoticz.devices('Staande_lamp').switchOff()
            domoticz.devices('Dressoir').switchOff()
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
DRB2019
Posts: 24
Joined: Saturday 06 July 2019 13:59
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: Netherlands
Contact:

Re: Sitch lights on when dark

Post by DRB2019 »

It now works the way I want it!
Thanks !!
Also got telegram working: D, below the script I use.
Could look like

Code: Select all

return
{
    on = {
        timer   = {'every 10 minutes between 15:30 and 22:20'},  
    },

    logging =
    {
        level = domoticz.LOG_ERROR,  -- change to LOG_ERROR when ok (was LOG_DEBUG)
        marker = 'dusk sensor',
    },

    execute = function(domoticz)
        local dusk = domoticz.devices('Schemerschakelaar1')

        if  dusk.state == 'On' and domoticz.devices('Staande_lamp').state == 'Off' then
            domoticz.devices('Staande_lamp').switchOn()
            domoticz.devices('Dressoir').switchOn()
            domoticz.notify('Donker', 'Het is donker binnen', PRIORITY_HIGH, SOUND_PERSISTENT, nil, domoticz.NSS_TELEGRAM)            
            domoticz.log('Device ' .. item.name .. ' was updated', domoticz.LOG_INFO)
        elseif 
            dusk.state == 'Off' and domoticz.devices('Staande_lamp').state == 'On' then
            domoticz.devices('Staande_lamp').switchOff()
            domoticz.devices('Dressoir').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: Sitch lights on when dark

Post by waaren »

DRB2019 wrote: Wednesday 01 July 2020 12:34 It now works the way I want it!
Please use English on the forum.

PRIORITY_HIGH and SOUND_PERSISTENT are no valid constants in dzVents you probably meant

domoticz.PRIORITY_HIGH and domoticz.SOUND_PERSISTENT

also please note that the SOUND constants are only used for the pushover notification subsystem in domoticz. For other notification susbsystems this parameter is ignored.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
DRB2019
Posts: 24
Joined: Saturday 06 July 2019 13:59
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: Netherlands
Contact:

Re: Switch lights on when dark

Post by DRB2019 »

I changed the script to

domoticz.PRIORITY_HIGH and domoticz.SOUND_PERSISTENT

Thanks!
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest