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?
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.
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
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
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.
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.
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 ) 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.
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.
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.