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
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
Please use VSCode. There are many many syntax errors in this brief bit of code.
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.
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.
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.
val pickupDateTime = new DateTime(Calendar_Upcoming_Junk.state.toString).withTimeAtStartOfDay
if(now.withTimeAtStartOfDay.isAfter(pickupDateTime.minusDays(2)) && now.withTimeAtStartOfDay.isBefore(pickupDateTime.minusDays(1))) {
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
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?
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.
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.