Page 1 of 1

Dim level by motion

Posted: Friday 25 December 2020 14:40
by BarryT
Hi All,

is it easy to fix to turn on a light, and then when motion detected dim a light to certain level and 2 mins after last motion dim back to last dim level?
for example:

when it is dark in a room, turn on and dim lights to 10%, when motion detected dim lights to 75%, and then 2 minutes after last motion back to last level?
So it might be that the light was off before motion wants to turn on the light to 75%, but after the motion is detected i want it BACK to OFF when the last state was OFF..

I know this can be done in dzvents, but is it also possible in lua?

Thanks!

Re: Dim level by motion

Posted: Friday 25 December 2020 15:48
by waaren
BarryT wrote: Friday 25 December 2020 14:40 I know this can be done in dzvents, but is it also possible in lua?
dzVents is Lua so if you know how to solve it in dzVents you can just copy the underlying Lua code from the dzVents/runtime directory and repeat what the dzVents developers coded.

Re: Dim level by motion

Posted: Saturday 26 December 2020 10:42
by BarryT
waaren wrote: Friday 25 December 2020 15:48
BarryT wrote: Friday 25 December 2020 14:40 I know this can be done in dzvents, but is it also possible in lua?
dzVents is Lua so if you know how to solve it in dzVents you can just copy the underlying Lua code from the dzVents/runtime directory and repeat what the dzVents developers coded.
I just want to try out a lua script, not dzvents since i de-activated this due some reasons.. ;)

Re: Dim level by motion

Posted: Saturday 26 December 2020 11:53
by waaren
BarryT wrote: Saturday 26 December 2020 10:42 I just want to try out a Lua script, not dzVents since i de-activated this due some reasons.. ;)
I did not write that you should use the complete dzVents framework. What is in my post is a suggestion to have a look at the Lua code in dzVents runtime directory and copy paste the relevant Lua code in your own Lua script.
I my book that is what coding is all about. Use what other coders already invented and build on that.

Re: Dim level by motion

Posted: Sunday 27 December 2020 5:15
by Ittiz
Should be doable, try these scripts. I adapted them from scripts I use in my own system. I haven't tested the changes however, you may have to fix typos/debug.

Device Script, turns the light on or brightens it when motion detected:

Code: Select all

commandArray = {}

local TimerName = "Living Room Minder" --Name of the dummy switch that determines whether the light should auto on 
local MotionSensor = "Living Room Motion Sensor" --Name of the motion sensor in the room
local LightSwitch = "Living Room Dimmer" -- Name of the light switch
local LightTimer = "Living Room Dimmer Blind Timeout" -- Name of auto-off timeout user variable
local LastLightStateHigh = "Living Room LghtSttHg" --  Light level to set to when motion detected user variable

if ((devicechanged[MotionSensor] == 'On') and (otherdevices[TimerName] == 'On') and (otherdevices[LightSwitch] == 'Off')) then
	local s1 = otherdevices_lastupdate[LightSwitch]
	if (s1 == nil) then
		s1 = "2000-01-01 00:00:00"
	end
	
	local year1 = string.sub(s1, 1, 4)
	local month1 = string.sub(s1, 6, 7)
	local day1 = string.sub(s1, 9, 10)
	local hour1 = string.sub(s1, 12, 13)
	local minutes1 = string.sub(s1, 15, 16)
	local seconds1 = string.sub(s1, 18, 19)
	
	local t1 = os.time()
	local t2 = os.time{year=year1, month=month1, day=day1, hour=hour1, min=minutes1, sec=seconds1}
	
	local difference1 = t1-t2
	local difference2 = t1-t3
	if (difference1 > tonumber(uservariables[LightTimer])) then -- If  the light was turned off don't turn it back on!
		if (tonumber(uservariables[LastLightStateHigh]) ~= "On") then
			commandArray[LightSwitch]='Set Level ' .. tonumber(uservariables[LastLightStateHigh])
		else
			commandArray[LightSwitch] = "On"
		end
	end
end

return commandArray
Timer script, turns the light off or sets it to low light level after timeout:

Code: Select all

commandArray = {}

local LightSwitch = "Living Room Dimmer" -- CHANGE: Name of light switch
local MotionSensor = "Living Room Motion Sensor" -- CHANGE: Name of motion sensor.
local LightTimer = "Lvngrm Light Off Timeout" -- CHANGE: Name of auto-off timeout user variable
local LvngRmMndr = "Living Room Minder"  -- Dummy switch to Enable/disable auto off.
local LastLightStateLow = "LvngRmLghtSttLw" --  Light level to set to when NO motion detected user variable

--LightSwitch could be a dimmer, so we check if it's not off rather than if it's on.
if (otherdevices[LvngRmMndr] == "On" and otherdevices[LightSwitch] ~= "Off") then
	local s1 = otherdevices_lastupdate[MotionSensor]
	if (s1 == nil) then
		s1 = "2000-01-01 00:00:00"
	end

	local year1 = string.sub(s1, 1, 4)
	local month1 = string.sub(s1, 6, 7)
	local day1 = string.sub(s1, 9, 10)
	local hour1 = string.sub(s1, 12, 13)
	local minutes1 = string.sub(s1, 15, 16)
	local seconds1 = string.sub(s1, 18, 19)
	
	local t1 = os.time()
	local t2 = os.time{year=year1, month=month1, day=day1, hour=hour1, min=minutes1, sec=seconds1}
	
	local difference1 = t1-t2

	if (difference1 > tonumber(uservariables[LightTimer])) then
		if (tonumber(uservariables[LastLightStateLow]) ~= "Off") then
			commandArray[LightSwitch]='Set Level ' .. tonumber(uservariables[LastLightStateLow])
		else
			commandArray[LightSwitch] = "Off"
		end
	end
end

return commandArray
You WILL need another script to set the light level variable when you change it at the switch. I don't have anything like it so I won't bother providing it here, but it should be simple enough. If you don't the light values will ALWAYS default to the values you set in the variables.

Re: Dim level by motion

Posted: Monday 12 July 2021 20:09
by BarryT
Ittiz wrote: Sunday 27 December 2020 5:15 Should be doable, try these scripts. I adapted them from scripts I use in my own system. I haven't tested the changes however, you may have to fix typos/debug.

Device Script, turns the light on or brightens it when motion detected:

Code: Select all

commandArray = {}

local TimerName = "Living Room Minder" --Name of the dummy switch that determines whether the light should auto on 
local MotionSensor = "Living Room Motion Sensor" --Name of the motion sensor in the room
local LightSwitch = "Living Room Dimmer" -- Name of the light switch
local LightTimer = "Living Room Dimmer Blind Timeout" -- Name of auto-off timeout user variable
local LastLightStateHigh = "Living Room LghtSttHg" --  Light level to set to when motion detected user variable

if ((devicechanged[MotionSensor] == 'On') and (otherdevices[TimerName] == 'On') and (otherdevices[LightSwitch] == 'Off')) then
	local s1 = otherdevices_lastupdate[LightSwitch]
	if (s1 == nil) then
		s1 = "2000-01-01 00:00:00"
	end
	
	local year1 = string.sub(s1, 1, 4)
	local month1 = string.sub(s1, 6, 7)
	local day1 = string.sub(s1, 9, 10)
	local hour1 = string.sub(s1, 12, 13)
	local minutes1 = string.sub(s1, 15, 16)
	local seconds1 = string.sub(s1, 18, 19)
	
	local t1 = os.time()
	local t2 = os.time{year=year1, month=month1, day=day1, hour=hour1, min=minutes1, sec=seconds1}
	
	local difference1 = t1-t2
	local difference2 = t1-t3
	if (difference1 > tonumber(uservariables[LightTimer])) then -- If  the light was turned off don't turn it back on!
		if (tonumber(uservariables[LastLightStateHigh]) ~= "On") then
			commandArray[LightSwitch]='Set Level ' .. tonumber(uservariables[LastLightStateHigh])
		else
			commandArray[LightSwitch] = "On"
		end
	end
end

return commandArray
Timer script, turns the light off or sets it to low light level after timeout:

Code: Select all

commandArray = {}

local LightSwitch = "Living Room Dimmer" -- CHANGE: Name of light switch
local MotionSensor = "Living Room Motion Sensor" -- CHANGE: Name of motion sensor.
local LightTimer = "Lvngrm Light Off Timeout" -- CHANGE: Name of auto-off timeout user variable
local LvngRmMndr = "Living Room Minder"  -- Dummy switch to Enable/disable auto off.
local LastLightStateLow = "LvngRmLghtSttLw" --  Light level to set to when NO motion detected user variable

--LightSwitch could be a dimmer, so we check if it's not off rather than if it's on.
if (otherdevices[LvngRmMndr] == "On" and otherdevices[LightSwitch] ~= "Off") then
	local s1 = otherdevices_lastupdate[MotionSensor]
	if (s1 == nil) then
		s1 = "2000-01-01 00:00:00"
	end

	local year1 = string.sub(s1, 1, 4)
	local month1 = string.sub(s1, 6, 7)
	local day1 = string.sub(s1, 9, 10)
	local hour1 = string.sub(s1, 12, 13)
	local minutes1 = string.sub(s1, 15, 16)
	local seconds1 = string.sub(s1, 18, 19)
	
	local t1 = os.time()
	local t2 = os.time{year=year1, month=month1, day=day1, hour=hour1, min=minutes1, sec=seconds1}
	
	local difference1 = t1-t2

	if (difference1 > tonumber(uservariables[LightTimer])) then
		if (tonumber(uservariables[LastLightStateLow]) ~= "Off") then
			commandArray[LightSwitch]='Set Level ' .. tonumber(uservariables[LastLightStateLow])
		else
			commandArray[LightSwitch] = "Off"
		end
	end
end

return commandArray
You WILL need another script to set the light level variable when you change it at the switch. I don't have anything like it so I won't bother providing it here, but it should be simple enough. If you don't the light values will ALWAYS default to the values you set in the variables.
Sorry for the late answer, but thanks for these scripts!!