I need an if function for a day of the year.
Is it possible to do something like this?
rule "Test"
when
Item testswitch received update ON
then
if(Time cron == * * * 18 9 ? *){ //if it is 18 september
//do some stuff
}
if(Time cron == * * * 19 9 ? *){ // if it is 19 september
//do some stuff
}
end
No. But you can do something with Joda’s DateTime now.
val month = now.getMonthOfYear
val day = now.getDayOfMonth
switch(month){
case 9: {
switch(day) {
case 18: {
// do some stuff
}
case 19: {
// do some stuff
}
}
}
}
I used switch statements assuming you would have a lot of these cases. If not the if equivalent is:
val month = now.getMonthOfYear
val day = now.getDayOfMonth
if(month == 9 && day == 18) {
// do some stuff
}
else if(month == 9 && day == 19) {
// do some stuff
}
Hi Rich, I too would like to use Cron in a if statement, but it appears not possible.
I want to define a rule that checks if its Nov, Dec, Jan , Feb, March or April and would trigger in every day of those months and then does stuff BUT, I’m using a Channel trigger with an offset as suggested by @rossko57[SOLVED] Astro binding - hours until / after sunrise as I want this to only run 1 hour before sunrise.
I got about this far before thinking this isn’t the right way!
I guess a proxy set using Cron, and then a check for that proxy being ON in my channel trigger is the go. Anything that may make more sense?
rule "Turn on Flag in Summer"
when
Time cron "0 0 0 ? JAN,FEB,MAR,APR,NOV,DEC * *"
then
Summer.sendCommand(ON)
end
rule "Turn on Sunrise flag 1Hr prior to Sunrise"
when
Channel 'astro:sun:timed:set#event' triggered START
then
if(Summer.state == ON){
gAllLivingLevelShutters.sendCommand(0)
}
end
And the Astro offset thing
astro:sun:timed "1hr Before Offset" [ geolocation="-33.78844,151.22889,0", interval=60 ]{
Channels:
Type rangeEvent : set#event [
offset=-60]
}
Both of your rules would run once every day, however you are never setting “summer” to off .
You could use JodaTime as shown by @rlkoshak and combine both into one rule.
rule "Turn on Sunrise flag 1Hr prior to Sunrise"
when
Channel 'astro:sun:timed:set#event' triggered START
then
val month = now.getMonthOfYear
if(month < 5 || month >10) {
gAllLivingLevelShutters.sendCommand(0)
}
end
Lovely! Ugh so much to learn about rules. Thanks opus, that’s a nice solution.
To take this further, I’d like to open them again at 11am.
rule "open shutters during summer at 11am"
when
Time cron "0 0 11 ? JAN,FEB,MAR,APR,NOV,DEC * *"
then
if(gAllLivingLevelShutters.state =! 100){
gAllLivingLevelShutters.sendCommand(100)
}
end