'Tis The Season (Almost)

Bit late to the party on this one. Lets just say im already getting ready for this year’s festivities.

I’ve taken work by @rlkoshak and the idea from @Steve40 and integrated them.

val logName = "Events"

rule "Calculate time of day state"
when
	System started or
	Channel 'astro:sun:local:civilDawn#event' triggered START or
	Channel 'astro:sun:local:daylight#event' triggered START or
	Channel 'astro:sun:local:civilDusk#event' triggered START or
	Channel 'astro:sun:local:civilDusk#event' triggered END or
	Time cron "0 0 12,0 * * ? *" // there is currently a bug where only one cron is triggered per rule so I've combined all three into one
then
	Thread::sleep(1000) // make sure we are a tad past midnight to give Astro a chance to recalculate DateTimes for today

	// Time of Day Calculations
	val long dawn_start = (vDawn_Time.state as DateTimeType).calendar.timeInMillis
	val long morning_start = (vDaylight_Time.state as DateTimeType).calendar.timeInMillis
	val long afternoon_start = now.withTimeAtStartOfDay.plusHours(12).millis
	val long dusk_start = (vDusk_Time.state as DateTimeType).calendar.timeInMillis
	val long night_start = (vNight_Time.state as DateTimeType).calendar.timeInMillis
	val long bed_start = now.withTimeAtStartOfDay.millis
	
	var curr = "UNKNOWN"
	
	switch now {
		case now.isAfter(dawn_start) && now.isBefore(morning_start):		curr = "DAWN"
		case now.isAfter(morning_start) && now.isBefore(afternoon_start):	curr = "MORNING"
		case now.isAfter(afternoon_start) && now.isBefore(dusk_start):		curr = "AFTERNOON"
		case now.isAfter(dusk_start) && now.isBefore(night_start):			curr = "DUSK"
		case now.isAfter(night_start):										curr = "NIGHT"
		case now.isAfter(bed_start) && now.isBefore(dawn_start):			curr = "BED"
	}

	if(vTimeOfDay.state.toString != curr) {
		logInfo(logName, "Current time of day is now " + curr)
		vTimeOfDay.sendCommand(curr)
	}
	
	// Time of Year Calculations
	val long xmas_dec_start = now.withDate(now.getYear(),12,1).withTime(0,0,0,0).millis		// 01/12 00:00
	val long xmas_dec_end = now.withDate(now.getYear(),12,31).withTime(23,59,59,999).millis	// 31/12 23:59
	val long xmas_jan_start = now.withDate(now.getYear(),1,1).withTime(0,0,0,0).millis		// 01/01 00:00
	val long xmas_jan_end = now.withDate(now.getYear(),1,6).withTime(0,0,0,0).millis		// 06/01 00:00
		
	curr = "UNKNOWN"
	
	switch now {
		case now.isAfter(xmas_jan_start) && now.isBefore(xmas_jan_end):		curr = "CHRISTMAS"
		case now.isAfter(xmas_dec_start) && now.isBefore(xmas_dec_end):		curr = "CHRISTMAS"
	}
	
	if(vTimeOfYear.state.toString != curr) {
		logInfo(logName, "Current time of year is now " + curr)
		vTimeOfYear.sendCommand(curr)
	}
end

This is currently showing as UNKNOWN for time of year. I’ve done it like this in case I want other times of year tracked. Although at the moment it’s only CHRISTMAS which I’m really interested in. It’s currently hiding the switches in my site map for the christmas lights. I will eventually write some rules which fire the light on automatically.

Idea being…

Outside lights ON if CHRISTMAS and DUSK and OFF at BED
Inside light ON if CHRISTMAS and someone is home (via presence detection) and only between certain hours and BED.

Thought I would share as i’ve take a lot from this community.

Chris

1 Like