Time Scheduling/Event Scheduler

So I’m working on this project now and I can’t move on to the part of time scheduling. I want to schedule a scheduled on and off time multiple times. Can someone please teach me how to do this? Now, I have only the off schedule part. It’s pretty hard and confusing. Any help will be appreciated. Thanks!

This is my code:

import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
import org.joda.time.*
import org.openhab.action.squeezebox.*

import java.util.concurrent.locks.ReentrantLock

var Timer timer1=null
var java.util.concurrent.locks.ReentrantLock lock1 = new java.util.concurrent.locks.ReentrantLock()
rule “Initialization”
when
System started
then

postUpdate(TimeHour,8)
postUpdate(TimeMinute,30)
postUpdate(DayMon, ON)
postUpdate(DayTue, ON)
postUpdate(DayWed, ON)
postUpdate(DayThu, ON)
postUpdate(DayFri, ON)
postUpdate(DaySat, ON)
postUpdate(DaySun, ON)

postUpdate(TimeHour1,10)
postUpdate(TimeMinute1, 00)
postUpdate(DayMon1, ON)
postUpdate(DayTue1, ON)
postUpdate(DayWed1, ON)
postUpdate(DayThu1, ON)
postUpdate(DayFri1, ON)
postUpdate(DaySat1, ON)
postUpdate(DaySun1, ON)

end

rule “Television Sched”
when

Item TimeHour changed or Item TimeMinute changed or Time cron “5 * * * * ?”
then
lock1.lock()
try
{
var String msg = “”
var hour = TimeHour.state as DecimalType
var minute = TimeMinute.state as DecimalType

if (hour < 10) {msg =“0” }
msg = msg + TimeHour.state.format("%d") + “:”

if (minute < 10) {msg = msg + “0”}
msg = msg + TimeMinute.state.format("%d")

postUpdate(ClockTimeMessage, msg)

var int time1
time1 = (TimeHour.state as DecimalType).intValue
time1= time1.intValue
var int time2
time2 = (TimeMinute.state as DecimalType).intValue
time2 = time2.intValue

var int comh
comh = now.getHourOfDay.intValue
var int comm
comm = now.getMinuteOfHour.intValue
var int comd
comd = now.getDayOfWeek.intValue

if ((time1 == comh) && (timea == comm) && (DayMon.state==ON) && (comd == 1))
{
KanKun1.sendCommand(OFF)
postUpdate(KanKun1, OFF)
}

if ((time1 == comh) && (timea == comm) && (DayTue.state==ON) && (comd ==2 ))
{
KanKun1.sendCommand(OFF)
postUpdate(KanKun1,OFF)
}

if ((time1 == comh) && (timea == comm) && (DayWed.state==ON) && (comd == 3 ))
{
KanKun1.sendCommand(OFF)
postUpdate(KanKun1, OFF)
}

if ((time1 == comh) && (timea == comm) && (DayThu.state==ON) && (comd == 4))
{
KanKun1.sendCommand(OFF)
postUpdate(KanKun1, OFF)
}

if ((time1 == comh) && (timea == comm) && (DayFri.state==ON) && (comd == 5))
{
KanKun1.sendCommdand(OFF)
postUpdate(KanKun1, OFF)
}

if ((time1 == comh) && (timea == comm) && (DaySat.state==ON) && (comd == 6 ))
{
KanKun1.sendCommand(OFF)
postUpdate(KanKun1,OFF)
}

if((time1 == comh) && (timea == comm) && (DaySun.state==ON) && (comd == 7))
{
KanKun1.sendCommand(OFF)
postUpdate(KanKun1, OFF)
}
}
finally
{
lock1.unlock()
}
end

rule “Electric Fan Sched”
when

Item TimeHour1 changed or Item TimeMinute1 changed or Time cron “6 * * * * ?”
then
lock1.lock()
try
{
var String msg1 = “”
var hour1 = TimeHour1.state as DecimalType
var minute1 = TimeMinute1.state as DecimalType

if (hour1 < 10) {msg1 =“0” }
msg1 = msg1 + TimeHour1.state.format("%d") + “:”

if (minute1 < 10) {msg1 = msg1 + “0”}
msg1 = msg1 + TimeMinute1.state.format("%d")

postUpdate(ClockTimeMessage1, msg1)

var int timeh
timeh = (TimeHour1.state as DecimalType).intValue
timeh = timeh.intValue
var int timem
timem = (TimeMinute1.state as DecimalType).intValue
timem = timem.intValue

var int comh1
comh1 = now.getHourOfDay.intValue
var int comm1
comm1 = now.getMinuteOfHour.intValue
var int comd1
comd1 = now.getDayOfWeek.intValue

if ((timeh == comh1) && (timem == comm1) && (DayMon1.state==ON) && (comd1 == 1))
{
KanKun2.sendCommand(OFF)
postUpdate(KanKun2, OFF)
}

if ((timeh == comh1) && (timem == comm1) && (DayTue1.state==ON) && (comd1 ==2 ))
{
KanKun2.sendCommand(OFF)
postUpdate(KanKun2,OFF)
}

if ((timeh == comh1) && (timem == comm1) && (DayWed1.state==ON) && (comd1 == 3 ))
{
KanKun2.sendCommand(OFF)
postUpdate(KanKun2, OFF)
}

if ((timeh == comh1) && (timem == comm1) && (DayThu1.state==ON) && (comd1 == 4))
{
KanKun2.sendCommand(OFF)
postUpdate(KanKun2, OFF)
}

if ((timeh == comh1) && (timem == comm1) && (DayFri1.state==ON) && (comd1 == 5))
{
KanKun2.sendCommdand(OFF)
postUpdate(KanKun2, OFF)
}

if ((timeh == comh1) && (timem == comm1) && (DaySat1.state==ON) && (comd1 == 6 ))
{
KanKun2.sendCommand(OFF)
postUpdate(KanKun2,OFF)
}

if((timeh == comh1) && (timem == comm1) && (DaySun1.state==ON) && (comd1 == 7))
{
KanKun2.sendCommand(OFF)
postUpdate(KanKun2, OFF)
}
}
finally
{
lock1.unlock()
}
end

To make you code easier to read please indent your lines by four spaces so it preserves your indentation… As it is it is very difficult to read.

From what I can tell with a quick scan this looks overly complicated and brittle. See this posting to see if it shows a way to implement what you are looking for.

Please, indeed, format the code. :wink:

I currently have an ‘alarm clock’ set up in OpenHAB which can be enabled and disabled per day and the time can also be set from the UI. It’s more or less straight from the example on Github - which you certainly should take a look at:

Obviously, timers are very easy to create and cancel again, but not persistent across restarts. OpenHAB could do with a ‘scheduler binding’ or something similar.

1 Like

Hi James. Is it possible to do Scheduling events with other applications other than OpenHAB?

There is a CalDav binding that lets you use any compatible calendar application to schedule events. These include Outlook and Google Calendar among others.

Scheduled events can be sent to OH using IFTTT.

If you have an Android phone, Tasker can be configured to send events to OH.

Really, any sort of external product that is either able to make REST API calls into OH or send MQTT messages, or any other form of communication that OH can support.