Can you help me make this simple piston more efficient?

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.

Was just about to suggest this as well. Simplify the math tho and just make it 5,400,000

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?

Totally understand explaining the 1000 milliseconds * 60 seconds * 90 minutes. Cause not everyone would get that

As for your other question, if there is one, I do not know what you are trying to ask.

Thank you all for chiming in. I guess my problem here is with Webcore and how it behaves. Is this the correct way to input it into Webcore?

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.

AH! that makes more sense!

This looks like it will work
Capture

Hey aflores3,

Can you please give us a link to the diffuser your using. Everything I have found requires a button to be pushed after power is reconnected.

Do you now have your piston working the way you want?

Sorry! I haven’t checked these forums in a while.

This is the diffuser I use. I bought it off of ebay from China, though, and I’m sure I didn’t pay more than $35-$40 for it.

https://www.amazon.com/dp/B00OXDKG8Q

I have it the way I want it now, yes.

I’ve added a safety measure on line 32 to make sure that the diffuser never runs more than 60 minutes regardless of how it is turned on.

So, the only thing that I think is missing would be should the diffuser still run when everyone leaves the living room?

Also, is there any reason to use the age expression instead of “was off”?