Dim level by motion

Moderator: leecollings

Post Reply
BarryT
Posts: 369
Joined: Tuesday 31 March 2015 22:06
Target OS: Linux
Domoticz version: 2024.3
Location: east netherlands
Contact:

Dim level by motion

Post 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!
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Dim level by motion

Post 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.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
BarryT
Posts: 369
Joined: Tuesday 31 March 2015 22:06
Target OS: Linux
Domoticz version: 2024.3
Location: east netherlands
Contact:

Re: Dim level by motion

Post 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.. ;)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Dim level by motion

Post 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.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Ittiz
Posts: 48
Joined: Tuesday 03 January 2017 0:37
Target OS: Linux
Domoticz version: 13939
Location: USA
Contact:

Re: Dim level by motion

Post 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.
BarryT
Posts: 369
Joined: Tuesday 31 March 2015 22:06
Target OS: Linux
Domoticz version: 2024.3
Location: east netherlands
Contact:

Re: Dim level by motion

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

Who is online

Users browsing this forum: No registered users and 1 guest