[HELP] Have I missed something in a Rule?

import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.Calendar

rule "Set Day Of The Week"
when
        System started or
        Time cron "0/5 * * * * ?"
then
        var Calendar cal = Calendar::instance
        var DateFormat fmt = new SimpleDateFormat("EEEE")
        var String todayString = fmt.format(cal.time)
        Day_Of_The_Week.postUpdate(todayString)
end

rule "Get time period for right now"
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:local:eveningNight#start' triggered START or
        Time cron "0 0 6,23,0 * * ? *"
then
    // Sleep for just a bit to make sure we are not exactly on the time period boundary
        Thread::sleep(1000)

    // Get the time period start times for today
        val long morning_start = now.withTimeAtStartOfDay.plusHours(6).millis
        val long day_start = (Sunrise_Time.state as DateTimeType).calendar.timeInMillis
        **val long afternoon_start = (Noon_Time.state as DateTimeType).calendar.timeInMillis**
        val long evening_start = (Sunset_Time.state as DateTimeType).calendar.timeInMillis
        val long night_start = now.withTimeAtStartOfDay.plusHours(23).millis
        val long bed_start = now.withTimeAtStartOfDay.millis

        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"
        }

    if(Time_Of_Day.state.toString != curr){
        Time_Of_Day.sendCommand(curr)
    }
        Thread::sleep(50)
        setMasterVolume(new PercentType(20))
        val String Time_Now = "The time of day has changed to;" + Time_Of_Day.state
        say(Time_Now)
end

I’m sure most of you will recognise the above rule, I’ve altered it just a little more my needs. However it’s not working and I’m not getting errors in the logs. The part that isn’t working is the change to Afternoon. Although if I resave the rule it updates fine.

Any pointers of where to look or have I just misunderstood something?

Maybe missing new DateTime( ... )

I’m running openHAB 2.1.0 for the time being. Not sure what you mean @josar as I’ve copied the rule from Design Pattern Time Of Day and there is nowhere that has; new DateTime((Sunrise_Time.state as DateTimeType).calendar.timeInMillis)

Besides if I re-save the rule so that it reloads the time of day changes fine. Just in case it helps further here’s my Astro.items.

DateTime Sunrise_Time   "Sunrise [%1$tH:%1$tM]" <clock> { channel="astro:sun:local:rise#start" }
DateTime Noon_Time      "Noon [%1$tH:%1$tM]" <clock> { channel="astro:sun:local:noon#start" }
DateTime Sunset_Time    "Sunset [%1$tH:%1$tM]" <clock>  { channel="astro:sun:local:set#start" }
DateTime Evening_Time   "Evening [%1$tH:%1$tM]" <clock> { channel="astro:sun:local:eveningNight#start" }

@Maximo what i referenced is in the first part but i see it is not neccessary, int the following examples it is left out.

I think maybe explain more what you expect and what does not work.

But what i think could be that you did not add Channel 'astro:sun:local:noon#event' triggered START or to your triggers.

And maybe its eveningNight#start vs eveningNight#event

I’ve made the adjustment as suggested but it’s still not working, however you’ve at least pointing me in a direction.

Thanks for your help so far.

What is Noon_Time? How is it populated? Which one of the events in the when clause corresponds with Noon_Time?

There must be a trigger on the Rule for the start of every time of day.

Why do you run “Set Day of the Week” every five seconds? You only need to run it once per day.

You know I hadn’t even noticed I was running that rule every 5 seconds. I’ve changed that now.

Noon_Time is obtained from Astro as below.

DateTime Noon_Time      "Noon [%1$tH:%1$tM]" <clock> { channel="astro:sun:local:noon#start" }

I’ve updated the rule with this;

rule "Get time period for right now"
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:local:noon#event' triggered START or
        Channel 'astro:sun:local:eveningNight#start' triggered START or
        Time cron "0 0 6,23,0 * * ? *"
then

I’m still having problems and would really like to get them resolved. I’ve changed the rule to what’s below, it seems to only run at 6,23 & 0 hours. The Sunrise, Sunset and Evening events aren’t executed. I can see that from the logs.

Once again I’m looking for pointers as I’ve looked and looked at the rule and items so many times now I just can’t see anything wrong.

import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.Calendar

rule "Set Day Of The Week"
when
        System started or
        Time cron "0 * * * * ?"
then
        var Calendar cal = Calendar::instance
        var DateFormat fmt = new SimpleDateFormat("EEEE")
        var String todayString = fmt.format(cal.time)
        Day_Of_The_Week.postUpdate(todayString)
end

rule "Get time period for right now"
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:local:eveningNight#start' triggered START or
        Time cron "0 0 6,23,0 * * ? *"
then
    // Sleep for just a bit to make sure we are not exactly on the time period boundary
        Thread::sleep(1000)

    // Get the time period start times for today
        val long morning_start = now.withTimeAtStartOfDay.plusHours(6).millis
        val long day_start = (Sunrise_Time.state as DateTimeType).calendar.timeInMillis
        val long afternoon_start = (Evening_Time.state as DateTimeType).calendar.timeInMillis
        val long evening_start = (Sunset_Time.state as DateTimeType).calendar.timeInMillis
        val long night_start = now.withTimeAtStartOfDay.plusHours(23).millis
        val long bed_start = now.withTimeAtStartOfDay.millis

        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"
        }

    if(Time_Of_Day.state.toString != curr){
        Time_Of_Day.sendCommand(curr)
    }
        Thread::sleep(50)
        setMasterVolume(new PercentType(20))
        val String Time_Now = "The time of day has changed to;" + Time_Of_Day.state
        say(Time_Now)
end

Here are the items that relate to the above rule.

DateTime Sunrise_Time   "The Sun with rise at [%1$tH:%1$tM]"    <clock> { channel="astro:sun:local:rise#start" }
DateTime Noon_Time      "Noon start's at [%1$tH:%1$tM]" <clock> { channel="astro:sun:local:noon#start" }
DateTime Sunset_Time    "Sunset is going to be at [%1$tH:%1$tM]" <clock>        { channel="astro:sun:local:set#start" }
DateTime Evening_Time   "The Evening will start at [%1$tH:%1$tM]" <clock> { channel="astro:sun:local:eveningNight#start" }
DateTime Night_Time   "Night [%1$tH:%1$tM]" <clock> { channel="astro:sun:local:night#start" }
String Sun_Phase        "Sun Phase [%s]"        { channel="astro:sun:local:phase#name" }
String Moon_Phase       "Moon Phase [%s]"       { channel="astro:moon:local:phase#name" }
String Season_Name      "Season [%s]"   { channel="astro:sun:local:season#name" }
Switch Sunrise_Event    { channel="astro:sun:local:rise#start" }
Switch Noon_Event       { channel="astro:sun:local:noon#start" }
Switch Sunset_Event     { channel="astro:sun:local:set#start" }
Switch Evening_Event    { channel="astro:sun:local:eveningNight#start" }
DateTime Season_Spring  "Spring [%1$td.%1$tm.%1$tY %1$tH:%1$tM]"        { channel="astro:sun:local:season#spring" }
DateTime Season_Summer  "Summer [%1$td.%1$tm.%1$tY %1$tH:%1$tM]"        { channel="astro:sun:local:season#summer" }
DateTime Season_Autumn  "Autumn [%1$td.%1$tm.%1$tY %1$tH:%1$tM]"        { channel="astro:sun:local:season#autumn" }
DateTime Season_Winter  "Winter [%1$td.%1$tm.%1$tY %1$tH:%1$tM]"        { channel="astro:sun:local:season#winter" }

Sunset was 17:55 today, but as you can see from the logfile below there is no Astro event.


```php
2018-03-07 17:54:38.982 [hingStatusInfoChangedEvent] - 'hue:0210:1:11' changed from ONLINE to OFFLINE: Bridge reports light as not reachable
2018-03-07 17:55:11.398 [ItemStateChangedEvent     ] - Hallway_SP3102_Motion changed from OFF to ON
2018-03-07 17:55:11.467 [ItemStateChangedEvent     ] - Hallway_SP3102_Alarm changed from 0 to 1
2018-03-07 17:55:28.950 [hingStatusInfoChangedEvent] - 'hue:0210:1:11' changed from OFFLINE: Bridge reports light as not reachable to ONLINE
2018-03-07 17:55:29.335 [ItemStateChangedEvent     ] - Louises_Bedroom_ST814_Temperature changed from 20.1 to 20
2018-03-07 17:55:29.337 [GroupItemStateChangedEvent] - Second_Floor_Temperature changed from 20.2 to 20.1 through Louises_Bedroom_ST814_Temperature
2018-03-07 17:55:58.854 [hingStatusInfoChangedEvent] - 'hue:0100:1:7' changed from ONLINE to OFFLINE: Bridge reports light as not reachable
2018-03-07 17:56:10.157 [hingStatusInfoChangedEvent] - 'hue:0220:1:8' changed from ONLINE to OFFLINE: Bridge reports light as not reachable
2018-03-07 17:56:24.086 [ItemStateChangedEvent     ] - Living_Room_Side_Radiator_Valve_Pos changed from 71 to 75
2018-03-07 17:56:28.834 [hingStatusInfoChangedEvent] - 'hue:0220:1:15' changed from ONLINE to OFFLINE: Bridge reports light as not reachable
2018-03-07 17:56:58.315 [ItemStateChangedEvent     ] - Living_Room_HRT4_ZW_Mode changed from 0 to 1
2018-03-07 17:56:58.322 [ItemCommandEvent          ] - Item 'Kitchen_SSR_302_Radiators_Switch' received command ON
2018-03-07 17:56:58.327 [ItemStateChangedEvent     ] - Kitchen_SSR_302_Radiators_Switch changed from OFF to ON
2018-03-07 17:56:58.437 [ItemStateChangedEvent     ] - Living_Room_HRT4_ZW_Switch changed from 0 to 1
2018-03-07 17:56:58.443 [ItemCommandEvent          ] - Item 'Kitchen_SSR_302_Radiators_Switch' received command ON

It may be an obvious question but I have to ask. Have you installed the Astro binding?

You have an astro:sun:local Thing defined?

This is not a trigger channel. You’d need to use…

Channel 'astro:sun:local:eveningNight#event' triggered START or

These channels are DateTime, so can’t be used as a Switch item. I’m surprised your items file wouldn’t log errors when saved or when starting up. I think the trigger channels can be used for a Switch item, but are just a momentary toggle (ON/OFF) when the event occurs. But it’s been a while since I tested this, so I could be mistaken.

Hi Rich,

Yes I have the Astro binding installed. Always worth asking the question.

Thing astro:sun:local  [ geolocation="51.5944211,-1.8185145", interval=60 ]
Thing astro:moon:local [ geolocation="51.5944211,-1.8185145", interval=60 ]

I made the change as suggested and this hasn’t made a difference. Sadly I’m not further forward. The Switch’s in the items file are something I need to clean up, but they aren’t causing any errors.

In the logfiles, do you have any astro events? In the openhab.log there should be something like

2018-03-09 00:00:30.254 [INFO ] [thome.binding.astro.internal.job.Job] - Scheduled Astro event-jobs for thing astro:sun:local

once a day, at 30secs past midnight. Then, in the events log, there should be lots of these:

2018-03-09 00:00:30.271 [vent.ChannelTriggeredEvent] - astro:sun:local:morningNight#event triggered START
2018-03-09 06:15:00.006 [vent.ChannelTriggeredEvent] - astro:sun:local:astroDawn#event triggered START
2018-03-09 06:15:00.010 [vent.ChannelTriggeredEvent] - astro:sun:local:morningNight#event triggered END
2018-03-09 06:44:00.004 [vent.ChannelTriggeredEvent] - astro:sun:local:nauticDawn#event triggered START
2018-03-09 06:44:00.008 [vent.ChannelTriggeredEvent] - astro:sun:local:astroDawn#event triggered END
2018-03-09 07:14:00.003 [vent.ChannelTriggeredEvent] - astro:sun:local:nauticDawn#event triggered END

If these are not there, you problem isn’t the rule.

To filter all the other clutter try the following the linux commands in the log directory

grep astro openhab.log
grep astro events.log

Here’s a long shot… have you been modifying items files while OH is running? This can cause issues. I wanted to make a quick change recently to update an item (I knew I shouldn’t :roll_eyes:) and noticed all of my cron rules stopped firing. Astro uses the Quartz scheduler too, so those channels were probably not updating either. Does restarting change anything?

But first, add some logging into your rule, especially at the start of it, and monitor the openhab.log file after a restart to see if it fires.

What version of OH are you running? I thought this got fixed before the release of OH 2.2. I’ve not seen many if any new threads complaining about it.

The standard approach is to edit .items et al while OH is running. The only thing you shouldn’t edit while OH is running is the JSONDB files.

2.3.0 snapshot 1224. I was seeing the order of items changing in the UI, groups not being updated, bindings restarting, etc. so I stopped editing items while OH was running. It’s been a while, so this may all be fixed, at least in the snapshot. But my cron rules definitely stopped, so this might be a new issue. I commented in ESH issue #4130, in case this is related.

Hi Scott,

I’m currently running with OH2.1, having gone though the process of upgrading from 1.8.3. The Next step will be to move onto version 2.2 or 2.3, depending on timing.

I’m not changing items files all the time and have restarted OH a number of times and still the rule doesn’t work as expected. The run works fine at the restart and picks up the current time of day.

I’ll look at adding additional logging to see if that helps. Thanks.

Since we are on the topic of the rule running when "system started’ but then with changing time of day, no command is executed. I have a query related to the same.
My items file:

String Operating_Mode_Seminar	"[MAP(op.map):%s]"		<settings> 	//(All)
String Operating_Mode_Lab		"[MAP(op.map):%s]"		<settings> 	//(All)

Switch Night_Seminar			"Operating Mode is [MAP(mode.map):%s]"			//(All)
Switch Occupied_Seminar			"Operating Mode is [MAP(mode.map):%s]"			//(All)
Switch Vacant_Seminar			"Operating Mode is [MAP(mode.map):%s]"			//(All)
Switch Night_Lab				"Operating Mode is [MAP(mode.map):%s]"			//(All)
Switch Occupied_Lab				"Operating Mode is [MAP(mode.map):%s]"			//(All)
Switch Vacant_Lab				"Operating Mode is [MAP(mode.map):%s]"			//(All)

Number Set_AirTemp_Seminar_Heating	"Set seminar heating temp. [%.1f °C]"		<temperature_hot>	
Number Set_AirTemp_Lab_Heating		"Set lab heating temp. [%.1f °C]"			<temperature_hot>

My Rules:

rule "Initialize system values"
       when
        	System started
        then
        	Set_AirTemp_Seminar_Heating.sendCommand(15)
        	Set_AirTemp_Lab_Heating.sendCommand(15)
end

rule "Operation Control"
	when
		System started or
		Time cron "0 0 6 * * ?" //(format "second minute hour day-of-month month day-of-week")
	then
		if ((now.isAfter(now.withTimeAtStartOfDay.plusHours(6).plusMinutes(0).millis) && now.isBefore(now.withTimeAtStartOfDay.plusHours(11).plusMinutes(0)))
			|| (now.isAfter(now.withTimeAtStartOfDay.plusHours(14).plusMinutes(0).millis) && now.isBefore(now.withTimeAtStartOfDay.plusHours(20).plusMinutes(0))) 
		){
			Operating_Mode_Seminar.sendCommand("OCCUPIED")
			Occupied_Seminar.sendCommand(ON)
			logInfo("rule", "Log: Occupied mode for Seminar started")
			Set_AirTemp_Seminar_Heating.sendCommand(19)
			Display_AirTemp_Seminar.postUpdate(Set_AirTemp_Seminar_Heating.state.toString)
			Thread::sleep(100)
			Operating_Mode_Lab.sendCommand("OCCUPIED")
			Occupied_Lab.sendCommand(ON)
			logInfo("rule", "Log: Occupied mode for Lab started")
			Set_AirTemp_Lab_Heating.sendCommand(19)
			Display_AirTemp_Lab.postUpdate(Set_AirTemp_Lab_Heating.state.toString)
		}
		if (now.isAfter(now.withTimeAtStartOfDay.plusHours(11).plusMinutes(0).millis) && now.isBefore(now.withTimeAtStartOfDay.plusHours(14).plusMinutes(0)))
		{
			Operating_Mode_Seminar.sendCommand("VACANT")
			Vacant_Seminar.sendCommand(ON)
			logInfo("rule", "Log: Vacant mode for Seminar started")
			Set_AirTemp_Seminar_Heating.sendCommand(15)
			Display_AirTemp_Seminar.postUpdate(Set_AirTemp_Seminar_Heating.state.toString)
			Thread::sleep(100)
			Operating_Mode_Lab.sendCommand("VACANT")
			Vacant_Lab.sendCommand(ON)
			logInfo("rule", "Log: Vacant mode for Lab started")
			Set_AirTemp_Lab_Heating.sendCommand(15)
			Display_AirTemp_Lab.postUpdate(Set_AirTemp_Lab_Heating.state.toString)
		}
		if (now.isAfter(now.withTimeAtStartOfDay.plusHours(20).plusMinutes(0).millis) && now.isBefore(now.withTimeAtStartOfDay.plusHours(6).plusMinutes(0))) 
		{
			Operating_Mode_Seminar.sendCommand("NIGHT")
			Night_Seminar.sendCommand(ON)
			logInfo("rule", "Log: Night mode for Seminar started")
			Set_AirTemp_Seminar_Heating.sendCommand(20)
			Display_AirTemp_Seminar.postUpdate(Set_AirTemp_Seminar_Heating.state.toString)
			Thread::sleep(100)
			Operating_Mode_Lab.sendCommand("NIGHT")
			Night_Lab.sendCommand(ON)
			logInfo("rule", "Log: Night mode for Lab started")
			Set_AirTemp_Lab_Heating.sendCommand(20)
			Display_AirTemp_Lab.postUpdate(Set_AirTemp_Lab_Heating.state.toString)
		}
end

So on every restart of openHAB, the Rule “Operation control” runs and based on the “if statement” the respective mode of operation is activated. So based on the above timings, here is the log:

2018-03-12 14:31:28.988 [ItemCommandEvent          ] - Item 'Set_AirTemp_Seminar_Heating' received command 15
2018-03-12 14:31:28.988 [ItemCommandEvent          ] - Item 'Set_AirTemp_Lab_Heating' received command 15
2018-03-12 14:31:28.989 [ItemStateChangedEvent     ] - Set_AirTemp_Seminar_Heating changed from 19 to 15
2018-03-12 14:31:28.989 [ItemStateChangedEvent     ] - Set_AirTemp_Lab_Heating changed from 19 to 15
2018-03-12 14:31:28.992 [ItemCommandEvent          ] - Item 'Operating_Mode_Seminar' received command OCCUPIED
2018-03-12 14:31:28.992 [ItemCommandEvent          ] - Item 'Occupied_Seminar' received command ON
2018-03-12 14:31:28.992 [INFO ] [.eclipse.smarthome.model.script.rule] - Log: Occupied mode for Seminar started
2018-03-12 14:31:28.993 [ItemCommandEvent          ] - Item 'Set_AirTemp_Seminar_Heating' received command 19
2018-03-12 14:31:28.994 [ItemStateChangedEvent     ] - Set_AirTemp_Seminar_Heating changed from 15 to 19
2018-03-12 14:31:29.095 [ItemCommandEvent          ] - Item 'Operating_Mode_Lab' received command OCCUPIED
2018-03-12 14:31:29.095 [INFO ] [.eclipse.smarthome.model.script.rule] - Log: Occupied mode for Lab started
2018-03-12 14:31:29.096 [ItemCommandEvent          ] - Item 'Occupied_Lab' received command ON
2018-03-12 14:31:29.097 [ItemCommandEvent          ] - Item 'Set_AirTemp_Lab_Heating' received command 19
2018-03-12 14:31:29.101 [ItemStateChangedEvent     ] - Set_AirTemp_Lab_Heating changed from 15 to 19

So why doesn’t my rule from the other “if statements” get activated when the time of day is changed.
FYI: For simulation purpose, I did change the timings in the other “if” blocks, to my current time and tried if it changed the mode of operation and it does change but only when “system started”.
What do I need to do here? has this anything to do with persistence?
I have been through the “Design Pattern: Time of day” but I found the above written rule to be useful.
Any thoughts?
Thanks in advance.

Which version of OH are you running?

Oops I forgot to mention.

Its openHAB 2.1 build #858