Motion Sensor script return to manual set light level

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

Moderator: leecollings

Post Reply
Skippiemanz
Posts: 230
Joined: Sunday 14 July 2013 20:21
Target OS: Linux
Domoticz version: 4.10233
Location: Alkmaar, The Netherlands
Contact:

Motion Sensor script return to manual set light level

Post by Skippiemanz »

Hi All,

Hoping that someone could help me out..

I have a script witch during night time and motion detect turns on the hallway light.

Right now it turns on at a preset level(25) when between sunset and midnight.

But i would like to remember at witch level the light was set manually lets see 37%

Is this possible?

Also the lights should go out after midnight when no motion detected.

But when they are one at 5% before midnight it doesnt automaticcly turns off at midnight or 5 min later.

Code: Select all

return {
	active = true,
	on = { 
		devices = { 
			['PIR Gang'] = { 'at nighttime' },
		timer = {'every 5 minutes at nighttime'}
		}
	},
	execute = function(domoticz, motion)
		local light = domoticz.devices('Lamp Gang')
      
		if domoticz.time.matchesRule("between sunset and 00:00") and (motion.state == 'On')  then
			light.dimTo(25)
		elseif domoticz.time.matchesRule("between sunset and 00:00") and (motion.state == 'Off')  then
			light.dimTo(5)
		elseif domoticz.time.matchesRule("between 00:00 and sunrise") and (motion.state == 'On')  then
			light.dimTo(15)
		elseif domoticz.time.matchesRule("between 00:00 and sunrise") and (motion.state == 'Off')  then
			light.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: Motion Sensor script return to manual set light level

Post by waaren »

Skippiemanz wrote: Tuesday 14 May 2019 23:02 Is this possible?
Could be something like this ( if my understanding of what you want is correct)

Code: Select all

--[[
I have a script witch during night time and motion detect turns on the hallway light.
Right now it turns on at a preset level(25) when between sunset and midnight.
But i would like to remember at witch level the light was set manually lets see 37%
Also the lights should go out after midnight when no motion detected.

But when they are one at 5% before midnight it doesnt automaticcly turns off at midnight or 5 min later.
]]--
return {
    on = { devices = { 'PIR Gang', 'Lamp Gang'} },

    data = { lastLevel = { initial = 0 }},
    
    execute = function(dz, item)
        local light = dz.devices('Lamp Gang')
        
        if item == light then
            dz.data.lastLevel = item.level
            return
        end    
        
        if item.state == 'On' then
            light.cancelQueuedCommands()
            if dz.time.matchesRule("between sunset and 00:00") then 
                light.dimTo(dz.data.lastLevel).silent()
                light.dimTo(5).afterMin(5).silent()
                if dz.time.matchesRule('at 23:51-00:00') then
                    light.switchOff.afterMin(10).silent()
                end    
            elseif dz.time.matchesRule("between 00:00 and sunrise") then
                light.dimTo(15).silent()
                light.switchOff().afterMin(5).silent()
            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
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest