Off with no Motion & Restore previous state/level when Motion is Sensed.

Hello All,

I’ve picked up a few HSM100’s and want to utilize these to detect when someone is in an area so lights will be off with no activity, then back on to previous levels when motion is sensed.

here is an example;

Someone is in the kitchen, they set the island lights to 50%, the Chandelier to 80% and turned the over-cabinet lights ON. When they walk out of the kitchen and no movement is sensed for 5 minutes, I want to shut those lights OFF. When motion is detected again, it restores all lights that were on to their previous set levels (unless the lights were on as part of a scene (if possible)).

This is probably an easy task for some of the members here, and I realize luup is required so i’m hoping someone has already done this and can simply paste their code, or someone is willing to whip it up for me and everyone else interested.

Thanks!

I’m confident you can get this working without the use of code; instead, utilize the “return to previous setting” option.
If the motion sensor turns the lights On, then the motion sensor should turn them Off at the expire time with the return to previous setting (the lights being Off)
If the lights were already On prior to the motion sensor sending the On again, at expire time & the return to previous setting (which was On) they should stay On.

JOD.

*Edit. I just re-read your subject title, I’m not as confident the above scenario will work to turn the lights On to a previously set level if the lights were Off, yeah, you’ll need (Miracle Whip) code for that. So, you’ll have to create a scene setting the initial levels you want.

If you associate the HSM100 with the lights directly, you will get that behavior. But you’d lose the things Vera would add: more complicated scenes (including more devices) and arm/bypass. Also, Vera wouldn’t see the state of the lights change, but you could set up a scene (through the [tt]Advanced[/tt] tab) that polls the associated lights a few seconds after a trip state change.

@mysticjay, can you help me with this part?

–put your devices here–MAKE SURE the array_devices and array_device_type have same # of elements
–S is for a Switch, D is for a dimmer
local array_devices = {10, 15}
local array_device_types = {“S”,“D”}

Lets say I have 3 dimmers and 3 switches, is this correct?

–put your devices here–MAKE SURE the array_devices and array_device_type have same # of elements
–S is for a Switch, D is for a dimmer
local array_devices = {59,89,106 15,112,135}
local array_device_types = {“S”,“D”}

I’ll edit if I figure this out sooner.

My other question is regarding your statement about this turning off eventually, what do you mean by resting it?

Thanks!

You’d probably need to specify what each device is. So if the order is 3 dimmers and 3 switches, possibly something like so:

local array_devices =      {59,89,106,15,112,135}
local array_device_types = {"D","D","D","S","S","S"}

@mysticjay, @big517,

Consider letting the sensor do the timing by setting an appropriate [tt]On time[/tt] and using separate scenes for tripped (lights should go on) and not tripped events (lights should go off).

@oTi,

Will these scenes return the lights to their previous values? I’m not too familiar yet, but was afraid it wouldn’t work that way.

Say someone enters the room. Adjusts 5 different components to the desired setting some on and some off, a light a x% and another at y%. then leaves the room for 10 minutes… When motion is not detected it will shut them off, but if I set a new scene to turn them on upon motion again, I believe it goes back to whatever Vera’s scene is programmed to do, not the person’s last settings. That is what I’m hoping to accomplish here. I tried using scenes to “TURN ON” a dimmable light but it doesnt go back to the last setting. If you have another method with scenes to accomplish this that would be cool.

Thanks!

The comment about the 2 scenes was related to the proposed code currently being in 1 scene and trying to handle turning the lights both on and off, driven by a single event. The sensor “knows best” when to turn the lights on and off, as that is its purpose. So you could have 1 scene that deals with turning the lights on (a trip event) and 1 scene that deals with turning the lights off (a not tripped event); no extra code required to figure out if the lights should really be switched off or not.

Also, the dimmers themselves “know best” what their last setting was. There is a Z-Wave command that will tell the dimmers to go back to that setting. The sensor is able to send that command if it controls the light directly.

Vera does not currently support this command, but I have asked this to be included (as part of UI5 beta feedback), as well as filed a formal feature request for this.

Linking to your separate topic.

@oTi@ and @big517
Here’s the better version, which I think is the right way to do this 8)

@big517:
Please copy the updated version I just posted - it doesn’t involve counters and all that weird logic. This is the better version and should do what you were expecting. Please do let me know if this works well.


-------------------------------------------------------------------------

-- CODE FROM MYSTICJAY
-- http://forum.micasaverde.com/index.php/topic,8283.0.html
-- put your devices here--MAKE SURE the array_devices and
-- array_device_type have same # of elements
-- S is for a Switch, D is for a dimmer

-- example: local array_devices =      {59,89,106,15,112,135}
-- example: local array_device_types = {"D","D","D","S","S","S"}

-------------------------------------------------------------------------


local array_devices =      {5}
local array_device_types = {"S"}
local device_type
-- SD_ID = StateDevice ID (THIS IS NOT THE MOTION SENSOR ID
-- COULD BE ANYTHING THAT IS A BINARY SWITCH OR A STATE DEVICE)
local SD_ID = 5

-- turn device after sometime -- 1 minute delay
local delay   = 60 * 1



local Switch_ServiceVar = "urn:upnp-org:serviceId:SwitchPower1"
local Dimmer_ServiceVar =  "urn:upnp-org:serviceId:Dimming1"
-----State Device
local SD_ServiceVar = "urn:upnp-org:serviceId:SwitchPower1"



-------------Store / Get State / Device On/Off related
function set_state(device,value,SD_ID)
	luup.variable_set(SD_ServiceVar, "Device_State" .. device, value, SD_ID )
end


function get_state(device,SD_ID)
	return luup.variable_get(SD_ServiceVar,"Device_State" .. device, SD_ID)
end

function set_device_state(device,value,devicetype)
    if devicetype=="S" then
		luup.call_action(Switch_ServiceVar,"SetTarget",{ newTargetValue=value },device)
	elseif devicetype=="D" then
		luup.call_action(Dimmer_ServiceVar,'SetLoadLevelTarget', { newLoadlevelTarget=value }, device)
	end
end


function get_device_state(device,devicetype)
   local retvalue
    if devicetype=="S" then
		retvalue = luup.variable_get(Switch_ServiceVar,"Status", device)
	elseif devicetype=="D" then
		retvalue = luup.variable_get(Dimmer_ServiceVar,"LoadLevelStatus", device)
	end
	return retvalue
end

-------------Store / Get State / Device On/Off related



-- State Save / Restore Functions
function save_state()
	--save the states of devices
	for i, device in ipairs(array_devices) do
		device_type = array_device_types[i]
		local cur_state = get_device_state(device,device_type)
		set_state(device,cur_state,SD_ID)
	end
	set_state("state_stored","yes",SD_ID)
end

function restore_state()
	--restore to their original state
	for i, device in ipairs(array_devices) do
		device_type = array_device_types[i]
		set_device_state(device,get_state(device,SD_ID),device_type)
	end
	set_state("state_stored","no",SD_ID)
end


-- State Save / Restore Functions


function turn_off()
	local last_trip_time = get_state("last_trip_time",SD_ID)
	local cur_time = os.time()

	if (cur_time - last_trip_time) < delay then
		--we dont want to turn off anything yet!!!
	else
		-- turn the devices off after saving state
		save_state()

		for i, device in ipairs(array_devices) do
			device_type = array_device_types[i]
			set_device_state(device,"0",device_type)
		end
	end

end


---turn devices back to their state if the state was saved previously
if get_state("state_stored",SD_ID) == "yes" then
	restore_state()
end


local this_trip_time = os.time()
set_state("last_trip_time",this_trip_time,SD_ID)

luup.call_delay( 'turn_off', delay )

@mysticjay,

Question looking at your latest code, in particular “local array_devices = {10, 15]” is there a reason for the spaces between the comma and 15?

@big517:

The code is updated to handle both conditions you were looking for. Give it a try. :slight_smile:

  1. Turn off devices after a specific amount of time since the motion is triggered. (If there is another motion trigger before the devices go off, then the last trigger is counted for delay)

  2. Put devices back to their original state if the motion is triggered after they go off.

Also, to avoid confusion: @ big517 - I think it maybe helpful to remove Reply #3, #4, #5, #6, #7 - Just to avoid confusion, because the latest one I updated is the properly working version :slight_smile:

@MNB - No reason, you can put them like this if you want {10,15}

what relationship should the timer in your code and the awake time of the motion sensor be?
For instance, the default awake time on the HSM100 is 20 minutes, so should we set the timer delay to 20 as well?

@big517 - the awake time has no effect on code. The code works on motion trip event and nothing else. For testing I would set the delay like 2 minutes and see if this satisfies what you were looking for 8) (I also tested couple of times )

BTW - I’m planning on updating this without the counter…I think that would be a better way to accomplish this…

@mysticjay,

What happens if the [tt]On time[/tt] in the sensor is greater than the timer delay set in the code?

Also, do the lights turn off X minutes after one leaves the room, or X minutes after the sensor was last tripped?

@oTi@

  1. I don’t think the “on time” have any effect on this…This is based on the last time the motion sensor was tripped.

  2. The lights turn off X minutes after the sensor was last tripped

So if the sensor gets tripped prior to X minutes, then X minutes would get reset to current sensor tirp time + delay
-other way of looking at it-
if there is motion at least every X minute, then the lights never go off.

What is keeping the code from resetting the variables, or overwriting them with the incorrect state? I walked downstairs this morning after successful testing last night, and nothing turned on. This may not be a bad feature however because if we had the lights at 100% yesterday, and walk downstairs at 6am and get blasted with light it would be very fun so i’m starting to think how this would incorporate into the other scenes. Will this conflict with scenes that rely on the same motion sensor to turn the lights on? If so could it be avoided by using the ARM and DISARM state?

Did you manually switch off the lights last night, or did you let the code do it?
Also, what is the delay you used in the code, and the [tt]On time[/tt] in the sensor?

@big517:

What is keeping the code from resetting the variables, or overwriting them with the incorrect state?

The only time the variables are ever changed are :

  1. — When motion is tripped - the devices are restored to their previous state.

  2. – when after X amount of time the devices are about to be turned off, the state of the deices are remembered.

Let’s say the devices are turned off after X amount of time, then the devices are manually turned off or on, the code doesn’t know about this…

From your post, if you left your lights on at night and they were turned off by the scene/code, if the motion detector got tripped in the morning it sure would put them back to the previous state. But how do you know if the motion detector was tripped or not?

I didn’t shut them off knowingly… but i’m think the “all off” scene may have shut them off at midnight…

I also had the timer set for 21 minutes, and the awake time on the motion detector at it’s default 20 minutes. I just changed it to 19 minutes, and will remove the subject lights from being shut off.

[quote=“oTi@, post:8, topic:169444”]Also, the dimmers themselves “know best” what their last setting was. There is a Z-Wave command that will tell the dimmers to go back to that setting. The sensor is able to send that command if it controls the light directly.

Vera does not currently support this command, but I have asked this to be included (as part of UI5 beta feedback), as well as filed a formal feature request for this.[/quote]

@oTI did this get implemented into Ui5? I’m going to pick this back up now and want to see if there is a better method now.

Thanks