I’ve belatedly come across this thread and thought I’d add my take.
Considering your first example (slightly reformatted):
if
temperature sensor is < 18 degrees
then
turn heater on
else
turn heater off
end if
This works as I’d expect an ‘if … then … else’ to. When the piston is fired and gets to this code it will either turn the heater on or turn it off and then it will continue with any further code or exit.
If the code is considered as representing a complete piston, the piston would, by default, fire every time the temperature sensor changes, which is probably what you would want. In the real world you might choose not to turn the heater on when it is already on, but if the heater equates to a smart switch, webCoRE would skip turning it on again anyway.
In reformatting the second example, I’ve corrected the then
to a do
and also added a wait
in the loop, as otherwise there is a pointlessly tight loop checking the comparison.
while
temperature sensor is < 18 degrees
do
turn heater on
wait 1 minute.
end while
turn heater off
Again, this works as I’d expect a ‘while … do’ loop to work. When the piston is fired and gets to this code it will loop around until the temperature reaches 18 or above, constantly turning on the heater, and then exit the loop and continue on its way, at which point it will turn off the heater and continue with any more code, or exit.
Again, considering the example as a complete piston, the piston would also be firing every time the piston changes, which doesn’t do any harm, but isn’t very elegant either.
The end result would be the same, but I would argue the if
example is the better solution as a standalone piston as it responds to temperature events and so only runs when there is something to do. The 'while` might make some sense as part of a larger piston which isn’t being triggered by temperature changes.
A while
makes more sense in the webCoRE / SmartThings environment for something like:
while
outdoor motion sensor is active
do
take a picture
wait ten seconds
end while
Here the while
is doing something useful with its time. It is working with the events.