I found the alarmclock article not quite what I was after - happy to share - I’ve never gone abck and tidies this up (just got it working after ages and that ticked the box) but I am about to embark on a second thing to apply a timer to so I will see then how portable it is.
Theres some gunk in here that is for debugging on the interface (tells you how many seconds till the timer turns off etc and its a but wonky when playing around but seems ok when not updating based on the quick cron job.
Feel free to tidy up and post version 2
Sitemap:
Frame label=“System”
{
Switch item=MySwitch icon=“fire”
Text item=StartTimeMessage icon=“calendar”
Text item=StartDurationMessage icon=“clock-on”
Text label=“Switch 1 Schedule” icon=“settings”
{
Frame label=“Switch 1” {
Text item=StartTimeMessage
Text item=TimeMessage
Text item=StatusMessage
}
Frame label="Start Time and Duration" {
Setpoint item=StartClockTimeHour minValue=0 maxValue=23 step=1 icon="clock-on"
Setpoint item=StartClockTimeMinute minValue=0 maxValue=55 step=5 icon="clock-on"
Setpoint item=StartDuration minValue=0 maxValue=1440 step=15 icon="clock-on"
}
Frame label="Day" {
Switch item= AlarmMonday
Switch item= AlarmTuesday
Switch item= AlarmWednesday
Switch item= AlarmThursday
Switch item= AlarmFriday
Switch item= AlarmSaturday
Switch item= AlarmSunday
}
}
}
Items
Switch MySwitch “My Switch” { zwave=“5:command=switch_binary” }
Switch AlarmMonday “Monday”
Switch AlarmTuesday “Tuesday”
Switch AlarmWednesday “Wednesday”
Switch AlarmThursday “Thursday”
Switch AlarmFriday “Friday”
Switch AlarmSaturday “Saturday”
Switch AlarmSunday “Sunday”
String StartTimeMessage “Start [%s]” (gSwitch1_Summary)
String StartDurationMessage “Duration [%s]” (gSwitch1_Summary)
String TimeMessage “Time [%s]” (gSwitch1_Summary)
String StatusMessage “Status [%s]” (gSwitch1_Summary)
Number StartClockTimeHour “Hour [%d]”
Number StartClockTimeMinute “Minute [%d]”
Number StartDuration "Duration [%d]
Rules
import org.openhab.core.library.types.DecimalType
import org.openhab.core.library.types.*
import org.openhab.model.script.actions.*
import java.lang.Math
import org.joda.time.*
import org.openhab.core.persistence.*
var Timer timer1 = null
rule “alarm”
when
Item StartClockTimeHour changed or
Item StartClockTimeMinute changed or
Item StartDuration changed or
Item AlarmMonday changed or
Item AlarmTuesday changed or
Item AlarmWednesday changed or
Item AlarmThursday changed or
Item AlarmFriday changed or
Item AlarmSaturday changed or
Item AlarmSunday changed
then
var String strhours = StartClockTimeHour.state.toString()
strhours = strhours.substring(0,strhours.length() -2)
var String strmins = StartClockTimeMinute.state.toString()
strmins = strmins.substring(0,strmins.length() -2)
if (strmins.length == 1) {strmins = “0” + strmins}
var String strstarttime
strstarttime = strhours + “:” + strmins
var String strDays = “”
if (AlarmMonday.state.toString() == “ON”) {strDays = strDays + “M,”}
if (AlarmTuesday.state.toString() == “ON”) {strDays = strDays + “T,”}
if (AlarmWednesday.state.toString() == “ON”) {strDays = strDays + “W,”}
if (AlarmThursday.state.toString() == “ON”) {strDays = strDays + “Th,”}
if (AlarmFriday.state.toString() == “ON”) {strDays = strDays + “F,”}
if (AlarmSaturday.state.toString() == “ON”) {strDays = strDays + “Sa,”}
if (AlarmSunday.state.toString() == “ON”) {strDays = strDays + “Su,”}
if (strDays.length() > 0)
{
strDays = strDays.substring(0,strDays.length() -1)
strstarttime = strstarttime + " (" + strDays + “)”
}
sendCommand(StartTimeMessage, strstarttime)
sendCommand(StartDurationMessage, StartDuration.state.toString())
//sendCommand(MySwitch, ON)
//sendCommand(MySwitch, OFF)
end
rule "alarm time"
when
Time cron "0 0/2 * * * ?" or
Item StartClockTimeHour changed or
Item StartClockTimeMinute changed or
Item StartDuration changed or
Item AlarmMonday changed or
Item AlarmTuesday changed or
Item AlarmWednesday changed or
Item AlarmThursday changed or
Item AlarmFriday changed or
Item AlarmSaturday changed or
Item AlarmSunday changed
then
var String strhours = StartClockTimeHour.state.toString()
strhours = strhours.substring(0,strhours.length() -2)
var String strmins = StartClockTimeMinute.state.toString()
strmins = strmins.substring(0,strmins.length() -2)
if (strmins.length == 1) {strmins = “0” + strmins}
var String strstarttime
strstarttime = strhours + “:” + strmins
var String strDays = “”
if (AlarmMonday.state.toString() == “ON”) {strDays = strDays + “M,”}
if (AlarmTuesday.state.toString() == “ON”) {strDays = strDays + “T,”}
if (AlarmWednesday.state.toString() == “ON”) {strDays = strDays + “W,”}
if (AlarmThursday.state.toString() == “ON”) {strDays = strDays + “Th,”}
if (AlarmFriday.state.toString() == “ON”) {strDays = strDays + “F,”}
if (AlarmSaturday.state.toString() == “ON”) {strDays = strDays + “Sa,”}
if (AlarmSunday.state.toString() == “ON”) {strDays = strDays + “Su,”}
if (strDays.length() > 0)
{
strDays = strDays.substring(0,strDays.length() -1)
strstarttime = strstarttime + " (" + strDays + “)”
}
sendCommand(StartTimeMessage, strstarttime)
//calculate mins since midnight for start time set
var int alarm1
alarm1 = (StartClockTimeHour.state as DecimalType).intValue * 60 +
(StartClockTimeMinute.state as DecimalType).intValue
alarm1 = alarm1.intValue
//calculate mins since midnight for now()
var int now1
now1 = now.getMinuteOfDay
now1 = now1.intValue
//calculate duration
var int duration1
duration1 = (StartDuration.state as DecimalType).intValue
sendCommand(TimeMessage, duration1 + alarm1)
var Number day = now.getDayOfWeek
if (((day == 1) && (AlarmMonday.state == ON)) ||
((day == 2) && (AlarmTuesday.state == ON)) ||
((day == 3) && (AlarmWednesday.state == ON)) ||
((day == 4) && (AlarmThursday.state == ON)) ||
((day == 5) && (AlarmFriday.state == ON)) ||
((day == 6) && (AlarmSaturday.state == ON)) ||
((day == 7) && (AlarmSunday.state == ON))
) {
if ((now1 < alarm1) || (now1 > alarm1 + duration1)) {
sendCommand(StatusMessage, now1 + " off")
sendCommand(MySwitch,OFF)
} else {
sendCommand(StatusMessage, "time till off " + ((alarm1 + duration1)-now1) + " mins")
sendCommand(MySwitch,ON)
}
} else {
sendCommand(StatusMessage, "off")
sendCommand(MySwitch,OFF)
}
end