Sunset time rule issue

I have a sunset time rule that is launching perfectly.

rule "Licht Voordeur aan" when Item Sunset_Event received update ON then sendCommand(Licht_BG_BV, ON) end

I have a second rule that stops the sunset time rule.

rule "Licht Voordeur uit" when Time cron "0 00 22 * * ?" // dagelijks 22:00 then sendCommand(Licht_BG_BV, OFF) end

But when the time of sunset is later than the time set in the second rule the light is switching on and not off anymore.
How can I combine these rules so that if the sunset time is later than the light off his not launching.

First rule: only switch on if before 22:00

Assumption: you do not need to switch on the lights if sunset is after 22:00.

Assumption is right. Lights do not have to be switched on before 22:00.
How do I implement that in the rule?

I haven’t tested it, but it should look something like:

import org.joda.time.*

rule "Licht Voordeur aan"
when
    Item Sunset_Event received update ON
then
    // determine time to check
    var DateTime timecheck = parse( "" + now.toLocalDate.toDateTimeAtStartOfDay ).plusHours(22)
    if (now < timecheck) {
        sendCommand(Licht_BG_BV, ON)
    }
end

If it’s not working, that’s probably because the variable types of now and timecheck do not yet match. Let me know if you need any help.