Trying to understand Time functions


#1

1) Give a description of the problem
I am trying to build a pretty simple piston to use a motion detector for light activation. I have looked at the “sample” pistons which do this, and they seem to include a function that doesn’t have any reference in the WebCoRE function documentation.

It is the $Now() function, which (I assume) returns the current time, or does it return more? Where do I find reference to how this function is used or defined? Is there a “hidden” function directory for other functions that return values can be used in expressions (like, for example, the value of pi), or something else which depends on a system, or real-world state, but which doesn’t depend on a value that you provide as input to the function?


#2

$now is not a function, it’s a system variable. These are always available, and start with a $. $now is just the datetime right now. Most useful for with functions like addMinutes($now, 5) which would be 5 minutes from now.
https://wiki.webcore.co/Variable

WHen you are on the code page of a piston, all the system variables are listed right there.

Is there something in particular you are trying to do?

milhouse


#3

Thanks, milhouse…

I understand what you are telling me, but still think I’m missing something. When you say “on the code page of a piston” do you mean the piston I am trying to write? I’m feeling dense here, because when I’m trying to build the code line that will set the light level, depending on the time of day, I don’t find any sort of menu or pull-down that will plug the system variable into the expression.

I’ve done coding before (it’s been a long, long time), but it was all just type it in yourself, instead of this pick and plug-in stuff. I think It’s great that we can do that, but it gets confusing when the “simplified” method for building a command suddenly doesn’t work very well. Of course, I might just be doing something wrong.

I was just just trying to build a motion light piston pretty much like one of the “samples” in the wiki. I hate, hate, hate, just copying code when I’m learning, so was trying to mostly duplicate it by starting from scratch and writing a piston to do pretty much the same things.

I get to the point where I’m setting the light level after the light is turned on, and (per the sample) put an expression in there, using the inBetween() function. I guess I just need to “know” what the system variable is that I have to put into the expression, rather than being able to choose that variable from a menu.

Or am I missing even more here?


Convert UTC / Epoch Time to Human Time
#4

I was trying to answer your original question “is there a list of these system variables”. The screen shot was from looking at an existing piston.

I’ll me more specific, since you are actually writing a piston.
-Click the “+” for “new piston”
-create a blank piston in the pop up
-name it and click “create” in the next window
-add a new statement
-make it an If block
-add a condition
-in the “what to compare” dropdown, set it to “variable”
-in the dropdown next to it, you’ll see all the variables available. This includes variables you have created in the piston, global variables you’ve created to use across pistons, and system variables that start with $.

So for what you want, you could do a couple of things.
if motion changes to active
if time is before noon
with light set level to 30
else if time is after sunset
with light set level to 100
else
with light set level to 0
end if

Or, you could use an expression and simplify it. When you add a task, if the po
if motion changes to active
with light set level = if($now<$noon,30,(if($now>$sunset,100,0)))

image

Most specifically, when you are looking at the Do…popup window in this screenshot, you could change the dropdown to “variable” and see what all the variables are, then to expression to actually use them.

Hope this is a better explanation for you.

milhouse


#5

Many thanks milhouse!

That was an excellent explanation, and helped a lot. Once I got past that, I was able to also figure out why I was having trouble understanding the expression used in the example to choose the appropriate light level. In the example, instead of an “if… then… else” expression, it was “simplified” by using the ternary operator, and it took me a while to understand just what had been done.

All the best regards,
DaveK