[SOLVED] CalDav Rule For Notification

I trying to create a rule that will send out notifications via Telegraph 2 days and again on 1 day prior to our Junk Waste pickup.

I have a Google Calendar that is populated with the pickup dates and have the CALdav binding installed which shows the date in HABpanel.

The item for the date is: Calendar_Upcoming_Junk

So I’d like to create a rule that checks if we are 2 days away from Calendar_Upcoming_Junk and if so, send an alert. There could be a secondary rule that checks if we are 1 day away and sends out an additional message.

I started cobbling together based on my little knowledge of Xtend and was hoping the community could assist me in getting closer.

Here’s what I have put together…

rule "send junk trash email 2 days out"

when
   System Started or
   Time cron "0 30 9 * * ? *"

then

if Calendar_Upcoming_Junk = now.plusDays(2)
sendTelegram("5517", "Junk Waste Is Scheduled To Be Picked Up This Friday. Make sure you put anything out Thursday night.")

end

Any guidance would be appreciated.

Squid

I use this rule

rule "trash"
when
Time cron "0 0 19 ? * MON,TUE,WED,THU,FRI *"
then
if
(now.plusMinutes(360).isAfter((CalDav_Date.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli)&& now.isBefore((CalDav_Date.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli))

{
	Muelltonne.postUpdate("heute kommt der "+CalDav_Muelltonne.state+" raus")
	say("heute kommt der "+CalDav_Muelltonne.state +"raus")
	sendBroadcastNotification("heute kommt der "+CalDav_Muelltonne.state +" raus")
}
else
{
Muelltonne.postUpdate("nichts für heute ,nächste "+CalDav_Muelltonne.state)
}


end
  1. Please use VSCode. There are many many syntax errors in this brief bit of code.

  2. I’m assuming that Calendar_Upcoming_Junk is a DateTime Item that represents the date and time for the trash pickup.

rule "send junk trash email 2 days and one day out"
when
    Time cron "0 30 9 * * ? *"
then
    // convert the DateTimeType to a Joda DateTime so we can use it with now
    val pickupDateTime = new DateTime(Calendar_Upcoming_Junk.state.toString)

    var message = ""
    // 2 days until trash pickup
    if(now.isAfter(pickupDateTime.minusDays(2)) && now.isBefore(pickupDateTime.minusDays(1))) {
        message = "Junk Waste Is Scheduled To Be Picked Up This Friday. Make sure you put anything out Thursday night."
    }

    // Day before trash pickup
    else if(now.isAfter(pickupDateTime.minusDays(1)) && now.isBefore(pickupDateTime)){
         message = "Junk Waste Is Scheduled To Be Picked Up This Friday. Make sure you put anything out tonight."
    }

    // If we have a message, send it via Telegram
    if(message != "") sendTelegram("5517", message)
end

I see no need to run this rule at system startup. the Rule runs every morning at 09:30. If the current time is two days before trash pickup one message is sent. If it is one day before trash pickup a slightly different message gets sent. In all other cases the Rule does nothing.

HI Rick -

Thanks for your assistance on this…

Are you saying that my code snippet was the one that had many syntax errors? If so, it was meant to be an outline of what i was trying to do…not a real word example. Sorry if I was not clear on that.

I took what @mpampinos provided above and modified it a bit to look like this…(have not tested it) but your solution looks more elegant.

rule "send junk trash email 2 days out"
when
   System Started or
   Time cron "0 30 9 * * ? *"
then
if
(now.plusDays(2).isBefore((Calendar_Upcoming_Junk.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli)

{
sendTelegram("5517", "Junk Waste Is Scheduled To Be Picked Up This Friday. Make sure you put anything out Thursday night.")
}

end

Hi,
the code you modified, means now that
it will reminds you every day before the 2 days.
Example:
Reminding day is Friday
Today is sunday
It will reminds you on monday, tuesday and wednesday.

he needs

Rick -

I implemented the rules code you provided above and set up a testing date on the calendar.

As you can see below, the testing date is being pulled in by the CALDAV plugin correctly

image

I added some logging to see what the calculated pickupDateTime was and as you can see below its pulling the correct date.

2018-09-20 18:19:31.707 [INFO ] [.eclipse.smarthome.model.script.time] - Time calculations:
 pickupDateTime = 2018-09-22T22:00:00.000-05:00

Yet I am not getting any notifications…Where do you think this is getting caught up?

Thanks,

Squid

The fact that the event starts at 10:00 pm may cause some problems. Thursday at 9:00 am is more than 48 hours from the scheduled time. Try using withTimeAtStartOfDay.

And as with the other thread, add logging to log out all the relevant date times when the Rule runs and make sure that they make sense.

Does this replace “now” ?

val pickupDateTime = new DateTime(Calendar_Upcoming_Junk.state.toString).withTimeAtStartOfDay


if(now.withTimeAtStartOfDay.isAfter(pickupDateTime.minusDays(2)) && now.withTimeAtStartOfDay.isBefore(pickupDateTime.minusDays(1))) {

and so on.

Still no luck…

2018-09-21 10:42:41.093 [INFO ] [.eclipse.smarthome.model.script.time] -  what is now.withTimeAtStartOfDay2018-09-21T00:00:00.000-05:00

How can I log the calculation?

logInfo("test",  <the calculation>)

You need to log pickupDateTime, now, pickupDateTime.minusDays(2), and pickupDateTime.minusDays(1)

Ok, Done…

It looks like the two calculations are ignoring the time and only using the date?

2018-09-21 11:55:22.432 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'junkwaste.rules'
2018-09-21 11:55:26.005 [INFO ] [.eclipse.smarthome.model.script.time] - Time calculations:
 pickupDateTime = 2018-09-22T00:00:00.000-05:00
2018-09-21 11:55:26.006 [INFO ] [.smarthome.model.script.system.rules] - Checking to see when the next junk trash day is
2018-09-21 11:55:26.007 [INFO ] [.eclipse.smarthome.model.script.time] -  what is now.withTimeAtStartOfDay2018-09-21T00:00:00.000-05:00
2018-09-21 11:55:26.007 [INFO ] [.eclipse.smarthome.model.script.time] - Pickup Minus Two 2018-09-20T00:00:00.000-05:00
2018-09-21 11:55:26.008 [INFO ] [.eclipse.smarthome.model.script.time] - Pickup Minus One 2018-09-21T00:00:00.000-05:00
2018-09-21 11:55:26.009 [INFO ] [.eclipse.smarthome.model.script.time] - Now2018-09-21T11:55:26.009-05:00

Because we told them to. That is what withTimeAtStartOfDay does. It moves the date time to midnight of that day.

OK, so here is the problem. I’m going to walk you though this because you will need to learn how to debug this type of Rule.

First, we need to add logging to see what all the relevant values are.

Next we need to look at each of the values that were logged. So what did we learn from the logging?

variable value
pickup time Midnight Sept 22
now.withTimeAtStartOfDay Midnight Sept 21
pickup minus 2 Midnight Sept 20
pickup minus 1 Midnight Sept 21
now 11:55 am Sept 21

The table above has all the information necessary to figure out why the Rule isn’t working.

So let’s replace the calculations in the if statement with actual values from that table:

    if( <Midnight Sept 21> isAfter <Midnight Sept 20> && <Midnight Sept 21> isBefore <Midnight Sept 21> )

    else if( <Midnight Sept 21> isAfter <Midnight Sept 21> && <Midnight Sept 21> isBefore <Midnight Sept 22>)

The first if evaluates to false because Midnight Sept 21 is not before Midnight Sept 21.

The second if evaluates to false because Midnight Sept 21 is not after Midnight Sept 21.

Hopefully you see the problem and solution by now.

Click for solution We need to remove the withTimeAtStartup from `now`.

This will make the comparisons become

    if( <11:55 am  Sept 21> isAfter <Midnight Sept 20> && <11:55 am Sept 21> isBefore <Midnight Sept 21> )

    else if( <11:55 am Sept 21> isAfter <Midnight Sept 21> && <11:55 am Sept 21> isBefore <Midnight Sept 22>)

The first if evaluates to false because 11:55 am Sept 21 is not before Midnight Sept 21.

The second if evaluates to true because 11:55 am Sept 21 is after Midnight Sept 21 and before Midnight Sept 22.

In otherwords, only apply withTimeAtStartOfDay to the pickupDateTime and remove it from everywhere else.

3 Likes

Rick -

Thank you for taking the time to help me understand the process better. This type of explanation is much better than just giving the answer and I will use it as a reference when working with time in the future.

A parting question though, is there a reference somewhere online that provides information on the various syntax one could use in the rules engine?

Something like this for CSS:

https://www.w3schools.com/css/css_positioning.asp

http://www.eclipse.org/xtend/documentation/203_xtend_expressions.html

https://community.openhab.org/search?q=Design%20pattern

There last link is too the design pattern postings whether you will be find good examples to learn from. The working with groups in rules DP is particularly useful for the syntax of working with groups and other collections.

Finally, https://www.openhab.org/docs/configuration/editors.html#openhab-vs-code-extension. install and use VSCode with the Oh I extension. You can type in the name of something and it will pop up and show you all the valid ways to complete what you typed. So if you type now. It will show you all the methods on now.

Rick -

Thanks for all of the links to documentation…trying my best to dig more and ask less!

I am a big fan of VSCode and was an early applauder of the effort. The trouble I have is the damn thing crashed my OH Install every time I use it. I have brought the issue up since February but no one seems to know what the issue might be. I’ll look at upgrading to 2.4 and hope that some of the system upgrades fix the issue as some have suggested it may be a Language Server problem.