Best way to make a task conditionally run for 20 minutes every hour?

Hi all - I have a fresh air intake for a single room that isn’t always occupied. I want to make the fresh air intake runs 20 minutes every hour, but only when the room is occupied, and only for up to 4 hours once it is vacated.

Example scenario: Room hasn’t been used in days, no fresh air. Someone goes into the room at 1pm at sits down. Fresh air intake starts and is set to run until 5pm. At 2pm the person gets up and moves around but is still in the room, so since the motion saw him move at 2pm it will now run the fresh air until at least 6pm. Person gets up at 2:30pm and leaves the room. Fresh air will now run until 6:30pm. No one else enters the room so motion is not tripped again. At 6:30pm the fresh air intake is shut off. Process then repeats the next time someone enters the room.

For the hardware side - I will use a motion sensor to detect last time of occupancy. And I will use a dry contact to trigger the motorized damper to open/close. I have the hardware part of the puzzle solved, but not sure if I can accomplish the above using existing smartapps, SmartThings and or IFTTT, or if I need to write something myself?

The logic for how I want things to work would look something like this (pseudo-code):

IF motion is detected THEN

IF motion not detected for 4 hours then
STOP RUN_JOB from running (somehow?)

send alert/log message "Room occupied, turning on fresh air"
time_until_room_is_considered_empty = now + 4 hours
while now < time_until_room_is_considered_empty DO
cycle_time=20 minutes
run_this_cycle_until = now + cycle_time
while now < run_this_cycle_until do
send alert/log message "Time to bring in fresh air for " + cycle_time + " minutes."
send signal to Z-Wave plug to switch on, so it powers 24VAC connected to damper to open damper
look for an override signal (somehow???) that may be sent to say to quit, and exit this loop if so
end do
send alert/log message "Fresh air for " + cycle_time + " minutes completed, closing damper, waiting for next time to run"
end while

I’m not sure if this is the right way to do things within the SmartThings universe, but the pseudo code will give you an idea of the functionality. Also I’m not sure if the job should keep track of its time or it should run forever until and outside job stops it somehow… And as you can see in the pseudo code I need a way to tell it to stop by sending it a z-wave command.

Any advice or pointers is greatly appreciated as to the best way to go about this. Thank you!!

1 Like

This is the sort of requirement that highlights the value of the Platform’s ability to allow us to write completely custom SmartApps (in Groovy); along with virtual Device Type Handler(s), etc…

So if you have coding experience (particularly in something Java or Ruby-ish), and want to make this work exactly as you wish, custom programming is likely the most satisfying…

Rule Machine (@bravenel) suddenly opens up a world of possibilities with Triggers, multiple boolean Conditions, override / cancellation of actions, etc., etc…

My guess is that Rule Machine can definitely handle 80%+ of your requirements (quite possibly 100%) and the remaining portion might even be accomplished by interaction with a Virtual Device and a simple SmartApp to handle the special case(s).

In the process, you / we would further learn the level of complexity that RM can handle.

I don’t think one way is particularly better than the other. It really depends on your experience. Custom SmartApp is typical IDE editor line by line coding; Rule Machine is not. The latter can be harder to grasp and debug, but the former leads you open to syntax errors and other complications.