Date time computation in rules

Hi,
I am using an item defined as summer from astro binding like this:
DateTime Ete “Été [%1$td.%1$tm.%1$tY ]” {astro=“planet=sun, type=season, property=summer”}
In a rule I would like to find out if today(ow) is 45 days before summer .
I have tried many statements to do this but always get incompatible types of all kinds.
Would certainly need help on this one.

Thanks for any suggestion

In your rule:

val preSummerStart = new DateTime((Ete.state as DateTimeType).calendar.millis - (45*24*60*60*1000))
if(now.isAfter(preSummerStart)) {
     // do stuff
}

Hi Rich,Thanks many times for your help.I believe that Java and myself are not very good friends…this rule is still giving me problems…maybe the syntax of this language is to complicated for me ???I still get this error from Java:2016-04-08 15:25:00.172 [ERROR] [.o.m.r.i.engine.ExecuteRuleJob] - Error during the execution of rule set date hiverjava.lang.RuntimeException: The name ‘.millis’ cannot be resolved to an item or type.
this is the rule : rule "set date hiver"when // Time cron “0 0 0 * * ?” Time cron "0 0/5 * * * ?"then val SummerStart = new DateTime((Ete.state as DateTimeType).calendar.millis - (602460601000)) val WinterStart = new DateTime((Hiver.state as DateTimeType).calendar.millis - (452460601000)) if(now.isAfter(SummerStart)||now.isBefore(WinterStart)) { saison= 1 println(“saison”+saison)} else saison=0 println(“saison”+saison) end

this is my imports:
import org.joda.time.import java.util.Dateimport java.util.calendarimport org.joda.time.
Sorry to bug you with this again

Gaston

It should be .timeInMillis. I was going from memory and apparently was wrong.

Also, just be aware that underlying all of this is Java but Rules are not written in Java.

I highly recommend using Designer. It will tell you when you have a syntax error like this. And with <ctrl><space> it will autocomplete the line so you can type .calendar.<ctrl><space> and it will give you a list of all the things you can validly end the line with, including timeInMillis.

Hi,Thanks a lot Rich…that did the trick…Indeed this language needs a lot of memory…not exactly what I would call a user friendly package…
Thanks again for your support

That is why I highly recommend using Designer. With <ctrl><space> you don’t really have to remember much of anything.

Hi,You’r wright, what about Habmin2 ?I am still struggling with this rule…
Here is what I get:
rule "set date hiver"when // Time cron “0 0 0 * * ?” Time cron "0 0/5 * * * ?"then println("astro summer’Ete’ = "+Ete.state) println("astro winter’Hiver’ = "+Hiver.state) val SummerStart = new DateTime((Ete.state as DateTimeType).calendar.timeInMillis - (602460601000)) val WinterStart = new DateTime((Hiver.state as DateTimeType).calendar.timeInMillis - (452460601000)) println("summerstart= "+SummerStart) println("winterstart= "+WinterStart) println(“now=”+now)

This is the println’s
astro summer’Ete’ = 2016-06-20T18:35:00 astro winter’Hiver’ = 2016-12-21T05:45:00 summerstart= 2016-06-10T11:37:47.296-04:00 winterstart= 2016-12-25T22:47:47.296-05:00 now=2016-04-09T09:50:00.467-04:00
Based on the rule, SummerStart should be : 2016-04-21 or close to that?Same for WinterStart … seams to add time instead of subtracting.Looks like I need your help again
Thanks

Gaston

Please format your rule so it is more legible in the future. I it exceptionally difficult to read with it is all jumbled together like that. Use

```
your code
```

and all of your spacing will be preserved.

rule "set date hiver"
when 
    // Time cron "0 0 0 * * ?" 
    Time cron "0 0/5 * * * ?"
then 
    println("astro summer'Ete' = "+Ete.state) 
    println("astro winter'Hiver' = "+Hiver.state) 
    val SummerStart = new DateTime((Ete.state as DateTimeType).calendar.timeInMillis - (60*24*60*60*1000)) 
    val WinterStart = new DateTime((Hiver.state as DateTimeType).calendar.timeInMillis - (45*24*60*60*1000))    
    println("summerstart= "+SummerStart) 
    println("winterstart= "+WinterStart) 
    println("now="+now)
astro summer'Ete' = 2016-06-20T18:35:00 
astro winter'Hiver' = 2016-12-21T05:45:00 
summerstart= 2016-06-10T11:37:47.296-04:00 
winterstart= 2016-12-25T22:47:47.296-05:00 
now=2016-04-09T09:50:00.467-04:00
Based on the rule, SummerStart should be : 2016-04-21

I’m not sure what is going on. The Summer one only subtracted 10 days, not 60 and the winter one added four days.This is truly odd behavior.

Here is what I recommend. Just create a couple more Astro Items and use the offset field to subtract the days. Remember offset is in minutes so you will need to calculate the number of minutes in 60 days and 45 days. That is what I would have done in the first place, though when I help people on this forum I try to stick as close to the original author’s style as possible. But in this case, it is acting weird and without spending a lot of time experimenting I don’t know why nor how to fix it.

Thanks Rich,Sorry for the formatting…I did not use the forum to reply, I went with email…sorry
and thanks again