Time based rule for PIR Sensor

Hi,
ive just installed a PIR Sensor using a Sonoff basic switch, which is connected to my existing light relay (with a timer), passing by Openhab (its not useful in that case). The only problem is, that the PIR Sesnor doesnt recognize if its day or night, so it switches the whole time. Now my idea is to build a rule in Openhab or Nodered, which looks for sunrise or sunset (from Yahoo weather) and disable the GPIO of the Sonoff switch. What do you think, could this work or do you have other ideas to make it work? Thanks Jesco

The Astro binding can calculate sunrise and sunset without an external dependency. Before I got motion sensors that could also detect light levels, I was doing pretty much the same thing. It did take me a while to get the exact numbers up.

In untested code:

var Timer lightTimer = null

rule "Hallway Light and Timer"
when
  Item Hallway_MotionSensor_Motion changed
then
  // Cancel any existing timer
  if(lightTimer != null) {
    lightTimer.cancel
    lightTimer = null
  }

  // The room starts to get dark when the sun's Azimuth is < 20 degrees
  if ((Hallway_MotionSensor_Motion.state == ON) && (Sun_Position_Azimuth.state < 20)) { 
    Hallway_DownstairsLight_Brightness.sendCommand(ON)
  }
  // Otherwise set a timer to turn off the light after 5 minutes of no motion.
  else {
    lightTimer = createTimer(now.plusMinutes(5), [|
      Hallway_DownstairsLight_Brightness.sendCommand(OFF)
    ])
  }
end