Need help with SWITCH CASE and NULL (EMPTY) evaluation


#1

He guys I need help with my SWITCH CASE statement to evaluate empty variable in CASE

my code is something like this

switch ({[physicalFan :fanSpeed]}) <-- this returns 0,1,2,3 (but if the switch is off then empty
case 0
with

do

end with;
case 2
with

do

end with;

etc …

now what would be syntax for CASE if empty?

case “” ?
or can I create constant variable like offState = “” (I don’t even know how to set empty variable?)
and then
case offState?

right now I am handling it within default section

Thanks a bunch for any advice


#2

I would try something like:
IF case is not between 0 and 4
but there are a lot of alternate wordings to this condition.


#3

There’s an isEmpty() function also.


#4

I am still bit unclear with the syntax
it would be

case isEmpty() ? or case isEmpty ({[physicalFan :fanSpeed]}) ?
with …
do …
end with;

or something like

case IF case is not between 0 and 4
with …
do …
end with;

sorry I am just learning the syntax
I done bunch of programming in other languages


#5

i’d just use Default … as in:

case 0:
case 1:
case 2:
case 3:
default:


#6

Yes that is what I have done
I am just trying to learn and only for my interest would like to figure out how to achieve this :slight_smile:

isEmpty() did not work :frowning:


#7

Yes. Case is looking for a number and IsEmpty returns a boolean.
Anyway, you could use

If IsEmpty({[phicalFan: fanspeed]})
Then
...
Else
    Switch ...
    end switch
end if

#8

Yes this is good, I already tried several attempts and isEmpty never returns true :frowning:
Even if the fan speed = (empty) at this time isEmpty never returns true
Am I missing something?
I already verified
If device is off fanSpeed is nothing
If device is on fanSpeed is 1,2, or 3

image

+268ms ║Fan Level = 0
+269ms Fan Speed =
+269ms ║Fan Switch = off
+283ms ║Value is not EMPTY

#9

The function is case sensitive.


#10

Good point I missed this in this particular statement, however still never returns true

image

Here I expect not to be true as fanSpeed is 1

+490ms ║Fan Level = 32
+490ms Fan Speed = 1
+491ms ║Fan Switch = on
+505ms ║Value is not EMPTY

But here I expect to be true as fanSpeed =

+413ms ║Fan Level = 0
+413ms Fan Speed =
+413ms ║Fan Switch = off
+430ms ║Value is not EMPTY

Id this BUG?


#11

Also, I thought I read above that your attribute is spelled fanSpeed.


#12

Damn I thought this will be it but not :frowning: Still doesn’t work
I even tried the real name of the device instead of variable device with the real device assign to it
I also tried to create Boolean constant isTrue and assign true to is and used that
Nothing

image


#13

Post the full log when you evaluate so people looking can try and spot what’s not working.


#14

Ok I will thanks a bunch for trying to help me
I thought isEmpty() doesn’t work but I just created empty variable
image
and try that with my if statement and it is working fine

This works
image

So it must be the speedFan property issue that is never empty even if is showing empty :frowning:


#15

Here is the full on event statement


Right now I am only interested in last if else to get it working

I have bunch of work around like my default statement is one or I can find out switch is off or level is 0 I am just trying to solve it more for my interest as I am sure it should work fine :slight_smile:


#16

Here is the full log

+2ms ╔Received event [Master Bedroom Fan Low].switch = off with a delay of 72ms
+9892ms ║RunTime Analysis CS > 17ms > PS > 9805ms > PE > 69ms > CE
+9893ms ║Piston waited at a semaphore for 9764ms
+9896ms ║Runtime (57536 bytes) successfully initialized in 9805ms (v0.3.10e.20190628) (9894ms)
+9897ms ║╔Execution stage started
+9910ms ║║Comparison (enum) off is (string) on = false (1ms)
+9912ms ║║Cancelling condition #160’s schedules…
+9913ms ║║Condition #160 evaluated false (7ms)
+9914ms ║║Cancelling condition #159’s schedules…
+9915ms ║║Condition group #159 evaluated false (state changed) (9ms)
+9930ms ║║Comparison (enum) off is (string) on = false (1ms)
+9933ms ║║Comparison (enum) off is (string) on = false (1ms)
+9935ms ║║Comparison (enum) off is (string) on = false (2ms)
+9939ms ║║Cancelling condition #136’s schedules…
+9940ms ║║Condition #136 evaluated false (22ms)
+9941ms ║║Cancelling condition #135’s schedules…
+9942ms ║║Condition group #135 evaluated false (state changed) (25ms)
+9949ms ║║Comparison (integer) 0 is_equal_to (integer) 0 = true (1ms)
+9950ms ║║Cancelling condition #138’s schedules…
+9951ms ║║Condition #138 evaluated true (6ms)
+9952ms ║║Cancelling condition #137’s schedules…
+9953ms ║║Condition group #137 evaluated true (state changed) (9ms)
+9955ms ║║Cancelling statement #139’s schedules…
+9963ms ║║Skipped execution of physical command [Master Bedroom Fan].off([]) because it would make no change to the device. (3ms)
+9964ms ║║Executed [Master Bedroom Fan].off (5ms)
+9966ms ║╚Execution stage complete. (69ms)
+9967ms ╚Event processed successfully (9967ms)
8/1/2019, 1:47:33 PM +456ms
+1ms ╔Received event [Master Bedroom Fan].level = 0 with a delay of 79ms
+111ms ║RunTime Analysis CS > 19ms > PS > 50ms > PE > 42ms > CE
+114ms ║Runtime (57489 bytes) successfully initialized in 50ms (v0.3.10e.20190628) (111ms)
+114ms ║╔Execution stage started
+122ms ║║Evaluating switch with values [[i:91:null:0, v:[t:device, v:[:a639dc1215cfc4d9a78574dd97e2a7a1:], a:fanSpeed, d:1, vt:string]]]
+129ms ║║Comparison (dynamic) is (integer) 1 = false (1ms)
+131ms ║║Comparison (dynamic) is (integer) 2 = false (1ms)
+133ms ║║Comparison (dynamic) is (integer) 3 = false (1ms)
+136ms ║║Comparison (dynamic) is (integer) 0 = false (1ms)
+144ms ║║Calculating (string) isEmpty( + (string) >> (string) isEmpty(
+147ms ║║Calculating (string) isEmpty( + (string) ) >> (string) isEmpty()
+149ms ║║Comparison (dynamic) is (string) isEmpty() = false (1ms)
+151ms ║║Cancelling statement #172’s schedules…
+160ms ║║Skipped execution of physical command [Master Bedroom Fan High].off([]) because it would make no change to the device. (3ms)
+160ms ║║Executed [Master Bedroom Fan High].off (5ms)
+418ms ║║Executed physical command [Master Bedroom Fan Low].off() (254ms)
+419ms ║║Executed [Master Bedroom Fan Low].off (257ms)
+425ms ║║Skipped execution of physical command [Master Bedroom Fan Medium].off([]) because it would make no change to the device. (3ms)
+426ms ║║Executed [Master Bedroom Fan Medium].off (5ms)
+428ms ║║Cancelling statement #177’s schedules…
+433ms ║║Executed default CASE!
+434ms ║║Executed virtual command log (2ms)
+436ms ║║Cancelling statement #179’s schedules…
+452ms ║║Calculating (string) Speed value = + (string) >> (string) Speed value =
+454ms ║║Calculating (string) Speed value = + (string) >> (string) Speed value =
+457ms ║║Speed value =
+458ms ║║Executed virtual command log (1ms)
+468ms ║║Calculating (boolean) false == (boolean) true >> (boolean) false
+482ms ║║Calculating (string) Return from evaluation 1 = + (string) 1 >> (string) Return from evaluation 1 = 1
+486ms ║║Calculating (string) Return from evaluation 1 = 1 + (string)
+486ms ║║Return from evaluation 3 = >> (string) Return from evaluation 1 = 1
+486ms ║║Return from evaluation 3 =
+490ms ║║Calculating (string) Return from evaluation 1 = 1
+490ms ║║Return from evaluation 3 = + (string) 1 >> (string) Return from evaluation 1 = 1
+490ms ║║Return from evaluation 3 = 1
+493ms ║║Calculating (string) Return from evaluation 1 = 1
+493ms ║║Return from evaluation 3 = 1 + (string)
+494ms ║║Fan Level = >> (string) Return from evaluation 1 = 1
+494ms ║║Return from evaluation 3 = 1
+494ms ║║Fan Level =
+497ms ║║Calculating (string) Return from evaluation 1 = 1
+498ms ║║Return from evaluation 3 = 1
+498ms ║║Fan Level = + (string) 0 >> (string) Return from evaluation 1 = 1
+498ms ║║Return from evaluation 3 = 1
+498ms ║║Fan Level = 0
+501ms ║║Calculating (string) Return from evaluation 1 = 1
+501ms ║║Return from evaluation 3 = 1
+501ms ║║Fan Level = 0 + (string)
+501ms ║║Fan Speed = >> (string) Return from evaluation 1 = 1
+501ms ║║Return from evaluation 3 = 1
+501ms ║║Fan Level = 0
+501ms ║║Fan Speed =
+508ms ║║Calculating (string) Return from evaluation 1 = 1
+508ms ║║Return from evaluation 3 = 1
+508ms ║║Fan Level = 0
+508ms ║║Fan Speed = + (string) >> (string) Return from evaluation 1 = 1
+508ms ║║Return from evaluation 3 = 1
+508ms ║║Fan Level = 0
+508ms ║║Fan Speed =
+511ms ║║Calculating (string) Return from evaluation 1 = 1
+511ms ║║Return from evaluation 3 = 1
+511ms ║║Fan Level = 0
+512ms ║║Fan Speed = + (string)
+512ms ║║Fan Switch = >> (string) Return from evaluation 1 = 1
+512ms ║║Return from evaluation 3 = 1
+512ms ║║Fan Level = 0
+512ms ║║Fan Speed =
+512ms ║║Fan Switch =
+518ms ║║Calculating (string) Return from evaluation 1 = 1
+518ms ║║Return from evaluation 3 = 1
+518ms ║║Fan Level = 0
+518ms ║║Fan Speed =
+518ms ║║Fan Switch = + (string) off >> (string) Return from evaluation 1 = 1
+519ms ║║Return from evaluation 3 = 1
+519ms ║║Fan Level = 0
+519ms ║║Fan Speed =
+519ms ║║Fan Switch = off
+521ms ║║Calculating (string) Return from evaluation 1 = 1
+521ms ║║Return from evaluation 3 = 1
+521ms ║║Fan Level = 0
+522ms ║║Fan Speed =
+522ms ║║Fan Switch = off + (string) >> (string) Return from evaluation 1 = 1
+522ms ║║Return from evaluation 3 = 1
+522ms ║║Fan Level = 0
+522ms ║║Fan Speed =
+522ms ║║Fan Switch = off
+525ms ║║Return from evaluation 1 = 1
+525ms ║║Return from evaluation 3 = 1
+525ms ║║Fan Level = 0
+526ms ║║Fan Speed =
+526ms ║║Fan Switch = off
+526ms ║║Executed virtual command log (1ms)
+533ms ║║Comparison (boolean) false is_equal_to (boolean) true = false (1ms)
+535ms ║║Condition #183 evaluated false (5ms)
+536ms ║║Condition group #182 evaluated false (state did not change) (6ms)
+537ms ║║Cancelling statement #184’s schedules…
+542ms ║║Value is not EMPTY
+543ms ║║Executed virtual command log (1ms)
+562ms ║║Comparison (enum) off is (string) on = false (1ms)
+563ms ║║Comparison (enum) off is (string) on = false (1ms)
+565ms ║║Comparison (enum) off is (string) on = false (1ms)
+569ms ║║Cancelling condition #136’s schedules…
+570ms ║║Condition #136 evaluated false (23ms)
+571ms ║║Cancelling condition #135’s schedules…
+571ms ║║Condition group #135 evaluated false (state changed) (24ms)
+577ms ║║Comparison (integer) 0 is_equal_to (integer) 0 = true (1ms)
+578ms ║║Cancelling condition #138’s schedules…
+579ms ║║Condition #138 evaluated true (6ms)
+580ms ║║Cancelling condition #137’s schedules…
+580ms ║║Condition group #137 evaluated true (state changed) (7ms)
+582ms ║║Cancelling statement #139’s schedules…
+588ms ║║Skipped execution of physical command [Master Bedroom Fan].off([]) because it would make no change to the device. (2ms)
+588ms ║║Executed [Master Bedroom Fan].off (3ms)
+590ms ║╚Execution stage complete. (476ms)
+591ms ╚Event processed successfully (591ms)
8/1/2019, 1:46:59 PM +735ms
+2ms ╔Received event [Master Bedroom Fan Low].switch = on with a delay of 76ms
+9904ms ║RunTime Analysis CS > 23ms > PS > 9819ms > PE > 62ms > CE
+9905ms ║Piston waited at a semaphore for 9770ms
+9908ms ║Runtime (57571 bytes) successfully initialized in 9819ms (v0.3.10e.20190628) (9906ms)
+9909ms ║╔Execution stage started
+9921ms ║║Comparison (enum) on is (string) on = true (1ms)
+9923ms ║║Cancelling condition #160’s schedules…
+9923ms ║║Condition #160 evaluated true (6ms)
+9924ms ║║Cancelling condition #159’s schedules…
+9925ms ║║Condition group #159 evaluated true (state changed) (8ms)
+9928ms ║║Evaluating switch with values [[i:163:null:0, v:[t:device, v:[:e12f456611fb2a42b8e7640bbda7656b:], d:1, vt:string]]]
+9933ms ║║Comparison (dynamic) Master Bedroom Fan Low is (dynamic) Master Bedroom Fan Low = true (1ms)
+9935ms ║║Cancelling statement #167’s schedules…
+9977ms ║║Executed physical command [Master Bedroom Fan].setFanSpeed([1]) (36ms)
+9978ms ║║Executed [Master Bedroom Fan].setFanSpeed (38ms)
+9980ms ║║Cancelling statement #149’s schedules…
+9990ms ║║Skipped execution of physical command [Master Bedroom Fan Medium].off([]) because it would make no change to the device. (3ms)
+9991ms ║║Executed [Master Bedroom Fan Medium].off (5ms)
+10000ms ║║Skipped execution of physical command [Master Bedroom Fan High].off([]) because it would make no change to the device. (6ms)
+10001ms ║║Executed [Master Bedroom Fan High].off (7ms)
+10016ms ║║Comparison (enum) off is (string) on = false (1ms)
+10018ms ║║Comparison (enum) on is (string) on = true (1ms)
+10020ms ║║Comparison (enum) off is (string) on = false (1ms)
+10025ms ║║Condition #136 evaluated true (21ms)
+10026ms ║║Condition group #135 evaluated true (state did not change) (22ms)
+10032ms ║║Comparison (integer) 1 is_equal_to (integer) 0 = false (1ms)
+10034ms ║║Condition #138 evaluated false (5ms)
+10034ms ║║Condition group #137 evaluated false (state did not change) (7ms)
+10036ms ║╚Execution stage complete. (128ms)
+10037ms ╚Event processed successfully (10037ms)
8/1/2019, 1:46:59 PM +432ms
+1ms ╔Received event [Master Bedroom Fan].level = 32 with a delay of 86ms
+111ms ║RunTime Analysis CS > 19ms > PS > 40ms > PE > 51ms > CE
+114ms ║Runtime (57456 bytes) successfully initialized in 40ms (v0.3.10e.20190628) (112ms)
+115ms ║╔Execution stage started
+123ms ║║Evaluating switch with values [[i:91:null:0, v:[t:device, v:[:a639dc1215cfc4d9a78574dd97e2a7a1:], a:fanSpeed, d:2, vt:string]]]
+130ms ║║Comparison (dynamic) 1 is (integer) 1 = true (2ms)
+132ms ║║Cancelling statement #96’s schedules…
+261ms ║║Executed physical command [Master Bedroom Fan Low].setLevel([32]) (123ms)
+262ms ║║Executed [Master Bedroom Fan Low].setLevel (125ms)
+266ms ║║Cancelling statement #98’s schedules…
+277ms ║║Skipped execution of physical command [Master Bedroom Fan Medium].off([]) because it would make no change to the device. (4ms)
+278ms ║║Executed [Master Bedroom Fan Medium].off (6ms)
+284ms ║║Skipped execution of physical command [Master Bedroom Fan High].off([]) because it would make no change to the device. (3ms)
+285ms ║║Executed [Master Bedroom Fan High].off (5ms)
+289ms ║║Cancelling statement #179’s schedules…
+306ms ║║Calculating (string) Speed value = + (string) 1 >> (string) Speed value = 1
+310ms ║║Calculating (string) Speed value = 1 + (string) >> (string) Speed value = 1
+314ms ║║Speed value = 1
+315ms ║║Executed virtual command log (1ms)
+328ms ║║Calculating (boolean) false == (boolean) true >> (boolean) false
+345ms ║║Calculating (string) Return from evaluation 1 = + (string) 1 >> (string) Return from evaluation 1 = 1
+350ms ║║Calculating (string) Return from evaluation 1 = 1 + (string)
+350ms ║║Return from evaluation 3 = >> (string) Return from evaluation 1 = 1
+350ms ║║Return from evaluation 3 =
+355ms ║║Calculating (string) Return from evaluation 1 = 1
+355ms ║║Return from evaluation 3 = + (string) 1 >> (string) Return from evaluation 1 = 1
+355ms ║║Return from evaluation 3 = 1
+360ms ║║Calculating (string) Return from evaluation 1 = 1
+360ms ║║Return from evaluation 3 = 1 + (string)
+360ms ║║Fan Level = >> (string) Return from evaluation 1 = 1
+360ms ║║Return from evaluation 3 = 1
+360ms ║║Fan Level =
+365ms ║║Calculating (string) Return from evaluation 1 = 1
+365ms ║║Return from evaluation 3 = 1
+365ms ║║Fan Level = + (string) 32 >> (string) Return from evaluation 1 = 1
+365ms ║║Return from evaluation 3 = 1
+365ms ║║Fan Level = 32
+370ms ║║Calculating (string) Return from evaluation 1 = 1
+370ms ║║Return from evaluation 3 = 1
+370ms ║║Fan Level = 32 + (string)
+370ms ║║Fan Speed = >> (string) Return from evaluation 1 = 1
+370ms ║║Return from evaluation 3 = 1
+370ms ║║Fan Level = 32
+370ms ║║Fan Speed =
+377ms ║║Calculating (string) Return from evaluation 1 = 1
+377ms ║║Return from evaluation 3 = 1
+377ms ║║Fan Level = 32
+377ms ║║Fan Speed = + (string) 1 >> (string) Return from evaluation 1 = 1
+377ms ║║Return from evaluation 3 = 1
+377ms ║║Fan Level = 32
+377ms ║║Fan Speed = 1
+381ms ║║Calculating (string) Return from evaluation 1 = 1
+381ms ║║Return from evaluation 3 = 1
+381ms ║║Fan Level = 32
+381ms ║║Fan Speed = 1 + (string)
+381ms ║║Fan Switch = >> (string) Return from evaluation 1 = 1
+381ms ║║Return from evaluation 3 = 1
+382ms ║║Fan Level = 32
+382ms ║║Fan Speed = 1
+382ms ║║Fan Switch =
+387ms ║║Calculating (string) Return from evaluation 1 = 1
+387ms ║║Return from evaluation 3 = 1
+387ms ║║Fan Level = 32
+387ms ║║Fan Speed = 1
+387ms ║║Fan Switch = + (string) on >> (string) Return from evaluation 1 = 1
+388ms ║║Return from evaluation 3 = 1
+388ms ║║Fan Level = 32
+388ms ║║Fan Speed = 1
+388ms ║║Fan Switch = on
+391ms ║║Calculating (string) Return from evaluation 1 = 1
+391ms ║║Return from evaluation 3 = 1
+391ms ║║Fan Level = 32
+391ms ║║Fan Speed = 1
+391ms ║║Fan Switch = on + (string) >> (string) Return from evaluation 1 = 1
+391ms ║║Return from evaluation 3 = 1
+391ms ║║Fan Level = 32
+391ms ║║Fan Speed = 1
+392ms ║║Fan Switch = on
+395ms ║║Return from evaluation 1 = 1
+395ms ║║Return from evaluation 3 = 1
+395ms ║║Fan Level = 32
+395ms ║║Fan Speed = 1
+396ms ║║Fan Switch = on
+397ms ║║Executed virtual command log (1ms)
+405ms ║║Comparison (boolean) false is_equal_to (boolean) true = false (2ms)
+406ms ║║Condition #183 evaluated false (7ms)
+407ms ║║Condition group #182 evaluated false (state did not change) (8ms)
+410ms ║║Cancelling statement #184’s schedules…
+415ms ║║Value is not EMPTY
+416ms ║║Executed virtual command log (1ms)
+434ms ║║Comparison (enum) off is (string) on = false (1ms)
+436ms ║║Comparison (enum) on is (string) on = true (1ms)
+438ms ║║Comparison (enum) off is (string) on = false (1ms)
+442ms ║║Cancelling condition #136’s schedules…
+443ms ║║Condition #136 evaluated true (22ms)
+444ms ║║Cancelling condition #135’s schedules…
+445ms ║║Condition group #135 evaluated true (state changed) (24ms)
+452ms ║║Comparison (integer) 1 is_equal_to (integer) 0 = false (1ms)
+453ms ║║Cancelling condition #138’s schedules…
+454ms ║║Condition #138 evaluated false (7ms)
+455ms ║║Cancelling condition #137’s schedules…
+456ms ║║Condition group #137 evaluated false (state changed) (9ms)
+458ms ║╚Execution stage complete. (344ms)
+459ms ╚Event processed successfully (459ms)

#17

It may be returning a null rather than empty.
You could see if concatenating it with something will make the comparison work. Something like this:
If {[phicalFan: fanspeed]}+"" is equal to “” …
(I don’t have a device with a fan so I can test this myself. Sorry)


#18

Ah, you have put me on right track I will try few things tomorrow and let you know
I have quickly tried

and got (first speed was blank then speed was 1)

image


#19

@qoheleth
I got it working
image
If I used number function to convert null to 0 by adding 0 to any speed returned
It seemed like the speed returned 1,2,3 is string
As you thought when the switch was off the return value wasn’t blank, empty or 0 but most likely NULL. It is beyond my understanding why isEmpty() would not recognized NULL or webocore doesn’t have function to convert NULL like nvl( )
To get isEmpty working I followed your logic
{((concat([Master Bedroom Fan :fanSpeed],""))=="" ? “Is Empty” : “Is Not Empty”)}"
After this concat with “” concat([Master Bedroom Fan :fanSpeed],"") even isEmpty was returning TRUE

Thank all of you guys @eibyer @elf @WCmore @qoheleth for your input it is much appreciated !!!


#20

I am not sure this will explain everything, but I can share my observations from other pistons.

Depending on the wording of your code, the response received and/or the type of the data storage, the exact same response may be seen as either:

(not set) | empty?
"null"    | 4 characters
"0"       | zero
"[]"      | empty square brackets
""        | truly empty

Here is a quick snapshot showing three different results for the exact same query:

temp

and using a different query, only one result stays the same:

temp


This is one reason why I tend to store the data I want into a variable first, and then use that variable to decide which path to take. It is one extra line of code, but it allows me to funnel the data thru a variable, which keeps the results uniform. (if I avoid using a dynamic variable)

In my quick testing, I was not able to find a query that returned truly empty, but I have seen those results many times in the past… My educated guess is there is something similar going on with “case”


Pro Tip:
This post shows one reason why I always try to avoid coding with 0 whenever possible.
It is way too open for interpretation. (Notice 0 is both true and false)