Add DateTime to Email Rule

Hello,

I have a simple rule that emails me when my front door is opened. Because sometime, my email is delayed, I would like to add the time into the message of the email.

I’m sure I can figure it out, but I’m hoping someone can quickly tell me what I need to add. I have the below, but I don’t think it’s working properly.

Is “DateTime.now()” correct? Thanks

Q

import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
import org.openhab.core.library.types.DecimalType
import org.joda.time.*

val String mailTo = "me@gmail.com"

rule "Front Door - OPEN"
when Item FrontDoor changed from CLOSED to OPEN
then
sendMail(mailTo, “Front Door - Opened”, "Front Door - Opened - " + DateTime.now())

end

To get the current time (i.e now) use the following:

new DateTimeType()

and then you may need to append a .toString() to convert it.

I haven’t tested this, but could it be like this?

(new DateTimeType()).toString()

This will work:
sendMail(mailTo, “Front Door - Opened”, "Front Door - Opened - " + FrontDoor.lastUpdate)

Elegant!

@qnology: Please note that you need to enable persistence on your FrontDoor item for this to work.

Thanks for pointing out the prerequisite :slight_smile:

AWESOME! Thank you for the help guys.

How do I make sure to “enable persistence” on your FrontDoor item? Thanks

[Edit] - I don’t think I have persistence setup yet. :frowning:

Its easy to setup rrd4j persistence, check the wiki for examples