[Deprecated] Design Pattern: Time Of Day

Unlikely and do you know it is an error? The rule doesn’t do anything if it doesn’t think the time of day had changed.

I have an issue using vTimeOfDay.

2018-07-07 01:16:31.591 [INFO ] [clipse.smarthome.model.script.lights] - Calculating time of day...
2018-07-07 01:16:31.807 [INFO ] [clipse.smarthome.model.script.lights] -  morning_start is 2018-07-07T06:00:00.000-07:00
2018-07-07 01:16:31.820 [INFO ] [clipse.smarthome.model.script.lights] -  night_start is 2018-07-07T23:00:00.000-07:00
2018-07-07 01:16:31.835 [INFO ] [clipse.smarthome.model.script.lights] -  bed_start is 2018-07-07T00:00:00.000-07:00
2018-07-07 01:16:31.840 [INFO ] [clipse.smarthome.model.script.lights] - vSunrise_Time.state is NULL
2018-07-07 01:16:31.858 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Error during the execution of startup rule 'Calculate time of day state': Could not
cast NULL to org.eclipse.smarthome.core.library.types.DateTimeType; line 35, column 33, length 35

The fact that vSunrise_Time.state is NULL makes me think that my binding does not work well.

Any idea of what I can do to progress in my debug?

Thank you by advance.

Do you have the astro building installed? Do you have a Thing defined? Is the right channel from the Thing linked to the Item?

Thank you for your reply.

You asked the good question.


I had home instead of local.

Can anyone post the weather.map file that is mentioned in the original post by rlkoshak?

String vTimeOfDay "Current Time of Day [MAP(weather.map):%s]" <tod>

Trying to figure out what that map looks like so that I can better follow this DP.


From:Comprehensive Wunderground using HTTP Binding Example

With a search for weather.map in the forum…



# Pressure: Note, if you have uninitialzed Items they may be transformed to "falling"

# Cloudy or not
Light\ Drizzle=true
Heavy\ Drizzle=true

Light\ Rain=true
Heavy\ Rain=true

Light\ Snow=true
Heavy\ Snow=true

Light\ Snow\ Grains=true
Heavy\ Snow\ Grains=true
Snow\ Grains=true

Light\ Ice\ Crystals=true
Heavy\ Ice\ Crystals=true
Ice\ Crystals=true

Light\ Ice\ Pellets=true
Heavy\ Ice\ Pellets=true
Ice\ Pellets=true

Light\ Hail=true
Heavy\ Hail=true

Light\ Mist=true
Heavy\ Mist=true

Light\ Fog=true
Heavy\ Fog=true

Light\ Fog\ Patches=true
Heavy\ Fog\ Patches=true
Fog\ Patches=true

Light\ Smoke=false
Heavy\ Smoke=true

Light\ Volcanic\ Ash=false
Heavy\ Volcanic\ Ash=true
Volcanic\ Ash=true

Light\ Widespread\ Dust=false
Heavy\ Widespread\ Dust=true
Widespread\ Dust=true

Light\ Sand=false
Heavy\ Sand=true

Light\ Haze=false
Heavy\ Haze=true

Light\ Spray=false
Heavy\ Spray=false

Light\ Dust\ Whirls=false
Heavy\ Dust\ Whirls=true
Dust\ Whirls=false

Light\ Sandstorm=false
Heavy\ Sandstorm=true

Light\ Low\ Drifting\ Snow=true
Heavy\ Low\ Drifting\ Snow=true
Low\ Drifting\ Snow= true

Light\ Low\ Drifting\ Widespread\ Dust=true
Heavy\ Low\ Drifting\ Widespread\ Dust=true
Low\ Drifting\ Widespread\ Dust=true

Light\ Low\ Drifting\ Sand=true
Heavy\ Low\ Drifting\ Sand=true
Low\ Drifting\ Sand=true

Light\ Blowing\ Snow=true
Heavy\ Blowing\ Snow=true
Blowing\ Snow=true

Light\ Blowing\ Widespread\ Dust=true
Heavy\ Blowing\ Widespread\ Dust=true
Blowing\ Widespread\ Dust=true

Light\ Blowing\ Sand=true
Heavy\ Blowing\ Sand=true
Blowing\ Sand=true

Light\ Rain\ Mist=true
Heavy\ Rain\ Mist=true
Rain\ Mist=true

Light\ Rain\ Showers=true
Heavy\ Rain\ Showers=true
Rain\ Showers=true

Light\ Snow\ Showers=true
Heavy\ Snow\ Showers=true
Snow\ Showers=true

Light\ Snow\ Blowing\ Snow\ Mist=true
Heavy\ Snow\ Blowing\ Snow\ Mist=true
Snow\ Blowing\ Snow\ Mist=true

Light\ Ice\ Pellet\ Showers=true
Heavy\ Ice\ Pellet\ Showers=true
Ice\ Pellet\ Showers=true

Light\ Hail\ Showers=true
Heavy\ Hail\ Showers=true
Hail\ Showers=true

Light\ Small\ Hail\ Showers=true
Heavy\ Small\ Hail\ Showers=true
Small\ Hail\ Showers=true

Light\ Thunderstorm=true
Heavy\ Thunderstorm=true

Light\ Thunderstorms\ and\ Rain=true
Heavy\ Thunderstorms\ and\ Rain=true
Thunderstorms\ and\ Rain=true

Light\ Thunderstorms\ and\ Snow=true
Heavy\ Thunderstorms\ and\ Snow=true
Thunderstorms\ and\ Snow=true

Light\ Thunderstorms\ and\ Ice\ Pellets=true
Heavy\ Thunderstorms\ and\ Ice\ Pellets=true
Thunderstorms\ and\ Ice\ Pellets=true

Light\ Thunderstorms\ with\ Hail=true
Heavy\ Thunderstorms\ with\ Hail=true
Thunderstorms\ with\ Hail=true

Light\ Thunderstorms\ with\ Small\ Hail=true
Heavy\ Thunderstorms\ with\ Small\ Hail=true
Thunderstorms\ with\ Small\ Hail=true

Light\ Freezing\ Drizzle=true
Heavy\ Freezing\ Drizzle=true
Freezing\ Drizzle=true

Light\ Freezing\ Rain=true
Heavy\ Freezing\ Rain=true
Freezing\ Rain=true

Light\ Freezing\ Fog=true
Heavy\ Freezing\ Fog=true
Freezing\ Fog=true

Patches\ of\ Fog=true
Shallow\ Fog=true
Partial\ Fog=true
Partly\ Cloudy=true
Mostly\ Cloudy=true
Scattered\ Clouds=false
Small\ Hail=true
Funnel\ Cloud=true
Unknown\ Precipitation=true
1 Like

@vzorglub Thank you, I had tried searching only on this post and didn’t find anything here but didn’t think to check the forum since I assumed the map was specific to the Time Of Day design pattern. I appreciate the quick response though! Thank You

As mentioned in the What is a DP link in the OP, a DP is not necessarily a complete and wholly self contained example. Above all the weather.map does is just take the all uppercase states (e.g DAY) and makes them lower case (e.g. day) for the sitemap

Thanks Rick! Working on getting this implemented now. Makes sense.

Thanks for your helpful and informative design patterns,

Here I want to ask why you use .millis in comparison switch:

switch now {
  	case now.isAfter(morning_start.millis)

It’s left over from a time when morning_start et. al. were not Joda DateTime Types. It is not needed and I missed removing them the last time I edited the DP.

I updated the OP. Thanks for pointing out this oversight!

Here is a version using JSR223-Jython (w/ openhab2-jython modules)…

from org.slf4j import Logger, LoggerFactory
from openhab.triggers import time_triggered
from org.joda.time import DateTime
from org.joda.time import Interval
log = LoggerFactory.getLogger("org.eclipse.smarthome.model.script.Rules")

@time_triggered("0 0 7 * * ?")
@time_triggered("0 0 9 * * ?")
@time_triggered("0 55 17 * * ?")
@time_triggered("0 0 21 * * ?")
@time_triggered("0 0 23 * * ?")
def updateMode():
    #log.debug("JSR223: Update Mode")
    modeIntervals = {
        "Morning" : Interval(DateTime.now().withTime(7,0,0,0),DateTime.now().withTime(9,0,0,0)),
        "Day" : Interval(DateTime.now().withTime(9,0,0,0),DateTime.now().withTime(17,55,0,0)),
        "Evening" : Interval(DateTime.now().withTime(17,55,0,0),DateTime.now().withTime(21,0,0,0)),
        "Night" : Interval(DateTime.now().withTime(21,0,0,0),DateTime.now().withTime(23,0,0,0))
    newMode = "Late Night"
    for mode in list(modeIntervals.keys()):
        if modeIntervals.get(mode).contains(DateTime.now()):
            newMode = mode
        log.debug("JSR223: Update Mode: interval=[{}], name=[{}]".format(modeIntervals.get(mode),mode))
    if items["Mode"] != StringType(newMode):
        log.debug("JSR223: Update Mode: Mode changed to [{}]".format(newMode))
        log.debug("JSR223: Update Mode: Job ran but Mode did not need to be changed [{}]".format(newMode))

And I add this to the end of the script file so that this runs on startup and when the file is saved, instead of adding a Startup trigger for it…

1 Like

Thanks for posting this. A couple of questions to make sure I’m not missing it.

  • I see cron triggers but not Astro event triggers. Are they there and I’m just not good enough with it to recognize them? Is DateTimeType easy to convert to DateTime in JSR223 (e.g. new DateTime(Sunset.state.toString))

  • Mode is the name of the Item? It’s the equivalent of vTimeOfDay in the OP?

  • Does this run at System start/when the file is loaded?

I don’t have Astro triggers… our time of day intervals are all time driven. Channel triggers can be used too, but there is no decorator setup for them. DateTimeType conversion is the same…

if items["Calendar_Upcoming_Gymnastics"] != UnDefType.UNDEF and Interval(Duration(TimeUnit.DAYS.toMillis(1)),DateTime(items["Calendar_Upcoming_Gymnastics"].zonedDateTime.toInstant().toEpochMilli())).contains(DateTime.now()):

Yes… something I carried over from ST.

Funny you should ask that. You can make a rule that triggers on startup, but I didn’t see a need… I just added the updateMode() function to the end of the script. I’ll add that to the example. This was the function runs when the file is saved and when OH starts up.

To cover the case where OH goes down, a time transition occurs, and OH comes back up. It would have missed the time transition so at best Mode would remain the previous state until the next transition, essentially skipping one whole time period.

Thanks for posting!

I didn’t communicate this well… I didn’t see a need to add it as a trigger, because the function will run when the file is reloaded, by putting it at the end of the file. This also makes it a little easier to spot what is going to run when I save the file.

You are very welcome, and thank you for all you do here!

OK, makes sense. A Systtem started trigger is not needed then.

ive got an error

2018-07-22 18:11:11.949 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Calculate time of day state': Could not cast NULL to org.eclipse.smarthome.core.library.types.DateTimeType; line 30, column 39, length 35

my rule is as follows

rule "Calculate time of day state" 
  System started or // run at system start in case the time changed when OH was offline
  Channel 'astro:sun:home:rise#event'    triggered START or
  Channel 'astro:sun:home:set#event'     triggered START or
  Channel 'astro:sun:minus90:set#event'  triggered START or
  Time cron "0 1 0 * * ? *" or // one minute after midnight so give Astro time to calculate the new day's times
  Time cron "0 0 6 * * ? *" or
  Item test7 changed from OFF to ON or
  Time cron "0 0 23 * * ? *"

  logInfo("Log", "Calculating time of day...")

  // Calculate the times for the static tods and populate the associated Items
  // Update when changing static times
  // Jump to tomorrow and subtract to avoid problems at the change over to/from DST
  val morning_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(18)

  val night_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(1)

  val bed_start = now.withTimeAtStartOfDay

  // Convert the Astro Items to Joda DateTime
  val day_start = new DateTime((vSunrise_Time.state as DateTimeType).getZonedDateTime.toInstant.toEpochMilli) 
  val evening_start = new DateTime((vSunset_Time.state as DateTimeType).getZonedDateTime.toInstant.toEpochMilli)
  val afternoon_start = new DateTime((vEvening_Time.state as DateTimeType).getZonedDateTime.toInstant.toEpochMilli)

  // Calculate the current time of day
  var curr = "UNKNOWN"
  switch now {
  	case now.isAfter(morning_start.millis)   && now.isBefore(day_start.millis):       curr = "MORNING"
  	case now.isAfter(day_start.millis)       && now.isBefore(afternoon_start.millis): curr = "DAY"
  	case now.isAfter(afternoon_start.millis) && now.isBefore(evening_start.millis):   curr = "AFTERNOON"
  	case now.isAfter(evening_start.millis)   && now.isBefore(night_start.millis):     curr = "EVENING"
  	case now.isAfter(night_start.millis):                                      curr = "NIGHT"
  	case now.isAfter(bed_start.millis)       && now.isBefore(morning_start.millis):   curr = "BED"

  // Publish the current state
  logInfo(logName, "Calculated time of day is " + curr)

any ideas as I’m a little lost?

On of these are not initialized correctly. My gues is you didnt define a thing. vSunrise_Time,vSunset_Time, vEvening_Time
So please define the thing correctly.

this is my items file, I presume that you meant items and not things? the only thing file that I have is for the astro binding and that has been setup and working for a while now.

String vTimeOfDay "Current Time of Day [MAP(weather.map):%s]" <tod>

DateTime vMorning_Time "Morning [%1$tH:%1$tM]" <sunrise>

DateTime vSunrise_Time "Day [%1$tH:%1$tM]" <sun> { channel="astro:sun:home:rise#start" }

DateTime vSunset_Time "Evening [%1$tH:%1$tM]" <sunset> { channel="astro:sun:home:set#start" }
DateTime vNight_Time "Night [%1$tH:%1$tM]" <moon>
DateTime vBed_Time "Bed [%1$tH:%1$tM]" <bedroom_blue>

DateTime vEvening_Time "Afternoon [ %1$tH:%1$tM]" <sunset> { channel="astro:sun:minus90:set#start" }