I have an oil diffuser plugged into a GE lamp module, and I want it to come on for 30 minutes when there is motion in my living room. However, I only want this piston to run once every so often. In other words, if it has already been on at any point in the last 90 minutes, I don’t want it to turn on again regardless of motion.
What I came up with is a virtual switch called withinHour. This boolean is defined as false, and as you can see, it changes to true after the lamp module is turned off. Then, the piston waits for 90 minutes and changes the variable back to false. Would the piston even run again if the first one is still running (i.e. during the “wait” periods)
This doesn’t sit well with me, though. I can’t help but think there’s a more efficient way of handling it. Suggestions?
Two thoughts, with slightly different effects. Maybe you could add a variable lastActivated and set it to $now inside your with block. Then add a new condition to your if: ($now - lastActivated) > 90 * 60 * 1000 (90 minutes in milliseconds).
Or if you really want at least 90 minutes between each run of the oil diffuser, maybe add these conditions to your if instead of checking the variable: Oil Diffuser's switch is off and age([Oil Diffuser: switch]) > 90 * 60 * 1000. This seems closest to what you want and doesn’t require another variable.
Personally, I like making it clear to the reader how the product was arrived at. But probably best to put it in a const integer variable rather than inline to be evaluated every time as I did, though. That ought to avoid re-evaluating it once the piston is initialized, right?
If I simply put “age([Oil Diffuser: switch]) > 90 * 60 * 1000” into the Expression box, it evaluates true/false as expected, but then webcore forces me to select the type of comparison. So I input it as shown above. Unfortunately, I’m not home to test.
I think you’ll want to either change your “Compare to” to be an Expression rather than Value, or use the actual numeric value of the expression as the Value.