Time of Day dependency

Preamble

I have a light in the kitchen which will turn on if someone enters the kitchen, but only if it is dark outside.

This page describes how to determine a particular time of day relative to the sun, and to determine a particular month of the year. This page does not describe the turning on and off of the switch.

Objective

My objective for turning on and off the lights is to only turn them on when it is not light enough in the kitchen.

It is not light enough inside, from 2 hours before sunset to 2 hours after sunrise. During the winter months, it is dark enough all day and all night.

I need to calculate when it is between 2 hours after sunrise to 2 hours before sunset, and whether it is a winter month. I also need to be able to store these conditions for use by other rules.

Overview

A virtual item of type ‘switch’ stores whether it is light or dark. It is set to ON if it is light enough outside, or OFF it is too dark. A rule is run to set the virtual switch ON or OFF.

“Light enough” is defined as

  • between 2 hours after sunrise to 2 hours before sunset, and
  • the month is October, November, December, January, February or March

“Too dark” is defined as not “light enough”.

Assumptions

The following details assume that already installed is

  • the Astro binding (which provides local sunset and sunrise times), and
  • the Expire binding (which turns the virtual log entry switch to ON after 4 hours)

Details

  • Create two items to store the sunrise and sunset times provided by the Astro binding (Note that you may already have these from when you installed Astro. I didn’t)
DateTime astro_sun_local_rise_start "Sunrise" {channel="astro:sun:local:rise#start"}
DateTime astro_sun_local_set_start "Sunset" {channel="astro:sun:local:set#start"}
  • In the same items file, create a virtual item of type Switch called vGood_Daylight. This will be set to ON if there is enough light in the kitchen, or OFF if too dark.

Switch vGood_Dayight "Enough light for working inside" <tod>

  • Create a rules file to set the vGood_Daylight to OFF or ON.
    • The virtual item only needs to be set on startup, and when it changes from dark to light, and v.v.
    • It will change from dark to light two hours after sunrise, and from light to dark two hours before sunset.
      • Two hours after sunrise can occur anywhere from 8am to 9:55am, which the second cron time reflects (checking every 5′ during this time)
      • Two hours before sunset can occur anywhere from 2pm to 4:55pm, which the first cron time reflects (checking every 5′ during this time)
    • Log an entry to track the changes.
    • To avoid a log entry every time a check is made to see if it has changed from light to dark, or v.v., I have created a switch which is set to OFF when the first log entry is made. This is set back on ON at system startup, and in the middle of the day, and in the middle of the night.
var logSubPackage = "daylight.rules"
var logEntry = "the kitchen light WILL turn on"
val extraTime = 2
rule "Calculate if it is good daylight"
when
// run at system start and during sunrise and sunset
System started or
Time cron "0 0/5 14-16 * * ?" or
Time cron "0 0/5 8-9 * * ?"
then
// Convert the Astro Items to Joda DateTime
var day_start = new DateTime(astro_sun_local_rise_start.state.toString)
var evening_start = new DateTime(astro_sun_local_set_start.state.toString)
// allow for a bit more light before preventing the light turning on
day_start = day_start.plusHours(extraTime)
evening_start = evening_start.minusHours(extraTime)
// always show the light from April to September, as not enough light enters the kitchen then
val currMonth = now.getMonthOfYear()
val lightMonth = currMonth < 4 || currMonth > 9
// Set vGood_Dayight to ON or OFF
if ((day_start < now && evening_start > now) && lightMonth) {
logEntry = "the kitchen light will NOT turn on when there is movement."
vGood_Dayight.sendCommand(ON)
} else {
logEntry = "the kitchen light WILL turn on when there is movement"
vGood_Dayight.sendCommand(OFF)
}
if (vShow_Log.state == ON ) {
logInfo(logSubPackage, "Currently, " + logEntry)
vShow_Log.sendCommand(OFF)
}
end

rule "Daylight System Startup Commands"
when
System started
then
// show Log results (of setting good light times)
vShow_Log.sendCommand(ON)
end

To see how the vGood_Daylight switch is used, see the page on turning on the light.

Return to Home Automation

Leave a Reply

Your email address will not be published. Required fields are marked *