Page 1 of 1

motion sensor at night

Posted: Sunday 06 February 2022 19:26
by fipse
Hi,

I'd like to accomplish, that the motion sensor only works at night. With this code it works all of the time:

Code: Select all

-- LUA trigger set to device

commandArray = {}
-- Set the time:
now = os.date("%H") * 60 + os.date("%M")
sunrise = timeofday['SunsetInMinutes']
sunset = timeofday['SunriseInMinutes']



if now >= sunset or now <= sunrise then
   	if (devicechanged['PIR2'] == 'On' and otherdevices['Vorne'] == 'Off') then
	commandArray['Vorne']='On FOR 5'
    end	
end    


return commandArray
I'm quite a newbe at lua but this should work. I really can't figure out, why it don't. Can someone please point me in the right direction? I'm more the C++ guy :D

73 fipse

Re: motion sensor at night

Posted: Monday 07 February 2022 19:31
by jake

fipse wrote:Hi,

I'd like to accomplish, that the motion sensor only works at night. With this code it works all of the time:

Code: Select all

-- LUA trigger set to device

commandArray = {}
-- Set the time:
now = os.date("%H") * 60 + os.date("%M")
sunrise = timeofday['SunsetInMinutes']
sunset = timeofday['SunriseInMinutes']



if now >= sunset or now <= sunrise then
   	if (devicechanged['PIR2'] == 'On' and otherdevices['Vorne'] == 'Off') then
	commandArray['Vorne']='On FOR 5'
    end	
end    


return commandArray
I'm quite a newbe at lua but this should work. I really can't figure out, why it don't. Can someone please point me in the right direction? I'm more the C++ guy :D

73 fipse
I see a something strange:
While defining sunrise and sunset, you mixed them up

When you are new to lua, I'd advise to start with dzvents. Much easier and more consistent in use of several devices.

Re: motion sensor at night

Posted: Tuesday 08 February 2022 19:50
by fipse
Thanks, that solved it :D

So if someone want exactly this without dzVents:

Code: Select all

commandArray = {}
now = os.date("%H") * 60 + os.date("%M")
sunset = timeofday['SunsetInMinutes']
sunrise = timeofday['SunriseInMinutes']

if now >= sunset or now <= sunrise then
   	if (devicechanged['PIR2'] == 'On' and otherdevices['Vorne'] == 'Off') then
	commandArray['Vorne']='On FOR 5'
    end	
end   
return commandArray
You in the domoticz scripting engine you need to set Trigger: to "Devices"

'PIR2' is the name of the motion sensor. (here via zigbee2mqtt)
'Vorne' is the name of the bulb (also a hue via zigbee2mqtt)
5 ... time in minutes the bulb is set to on


73 de fipse

Re: motion sensor at night

Posted: Saturday 12 February 2022 12:46
by EddyG
And if someone wants to solve it in dzVents:

Code: Select all

local SENSOR = 'PIR2'
local LIGHT = 'Vorne'

return {
    on = {
        devices = { [ SENSOR ]  = { 'between sunset and sunrise'  } }
    },

    execute = function(dz, item)
        if item.active then
            if not dz.devices(LIGHT).active then
                dz.devices(LIGHT).switchOn().forMin(5)
            end
        end
    end
}