Link to code on GitHub: https://github.com/RMoRobert/Hubitat/tree/master/apps/TimedSwitchHelper
I thought I'd share a small app I threw together to handle a situation I use quite a bit, virtual "timed switches" that I want tied to "real" switches. For example, if I have a virtual switch called "Timed Kitchen Fan," I want it to turn on Kitchen Fan ("real" switch) for the specified period of time, then turn it off. If I manually turn off "Timed Kitchen Fan" before the timer is up, I'd also like "Kitchen Fan" to turn off (and delete/reset the timer so it will start a new countdown if I turn it on again).
This is actually pretty easy to handle in Rule Machine:
However, I got tired of creating the same rule again and again, and since there's no way to clone existing rules (EDIT: there is now, but I still find this easier than writing a rule; also, the screenshot above is from an older version of Rule Machine that most people will no longer have, though could do something equally easy in current versions), I thought I'd write an app to make it easier. Note that my app does exactly what this rule does--it effectively turns off the "real" switch when the timer is up, but it actually does this by way of turning off the "timed" switch; turning off the timed switch is technically what triggers handles turning off the real switch. That way, you can turn off the timed switch yourself before the timer is up if you so choose. Here's my app:
For the "timed" switch, you can use anything, but I'd recommend using a virtual switch you create just for this purpose. Do NOT set the virtual switch to automatically turn off after any amount of time in the driver (unless for some reason you really need that for something else and also want it here)--this app handles that on its own. As of version 2, you can also use this app to simply turn off any (real or virtual) switch after it turns on and/or stays on for a certain amount of time.
See the second post in this thread, below, for screenshots of the current version.
Hope someone finds this useful! I may expand it in the future to add additional features (I have a virtual "Overnight Window Fan" switch I want to turn off at a specific time, not in a specific amount of time, for example, but I'm currently handling that with RM). Again, it's not too hard to do in RM, but this is a bit less clicking if you do this a lot.