# Turning a light on before sunrise

#1

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)
(PASTE YOUR LOGS HERE BETWEEN THE MARKS THEN HIGHLIGHT ALL OF THE LOGS SND CLICK ON THE </> ICON TO FORMAT THEM)

#2

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?

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

#3

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.

#4

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.

#5

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

random(5,20)

(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.

#6

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

#7

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.

#8

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.

#9

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.

#10

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)`

#11

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

#12

Orā¦ challenge accepted!

#13

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.