DateTime Conversion

Tags: #<Tag:0x00007f616f77cec8> #<Tag:0x00007f616f77ce00> #<Tag:0x00007f616f77cd38>

I believe he was referring to the updates sent by Habdroid for the alarm clock, but I could be wrong!

This is what I was thinking of…

Oh, wait… there’s this too!

1 Like

That’s an awesome HowTo!
I always struggle with the time formats.
This helps a lot in this Jungle!
Thanks a mil!

Hi
I’m just checking before I go down this rabbit hole of wonders…

Is your Phone_AlarmClock item getting updates from the Android openHAB2 app, that in turn gets updates from the Android alarm clock feature… ???

Sorry to disturb you.

May I pick your brains?

I’m trying to separate just the hour and minute from a dateTime Item.

Would something like this work?

var Hour = new DateTime((Phone_AlarmClock.state as Number).hour)
var Min = new DateTime((Phone_AlarmClock.state as Number).minute)

I haven’t been able to get my head around how to handle DateTime Type

Update

I’m not entirely sure why, but this seems to be doing the trick

   rule "Stu phone alarm time"
 when
 Item Stu_AlarmClock changed
 then


 val DateTime TodayJoda = now
 val TodayDoY = TodayJoda.toString("D")

 val DateTime myJoda = new DateTime(triggeringItem.state.toString)
 var DoY = myJoda.toString("D")
 var Hour = myJoda.toString("k")
 var Min = myJoda.toString("m")

 logInfo("test","Today is DoY = "+TodayDoY+" Alarm Day of Year = "+DoY+" Hour = "+Hour+" & Minute = "+Min)
 TestHour.sendCommand(Hour)
 TestMinute.sendCommand(Min)

 end

There’s some more to do with this to get to where I want, but I think I’m in the right direction now

Its like the evolution of man

Its taken an hour or so, but I think this is what I was hoping for

  rule "Stu phone alarm time"
 when
 Item Stu_AlarmClock changed
 or
 System started
 or
Time cron "0 10 0 1/1 * ? *"

 then


 val Number AlarmEpoch = (Stu_AlarmClock.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli

 val DateTime TodayJoda = now
 val TodayDoY = TodayJoda.toString("D")

 val DateTime myJoda = new DateTime(Stu_AlarmClock.state.toString)
 var DoY = myJoda.toString("D")
 var Hour = myJoda.toString("k")
 var Min = myJoda.toString("m")

 logInfo("test","Today is DoY = "+TodayDoY+" Alarm Day of Year = "+DoY+" Hour = "+Hour+" & Minute = "+Min)


 logInfo("test"," Alarm Epoch = "+AlarmEpoch)

 if (AlarmEpoch >=0){ if ( DoY == TodayDoY ){
 logInfo("test","Conditions True")





 TestHour.sendCommand(Hour)
 TestMinute.sendCommand(Min)
         // SwitchAlarm.sendCommamd(ON)
         logInfo("test","Switch Alarm ON")
 return
         }        }
 TestHour.sendCommand(0)
 TestMinute.sendCommand(0)
 logInfo("test","Switch alarm OFF")
 // SwitchAlarm.sendCommamd(OFF)

 end

im hoping it can help

A great resource for understanding this better is the awesome time conversion thread by rud @anfaenger found here here

1 Like

What part are you unsure about? The code

  • Gets the current time as a Joda DateTime (TodayJoda)
  • Converts the DateTimeType from Stu_AlarmClock to a Joda DateTime (myJoda)
  • Extracts the day (DoY), hour, and minute as a String
  • Sends the hour to TestHour and minute to TestMinute as commands

To get those values as numbers instead of Strings

val DoY = myJoda.dayOfYear
val Hour = myJoda.hourOfDay
val Min = myJoda.minuteOfHour

I’m going from memory, the calls might be a little different. Use VSCode and it should show you the right ones.

1 Like