DateTime Conversion (openHAB 2.x)

@anfaenger This page is extremely useful. Thank you for writing it. However, I noticed a tiny error, which, if fixed would greatly help:

Please update the link to Joda DateTime class to DateTime (Joda time 2.2 API)

thank you

2 Likes

Done.
Thanks a lot.

1 Like

Hi,
I had a lot of trouble with this yesterday and today and just wanted to share my solution in case anyone else has the problem:

Taking the Unix timestamp (Epoch) from a Number item and constructing a new DateTime variable.
Edit:
Preferrable solution, thanks to @rlkoshak:

var DateTime test = new DateTime((AlarmTime_Unix.state as Number).longValue * 1000)

My solution:

var DateTime test = new DateTime(Long::parseLong(AlarmTime_Unix.state.toString) * 1000)
logInfo("Test_Button", "Variable test: " + test.toString)
// AlarmTime_Unix.state: 1562909701
// Variable test: 2019-07-12T07:35:01.000+02:00

The initial post did not help me with this.

1 Like

Assuming AlarmTime_Unix is a Number Item like you say, you can

var DateTime test = new DateTime((AlarmTime_Unix.state as Number).longValue * 1000)

This avoids the unnecessary conversion from a Number to a String and then back to a Number.

1 Like

Hi Rich,

Thanks, indeed it works. I could swear Iā€™ve tried that aswell with the five other waysā€¦
Iā€™ve edited your code into my answer.

Hi @Chewie,

It shows how to convert from Epoch to JodaDateTime, but you want to convert from NumberItem to DateTime.
As you and Rich has shown, in this case it is first necessary to convert the NumberItem into an Epoch value.
I added your solution to my inital post:

Thank you for sharing!

1 Like

Great, happy to see it in the initial post. Maybe it helps somebody :slight_smile:

1 Like

Hi, please can someone help me with my problem? I want to convert a time in milliseconds (this is joda, right?) to a readable DateTime item:

I get the next alarmclock time from my android app (habdroid) in this format:

// provided from the phone in this format --> 1570744800000
Number Phone_AlarmClock   "Alarm Clock Phone [%d]"

// this item is for the rule
DateTime Phone_AlarmClock_DT   "Alarm Clock Phone DT [%1$tA, %1$td.%1$tm.%1$tY %1$tT]"

My rule which isnĀ“t workin:

rule "Phone Alarm Clock to DateTime"
	when
		Item Phone_AlarmClock received update or
		Time cron "0/5 * * * * ?"   // only for testing
	then
		val alarmtime = Phone_AlarmClock.state
		val MyDateTimeTypeFromJoda = new DateTimeType(alarmtime.toString)
		Phone_AlarmClock_DT.postUpdate(MyDateTimeTypeFromJoda)
end

Error 1:
Error during the execution of rule ā€˜Phone Alarm Clock to DateTime umrechnenā€™: 1570744800000 is not in a valid format.

When i change

val alarmtime = Phone_AlarmClock.state

to

val alarmtime = Phone_AlarmClock.state as number

i get this error:
Error 2:
Error during the execution of rule ā€˜Phone Alarm Clock to DateTimeā€™: Could not cast 1570744800000 to void; line 29, column 19, length 37

WhatĀ“s wrong here?

Try that:

rule "Phone Alarm Clock to DateTime"
when
    Item Phone_AlarmClock received update or
    Time cron "0/5 * * * * ?"   // only for testing
then
    val alarmtime = Phone_AlarmClock.state.toString
    val MyDateTimeTypeFromJoda = new DateTimeType(alarmtime)
    Phone_AlarmClock_DT.postUpdate(MyDateTimeTypeFromJoda)
end

Canā€™t the Phone_AlarmClock be a DateTime Item?

I donĀ“t know?

Here you can see it in the docs: https://www.openhab.org/docs/apps/android.html#alarm-clock

@vzorglub
With your changes i get this error: : String index out of range: 10

Ok, according to the first post we need to use method # 2

new DateTimeType(new DateTime(MyEpoch).toString)

So:

rule "Phone Alarm Clock to DateTime"
when
    Item Phone_AlarmClock received update or
    Time cron "0/5 * * * * ?"   // only for testing
then
    val alarmtime = Phone_AlarmClock.state.toString
    val MyDateTimeTypeFromJoda = new DateTimeType(new DateTime(alarmtime).toString)
    Phone_AlarmClock_DT.postUpdate(MyDateTimeTypeFromJoda)
end

BUT @5iver has a good point, it would be worth trying to set the item as a DateTime just to try

Thanks, it seems that my given time from android app is to long.

I get this error: Invalid format: ā€œ1570744800000ā€ is malformed at ā€œ0000ā€


I will try the other hint with changing the item too.

EDIT: Then i get this error:
Tried to set invalid state 1570744800000 (DecimalType) on item PhoneBernd_AlarmClock of type DateTimeItem, ignoring it

Now i changed some things and it works:

rule "Alarm Clock"
when
    Item Phone_AlarmClock changed or
    Time cron "0/5 * * * * ?"     // for testing
then
    if (Phone_AlarmClock.state as Number != 0 && Phone_AlarmClock.state as Number != NULL) {
        	var alarmtime = new DateTime((Phone_AlarmClock.state as Number).longValue)
        	logInfo("RULES", "AlarmClock - alarmtime " +  alarmtime.toString)
		Phone_AlarmClock_DT.postUpdate(alarmtime.toString)
    }
end

Hello,

is there a way to get EPOCH Millis (or other ) from time format like this 17:16:39 23.10.2019 ?
I mean direct converstion and not extra javascript function.

Thx

In what context? In a Rule? On your sitemap?

Oh sorry, in rule.

You will need to parse out the different parts of the date and time and build a DateTime using the parts. See http://joda-time.sourceforge.net/apidocs/org/joda/time/DateTime.html for details. Once you have the DateTime just call .millis.

Since M5 exists capability to accept EPOCH values directly by DateTimeType

2 Likes

So I can postUpdate(longValue) now? Thatā€™s pretty cool and useful. Has this made it to the docs yet? Iā€™m not sure all the places it would need an update. The Items table for sure, probably the Items config page and Rules Type conversions section too. I donā€™t see it mentioned anywhere so I guess that answers that question.

Can I pass a long to postUpdate or only a String? e.g. MyDateTime.postUpdate(long)

Iā€™ll need to do some experiments.