Rewrite simple alarm clock

Hi, I am not a developer. so I used examples and copied and pasted till I got things to work.
I used an alarm clock that worked quite well for me. But since I migrated to OH3 it is no longer working.
I know it’s quite old, but I like what it did

It was based on this one: https://github.com/openhab/openhab1-addons/wiki/AlarmClock#alarm-clock—example-ii

here is my rule

import java.time.ZonedDateTime
import java.time.DayOfWeek

var Timer timer1 = null
var java.util.concurrent.locks.ReentrantLock lock1 = new java.util.concurrent.locks.ReentrantLock()

rule "Initialization"
   when
      System started
   then
      postUpdate(alarmTimeHour, 6)
      postUpdate(alarmTimeMinute, 00)

      postUpdate(alarmMonday, ON)
      postUpdate(alarmTuesday, ON)
      postUpdate(alarmWednesday, ON)
      postUpdate(alarmThursday, ON)
      postUpdate(alarmFriday, ON)
      postUpdate(alarmSaturday, OFF)
      postUpdate(alarmSunday, OFF)
end

rule "Alarm"
   when
      Item alarmTimeHour changed or
      Item alarmTimeMinutes changed
   then  
      lock1.lock()
   try {
      var String msg = ""
      var hour = alarmTimeHour.state as Number
      var minute = alarmTimeMinute.state as Number
   if ( hour < 10 ) { msg = "0" }
      msg = msg + alarmTimeHour.state.format ( "%d" ) + ":"
   if ( minute < 10 ) { msg = msg + "0" }
      msg = msg + alarmTimeMinute.state.format ( "%d" )
      postUpdate(alarmTimeMessage,msg)
      
      var int alarm1
      alarm1 = (alarmTimeHour.state as DecimalType).intValue * 60 + (alarmTimeMinute.state as DecimalType).intValue
      alarm1 = alarm1.intValue
      
      var int now1
      now1 = ZonedDateTime::now.getMinute
      now1 = now1.intValue
      
      var int delta1
      delta1 = ( alarm1 - now1 )
      delta1 = delta1.intValue
      
   if (now1 > alarm1) { delta1 = delta1 + 1440 }
   
   if (timer1 !== null ) {
      timer1.cancel
      timer1 = null
      } 
      
      timer1 = createTimer(ZonedDateTime::now.plusMinutes(delta1)) [|
         
         var Number day = ZonedDateTime::now.getDayOfWeek.getValue
         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))) {
         
            // The things to do if the alarm clock is enabled for this day of week: 
            Light_Bedroom.sendCommand(ON)
            IpRadio_Power.sendCommand(ON)
            // ...
            }

      timer1.reschedule(ZonedDateTime::now.plusHours(24))
   ]
   }
   finally {
      lock1.unlock()
   }
end
//End Alarm Clock

and my sitemap

 Frame label="Alarm clock" {
            
        Text label="Alarm Clock [%s]" item=alarmTimeMessage icon="time" {
            Frame label="Time" {
                Setpoint item=alarmTimeHour minValue=0 maxValue=23 step=1
                Setpoint item=alarmTimeMinute minValue=0 maxValue=55 step=5
            }
            Frame label="Weekdays" {
                Switch item=alarmMonday
                Switch item=alarmTuesday
                Switch item=alarmWednesday
                Switch item=alarmThursday
                Switch item=alarmFriday
                Switch item=alarmSaturday
                Switch item=alarmSunday
            }
        }

I’d like to keep the same functionality but rewrite the code so it will work in OH3
How do I get even started?

Thanks in advance, A lot!

Post what you specifically mean by “does not work”. That can be an infinite number of things. Logs? Describe what you expect to happen compared to what is actually happening.

Look around to see if there are better ways to achieve this. For example, Creating Capabilities with Rule Templates: Time of Day shows you how you can achieve this without hardly any code at all. All you’d need to write is a rule with:

rule "Alarm triggered"
when
then
    Light_Bedroom.sendCommand(ON)
    IpRadio_Power.sendCommand(ON)
end

(in the UI you’d skip the rule/when/then/end stuff and just post the two lines) and everything else is just configuration.

Another approach, since you care about the type of day, look at Time Based State Machine [3.2.0;3.4.9].

Hi thank you for pointing me this direction. I will check it out

what I meant by it is not working, is when I give input of 6:00 my alarm goes off at 2:00. unfortunately, that is 2am. I thought it had to do with timezone settings, but they seem correct.
It shows time in CET which corresponds to my current timezone

What shows time in CET? There are three sources of time in a typical OH install and they all need to be correct. There’s the operating system’s time. You can see that from the command line (date command on Linux). There’s the Java time (you can verify that’s correct by looking at the timestamps on your log statements. And finally OH itself has a timezone set in MainUI under Settings → Regional Settings. You can see if that’s correct by looking at it in MainUI.

If any one of these got off somehow it can cause problems.

Logs, mainUI and Linux console all show good time settings.
I wonder if there is not something going on with conversion to epoch, but that is a part I don’t understand

That could be the case which is why I pretty much always recommend against messing with epoch directly. When you use proper date time libraries almost all of that stuff is handled for you.

Honestly, that “simple” alarm clock rule is so overly long and complicated compared to what the rule actually does your time is probably much better spent getting a simpler and more straight forward solution working. It’ll be less time and work in the long run.

actually I start to think it’s the iphone app.

I reset the raspberry, I haven’t used the app since and now it works correctly…