Using a timer switch "interface"

I have a working interface where I can set the hour, minute and duration I want a switch to be on for on any given day.

My dilemma is using this information once I have it.

At the moment I am using a cron job to check the time start + duration and then if it is greater than now, turn off or if the time is less than timestart, turn off. This all works but the trouble is it resets the values even if I want to override them.

I would have thought a createtimer would have been the go after running the script at the start time, but I am not sure how to do this bit (or even write it down properly to explain for that matter)

see this for timer examples and resetting/overriding.

Hi thanks for the link - I understand how to creat a timer - it is more how to trigger the rule based on a time value - would it be worth me posting my sitemap?

1 Like

I dont know if I exactly understand your problem - but I had difficulties to configure time based behaviour too.

In my setup I switch some outer lights if

  • Doors are opened to the outside (A)
  • Some one rings the door bell on the entry gate (B)
  • The automatic gate at the carport opens ©
  • The motion detection of the house entry camera fires (D)

The triggered lights are not distinct on the above events. That means that e.g. a Lights L1 and L2 are triggered by Events
A and D while Event B triggers only L1 and Event C triggers only L2.

In addition lights will only switch if it’s “dark” outside which I determine by sunrise/sunset time (from astrodate binding) +/- a configurable offset.
The lights will be on for a configurable amount of minutes, extended by a different configurabel amout of minutes if a trigger is still in the condition. Shortly:
Light switches on for 5 minutes if the door is opened. If the door is still open after 5 minutes, lights stay on for one minute longer and so on.

I will not post all the rules here but summarize my findings:

  • Use Time-based cron rule trigger and compare with now() to determine any state based on absolute timestamps
  • Use Timers to determine any state based on relative timestamps
  • Use Number items to change/store/persist any configuration values you need
  • Use Switch items without persistence as booleans to coordinate different timers (otherwise you’ll have no chance to determine if a timer is “armed”)

Hope it helps.
Cheers - Robert

Exactly @rbausdorf

So looking at your example, you have cron rules to manage the trigger? And then check the times based on the stored values against those?

This means running a cron rule every minute?

This means running a cron rule every minute?

No - every 30 seconds. :smile:

Hi Andrew! I’ve been working on that kind of timer for months now. And I can’t figure out how to do that. Would you mind sharing your code to me? Thanks buddy.

2nd!!!

Hmm, this thread is months old, Drew_Brown and James1. But, I believe this is what you’re looking for:

Cheers,

Pel

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


1 Like

Hey thanks! It will take me some time before I can give this a try but I will for sure.
Thank you for sharing.

no probs - report back with any improvements!