Converting DateTime to String


first… I’m a JAVA developer since > 15 years and I have experience in reading documentations, trying things out and lot of patience. BUT… since more then 2 hours and browsing through this forum and others I cannot find any solution for the simplest stuff that may exist.

As background I have a rule where I want to control time based my outputs.

I have

strSunset is an item of type String
strDownTime is an item of type String

var DateTime sunset = parse(year+"-"+month+"-"+day+“T”+strSunset.state)
strDownTime = [ something like ‘sunset.plusMinutes(30).format("%1$tH:%1$tM:%1$tS")’ ]

I have no idea. I cannot find any documentation

also more test code like

In other topics I found
String::format("%1$td %1$tb %1$tl:%1$tM %1$tp", new Date()) ends up in NullPointerException
Then I tried
String::format("%1$td %1$tb %1$tl:%1$tM %1$tp",sunset) ends up in java.util.IllegalFormatConversionException: d != org.joda.time.DateTime

Further - what is the format of Dates? I cannot find any documentation. All official is just like %H and not %1$H

What the hell am I doing wrong? Where can I find appropriate documentation. All I find is just not working code snippets, so called examples, where probably not every setting is mentioned.

Thanks & Kind regards

DateTime is challenging in OH, speaking as someone who has programmed in Java since 1.2.

But to help your specific problem I need more context and information.

Please post your exact Item definition.

Please post where the sunset string Congress from and how it gets populated.

Why are you not using a DateTime Item?

Please post your Rule.

I can point you at a lot of docs but i need more context to understand where you are going wrong.

Finally i will leave with a note of caution. As I’m sure you are aware, the rules are written in a domain specific language. In my experience, if you approach worrying your rules from a structured or OO perspective you will be frustrated. Rules are allowed about managing Items and events not defining data structures and methods. I’ve written a series of Design Pattern postings in which I try to describe some approaches to code some things that are difficult when using a more traditional coding approach.

Or there is the JSR233 add-on which lets you write rules in JavaScript or Jython. Though at this time it only works with OH 1.x, but support for OH 2 is in work.

As per the thread title suggests I’m searching for a way to convert the data held in a DateTime item into a String. Why you ask?

I have a rule that grabs the latest train times using an API and stores that required information in a series of items. What I then want to do is either send a push notification or make a voice announcement via my Sonos speaker. But for specific train times only. I also only want to display the information for certain trains in a sitemap. This is where I need a little help. The expected departure time is held in an item using DateTime. What I thought might be possible was to convert that item into a String and use the Visibility option in the sitemap.

Here’s the item

DateTime TrainDeptSWI1TimeAimed "Aimed departure at [%1$tR]"

and the sitemap line

Text item=TrainDeptSWI1TimeAimed visibility=[TrainDeptSWI1TimeAimed == "06:30"]

Am open to alternative ideas, but thought this would have the simplest approach.

Thanks in advance.

Your biggest problem is that a DateTime is an instant in time. At its core it is literally the number of milliseconds since midnight January 1, 1970 GMT. So 06:30 today is a different DateTime from 06:30 tomorrow.

The formatting you have on the Item’s label to only show the hours and minutes is just that, formatting. The state of the Item is still 06:30 on a specific day. Thus the visibility will never be true.

Given the above, using a DateTime in a visibility comparison will almost never work.

What might work is to create a proxy String item and a Rule that updates the proxy whenever the DateTime Item receives an update. Populate the proxy item with just the hour and minute (user String. Formatter) and user the Proxy Item in your visibility comparison.

1 Like