case PowerHome formula function
Description
Tests the values of a expression and returns values based on the results of the test.
Syntax
case ( expression WHEN value1 THEN result1 { WHEN value2 THEN result2 { ... } } { ELSE resultelse } )
Argument Description
expression Expression. An expression whose values you want to test. Expression is compared to each valuen in turn until success or ELSE. The expression can evalute to nearly any datatype including string, numeric, date, time, datetime, or boolean. The datatype of the expression MUST match the datatype of the value list.
WHEN (optional) Introduces a value-result pair. At least one WHEN is required.
valuen Value with same datatype as expression. One or more values that you want to compare to values of expression (value datatype must match expression datatype). A value can be:
A single value
A list of values separated by commas (for example: 2, 4, 6, 8 or: "Play","Pause","Mute")
A TO clause (for example: 1 TO 20 or: "AAA" TO "BBB")
IS followed by a relational operator and comparison value (for example: IS>5 or: IS<"DDD")
Any combination of the above with an implied OR between expressions (for example, 1,3,5,7,9,27 TO 33, IS>42)
THEN Introduces the result to be returned when expression matches the corresponding valuen
resultn Expression. An expression whose value is returned by Case for the corresponding valuen. All resultn values must have the same datatype.
ELSE (optional) Specifies that for any values of expression that don't match the values of valuen already specified, Case returns resultelse.
resultelse Expression. An expression whose value is returned by Case when the value of expression doesn't match any WHEN valuen expression.
Return value
The datatype of resultn. Returns the result you specify in resultn.
Usage
If more than one WHEN clause matches the expression, Case returns the result of the first matching one.
Examples
The following examples demonstrate typical syntax/usage for this function.
• case([TEMP2] when 17 TO 20 then 4 Else 1)
• case([LOCAL1] when 1 then "DriveCam" when 2 then "HouseCam" when 3 then "PorchCam" when 4 then "TheaterCam" Else "Unknown Cam #[LOCAL1]")
• case({CURTEMP} WHEN is> 50 THEN "1" WHEN is> 30 THEN "3" ELSE "999")
• case(ph_getinsteonstat("[LOCAL2]") when 1 then "ifastoff" when 2 then "ifaston" else "?")
• case("[TEMP10]" WHEN "Play" THEN 1 WHEN "Pause" THEN 2 WHEN "Mute" THEN 3 ELSE 999)
• case(now() WHEN time("10:00:00) TO time("13:25:00") THEN 1 WHEN time("15:00:00) TO time("17:45:00") THEN 2 ELSE 0)