[SOLVED] How to move timeDate to item in a rule?

  • Platform information:
    • Hardware: RPI4
    • OS: latest openhabian
    • Java Runtime Environment: default
    • openHAB version: latest openhabian
      Hello,

I’m trying to display the date and alarm of my android phone on openhab. I managed to get the epoch time to an item in openhab via the android app. I allso managed to display the date and time in the logs using loginfo. My problem starts when i want to move the datetime to a DateTime item using the rule. I searched the forum but cant find any example.

.items

Number AlarmClockJimmy {expire="2s, state=OFF"} //epoch
DateTime AlarmClockJimmyDateTime

.rule

var Timer timerAlarm = null

rule "Alarm Clock"
when
    Item AlarmClockJimmy changed
then
logInfo("Android alarm", "rule started")
    if (AlarmClockJimmy.state as Number == 0) {
        if (timerAlarm !== null) {
            timerAlarm.cancel
            timerAlarm = null
        } 
        logInfo("Android alarm", "All alarms are cancelled")
        
    } else {
        var epoch = new DateTime((AlarmClockJimmy.state as Number).longValue)
        val DateTime datetime = new DateTime(epoch)
        val String time = datetime.toLocalTime().toString("HH:mm")
        val String date = datetime.toLocalDate().toString("dd-MM-yyyy")
        logInfo("Android alarm", "Jimmy's alarm instellen voor " +  time.toString + " op " + date.toString)
        AlarmClockJimmyDateTime.sendCommand(datetime , new DateTimeType() ) 

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

.sitemap

Text item=AlarmClockJimmyDateTime label="Alarmijd [%1$tl:%1$tM %tp]"
Text item=AlarmClockJimmyDateTime label="Alarmdatum [%tA %1$te %1$tB %1$tY]"

Any advice is welcome

Does the example in the documentation not work for you?

it works for me but i would like to display alarm date & time in my sitemap like this: time HH:mm, date dd-MM-yyyy. Sorry this DateTime stuff is very new to me

Ok,
Read the doc.

I had:

val DateTime datetime = new DateTime(epoch)

The doc said:

val DateTime datetime = new DateTime(new DateTime(epoch).toString)

Both seem to work (i think) because

val String time = datetime.toLocalTime().toString(“HH:mm”)
val String date = datetime.toLocalDate().toString(“dd-MM-yyyy”)
logInfo(“Android alarm”, "Jimmy’s alarm instellen voor " + time.toString + " op " + date.toString)

puts the correct time and date in the logvieuwer

But my problem is that te sitemap displays “-” for alarm time and alarm date. So i think my problem is in this line:

AlarmClockJimmyDateTime.sendCommand(datetime , new DateTimeType() )

or in .items or .sitemap

But thanks for the docs, makes for an interresting read :wink:

Indeed!
changed the line from:

AlarmClockJimmyDateTime.sendCommand(datetime , new DateTimeType() )

to:

AlarmClockJimmyDateTime.sendCommand(datetime.toString , new DateTimeType() )

Now both time and date show up in my sitemap.

Thanks for the help

Text item=AlarmClockJimmyDateTime label="Alarmdatum [time %1$tR date %1$td/%1$tm/%1ty ]"

https://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html

sendCommand method only takes one parameter, the command.
new DateTimeType() (which would be “now”) is presumably ignored.

If you only want to set the Item state, unless you have some particular reason for using a command,
AlarmClockJimmyDateTime.postUpdate(datetime.toString)

Ok, thanks. sort of a bad habbit of using command

:grinning:

Thanks for the help