Page 1 of 1

Motion Sensor script return to manual set light level

Posted: Tuesday 14 May 2019 23:02
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
}

Re: Motion Sensor script return to manual set light level

Posted: Tuesday 14 May 2019 23:47
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
}