Send Mail some Time before Calendar entry

  • Platform information:
    • Hardware: Raspberry Pi 3 Model B Rev 1.2_
    • OS: what OS is used and which version
    • Java Runtime Environment: _ Zulu Embedded (build 1.8.0_152-b76)_
    • openHAB version: OH 2.3

Hello Community
I´m running OH 2.3 on a Raspberry since 1 Month now and found all necessary informations here in the Community. How to set up the System, creating Items, Things, Sitemaps and so on.
Now i get stuck with a rule, i guess it´s just a simple one.

Here is the code i tryed.

rule "Mail"

     Item Tag received update   
     if (now.isBefore(new DateTime((Date1.state as DateTimeType).getZonedDateTime.toInstant.toEpochMilli).minusHours(6))){

     sendMail("*******@**********.at", "Müllkalender", "Müll nicht vergessen")

Item Tag is updated every minute
Date1 is a DateTime Item, fixed Date from CalDav import.

There are no log entries and the Mail is sent every minute.

I want to send a Mail, 6 hours before the Date1 calendar entry,

what do i miss in my code
Thanks for help

I don’t have the answer (obviously your if expression is wrong and it results to true all the time…)

Isn’t there a better way for you to get notified of upcoming appointments?
Triggering a rule every minute and performing DateTime comparisons seems a bit inefficient to me.
Doesn’t your CalDav server have the ability to send you an email based on time schedules?

Did you use: Design Pattern: Time Of Day ?

What your rule is doing right now, is that every time your item receives an item, it checks if the current time is 6 hours or more before Date1
So for example, you set Date1 to tomorrow, 14.00. So your 6 hours threshold is at tomorrow 08.00.
Then at 20.00 today, this evaluates to true as it is before 08.00 tomorrow, so you get an email, at 20.01, it is still true, and so on, all the way to 7.59 tomorrow… that will be a lot of emails…

What you need, is to check if now is after Date1 minus 6 hours, and hasn’t been sent yet, or you could check if now is less than 1 minute off Date1 minus 6 hours. For the last one you need to be sure that the rule is indeed triggered every minute, or you might miss the small timeframe. For the first one you need a variable that you can set and reset.

1 Like

The Rule Trigger will be changed to daily once the rule is working.
I can send notifications via CalDav server but i hav to edit each entry by hand. one working rule would be very helpful.
I went through Design Pattern: Time Of Day and i understand how to use and did similar rules,
but with this Rule i tied a knot in my brains.

Personally, I would take a completely different approach:
Let’s say you have to take the trash out on Friday, you might not really be looking for a rule that sends an e-mail 6 hours before it’s Friday, but what I would do, is to make a rule that runs every day at a certain time (using cron) and then check if the trash needs to be outside tomorrow, and if that evaluates to true, send an e-mail.

So something like:

rule "Mail"
		Time cron "0 0 22 ? * * *"
		if (now.getDayOfWeek + 1 == new DateTime((Date1.state as DateTimeType).getZonedDateTime.toInstant.toEpochMilli).getDayOfWeek){
			sendMail("*******@**********.at", "Müllkalender", "Müll nicht vergessen")

Disclaimer: this is just from the top of my head, can’t check it right now, so please check it for me :slight_smile:

Thank you Rolf for untie the knot in my brains.
Your approach is just perfect and simple and it works.
Thanks for your Help