Design Pattern: Time Of Day

jodatime
designpattern
time
Tags: #<Tag:0x00007fe054f8f8b8> #<Tag:0x00007fe054f8f778> #<Tag:0x00007fe054f8f548>

(René Hassfeld) #263

Yes, that is installed. I discovered the issue. I needed to add a file myitems.things in the things folder that contains

astro:sun:home  [ geolocation="48.00516577224242,6.4473223482074,100", interval=60 ]

(Christoph) #264

I had been tinkering for many days now to get “Morning”, “Night” and “Bed” calculated until I stumbled across this in my logs:

[ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Calculate time of day state': The name 'logName' cannot be resolved to an item or type

After uncommenting logName such as:

//logInfo(logName, "Calculating time of day...")
...
//logInfo(logName, "Calculated time of day is " + curr)

…it all seems to work fine now! Though I really don’t know what to make of the error message maybe somebody finds this helpful.


(René Hassfeld) #265

Add a definition for logname at the top of the script:

val logName = "Calc tod-state"

(Rich Koshak) #266

The example is missing a global variable that defines the logName variable. I’ll update the top post to include that.


(Christoph) #267

Thank you! I’ll implement that.


(Jerome Luckenbach) #268

Just as information:
My error was a different one.

I had problems with the vEvening_Time item which where caused by a file cleanup from my side.
I deleted the astro minus90 thing since i couldn’t find a reference at that point.

Now i have found one. :sweat_smile:


(Ian C) #269

Sorry for posting a very basic question here.
I’m running OpenHabian with OH2.3
I installed the Astro Binding via Paper UI and then selected “Local Sun” and “Local Moon” in the inbox

I then copied the items file and the rules file verbatim (I did delete the “Some rule” rule!) into the appropriate directories and saved them.

I then get an error in my log

2019-01-28 23:46:14.506 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'time_of_day.rules'
2019-01-28 23:46:19.616 [INFO ] [org.quartz.core.QuartzScheduler     ] - Scheduler openHAB-job-scheduler_$_NON_CLUSTERED started.
2019-01-28 23:46:20.411 [INFO ] [e.smarthome.model.script.Time Of Day] - Calculating time of day...
2019-01-28 23:46:20.436 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Error during the execution of startup rule 'Calculate time of day state': Invalid format: "NULL"

This is getting caused by the lines
val day_start = new DateTime(vSunrise_Time.state.toString)
val evening_start = new DateTime(vSunset_Time.state.toString)
val afternoon_start = new DateTime(vEvening_Time.state.toString)

Why? What am I missing?


(Rich Koshak) #270

astro:local:blah blah blah is a different Channel ID than the Things defined in the DP.

Copy the Channel ID from PaperUI to the Item.


(Ian C) #271

Sorted Thanks :slight_smile:


(Mash Phone) #273

Hi there, ive been new to this approach as well as oh2 and are grateful for a helping hint.

what I try to achieve is a rule like this:

when
    it is evening
then
    if motion sensor living room (="BewegungsmelderWohnzimmer") is triggered or TV is on then
        turn on living room lights (="sonoff_Wohnzimmer_LucePlan")
    else
        turn lights off after 10 minutes
end

what I have so far is this non-working rule:

rule "if its evening and tv is on or motion detected in living room"

when
Item vTimeOfDay received command
then
    if(vTimeOfDay.state != "EVENING") return;
        if (BewegungsmelderWohnzimmer_Motion.state == ON || PhilipsTV_Online.state == ON){
            mqttActions.publishMQTT("cmnd/sonoff_Wohnzimmer_LucePlan/POWER", "ON")
            BewegungsmelderWohnzimmer_MotionOffTimer.sendCommand(600)
    }
   
    else if (BewegungsmelderWohnzimmer_Motion.state == OFF && PhilipsTV_Online.state == OFF ) {
       mqttActions.publishMQTT("cmnd/sonoff_Wohnzimmer_LucePlan/POWER", "OFF")         
    }  
end

throws an error in visual studio code editor The method or field vTimeOfDay is undefined in the line after the “then”) and doesn’t get triggered either…

Any one an idea what im doing wrong here?

thanks in advance!!!


(Rich Koshak) #274

First, does it throw the error every time the Rule runs on only at OH startup?

If it’s everytime then you didn’t define the required Items (in this case vTimeOfDay).

Now also realize that Rule triggers are not based on state. They are based on events. So if you trigger off of vTimeOfDay, the Rule will only trigger when vTimeOfDay changes or receives a command. You need to change to trigger the Rule on the Motion Sensor Item and look to see if it is the right time of day inside the Rule.


(Mash Phone) #275

Thank you for your reply!

ive changed the rule now according to your suggestions:

when
 Item BewegungsmelderWohnzimmer_Motion changed to ON or Item PhilipsTV_Online changed to ON
then
   if (vTimeOfDay.state == "EVENING") {
       mqttActions.publishMQTT("cmnd/sonoff_Wohnzimmer_LucePlan/POWER", "ON")
       BewegungsmelderWohnzimmer_MotionOffTimer.sendCommand(60)
   }

else if (BewegungsmelderWohnzimmer_MotionOffTimer == 0 && PhilipsTV_Online.state == OFF ) {

     mqttActions.publishMQTT("cmnd/sonoff_Wohnzimmer_LucePlan/POWER", "OFF")
  }  
        
  end

but its still not working. now I get this in the log, when the rule is triggered:

[rest.core.item.EnrichedItemDTOMapper] - Failed transforming the state 'NULL' on item 'vTimeOfDay' with pattern 'MAP(weather.map):%s': An error occurred while opening file.

Apparently, vtimeofday is NULL, not EVENING, and therefore the rule doesn’t work.
Any ideas?

thanks in advance!

UPDATE: its working now, hooray! refreshing the timeofday.rules helped:

2019-02-06 12:42:36.027 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'tageszeiten.rules'

2019-02-06 12:42:42.500 [INFO ] [e.smarthome.model.script.Time Of Day] - Calculating time of day...

2019-02-06 12:42:42.535 [INFO ] [e.smarthome.model.script.Time Of Day] - Calculated time of day is DAY

2019-02-06 12:42:42.540 [ome.event.ItemCommandEvent] - Item 'vTimeOfDay' received command DAY

2019-02-06 12:42:42.555 [vent.ItemStateChangedEvent] - vTimeOfDay changed from NULL to DAY

d

however, how do I get rid of this:

[rest.core.item.EnrichedItemDTOMapper] - Failed transforming the state 'DAY' on item 'vTimeOfDay' with pattern 'MAP(weather.map):%s': An error occurred while opening file.

I did install the map transformation anyway, but as I don’t really need this, how do I change

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

to get rid of this error?


(Scott Rushworth) #276
String vTimeOfDay "Current Time of Day [%s]" <tod>

(Mash Phone) #277

thanks, that did the trick, before i also deleted the [%s] as well, which couldn’t work…

Now, im struggling with the motionofftimer part of the rule, which isn’t working, I guess I have to change this

if (BewegungsmelderWohnzimmer_MotionOffTimer == NULL && ... ) {

but to what?

UPDATE: It all works now, ive deleted the else if part completely and just added another rule:

rule "WohnzimmerLampen aus bei Inaktivität"
when
 Item BewegungsmelderWohnzimmer_Motion changed from ON to OFF or
  Item PhilipsTV_Online changed from ON to OFF
then
  mqttActions.publishMQTT("cmnd/sonoff_Wohnzimmer_LucePlan/POWER", "OFF")  
 end

im sorry if Ive bloated up the thread, but maybe it helps someone else.
its just learning in progress for me and ive finally learned that the motionofftimer.sendCommand(X) just delays the motion off event by X seconds, while I was expecting the motionofftimer to go down to zero, which I wanted to use as a trigger …

another lesson learned… :slight_smile:


(Rich Koshak) #278
if (BewegungsmelderWohnzimmer_MotionOffTimer.state == NULL && ... ) {

Each Item carries a lot of information like name, state, last update, etc. You need to compare the state to NULL.


(Kris K) #279

Hi Guys

I seem to be having an issue recently with the time of day being out, or showing not the correct time of day.

Right now its 11.23AM and the vTimeOfDay item shows evening, which is throwing out the log messages

My logs show it ran at 10.51AM as below:

ris@openhab2:/etc/openhab2/rules$ cat /var/log/openhab2/events.log | grep "astro"
2019-02-10 06:00:00.001 [vent.ChannelTriggeredEvent] - astro:sun:minus90:nauticDawn#event triggered END
2019-02-10 06:00:00.003 [vent.ChannelTriggeredEvent] - astro:sun:home:nauticDawn#event triggered END
2019-02-10 06:00:00.005 [vent.ChannelTriggeredEvent] - astro:sun:home:civilDawn#event triggered START
2019-02-10 06:00:00.006 [vent.ChannelTriggeredEvent] - astro:sun:minus90:civilDawn#event triggered START
2019-02-10 06:26:00.001 [vent.ChannelTriggeredEvent] - astro:sun:minus90:rise#event triggered START
2019-02-10 06:26:00.003 [vent.ChannelTriggeredEvent] - astro:sun:home:rise#event triggered START
2019-02-10 06:26:00.005 [vent.ChannelTriggeredEvent] - astro:sun:minus90:civilDawn#event triggered END
2019-02-10 06:26:00.007 [vent.ChannelTriggeredEvent] - astro:sun:home:civilDawn#event triggered END
2019-02-10 06:29:00.001 [vent.ChannelTriggeredEvent] - astro:sun:minus90:rise#event triggered END
2019-02-10 06:29:00.003 [vent.ChannelTriggeredEvent] - astro:sun:home:rise#event triggered END
2019-02-10 06:29:00.005 [vent.ChannelTriggeredEvent] - astro:sun:minus90:daylight#event triggered START
2019-02-10 06:29:00.007 [vent.ChannelTriggeredEvent] - astro:sun:home:daylight#event triggered START
2019-02-10 10:51:00.002 [vent.ChannelTriggeredEvent] - astro:moon:home:rise#event triggered START
kris@openhab2:/etc/openhab2/rules$

marthome:status vTimeOfDay
EVENING
openhab>

The log shows these parameters changing frequently. But not much else!

2019-02-10 11:05:25.736 [vent.ItemStateChangedEvent] - vSun_Azimuth changed from 64.08110370652155 ° to 63.25804592677622 °
2019-02-10 11:05:25.738 [vent.ItemStateChangedEvent] - vSun_Elevation changed from 55.36837216280727 ° to 55.92599331672058 °

Im using 2.4 Stable.

May someone suggest a starting point to troubleshoot?

Thanks


(Rich Koshak) #280

First confirm the problem is Astro. Is every time of day change event between EVENING and 11:23AM from Astro, or are there some cron triggers there too?


(Kris K) #281

Sorry Rich i didnt understand your post :frowning:


(Rich Koshak) #282

Presumably, you have a Night and a Morning. Do those states start with an Astro event or a fixed time?


(Kris K) #283

Yes :slight_smile:

How do I work that out? Right now, its showing correctly as morning after a reboot.

I’m using your rule:


rule "Calculate time of day state"
when
  System started or
  Channel 'astro:sun:local:rise#event'    triggered START or
  Channel 'astro:sun:local:set#event'     triggered START or
  Channel 'astro:sun:minus90:set#event'  triggered START or
  Time cron "0 1 0 * * ? *" or
  Time cron "0 0 6 * * ? *" or
  Time cron "0 0 23 * * ? *"

then

  logInfo("Time Of Day", "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)
  vMorning_Time.postUpdate(morning_start.toString)

  val night_start = now.withTimeAtStartOfDay.plusDays(1).minusHours(1)
  vNight_Time.postUpdate(night_start.toString)

  val bed_start = now.withTimeAtStartOfDay
  vBed_Time.postUpdate(bed_start.toString)

  // Convert the Astro Items to Joda DateTime
  val day_start = new DateTime(vSunrise_Time.state.toString)
  val evening_start = new DateTime(vSunset_Time.state.toString)
  val afternoon_start = new DateTime(vEvening_Time.state.toString)

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

  // Publish the current state
  logInfo("Time of Day", "Calculated time of day is " + curr)
  vTimeOfDay.sendCommand(curr)
end