Converted my rules around the astro binding:
Setting my Dark virtual switch based on Sunrise/Sunset:
Rules DSL:
rule "Sunrise"
when Channel 'astro:sun:home:rise#event' triggered START
then
logInfo(logName, "Sunrise - Disabling Dark")
Dark.sendCommand(OFF)
end
rule "Sunset"
when Channel 'astro:sun:home:set#event' triggered START
then
logInfo(logName, "Sunset - Enabling Dark")
Dark.sendCommand(ON)
end
Ruby:
Using the new trigger attachments in 4.10 of the JRuby library.
rule 'Set Dark' do
channel 'astro:sun:home:rise#event', triggered: 'START', attach: OFF
channel 'astro:sun:home:set#event', triggered: 'START', attach: ON
run { |event| Dark << event.attachment }
end
Set dark mode at startup (outside of Sunset/Sunrise events:
Rule DSL:
Note I think this rule is broken because it doesnāt seem to handle the case if being restarted after midnightā¦
rule "System Started Check Dark"
when System started
then
if(now.isAfter((Sunset_Time.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli)) {
logInfo(logName, "After Sunset - Enabling Dark")
Dark.sendCommand(ON)
}
else{
logInfo(logName, "After Sunrise - Disabling Dark")
Dark.sendCommand(OFF)
}
end
Ruby version:
Using the between ārangeā syntax to capture the after midnight case:
The only_if block only executes the block if those items are defined (not UNDEF or NULL) removing the need for checking each item in the run block.
rule 'Set dark on startup' do
changed [Sunset_Time, Sunrise_Time]
on_start
run do
if between(Sunset_Time..Sunrise_Time).include? Time.now
logger.info ("After Sunset and before sunrise - Enabling Dark")
Dark << ON
else
logger.info ("After Sunrise - Disabling Dark")
Dark << OFF
end
end
only_if [Sunset_Time, Sunrise_Time]
end
I have a rule to set and format the dawn/sunrise and dusk/sunset times so I know when each is:
DSL:
rule "Dawn/Sunrise changed"
when
Item Dawn_Time changed or
Item Sunrise_Time changed or
System started
then
if( Dawn_Time.state != null && Sunrise_Time.state != null) {
Dawn_Sunrise.postUpdate(Dawn_Time.state.format("%1$tI:%1$tM%1$Tp") + " / " + Sunrise_Time.state.format("%1$tI:%1$tM%1$Tp"))
}
end
rule "Dusk/Sunset changed"
when
Item Dusk_Time changed or
Item Sunset_Time changed or
System started
then
if( Dusk_Time.state != null && Sunset_Time.state != null) {
Dusk_Sunset.postUpdate(Sunset_Time.state.format("%1$tI:%1$tM%1$Tp") + " / " + Dusk_Time.state.format("%1$tI:%1$tM%1$Tp"))
}
end
The ruby rule takes advantage of the ability to āopen up classesā and add a method to them. Adding a method called āsimpleā that return the DateTime object in a simple format. the opening up of the class has no impact outside the rule in which it executes:
# Open up the DateTimeItem class and add a simple time format
class OpenHAB::DSL::Items::DateTimeItem
def simple
format('%1$tI:%1$tM%1$Tp')
end
end
rule 'Set Dawn/Sunrise' do
changed [Dawn_Time, Sunrise_Time]
on_start
run { Dawn_Sunrise << "#{Dawn_Time.simple} / #{Sunrise_Time.simple}"}
only_if [Dawn_Time, Sunrise_Time]
end
rule 'Set Dusk/Sunset' do
changed [Dusk_Time, Sunset_Time]
on_start
run { Dusk_Sunset << "#{Sunset_Time.simple} / #{Dusk_Time.simple}" }
only_if [Dusk_Time, Sunset_Time]
end