Switch garden lights with motion sensor and on time  [Solved]

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

Moderator: leecollings

Post Reply
PatrickDV
Posts: 4
Joined: Sunday 05 July 2020 21:15
Target OS: OS X
Domoticz version:
Contact:

Switch garden lights with motion sensor and on time

Post by PatrickDV »

Hello

I want to switch my garden lights on from sunset to switch off at 23:30
from 23:30 to sunrise it my only go on when the motions sensor detects motion.
the light has to go on for 5 minutes
from sunrise to sunset the light have to be off

I have set the timers at the tab groups
I made an little script to switch on the lights for 5 minutes.
what I can't get work is the timer functions that the light may only go on when it is between 23:30 and sunrise.

If someone has an suggestion?

Code: Select all

return
{
    on =
    {
        devices = { 3736 }
    },

    execute = function(dz, item)

        if item.active then
        
            buitenverlGroup = dz.groups('Minimaal')
            buitenverlGroup.switchOn().forMin(5)
        end
    end
}
Best Regards,

Patrick
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Switch garden lights with motion sensor and on time  [Solved]

Post by waaren »

PatrickDV wrote: Wednesday 08 July 2020 20:17 from 23:30 to sunrise it my only go on when the motions sensor detects motion. The light has to go on for 5 minutes
what I can't get work is the timer functions that the light may only go on when it is between 23:30 and sunrise.
Can look like below.

Code: Select all

return
{
    on =
    {
        devices = 
		{ 
			[ 3736 ] = 
			{ 
				'between 23:30 and sunrise',
			},
		},
    },

    execute = function(dz, item)

        if item.active then
        
            buitenverlGroup = dz.groups('Minimaal')
            buitenverlGroup.switchOn().forMin(5)
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
PatrickDV
Posts: 4
Joined: Sunday 05 July 2020 21:15
Target OS: OS X
Domoticz version:
Contact:

Re: Switch garden lights with motion sensor and on time [SOLVED]

Post by PatrickDV »

Thanks, is is working!
heintjep
Posts: 3
Joined: Wednesday 08 July 2020 13:45
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Switch garden lights with motion sensor and on time

Post by heintjep »

Hi all,

I'm pretty new to dzVents scripting. I watched the video, searched the wiki and this forum, but I'm getting more and more frustrated, not getting a simple script with a PIR motion sensor to work. I'm not even so far to actually swith a light. I cannot detect in the script if the sensor is on or off..

This is the first start of my script:

Code: Select all

return {
	on = {
		devices = {
			'PIR WC beneden'
		},
	},
	execute = function(domoticz, pir)
		print('**** DEBUG: Script execution started. ****')
		print('**** DEBUG: Device ' .. pir.name .. ' was changed.' .. ' ****', domoticz.LOG_INFO)

		if (pir.state == 'on') then
			print('**** DEBUG: PIR switch is on. ****')
        	else
            		print('**** DEBUG: Nothing is on. ****')
		end
	end
}
The log results in this when the sensor detects motion:

Code: Select all

2020-07-11 19:18:04.547 Status: OpenZWave: Alarm received (Home Security: Motion Detected at Unknown Location), NodeID: 8 (0x08)
2020-07-11 19:18:04.652 Status: dzVents: Info: Handling events for: "PIR WC beneden", value: "Off"
2020-07-11 19:18:04.652 Status: dzVents: Info: ------ Start internal script: PIR_motion_detection: Device: "PIR WC beneden (Zwave)", Index: 173
2020-07-11 19:18:04.652 Status: dzVents: **** DEBUG: Script execution started. ****
2020-07-11 19:18:04.652 Status: dzVents: **** DEBUG: Device PIR WC beneden was changed. ****
2020-07-11 19:18:04.652 Status: dzVents: 3
2020-07-11 19:18:04.652 Status: dzVents: **** DEBUG: Nothing is on. ****
2020-07-11 19:18:04.652 Status: dzVents: Info: ------ Finished PIR_motion_detection
2020-07-11 19:18:04.750 Status: dzVents: Info: Handling events for: "PIR WC beneden", value: "On"
2020-07-11 19:18:04.750 Status: dzVents: Info: ------ Start internal script: PIR_motion_detection: Device: "PIR WC beneden (Zwave)", Index: 173
2020-07-11 19:18:04.750 Status: dzVents: **** DEBUG: Script execution started. ****
2020-07-11 19:18:04.750 Status: dzVents: **** DEBUG: Device PIR WC beneden was changed. ****
2020-07-11 19:18:04.750 Status: dzVents: 3
2020-07-11 19:18:04.750 Status: dzVents: **** DEBUG: Nothing is on. ****
2020-07-11 19:18:04.750 Status: dzVents: Info: ------ Finished PIR_motion_detection 
I'm using a Neo CoolCam motion sensor. I noticed that the device settings seem not to be editable in Domotics, so I'm using the default settings. When motion is detected, the switch remains on for about 30 seconds. That would be ok for detecting in the script if the motion switch is on or not, I suppose.

Besides the pir.state == 'on' test I tried practically everything except for a working test in the if statement. Maybe I'm forgetting someting?

What am I missing here? Can someone help me out?
Thanks a lot in advance.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Switch garden lights with motion sensor and on time

Post by waaren »

heintjep wrote: Saturday 11 July 2020 19:27 What am I missing here? Can someone help me out?
Case matters

Code: Select all

return {
    on = {
        devices = {
            'PIR WC beneden'
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'PIR',
    },

    execute = function(domoticz, pir)
        domoticz.log('Device ' .. pir.name .. ' was changed to ' .. pir.state, domoticz.LOG_DEBUG)

        if pir.state == 'On') then
            domoticz.log('Device ' .. pir.name .. ' is On', domoticz.LOG_DEBUG)
        else
            domoticz.log('Device ' .. pir.name .. ' is not On', domoticz.LOG_DEBUG)
        end

    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
heintjep
Posts: 3
Joined: Wednesday 08 July 2020 13:45
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Switch garden lights with motion sensor and on time

Post by heintjep »

Thanks! Now it works!
Next step is to trigger the script on all PIR sensors and switch the appropiate light...

Thanks again. See if i can figure that out.
heintjep
Posts: 3
Joined: Wednesday 08 July 2020 13:45
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Switch garden lights with motion sensor and on time

Post by heintjep »

Well,

See here the version that I managed to get going:

Code: Select all

return {
    on = {
        devices = {
            'PIR*'
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        -- level = domoticz.LOG_INFO,
        marker = 'PIR',
    },

    execute = function(domoticz, pir)
        domoticz.log('Device ' .. pir.name .. ' was changed to ' .. pir.state, domoticz.LOG_DEBUG)

        if (pir.state == 'On') and (pir.name == 'PIR WC beneden') then
            domoticz.log('Device ' .. pir.name .. ' is On. Switching on WC beneden.', domoticz.LOG_DEBUG)
            domoticz.devices('WC beneden').switchOn().forMin(3)
        end

    end
}
The script is running for a day or what, but i notice that the light is not always switched off after the set duration of 3 minutes. That's weird, isn't it? Somebody else ever faced this problem too?

Thanks in advance,
Hein.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Switch garden lights with motion sensor and on time

Post by waaren »

heintjep wrote: Monday 13 July 2020 9:54 The script is running for a day or what, but i notice that the light is not always switched off after the set duration of 3 minutes. That's weird, isn't it?
Is indeed unexpected. Can you try if this one improves the hit rate?

Code: Select all

return
{
    on =
    {
        devices =
        {
            'PIR WC beneden',
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        -- level = domoticz.LOG_INFO,
        marker = 'PIR',
    },

    execute = function(domoticz, pir)

        domoticz.log('Device ' .. pir.name .. ' was changed to ' .. pir.state, domoticz.LOG_DEBUG)

        local WCLight = domoticz.devices('WC beneden')
        if pir.state == 'On' then
            domoticz.log('Device ' .. pir.name .. ' is On. Switching on WC beneden.', domoticz.LOG_DEBUG)
            WCLight.cancelQueuedCommands()
            WCLight.switchOn().checkFirst()
            WCLight.switchOff().afterMin(3)
        end

    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest