Turning a light on before sunrise


1) Give a description of the problem
Trying to get a light to turn on 30 minutes (plus or minus 6 minutes) before sunrise

2) What is the expected behavior?
Each day the light will turn on between 36 and 24 minutes before sunrise.

3) What is happening/not happening?
Light is not turning on.

**4) Post a Green Snapshot of the piston!

5) Attach any logs (From ST IDE and by turning logging level to Full)


The problem is the use of a condition to trigger the piston. If you think about it, isn’t it always 30 minutes or more before sunrise?

Use a timer statement instead

every day, at 30 minutes before sunrise
Turn On Switch
end every;


Try using the trigger ‘happens daily at’ rather than the condition. ‘is before’ is ambiguous. If you want it turning on at a random time between 24-36 minutes before sunset, you will need to use an expression for your offset and generate a random number.


Thanks for the comments. New to webCore. Here is another followup question. Same topic just more specific. Let’s say sunrise is at 0800 and I want to turn on a light turned on randomly from 0642 to 0700 (which i think is sunrise -49 +/- 9 minutes). Would someone look at my screen shot and tell me what is wrong with my logic expression.


I know the wiki says

To output a random integer between 5 and 20 use (5 + random(15))

but there’s an undocumented method that also works and is simpler


(Note that this does not work with negative numbers — i.e., random(-5,-20). Not sure why, it just doesn’t.)

Anyway, I’m a little unclear on your precise intent, insofar as your description of what you’re trying to accomplish doesn’t appear to match the numbers or the formula you posted. So I’m just going to provide a couple of examples of the way you might use this.

  • If Sunrise is at 8:00 AM and you want the light to turn on randomly between 6:42 and 7:00 AM, then the expression -random(60,78) would work as an offset. You will get an offset between -60 and -78 minutes.

  • If Sunrise is at 8:00 AM and you want the piston to trigger between 6:51 and 7:09. Then you could simply set your offset as -random(51,69) You will get an offset between -51 and -69 minutes.

Again, it’s possible I misread your post, so feel free to clarify if necessary.


Thanks. Yes, I was imprecise in my description but this really helps. That is a lot simpler than what I was thinking of doing.


Glad that helps.

FWIW, I’ve never been sure whether the wiki needed to updated, or whether this capability was purposely omitted from the wiki for some reason (like it not working with negative numbers).

The wiki says the expression returns “a randomly selected argument if two or more arguments are provided,” which is a bit of doublespeak and doesn’t really make any sense. But I think it was in part that language, proposing the possible use of two arguments, that made me try this approach way back when.

Maybe @ipaterson knows something about this. I’ve meant to ask, but it was never really all that important. :grinning:


Very interesting! Given negative numbers rather than returning a negative integer it is returning a - negation operator and a positive integer. The expression evaluator doesn’t know what to do with that and shows undefined and attempting to assign to an integer variable results in 0.

I think the problem here is that the random function is receiving negative integer parameters as expressions rather than values. When called random(-5, -1) it gets two parameters, but they are each represented by parsed expressions – a pairing of the negation operator and the positive integer:

   [l:7, o:-, ok:true, t:operator], 
   [l:8, ok:true, t:integer, v:5]

Since those values are not integers it does the thing it normally does when given text parameters – chooses one of the parameters randomly and returns it as-is.

This is probably far too core of an issue to consider fixing, high risk of breaking other stuff. If you ever do need to use negative numbers it should be just fine if you store them to variables first.


Thanks for the insight. Now that you mention it, I remember using variables in place of integers successfully in one of my vacation lighting pistons, but it’s been a while.

Still oddly fascinated by the language in the wiki that states the function returns “a randomly selected argument if two or more arguments are provided.” My first thought was that It sounds like something that happens when I’m explaining something to one of the kids. :rofl:


Yep the docs seem outdated, currently that statement only applies to two non-integer non-decimal arguments (e.g. random('a', 'b') chooses either a or b) or three or more arguments of any type (e.g. random(1, 2, 4) returns either 1, 2, or 4). Seems the docs predate the decimal/integer range random(1, 10)


I have been testing the random function highlighted by bthrock and it seems to be working. Should I be using variables instead?


Or… challenge accepted! :sunglasses:


There’s no reason to use variables instead as long as your the numbers you put in the random function are positive. The discussion was mostly academic and had less to do with your particular circumstances and more to do with why the function wasn’t working in certain circumstances. Perhaps you would have been better served had we moved that discussion to a separate topic.

However, as you create larger and more complex pistons, using variables can make it much easier to modify your piston’s operating parameters, particularly if those parameters are used multiple times in different parts of the piston.