@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