Synchronize your alarm clock times with your lights with OpenHab in just 3 easy steps

Why?

What is needed?

  • an OpenHab 2 setup (other software might have similar functionality, but this is what I am using)
  • an Android smartphone with an alarm software that uses the build-in alarm API and the official OpenHab App. There is an iOS version of the app but it seems to not support syncing the alarm times
  • Some dimm-able lights you can control via OpenHab

How to?

/* Alarms */
Number AlarmClock "Smartphone Alarm" <time> // timestamp sent from the app (e.g. 123456789
DateTime AlarmClockTime "Next Alarm" <time> // time as an actual DateTime object (e.g. 16.04.2020 09:45:00)
Switch AlarmActivated <switch> // activates or deactivates the feature
Frame label="Alarm" {
Text item=AlarmClock // this is just for seeing your initial data, it can be removed later
Text item=AlarmClockTime label="Next Alarm [%1$td.%1$tm.%1$ty %1$tH:%1$tM]"
Switch item=AlarmActivated label="Alarm active"
}
var DateTime alarmTime
var Timer timer1 = null
var Timer timer2 = null
var Timer timer3 = null
var Timer timer4 = null
var Timer timer5 = null
val validAlarmDays = newArrayList(1,2,3,4,5) // Monday 1, Sunday 7)
rule "Alarm Clock time"
when
Item AlarmClock changed
then
alarmTime = new DateTime((AlarmClock.state as Number).longValue) // conversion
logDebug("Rule", "Alarm in millis is {}", (AlarmClock.state as Number).longValue)
AlarmClockTime.postUpdate(alarmTime.toString)
val Integer alarmDayOfWeek = alarmTime.getDayOfWeek()
logDebug("Rule", "Next alarm is on day " + alarmDayOfWeek + " of the week, which is on a valid day: " + validAlarmDays.contains(alarmDayOfWeek))
if (validAlarmDays.contains(alarmDayOfWeek)) {
logDebug("Rule", "Starting dimm up timers at {}", alarmTime.minusMinutes(10))
// start lights on dimmers
timer1 = createTimer(alarmTime.minusMinutes(9)) [|
if (AlarmActivated.state == ON) {
sendCommand(grDimmerWZ, 20)
logDebug("Rule", "Dimming alarm light to 20%")
} else {
logDebug("Rule", "Not changing lights because the alarm is deactivated")
}
]
timer2 = createTimer(alarmTime.minusMinutes(8)) [|
if (AlarmActivated.state == ON) {
sendCommand(grDimmerWZ, 40)
logDebug("Rule", "Dimming alarm light to 40%")
} else {
logDebug("Rule", "Not changing lights because the alarm is deactivated")
}
]
timer3 = createTimer(alarmTime.minusMinutes(7)) [|
if (AlarmActivated.state == ON) {
sendCommand(grDimmerWZ, 60)
logDebug("Rule", "Dimming alarm light to 60%")
} else {
logDebug("Rule", "Not changing lights because the alarm is deactivated")
}
]
timer4 = createTimer(alarmTime.minusMinutes(6)) [|
if (AlarmActivated.state == ON) {
sendCommand(grDimmerWZ, 80)
logDebug("Rule", "Dimming alarm light to 80%")
} else {
logDebug("Rule", "Not changing lights because the alarm is deactivated")
}
]
timer5 = createTimer(alarmTime.minusMinutes(5)) [|
if (AlarmActivated.state == ON) {
sendCommand(grDimmerWZ, 100)
logDebug("Rule", "Dimming alarm light to 100%")
} else {
logDebug("Rule", "Not changing lights because the alarm is deactivated")
}
]
}
end

--

--

--

I am a DIY, smart home and automation enthusiast. I am sharing my journey here and on my blog thesmarthomejourney.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Domain Enumeration with Active Directory PowerShell Module

Leetcode 393. UTF-8 Validation

Extend the disk space of Windows 2012/2016 VMs running on Hyper-V

Download In ^*PDF Computer Architecture: From Microprocessors to Supercomputers (The Oxford Series…

Getting Your First Bug (Part I)

Keep your coding skills relevant for the next 200 years.

Summary Of LFX’21 Summer Mentorship With CNCF: Buildpacks

MongoDB Migration — Zero Downtime

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
TheSmartHomeJourney

TheSmartHomeJourney

I am a DIY, smart home and automation enthusiast. I am sharing my journey here and on my blog thesmarthomejourney.com

More from Medium

The director with nine lives

Drama Therapy in Lebanese Prisons: Voice of the Voiceless

A Tale of Two Troels