It does seem strange doesn’t it?
Imagine a switch is off. A considerable time later it is turned on and then immediately turned off again. Assume this happens so quickly that by the time the piston starts up to process the on
event the switch is already off
. What happens when the piston is processing the on
event?
-
If the piston includes the trigger condition if switch changes to on
, what should it return? The switch did change to on
but it has already changed back to off
. Clearly the piston wanted to do something when the switch changed to on
. Should it still do that thing?
-
If the piston includes the condition if switch is on
, what should it return? The event says the switch is now on
, but if the state of the device is looked up in ST or HE it will say it is off
, and indeed in this scenario it is actually off
.
The actual behaviour is:
-
The piston respects the event data. The event data says the switch is on
so as far as the piston is concerned it is. If it believes the switch was previously off
then the switch has changed state and so it returns true
.
-
The piston still has to respect the event data. The event data says the switch is on
so the switch is on. It can’t have it both ways. So it returns true
. It will keep doing that for the duration of the piston execution, even if it sleeps for two hours in a wait
and the switch has changed state dozens of times since.
Note that I said 'If it believes the switch was previously off
'. How does it know that? It has already been established that the piston is governed by the events it receives so it is no use asking ST / HE as they don’t know what order the events were in when the piston processed them. So whenever the piston evaluates a changes
trigger condition it caches the event value. You could perhaps argue that the piston should cache that when it starts up, but it doesn’t, it caches it in each trigger condition when the condition is evaluated.
Now supposing the piston is actually a bit more complicated and says if time is between sunrise and sunset AND switch changes to on
. If last time the switch changed to off
was in the middle of the night the piston would have evaluated the time restriction as false
and so not bothered to check the trigger condition as it wouldn’t change the result of the AND
. So the off
value would never have been cached.
That is basically the mechanism in play. Does it make sense? Could it be done differently? I don’t really know. It may be the price you have to play for being able to work with events rather than just the current conditions. The one thing I can say is that if you put an explicit trigger condition in your piston (like changes
) you should make sure that it will always be evaluated if the trigger event happens.
Somehow I doubt that has made things any clearer.