Difference between If and While in WebCore


#1

I have done some programming before and I understand the difference between an “if” and “While loop”.
However can someone explain the difference in WebCore?

It would appear to me that if I write something along the lines of:

If temperature sensor is <18 degrees then turn heater on.
Else
Turn heater off

Works exactly the same as:

While temperature sensor is <18 degrees then turn heater on.
End While
Turn heater off

In programming the IF would be a one hit and would move on once executed. However in WebCore it subscribes and therefore does not move until expression is false.

So what is the difference and in what situation should each command be used?
.


#2

If your IF is a trigger (my preferred method)… it is only true for a brief moment. It fires once, the code runs top to bottom, and the piston stops. It will run again when the device’s attributes changes again… On the other hand, if your IF is a condition (like your example above) and there are no other triggers in the piston, that condition is treated like a vague trigger, and may actually be true for some time. (I usually avoid this vague logic)

A while loop does nothing until a trigger starts the execution… and then keeps looping until the condition changes…


Generally speaking, I want my code to execute promptly, run swiftly, and then stop.

Looping in a SmartHome should be used as infrequently as possible… If I must use it, I code specifically to keep it as short of an execution as possible.


Side Note:

Personally, I would never use either of your two examples in my house, because it takes away the households free-will. (not to mention the turn on, turn off, turn on, turn off while the temp hovers around 18 degrees)

I usually use this method:

IF Temp drops below 18 degrees
    Then turn on heater
END IF

IF Temp rises above 20 degrees
    Then turn off heater
END IF

This makes two clean & concise triggers… It only runs at a precise moment… and also allows for human interaction and flexibility.


#3

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 of 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.