Manually run piston, but listening to attribute changes


#1

1) Give a description of the problem
I am migrating from ST to HE and I guess I still suffer from phantom pains of things not happening on time all the time, so for most important actions I would like a piston to look like this

if switch is off then
switch.on()
end if
if switch.switch stays off for 1 minute then
notify “switch did not turn on - trying again”
switch.on()
end if

Now, as the piston is always executed manually, by the way of being called from other pistons, I thought of turning off the “subscribe to events” settings. However, apparently that also turns off the “stays for X minute” subscription". My concern is that if I turn subscription on, then the second if will execute whenever the switch stays off for 1 minute, whether it was triggered by this trigger or some other action.
I guess my question is about the mechanics of the piston execution. Here’s the workaround I though of

local var turningOn = true
if switch is off then
switch.on()
end if
only when turningOn is true and switch.switch is off
if switch.switch stays off for 1 minute then
notify “switch did not turn on - trying again”
switch.on()
end if

At this point I would expect the following behavior:

  1. if the piston was run from another piston, its execution started from the beginning, turningOn variable was set to true, and the whole thing executes properly.
  2. if the piston triggered by switch.switch staying off for 1 minute (not invoked by another piston), the turningOn is not set, so only when block will prevent any execution here.

Are these assumptions correct? Is there any easier way to get what I want here?

Thanks!


#2

Short answer: Yes, your second design is both necessary (for the reasons you gave) and should work as you want it to.
That said, the only when… could be simplified to

only when turningOn is true
  if switch.switch stays off for 1 minute then
  notify “switch did not turn on - trying again”
  switch.on()

or

if switch.switch stays off for 1 minute then
  if turningOn is true
    notify “switch did not turn on - trying again”
    switch.on()
  end if;
end if;

#3

Thanks for that. I presume the 1st snippet would be more efficient as the 2nd would spend a minute waiting just to realize that turningOn is false, right?


#4

It’s actually fairly close to a draw. The only statement that subscribes to events (and thus triggers the ifs) is If switch.switch stays off for 1 minute. The event that triggers the execution of the if is “the switch has been off for a minute” (rather than triggering at “the switch is off” and then waiting a minute).


#5

makes sense.