In my rules, I don’t really care whether it’s sunset or sunrise because they are simply using the elevation to decide whether to perform something or not. For example:
“if someone turned off the living room light, and it stays off for 10 minutes, and the sun is not visible (elevation < 0), then turn off the wall mounted display screen”.
Here’s my actual rule written in jruby:
rule 'Turn off photo frame' do
changed gLivingRoomLights, to: OFF, for: 10.minutes
only_if { Sun_Elevation.negative? }
run { PhotoFrame_Screen.ensure.off }
end
If on the other hand you actually need to run specific tasks on sunset e.g turn lights on 10 minutes before sunset, and turn them off 5 minutes before sunrise, the Astro binding has that covered using offset
.
For a task to be performed after the event (sunset/sunrise) you can have your rule fire on the event (so you don’t have to configure the “offset” as per the above link), and then inside your rule, create a timer that will execute the task X minutes later.
Others can probably give you more ideas. If you can describe the exact scenario, we’ll be happy to go into it further.
Most importantly do not hesitate to ask here if you are stuck. We’ve all been there. I am constantly learning new things about openhab.
I would write something like this (in jruby):
# cycles the light from OFF -> 30% -> 100% -> OFF
def cycle_light_level(light)
light << case light
when OFF then 30
when 100 then OFF
else 100
end
end
# Returns a range from sunset to sunrise, minus/plus the given offset
# can be given as argument for TimeOfDay#between?
def sunset_to_sunrise(offset = 0.seconds)
astro = things['astro:sun:home']
pre_sunset = astro.getEventTime('SUN_SET', nil, nil).minus(offset)
post_sunrise = astro.getEventTime('SUN_RISE', nil, nil).plus(offset)
# convert to DateTimeType range
DateTimeType.new(pre_sunset)..DateTimeType.new(post_sunrise)
end
rule 'Hall light switch' do
updated Hall_Switch, to: "click" # assuming this switch sends "click" when it's tapped / clicked
run do
if TimeOfDay.now.between? sunset_to_sunrise(15.minutes)
cycle_light_levels(Hall_Light)
else
Hall_Light << 100
end
end
end
Or you can have two other rules that control Night_Flag and then use Night_Flag in the “Hall light switch” rule as others have suggested above. One thing to consider when using a “Night_Flag” type system: you’ll also need to set this flag when openhab starts up, so you’ll still need to actually obtain the sunset/sunrise time and not just rely on the astro trigger event
. This is to cover the time when openhab gets restarted after the set time for night flag, otherwise your rule will run incorrectly until the following day.