OH2 Time and Date in SendMail

With OH1, I had a rule that emailed me the time and date of when my garage door was opened. But in OH2, this throws a java error in the log and no email received.

When I remove the time and date info, the email goes through and is received. So I’m guessing something changed within the NTP binding with OH2 to show this info?

In my former OH1 rule, I had this previously:

sendMail("myemail@xxx.com", "Garage Door Opened", "The Garage Door was opened at " + Date.state.format("%tr") + " on "+ Date.state.format("%tD") + ".")

Did you ever figure this out? I’m working on the same problem right now…



I’m using this with success (even though the formatting is gnaaa… like: 8:9 instead of 08:09):

sendMail("my@mail.host","Message","Something happened at  "+ now.getHourOfDay+":"+ now.getMinuteOfHour )

Does not give the date, but the mail client kind of does that for you…

My Door sensor isn’t working but I can test it again tonight. But if I remember I had an issue with the NTP binding not being installed and the original code did work.

Did you try this?

sendMail("myemail@myaddress.com", "Garage Door Opened", "The Garage Door was opened at " + Date.state.format("%tr") + " on "+ Date.state.format("%tD") + ".")

Here’s what I ended up doing. Not pretty, but seems to work well…

var hour   = now.getHourOfDay
var ampm = "AM"
if(hour > 12){
	hour = hour - 12
	ampm = "PM"
sendMail(txtTo,"openHAB Notification", "Device changed at " + hour + ":"+ now.getMinuteOfHour + " " + ampm + " on " + now.getMonthOfYear + "/" + now.getDayOfMonth) 

I just tested with what I am using above and seems to work fine. This is the output.

The Garage Door was opened at 07:06:39 PM on 04/05/17.

But I have started using push notifications via myopenhab as well.

I just tried your method as I like it better, but it didn’t work. I get this error message:

Error during the execution of rule FRDoorbellTesting: An error occured during the script execution: The name 'Date' cannot be resolved to an item or type.

All I changed was the e-mail address… Strange…

Actually, the error is saying what issue is. You probably do not have a Date item in your items files. Add this to your items file.

/* NTP Binding */
DateTime Date 					"Date & Time [%1$tm/%1$td/%1$tY %1$tT]" {channel="ntp:ntp:local:dateTime"}

I didn’t realize that Date was an item. I figured it was a core function or something… Added the item (just copied and pasted yours), and now I get this in the log:

Error during the execution of rule FRDoorbellTesting: r != java.lang.String

Can you confirm you have the NTP binding installed in either PaperUI or Habmin?

And possibly post your rule to look at?

It was, and the Thing was added as well (did this just before adding the item). I wondered if something got hung up, so I restarted OH2, and it fixed it. Your date method now works. Yay!!..

Now to go through all my rules and change them…

Thanks for the help,