Hi
I am struggling with setting a timer correctly. I am actually uncertain as to the way to do it correctly and while I have read many examples in the forums, the timers seem to be implemented in different ways (which of course is not unusual)
The rule below is meant to run at midnight and calculate irrigation for the day.
import org.eclipse.smarthome.model.script.actions.Timer
var Timer itimer_am = null
var Timer itimer_pm = null
var morning_watering = null
var evening_watering = null
var Number vZone1 = null
var Number itimer_start_hour_am
var Number itimer_start_minute_am
var Number itimer_start_hour_pm
var Number itimer_start_minute_pm
var Number icurrent_time
var int delta_itimer_am
var int delta_itimer_pm
var Number irrigation_day
rule "Irrigation 1"
when
//System started or
Item Test_Switch_1 received command ON or
Time is midnight
then
logInfo("Crib","Welcome to new irrigation rule")
Test_Switch_1.postUpdate(OFF)
// check if there is already an alarm timer; cancel it if present
itimer_am?.cancel
itimer_am = null
itimer_pm?.cancel
itimer_pm = null
switch(Season_Name.state){
case "AUTUMN":{
morning_watering = ON
evening_watering = ON
itimer_start_hour_am = 15
itimer_start_minute_am = 0
itimer_start_hour_pm = 15
itimer_start_minute_pm = 5
irrigation_day = 8 //1=Monday, 2=Tuesday, 3=Wednesday, 4=Thursday, 5=Friday, 6=Saturday, 7=Sunday, 8=everyday
}
case "WINTER":{
morning_watering = ON
evening_watering = OFF
itimer_start_hour_am = 7
itimer_start_minute_am = 0
itimer_start_hour_pm = 16
itimer_start_minute_pm = 0
irrigation_day = 8 //1=Monday, 2=Tuesday, 3=Wednesday, 4=Thursday, 5=Friday, 6=Saturday, 7=Sunday, 8=everyday
}
case "SPRING":{
morning_watering = ON
evening_watering = ON
itimer_start_hour_am = 7
itimer_start_minute_am = 0
itimer_start_hour_pm = 17
itimer_start_minute_pm = 0
irrigation_day = 8 //1=Monday, 2=Tuesday, 3=Wednesday, 4=Thursday, 5=Friday, 6=Saturday, 7=Sunday, 8=everyday
}
case "SUMMER":{
morning_watering = ON
evening_watering = ON
itimer_start_hour_am = 7
itimer_start_minute_am = 0
itimer_start_hour_pm = 18
itimer_start_minute_pm = 0
irrigation_day = 8 //1=Monday, 2=Tuesday, 3=Wednesday, 4=Thursday, 5=Friday, 6=Saturday, 7=Sunday, 8=everyday
}
}
// calculate current time [min]
var int icurrent_time
icurrent_time = now.getMinuteOfDay
icurrent_time = icurrent_time.intValue
logInfo("Crib","Current Time " + icurrent_time)
if ((morning_watering == ON) && (now.getDayOfWeek == irrigation_day) || (irrigation_day == 8)){
// calculate the alarm time [min]
var int alarm_timer_am
alarm_timer_am = (itimer_start_hour_am.intValue * 60) + itimer_start_minute_am.intValue
logInfo("Crib","alarm_timer_am " + alarm_timer_am)
// calculate the difference between the requested alarm time and current time (again in minutes)
delta_itimer_am = (alarm_timer_am - icurrent_time)
logInfo("Crib","Delta time AM " + delta_itimer_am)
// add one day (1440 minutes) if alarm time for today already passed
if (icurrent_time > alarm_timer_am) {
delta_itimer_am = delta_itimer_am + 1440
logInfo("Crib","Delta time AM " + delta_itimer_am)
}
//create the timer
itimer_am = createTimer(now.plusMinutes(delta_itimer_am)) [|
logInfo("Crib","Irrigation AM Scheduler Running")
Commence_Irrigation.sendCommand(ON)
//itimer_am = null
]
}
if ((evening_watering == ON) && ((now.getDayOfWeek == irrigation_day) || (irrigation_day == 8))){
// calculate the alarm time [min]
var int alarm_timer_pm
alarm_timer_pm = (itimer_start_hour_pm.intValue * 60) + itimer_start_minute_pm.intValue
logInfo("Crib","alarm_timer_pm " + alarm_timer_pm)
// calculate the difference between the requested alarm time and current time (again in minutes)
delta_itimer_pm = (alarm_timer_pm - icurrent_time)
logInfo("Crib","Delta time PM " + delta_itimer_pm)
// add one day (1440 minutes) if alarm time for today already passed
if (icurrent_time > alarm_timer_pm) {
delta_itimer_pm = delta_itimer_pm + 1440
logInfo("Crib","Delta time PM " + delta_itimer_pm)
}
//create the timer
itimer_pm = createTimer(now.plusMinutes(delta_itimer_pm)) [|
logInfo("Crib","Irrigation PM Scheduler Running")
Commence_Irrigation.sendCommand(ON)
//itimer_pm = null
]
}
end
However, I get an exception error at the itimer_am = null and itimer_pm = null parts in the timers.
I can see that it was discussed at the below post, but II canât figure out how to correctly set the timer back to null outside of the timer - really not sure how to do that.
https://community.openhab.org/t/error-when-resetting-timer/33265/3
The rule seems to work fine with not setting the timer back to null, but I just wanted to double check whether there is anything wrong with this to avoid any unintended consequences.
If the entire exception is required to assist I can put that in.
Advice is very much appreciated