Comparing Astro binding event time with now

I know you’re just upgrading so you’d probably prefer to keep changes to a minimum. However, openHAB 4 brings modern automation languages such as JS Scripting and JRuby that you should check out because it offers many advantages over RulesDSL.

Here’s three versions of your rule implemented in JRuby. The second one uses the sun elevation from the astro binding which is my preferred method of tracking sunrise/sunset.

Note that JRuby has handy built in timers, in this case, implicitly created by the for: 15.minutes argument. It saves you from having to manually create a timer and keeping track of it yourself. It will auto reset the timer should the garden door closed and reopened before the timer had expired. There are other handy little things that help you deal with timers and other things more easily.

rule "Garden Gate" do
  changed os_garden_door, to: ON
  run do
    if Time.now.between?(astro_dusk_start.state, astro_dawn_end.state)
      Outside_walk_lamps.ensure.on, for: 15.minutes
    end
  end
end

# Option 2, use the between guard
rule "Garden Gate" do
  changed os_garden_door, to: ON
  between astro_dusk_start.state..astro_dawn_end.state
  run do
    Outside_walk_lamps.ensure.on, for: 15.minutes
  end
end


# Alternative implementation
rule "Garden Gate" do
  changed os_garden_door, to: ON
  run do
    if things["astro:sun:home"].get_elevation(nil).negative?
      Outside_walk_lamps.ensure.on, for: 15.minutes 
    end
  end
end

# Using only_if guard
rule "Garden Gate" do
  changed os_garden_door, to: ON
  only_if { things["astro:sun:home"].get_elevation(nil).negative? }
  run do
    Outside_walk_lamps.ensure.on, for: 15.minutes 
  end
end