motion sensor at night

Moderator: leecollings

Post Reply
fipse
Posts: 4
Joined: Sunday 06 February 2022 19:10
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

motion sensor at night

Post 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
jake
Posts: 744
Joined: Saturday 30 May 2015 22:40
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

Re: motion sensor at night

Post 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.
fipse
Posts: 4
Joined: Sunday 06 February 2022 19:10
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: motion sensor at night

Post 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
EddyG
Posts: 1042
Joined: Monday 02 November 2015 5:54
Target OS: -
Domoticz version:

Re: motion sensor at night

Post 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
}
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest