How to configure openHAB for setting alarm/schedule times thru the UI

7,822 views
Skip to first unread message

Ben Jones

unread,
Dec 17, 2014, 5:48:47 AM12/17/14
to ope...@googlegroups.com
This has been bugging me for ages, how to set alarm times in openHAB. There is no 'time' widget and using numbers/dimmers/setpoints just doesn't seem to work - plus I didn't want to have two items, one for hours and one for minutes...

So here is what I came up with this evening if you are interested - not the simplest bit of config but it does work and look pretty good...

ITEMS
Switch     Alarm_Master          "Master Alarm"       <presence>       (Alarms)
Number     Alarm_MasterTimeMins  "Master Alarm"       <clock>          (Alarms)
String     Alarm_MasterTime      "Master Alarm [%s]"  <clock>
Switch     Alarm_MasterEvent     "Master Alarm Event" <alarm>          (AlarmEvents)      { autoupdate="false" }

RULES
var Timer masterAlarmTime = null

rule
"Master bedroom alarm time"
when
   
Time cron "0 5 0 * * ?" or
   
Item Alarm_MasterTimeMins received update
then
   
var int minutes = (Alarm_MasterTimeMins.state as DecimalType).intValue()

   
if (masterAlarmTime != null)
        masterAlarmTime
.cancel()

   
// work out when the alarm is to fire - start from midnight
   
var DateTime alarmTime = parse(now.getYear() + "-" + now.getMonthOfYear() + "-" + now.getDayOfMonth() + "T00:00")

   
// add the number of minutes selected
    alarmTime
= alarmTime.plusMinutes(minutes)

   
// if we have already past the alarm time then set it for the following day
   
if (alarmTime.beforeNow)
        alarmTime
= alarmTime.plusDays(1)

   
// create a timer to execute the alarm at the specified time
    masterAlarmTime
= createTimer(alarmTime) [|
       
if (Alarm_Master.state == ON && Holiday.state == OFF && now.getDayOfWeek() < 6)
           
Alarm_MasterEvent.sendCommand(ON)
   
]

   
// update the alarm display time    
   
Alarm_MasterTime.sendCommand(String::format("%02d:%02d", alarmTime.getHourOfDay(), alarmTime.getMinuteOfHour()))
end
rule "Master bedroom alarm"
when
    Item Alarm_MasterEvent received command ON
then
    // do your alarm stuff - turn on radio, dim up lights, start the coffee machine...
end

SITEMAP
Frame label="Alarm" {
    
Text item=Alarm_MasterTime icon="presence-on" visibility=[Alarm_Master==ON] {
        
Frame label="Master Alarm" {
            
Switch item=Alarm_Master
            
Text item=Alarm_MasterTime
            
Setpoint item=Alarm_MasterTimeMins minValue=0 maxValue=600 step=5
        
}
   
}
    Text item=Alarm_MasterTime icon="presence-off" visibility=[Alarm_Master==OFF] {
        
Frame label="Master Alarm" {
            
Switch item=Alarm_Master
            
Text item=Alarm_MasterTime
            
Setpoint item=Alarm_MasterTimeMins minValue=0 maxValue=600 step=5
        
}
    
}
}

So a little explanation. Basically you are specifying the alarm time as a number from 0-600 mins (i.e. 0-10 hours). The base time is midnight, so this corresponds to a time between midnight and 10am. Obviously very easy to change the Setpoint config in your sitemap to extend or restrict this. I have it set to 'step' in 5 min increments, but you could make this 15 mins to make it easier to quickly set coarse alarm times if you wanted.

Whenever you change the alarm time mins item using the Setpoint widget, the alarm time is calculated and a display item is updated, so you can show the nicely formatted alarm time in the sitemap. This item is not used for anything other than display. 

The reason I have two frames for displaying the alarm details is so I can merge both the alarm switch and time items into one sitemap widget. So if the alarm is enabled I hardcode the presence-on icon and display the alarm time, if it is disabled I hard-code the presence-off icon. By doing this I can display the alarm time and have an icon showing the alarm state. Drilling down into that frame gives you the option to disable the alarm and change the time.

I am pretty happy with the end result - I can now easily change my alarm times via the UI and view the current time at a glance.


Patrik Gfeller

unread,
Dec 18, 2014, 11:29:37 AM12/18/14
to ope...@googlegroups.com
Hi Ben,

thank you very much for sharing :-) ... such examples are invaluable for beginners like me. If I find time I'll add your approach to the wiki as well (if you do not mind): https://github.com/openhab/openhab/wiki/AlarmClock

thanks and kind regards,
Patrik

Ben Jones

unread,
Dec 18, 2014, 2:20:37 PM12/18/14
to ope...@googlegroups.com
No worries Patrik - feel free to add it to the WIKI. I saw your example in there and like the addition of the day switches. Might have to add that to mine!

Thomas Eichstädt-Engelen

unread,
Dec 19, 2014, 11:48:56 AM12/19/14
to ope...@googlegroups.com
Hi Ben,

wow, thanks for sharing this solution! It would be really great to have it in the Wiki.

Best, Thomas E.-E.


--
You received this message because you are subscribed to the Google Groups "openhab" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openhab+u...@googlegroups.com.
To post to this group, send email to ope...@googlegroups.com.
Visit this group at http://groups.google.com/group/openhab.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages