Is there an efficient way to detect Sun elevation crossing an arbitrary threshold

TLDR; Is there an efficient way of detecting that the sun elevation has just crossed a certain threshold?

I’d like to run a task when the sun elevation just dipped below or risen above a certain threshold, say 10 degrees, or 15 degrees.

I’m using the astro binding. I know I can detect the sun rise start / stop, dusk, dawn, etc. via the channel trigger, but not a specific elevation.

Currently I am just triggering on the change in Sun elevation, and detecting whether the new elevation is above my threshold, and its last value was below it. While this works, it seems wasteful to fire this rule every time the sun elevation changed which occurred quite often.

FWIW, this is my current rule in jruby, but of course this can be implemented in any supported language

SUN_ELEVATION_THRESHOLD = 5

rule 'sun elevation trigger' do
  changed Sun_Elevation
  run do |event|
    if Sun_Elevation > SUN_ELEVATION_THRESHOLD && event.was <= SUN_ELEVATION_THRESHOLD
      # do something when the elevation went above the threshold
    elsif Sun_Elevation < SUN_ELEVATION_THRESHOLD && event.was >= SUN_ELEVATION_THRESHOLD
      # do something when the elevation dipped below the threshold
    end
  end
end

It really does not cost much.

You can apply time offsets to Astro events, which is not exactly the same but a good approximation.

I have been using a similar approach to work out when to close my roof blinds to stop the sun shining on the kitchen surface. Works with azimuth rather than elevation. As you say, not the most efficient but as a principal, it has been working fine for 3 years.