Prevent Same Rule from running more than once at a time

I’m using a rule to auto close my driveway gate, but that really doesn’t matter.

There are some scenarios in my situation where I can get the system a bit confused as it can have the same rule running more than once at a time.

Is there a way to set a rule so that if the rule start trigger is initiated, it will stop all other cases of this rule that may be currently running to prevent potential conflicts with itself?

I usually do this by creating a variable that gets set to “on” at the start of the rule.
It only runs if the variable is “off”. At the end of the rule I turn it back “off” so it can run the next time.

You can also reset the variable after a time period.


That is quite logical, however, I’m trying to do it just a little bit differently.

lets say my trigger starts at 12:00. The rule starts iteration “A”.
Lets say at 12:01 my trigger event happens again. If this is the case, I want the rule to no longer finish Iteration “A”, and instead start the rule all over again from the beginning (Iteration B).

In your example, B wouldn’t run, but A would finish running. I want the opposite. I want A to stop running and run B from the beginning.

You can still reach that with variables, it just gets a bit more complicated.

Could you explain the full use of your rule, it would make it easier to think about it and not get into “yes, but then I need…”

Sure…I"m going to simplify it just a bit as I have some other elements to my rule that have nothing to do with this.

I have it on a driveway gate (same concept as garage door). My clickers are connected to a smart relay which in turn sends the “push button” signal to the gate.

The Smart Relay is my trigger.

When the rule is triggered, it waits for 1 minute. After that time, the rule looks at my “Gate open” sensor to see if the gate is open. If it is open, sends the “push button” signal to the relay again to close the gate.

When it does close the gate, the rule is triggered again, but this time after 60 seconds it sees the gate is closed so ends the rule right there.

Where it gets tricky…there are times where I walk through the gate to go to my car. From the time I open the gate, get in my car and drive up to the gate, it maybe takes 30-40 seconds. Because my gate is old and doesn’t have any sensors on it, the gate will close on a vehicle and damage it pretty bad.

So, rather than wait the 30-40 seconds for the gate to close on it’s own, and then I re-open it, I would like to press the clicker button to close the gate right then and immediately press it again to reopen it, starting my 60 second cycle all over again.

HOWEVER, with how it currently runs, if I do this, my very first iteration of the rule is still running, and if timing is just right, the system can automatically close my gate while I’m inside it’s path.

Clear as mud?

That’s clear yes. Now, first thing I would say is to never have a gate without security measures close automatically. I also have a gate, but there’s a sensor to check if anything is in between and am scared to have it close automatically.

But I can’t stop you, so here goes what I would do, I have something similar for my lighting near my gate.

It’s difficult for me to put this into words, but are you using the gate open sensor to close it again?
I’d run a variable in the button press sequence. The variable checks the need for the 60 second rule, I assume you would want to open the gate in other cases where it doesn’t close after 60 seconds.
You can turn the variable True at the button press and have another rule wait like a few minutes to turn it back false. Best practice is to use “variable state stays false for 2 minutes” → turn variable false.

Then have your gate open sensor, when the gate open state stays 60 seconds AND the variable is true, close gate. If you now open your gate in another fashion, it won’t close after 60 seconds.

Since the rule is run by the state of your open sensor, it won’t run randomly again, since the state has changed. Now it will always start counting from the point it was last fully open.

PS: what are you using as open and closed sensors? I have some basic window sensors, lightly shielded under the guideway for my gate, but it’s not very reliable… My open sensor is closed at the moment, while the closed sensor is closed as well :sweat_smile:

1 Like

Ah…I think I’m following…I’ve not quite had enough coffee so will read this again in a few when I’m more alert, but I see where you are going on this one. Any yes, as you pointed out, this solution does allow for a scenario for the gate to open and stay open (which I currently have additional code for that I removed when simplifying my previous post)

‘don’t use gate without security measures’…I concur 100%. This is on my to-do list. We bought the house and it didn’t have any device. My gate, is compatable with them though, but is a pretty old gate so need to sort compatibility with modern sensors, bla bla bla. That’s a “me” problem, not a “forum” problem…just something to sort.

I’m using zen-17’s on my gate. I one of the relays wired in series with the gate “button” and the remote clicker sensor. Press the remote sensor, and the zen-17 senses it, which in turn closes the relay on the zen17 which is connected to the “button” on the gate to open or close the gate.

My gate circuit board has the ability to see when the gate is fully open via the status. I have the gate’s circuit board status signal wired to my other relay on the zen17. When the gate is fully open, the zen17 sees a closed circuit on its input which my smart home situation sees as an “open gate”. This half of my zen17 I only use the input/sensor portion (S2). The corresponding relay (R2) for this sensor, is connected to nothing.

Super helpful. I’ll real through all this later this morning and see what I can’t sort out. I have yet to mess with variables in Sharptools (still pretty new to the site), but I’m sure I can sort it.

Thanks! This post was exactly what I needed to look at my problem from a different angle. In my specific situation i didn’t need to include any variable, but it was the simple “has stayed” feature looking at my gate open sensor that did the trick for me. By including that little bit in there, the other iterations still do run in the background, but any other background running rule won’t be able to pass the “has stayed” portion of my IF statement and thus all continue running, but down my “else” path which is the end the rule with no commands.

I feel foolish for not having thought of this before. This issue has stumped me for a while and had such a simple solution.