Get epoch new DateTime ZonedDateTime

Hi there!
I’m still strugiling with OH3 new date time library. I managed to narrow down, why my script won’t wrok to this line:

var epoch = new DateTime(((AlarmClock.state as Number)).longValue)

Can someone help me how should I rewrite this so it works in OH3? I couldn’t get a deffinitve answer from my hour long google session. Thanks!

Why google when the answer is here, in the forum.

Yes, thanks I found this post, but sadly I couldn’t interpret it to my case. Could you perhaps specify it a bit for me. I’m not coding in Java, I only use it for openhab rules, so I’m very novice to this field. Thanks!

You would need to show your full rule and tell what type AlarmClock is and what error is given.
But frankly I won’t be debugging every single line of your code, that everyone has to understand and do himself.
Either way if ((AlarmClock.state as Number)).longValue is a long you cannot do DateTime(long).
And it does not make sense to do that as the Epoch (which your variable is supposed to represent as I’m guessing from the name) is represented as a long already i.e. it’s wrong to convert that into DateTime (even if that worked your “epoch” variable would be of DateTimeType which is wrong).

I’m using this template rule I have found online for android alarmclock:

var Timer timerAlarm = null

rule "Alarm Clock"
when
    Item AlarmClock changed
then
    if (AlarmClock.state as Number == 0) {
        if (timerAlarm !== null) {
            timerAlarm.cancel
            timerAlarm = null
        }
        logInfo("alarm", "All alarms are cancelled")
    } else {
        var epoch = new DateTime((AlarmClock.state as Number).longValue)
        logInfo("alarm", "Scheduling alarm for " +  epoch.toString)

        if (timerAlarm !== null) {
            logInfo("alarm", "Reschedule alarm")
            timerAlarm.reschedule(epoch)
        } else {
            logInfo("alarm", "New Alarm")
            timerAlarm = createTimer(epoch,
                [ k |
                    // Turn on stuff, e.g. radio or light if somebody at home
                    if(gPresent.state == ON){
                    GF_MasterBedroom_Light.sendCommand(ON)
                    }
                    logInfo("alarm", "alarm is expired")
                ]
            )
            
            
        }
    }
end

I understand that I’m very on the edge of what expected here on this forum, but yet I have no idea how to “upgrade” my rules to OH 3. So I’m gonna leave it here just in case someone else’s willing to help. Cause frankly openHab’s image is it fit for hobbyists nad didn’t have too much trouble getting to know it and getting to love it, and I managed to make a couple of complex scripts, yes with this community forum’s help from time to time, but this new version change with all it’s deprecated libraries is leaving me in the dark and frankly I don’t know how to proceed other than leaving the platform… :frowning: (I got this answer for the second time)

not sure what you are trying to accomplish. As Mstormi says, it would be helpfull to show the rest of the code. What data does the variable epoch has to contain? A long epoch number (1619385198), a normal date notation (25/04/2021 23:06) or just an hour number (23), a string (“25-04-2021 23:06”), etc.? So what type of data does AlarmClock contain?

    var Long lngSetTime

    lngSetTime = now.toInstant().toEpochMilli()

Preset_status.postUpdate(new DateTimeType(lngSetTime.toString))

This is how i fill a variable with a epochmilli value and fill a datetime type item with it.

lngUpdateTime = (lastSeenP3.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli

This way I fill a long with millis from a Datetime item.

    val String strTime1 = "21:05:00"
    val String strDate = now.toLocalDateTime.toString.split("T").get(0) + "T"
    val ZonedDateTime dteTime1 = ZonedDateTime.parse(strDate + strTime1 + ".000Z").withZoneSameLocal(ZoneId.systemDefault())

This is how i convert a string time to a zonedatetime variable. Just handy and readable and easy to calculate with as in: [ now.isBefore(dteTime1.plusMinutes(75)) ].

I’m sure there are better ways then I show you, but it was the most I could do :slight_smile:

[edit] My post took to long, so didn’t read your last post :slight_smile:

1 Like

S o I was too deep in my rule, forgot, that I was using the template rule from OH 2.5’s android alarm. I found the solution, it was in the documentation of OH 3’s exampels. I would n’t have a chance to rewrite the rule by myself.
https://www.openhab.org/docs/apps/android.html

var Timer timerAlarm = null

rule "Alarm Clock"
when
    Item AlarmClock changed
then
    if (newState instanceof DateTimeType) {
        val epoch = newState.toLocaleZone.zonedDateTime.toInstant.toEpochMilli
        logInfo("alarm", "Scheduling alarm for {} ({})", newState.toLocaleZone, epoch)
        if (timerAlarm !== null) {
            logInfo("alarm", "Reschedule alarm")
            timerAlarm.reschedule(newState.toLocaleZone.zonedDateTime)
        } else {
            logInfo("alarm", "New alarm")
            timerAlarm = createTimer(newState.toLocaleZone.zonedDateTime, [ |
                // Turn on stuff, e.g. radio or light
                logInfo("alarm", "Alarm expired")
                timerAlarm = null
            ])
        }
    } else {
        if (timerAlarm !== null) {
            timerAlarm.cancel
            timerAlarm = null
        }
        logInfo("alarm", "Alarm canceled")
    }
end

1 Like