Lights turning on at sunset

Hi all,

I’m a long time user of CoRE and just installed WebCoRE and it got me thinking about my lighting automations now that we’re at the height of summer.

I currently have a rule that is: “if motion is detected and its after 30 mins before sunset turn on the lights. after 30 mins of no motion turn them off”

In the winter, it gets dark pretty fast so I have my lights turn on 30 mins before sunset. However in the summer it takes a lot longer to get dark and 30 mins before sunset, it is already pretty dark! I’d say I need to increase the timing to about 2 hours.

My choices are:

  1. do something whereby I look at the month of the year and adjust the amount of time before sunset
  2. do something with the hour of sunset and adjust the amount of time before sunset
  3. use LUX although I’m not sure how reliable this is on my sensor. Example LUX from my logs today are below. Any advice on what approximate LUX to use as the threshold would be appreciated. Also, I’m assuming I can add something to the rule that says “if the lights are on, ignore the lux level” and just refresh the timer on new motion detection?
  4. anything else someone has come up with?

Example LUX from today:
5:56 - 189
6:26 - 126
6:56 - 86
7:26 - 42
7:56 - 34
8:26 - 17
8:56 - 7
9:26 - 2
9:56 - 0

You could do every day at sunset -30 minutes but only in the months of May, June, July, August, September and then another timer every day at sunset -60 minutes but only in the months of October - April.

Or you can use an expression for the offset :smiley:

($month < 5) || ($month > 9) ? -30 : -60
4 Likes

Ooooooo nice.

Could you get really clever and put it as an equation so it adjusts the number of minutes before sunset gradually? Sorry I’m not a code guy but I do know some maths :slight_smile:

y = 2x^2 - 24x + 100

minutes before sunset = (2 x $month ^2) - 24 x $month + 100

Example of what this looks like:

https://www.graphsketch.com/?eqn1_color=1&eqn1_eqn=2x^2-24x%2B100&eqn2_color=2&eqn2_eqn=&eqn3_color=3&eqn3_eqn=&eqn4_color=4&eqn4_eqn=&eqn5_color=5&eqn5_eqn=&eqn6_color=6&eqn6_eqn=&x_min=-17&x_max=17&y_min=-10.5&y_max=100&x_tick=1&y_tick=1&x_label_freq=5&y_label_freq=5&do_grid=0&do_grid=1&bold_labeled_lines=0&bold_labeled_lines=1&line_width=4&image_w=850&image_h=525

Well this just turned the lights on but don’t know if it a fluke!

Yeah except in programming multiplication is represented by an asterisk and power is **, but yeah, webCoRE does that kind of things :wink:

2 * $month**2 - 24 * $month + 100

Awesome. What’s the best way to check the time that is being returned by the expression? Just to test and see what time it is calculating. Store it to a variable?

It depends on how you calculate that time, but assuming value is a time or datetime variable, you could do this:

Log to console “Time is {time(value)}”

I think I cracked it. I had to multiply by 1000 and 60 to account for milliseconds and minutes. Here’s my example if anyone is interested. This will turn on the lights 2 hours before sunset in winter (December/January) and gradually adjust to 30 minutes in summer (June/July).

Number of minutes in each month can be seen in this graph:

https://www.graphsketch.com/?eqn1_color=1&eqn1_eqn=2.5x^2-30x%2B120&eqn2_color=2&eqn2_eqn=&eqn3_color=3&eqn3_eqn=&eqn4_color=4&eqn4_eqn=&eqn5_color=5&eqn5_eqn=&eqn6_color=6&eqn6_eqn=&x_min=-5&x_max=15&y_min=-10.5&y_max=150&x_tick=1&y_tick=1&x_label_freq=5&y_label_freq=5&do_grid=0&do_grid=1&bold_labeled_lines=0&bold_labeled_lines=1&line_width=4&image_w=850&image_h=525

There is a function addMinutes(datetime, integer). The integer is the number of minutes, can be a negative number, or you multiply the minutes by 60k

Ah…nice…

1 Like