SWITCH vs IF statement for time based comparisons/second manipulation in virtual device time comparisons


#1

This is just a general which way should I do this and why type question…

I have a piston where I want to compare $now to a time range and then base the response on where the current time falls. For example…

Between 10 PM & Midnight light is 100%
Between Midnight & 2 AM light is 75%
Between 2 AM & 5 AM light is 50%

Since we are comparing to the same value it makes sense to me to use a switch statement. But when I try to design a switch statement using a virtual time device I get this dialog…

No virtual device for Time is available

However, if I use an IF statement I get this dialog…

Is this webCoRE’s way of telling me it’s a bad idea to use a SWITCH (and I may cause the heat death of the universe if I do), webCoRE never thought anyone would try this with a SWITCH statement, it wouldn’t work for some other reason, or for some reason it’s just better to us multiple IF statements and do each comparison individually.

Using multiple IF statements will work - it just doesn’t seem quite as elegant as a SWITCH statement.

More of a inquiring minds want to know question

==================

Follow up question. When using an IF statement you are presented with a dialog that allows you to set hours:minutes for the comparison:

However the code shows hh:mm:ss:

image

Is there a way to set the seconds in the evaluation to avoid overlaps (and webCoRE freaking over two incompatible statements)?

If I use a time comparison of

20:00 and 00:00

I get code

Time is between 8:00:00 PM and 12:00:00 AM

and a time comparison of 00:00 and 0500

I get code
Time is between 12:00:00 AM and 5:00:00 AM

Those two comparisons give me an overlap for each condition fro 00:00:00 AM until 00:00:59. It seems to me to be better to use 23:59:59 as the ending for the first statement to remove ambiguity from the time comparison.


#2

I believe there is no overlap with these two separate IFs:

IF Time is between 8:00:00 PM and 12:00:00 AM, Then do stuff
IF Time is between 12:00:00 AM and 5:00:00 AM, Then do other stuff

Well, there may be a millisecond overlap at precisely midnight, but the 59 seconds after midnight should only trigger the second statement.


#3

It might be safer to use 8 pm to 11:59:59 pm and 12 am to 4:59:59 am etc.
Also, as to your original question the Switch statement generally used when there is a single discrete value for each case rather than a range. That’s probably why it wasn’t implemented to allow a range.


#4

I don’t believe seconds can be used in a trigger, but I would love to see it made possible!


#5

On the switch statement for time, it can be not what you expect

Time is ever “exact” (except for 1 ms), so a switch statement based on times is likely to never match as the comparison will never be checked at the exact ms that it could be true (due to timing). Ie switch was a exact match and with time, exactness is difficult to match.


#6

A switch statement looks for specific values (each case statement is an ‘=’ comparison. It cannot be used for a range. Typically, use a switch statement when your variable can only have a discrete set of possible values. Use an if statement for more complex logic.


#7

By the way, before I forget, here is one way to use conditions to guarantee no overlapping:

IF $hour24 is between 20 and 23, Then do stuff, END IF
IF $hour24 is less than 5, Then do other stuff, END IF

This effectively translates to
8:00:00.000 PM thru 11:59:59.999 PM
and
12:00:00.000 AM thru 4:59:59.999 AM