If door is opened for 5 minutes...turn off air

@MCVFlorin
Thanks for the updated code!
However, 1st, I think the device name is incorrect for motion detectors, see substitute name below.
2ndly, this code will shut off AC if either motion detector hasn’t tripped for the “period” length of time. That won’t work since all of the activity may be around one motion detector and not the other and in that case, I wouldn’t want the AC shut off. We need to be sure that the lastTrip for all motion detectors is > “period” length.
Obviously, I am a programming novice. Do you think the following changes to the pertinent section of the code would work?
BTW I changed the “period” from 20 to 30 min and time from 9 AM to 7 PM.


local now = os.date(“*t”)
– If it’s between 9 AM and 7 PM then check the motion sensors.
if (now.hour >= 9 and now.hour <= 19) then

-- Assume that no motion sensor has been tripped in the last 30 minutes.
local motionSensorTripped = false
local mostrecentTrip = os.time()

-- Get time that the last time each motion sensor was tripped.
for k, v in pairs(motionSensors) do
    local lastTrip..k = luup.variable_get("urn:schemas-micasaverde-com:device:MotionSensor:1", "LastTrip", k)
    lastTrip..k = tonumber(lastTrip..k, 10) or os.time()
    If lastTrip..k < mostrecentTrip then mostrecentTrip = lastTrip..k
end
if (((os.time() - mostrecentTrip) / 60) < 1800) then
        motionSensorTripped = true
   break
end
end

--  If no motion sensor has been tripped in the last 30 minutes (1800 seconds), then turn off the AC.
if (not motionSensorTripped) then
    for k, v in pairs(thermostats) do
        luup.log("Turning thermostat '" .. v .. "' OFF.")
        luup.call_action("urn:upnp-org:serviceId:HVAC_UserOperatingMode1", "SetModeTarget", {NewModeTarget = "Off"}, k)
    end

    return -- AC has been turned off: don't check the door and window sensors anymore.
end

end

Currently, I have a 2 story home with a motion detector in Great Room (living/dining area) and the hallway between 3 bedrooms on lower level. However, I could easily foresee someone staying in a bedroom and watching TV or reading or using the bathroom and/or working in the kitchen for longer than the specified “period” and not tripping these two motion detectors. If one is going to use this code to shut down AC when motion detectors are not tripped, the motion detectors should be located where persons may be spending significant time during the monitoring period. In that case I should have motion detectors in each bedroom (4), the kitchen and great room. The down side is more expense for motion detectors and batteries and more effort to keep batteries alive. The upside is avoid AC use when guest are not home and avoiding annoying them when they are home. In this home, guest are frequently not home during the hotest part of the day and return in the cooler evening.
Any thoughts or suggestions?

Actually the code shuts off the AC only if ALL the sensors have been inactive for x minutes or more.

First I assume that no motion sensor has been tripped in the last 20 minutes. Then I check when was the last time each motion sensor was tripped. If it was less than x minutes ago then I set the motionSensorTripped flag. If this flag is set, the AC won’t be turned off.

OMG, the script works! Thank you very much to all of you who made it happened.
I have only one question. How to make it to turn to Auto again, after the door is closed. What should I add to the script, so when it runs the next time, it turn the thermostat to Auto. I think, that was the idea at the beginning of the thread.

@ArtemPCB
Simply create a scene that turns on the thermostat when the door is closed. No code required.

[quote=“mcvflorin, post:83, topic:166538”]Actually the code shuts off the AC only if ALL the sensors have been inactive for x minutes or more.

First I assume that no motion sensor has been tripped in the last 20 minutes. Then I check when was the last time each motion sensor was tripped. If it was less than x minutes ago then I set the motionSensorTripped flag. If this flag is set, the AC won’t be turned off.[/quote]

MCVFlorin
I hope that I am misunderstanding this message. The goal of this program was to check the sensors and turn off the AC if the sensors indicated that any door or window (with sensor) has been open for the “period” length of time. Is that what this program does? You say above that the AC will only be shut off if all doors or windows are inactive. By inactive do you mean tripped? I presume that leaving a sensored door open causes it to be in the tripped mode.
PLEASE clarify.

In that message I was referring to the code that does this:

I didn’t say anything about the door and window sensors, but only about the motion sensors.

I finally managed to get 5 motion detectors installed last week with the rest of the zwave system operating properly now. I decided to try the new code that MCVFlorin helped us write that would also shut off the AC if no motion was detected in any of the 5 motion detectors for more than 20 min between 9 AM and 5 PM.
However, after I loaded the lua script and saved it and clicked the button to run now, I saw a message saying that there was an error in a Lua script. I then replace the new script with the old script, saved it and ran it without any error message generated. I will paste the new script below. Does anyone see any errors?
Thanks


– Begin user input variables

– Table of window and door sensors ID# with descriptor
local wdSensorIdPair = {
[35]=“LwHall_D”, [38]=“UpBed_W”, [41]=“GR_Lanai_D”, [42]=“UPHall_W”, [49]=“GRFrnt_D”,
[51]=“LwHall_W”, [60]=“LwFrtBRDr”, [61]=“LwBkBR_D”, [66]=“LW_MastBR_D”
}

– Table of motion sensors ID# with descriptor
local motionSensors = {
[58]=“GR_Foyer_MD”, [59]=“LH_MD”, [63’]=“GR_TV_MD”, [64]=“Up_Mstr_BR_MD”,
[65]=“Lw_Mstr_BR_MD”
}

– Table of thermostats ID# with descriptor
local thermostats = {[4]=“GRThermostat”, [20]=“LHThermostat”}

– End user input variables

– Check if there are tripped sensors. If there are, then turn the thermostats off.
function checkForTrippedSensors()
– Assume that no sensors are still tripped.
local wdSensorTripped = false

for k, v in pairs(wdSensorIdPair) do
    local tripped = luup.variable_get("urn:micasaverde-com:serviceId:SecuritySensor1", "Tripped", k) or "0"
    if (tripped == "0") then
        luup.log("Sensor '" .. v .. "' is not tripped.")
    else
        luup.log("Sensor '" .. v .. "' IS TRIPPED!")
        wdSensorTripped = true
        break
    end
end

if (wdSensorTripped) then
    for k, v in pairs(thermostats) do
        luup.log("Turning thermostat '" .. v .. "' OFF.")
        luup.call_action("urn:upnp-org:serviceId:HVAC_UserOperatingMode1", "SetModeTarget", {NewModeTarget = "Off"}, k)
    end
end

end

local now = os.date(“*t”)
– If it’s between 9 AM and 5 PM then check the motion sensors.
if (now.hour >= 9 and now.hour <= 17) then

-- Assume that no motion sensor has been tripped in the last 20 minutes.
local motionSensorTripped = false

-- Check when was the last time each motion sensor was tripped.
for k, v in pairs(motionSensors) do
    local lastTrip = luup.variable_get("urn:micasaverde-com:serviceId:SecuritySensor1", "LastTrip", k)
    lastTrip = tonumber(lastTrip, 10) or os.time()
    if (((os.time() - lastTrip) / 60) < 1200) then
        motionSensorTripped = true
        break
    end
end

--  If no motion sensor has been tripped in the last 20 minutes (1200 seconds), then turn off the AC.
if (not motionSensorTripped) then
    for k, v in pairs(thermostats) do
        luup.log("Turning thermostat '" .. v .. "' OFF.")
        luup.call_action("urn:upnp-org:serviceId:HVAC_UserOperatingMode1", "SetModeTarget", {NewModeTarget = "Off"}, k)
    end

    return -- AC has been turned off: don't check the door and window sensors anymore.
end

end

– Assume that no sensors are tripped.
local wdSensorTripped = false

– If any sensor is tripped, set the global variable for tripped status.
for k, v in pairs(wdSensorIdPair) do
local tripped = luup.variable_get(“urn:micasaverde-com:serviceId:SecuritySensor1”, “Tripped”, k) or “0”
if (tripped == “0”) then
luup.log(“Sensor '” … v … “’ is not tripped.”)
else
luup.log(“Sensor '” … v … “’ IS TRIPPED!”)
wdSensorTripped = true
break
end
end

– If there was any tripped sensor, check again in 1 minute if there still are tripped sensors.
if (wdSensorTripped) then
luup.call_delay(“checkForTrippedSensors”, 60, “”)
end

/opt/local/bin/lua: stdin:11: unfinished string near '']="GR_TV_MD", [64]="Up_Mstr_BR_MD",'

It’s the stray apostrophe near the 63.

Thanks!
That got rid of the error message. Now we will see if it runs okay.

My electric bill have been running $650-750, down from my highest of $1150/mo. That was before I implemented the additional Luup code to shut off the AC if none of the 5 motion detectors (2 bedrooms, 1 great room, 1 upper hall, 1 lower hall) trip for more than 20 minutes. I hope that this will lower my electric bills even more now. (I have also set my minimum temp to 74F.)

I had to implement this code when I was not staying at the house so can’t be sure how it is working. I have had 2 groups of guests there so far and neither has complained about the AC shutting off.

When I look at “My Alerts”, I see that once or twice per day, both of my thermostats turn off at the same time. That indicates that they are being shut off by my scene with the above code. Is there some additional code that we can add to inform me why the thermostat was shut off, i.e. window/door sensor tripped > 1 min or no motion for > 20 min?

Also, is there a way to generate a report showing how much time my AC is on during a day?

BTW
It would be nice if someone could make a plugin for the market place with this luup code. I am sure that many other rental home owners would like to implement this type of scene to save them money.

Note: This and your current strategy will turn off the AC at 9:00 if people sleep in (no motion)
You could do this with the Program Logic Event Generator (PLEG) plugin and a scene that only turns off the thermostats.

For each Motion Sensor create a trigger for the PLEG using the Triggers tab.

Then in the PLEG Settings tab create a condition expression:
AutoOff = (Motion1; NOW > 30:00) and (Motion2; NOW > 30:00) … and (MotionN;Now) > 30:00) and (09:00:00 < NOW < 17:00:00)

Then trigger your thermostat off scene when the PLEG device satisfies condition AutoOff

No LUUP code!

I just learned that one can add luup code to send an alert to “Vera Alerts”.
I modified the following section:

if (wdSensorTripped) then
for k, v in pairs(thermostats) do
luup.log(“Turning thermostat '” … v … “’ OFF.”)
luup.call_action(“urn:upnp-org:serviceId:HVAC_UserOperatingMode1”, “SetModeTarget”, {NewModeTarget = “Off”}, k)
luup.call_action(“urn:richardgreen:serviceId:VeraAlert1”, “SendAlert”,
{Message = “Wind/Door Tripped AC Off {tone:1}”, Recipients = “3vp209j1sjz”}, 67)
end

It worked very well and told me that the AC was shut off because a window or door was open for more than 1 minute. I was able to view my devices on the vera desktop and watched it work perfectly. Unfortunately, I now have to remove that line of code because every 5 minutes when this scene is run, it again tries to turn off the AC and notifies me of such, if a door or window is still open. At least it helped me confirm that that portion of the code works!
I also inserted the same line of code in the loop that shuts off the AC if a motion detector hasn’t been tripped for more than 20 min between 9 AM and 7 PM. I will have to remove that extra line of code to avoid repetitive notices but it can help me to confirm that the luup program code is working.

Addendum
I added vera alert message to the luup code segment that turns off the AC if there is no motion for 20 min about 3 days ago. Today was the first day that I started to receive the messages that the luup code was attempting to turn off my AC. The house was occupied until yesterday afternoon. I am surprised that it didn’t start messaging me yesterday afternoon or earlier today until it finally started around 5 pm. Can shadows from clouds trip ZIR000 motion detectors? Without the vera alert messaging I couldn’t tell if it was working because I can’t read my logs remotely. I had to remove the vera alert code because it was messaging me every 5 minutes!

RTS noted that my AC will shut off at 9 AM if someone is sleeping. That is probably true though I have a motion sensor facing the bed in both master bedrooms. In Hawaii, most guests are up very early because they are from West Coast or further East time zones so guests rarely sleep past 9 AM.
Thanks for the suggestion and thanks for your modifications to Vera Alerts!

I also learned about the DataMine plugin. Once I get back to my vacation home and plug a USB drive into Vera, I hope to use that plugin to track/graph both of my thermostat’s ON and OFF modes and average temperatures. I will also try the google weather plugin to include outside temp for comparison purposes.

I get “ERROR : Error in lua for scenes and events” I pasted the code and changed the device numbers into the LUUP of several doors/window triggers i have set up in the GUI. Do I need to do anything to the lua below, beyond change the device ID if I paste it into each of the triggers luup events?


os.execute(“sleep 300”)

if( luup.variable_get(“urn:micasaverde-com:serviceId:SecuritySensor1”, “Tripped”,33)==“0” ) then
luup.log(“Sensor Not Tripped, ending scene”)
return false
end

os.execute("sleep 300")

This should NOT be called in your code. You are basically telling ALL of VERA to shut down for 5 minutes and do NOTHING!!!

It might be timing out and reporting this as an error.

Thanks for the info. What should I use to get the results I need?

What is the purpose of the SLEEP ?

There are options for delayed actions:

[ul][li]Countdown Plugin[/li]
[li]Program Logic Timer Switch Plugin[/li]
[li]Program Logic Event Generator Plugin (Generating a delayed event)[/li][/ul]

These all use safe mechanisms in Vera (luup.call_delay and luup.call_timer) to have an action completed at a later point in time.
In general your lua code should execute in it’s entirety in a few seconds … for most plugins execution is usually a small fraction of a second.