After upgrade to OH 4, I have an error in System Start rule

Hello all,

I’ve just upgraded from OH3 to OH4 on a raspberry. And I have an error in my rules and it wasn’t the case in OH3 afaik.

Here is the rule:

rule "System Start"
when
  System started
then
    var DateTime vSunrise = new DateTime(Sunrise.state.toString)
    var DateTime vSunset  = new DateTime(Sunset.state.toString)

    switch now
    {
        case now.isAfter(vSunrise) && now.isBefore(vSunset): isNight.postUpdate(OFF)
        default: isNight.postUpdate(ON)
    }
end

Sunrise, Sunset and IsNight are all declared in items:

Switch isNight "Night"
DateTime Sunrise "Lever du soleil  [%1$tH:%1$tM]" {channel="astro:sun:local:rise#start"}
DateTime Sunset "Coucher du soleil  [%1$tH:%1$tM]" {channel="astro:sun:local:set#end"}

The error displayed each time the rule is loaded:

2023-07-30 21:50:34.479 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'default.rules'
2023-07-30 21:50:42.596 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'default-6' failed: An error occurred during the script execution: Cannot invoke "org.eclipse.xtext.common.types.JvmType.eIsProxy()" because "type" is null in default

I suspect isNight is null ? But it’s not the case.

openhab> openhab:status isNight
ON

Anyone know what could cause that ?
Thank you.

Maybe both items Sunrise and Sunset are not yet initialized when the rule gets started.
add a logInfo() after setting the vars:

logInfo("systemStarted","Sunrise: {} Sunset: {}",vSunrise,vSunset)

When the rule gets triggered, the log line will be printed at openhab.log and you can determine if the values are correct.

This whole rule may not be needed in the first place.

val sunActions = getActions("astro","astro:sun:local")
if(sunActions.getElevation() == 0) // it's night time
if(sunActions.getAzimuth() == 0) // it's night time

I wonder if it’s not a language problem ?
Because if I modify my rule:

rule "System Start"
when
  System started
then
    logInfo("systemStarted","Sunrise: {} Sunset: {}",Sunrise.state.toString,vSunset.state.toString)

    var DateTime vSunrise = new DateTime(Sunrise.state.toString)
    var DateTime vSunset  = new DateTime(Sunset.state.toString)

    switch now
    {
        case now.isAfter(vSunrise) && now.isBefore(vSunset): isNight.postUpdate(OFF)
        default: isNight.postUpdate(ON)
    }
end

I have no error but if I move the loginfo just before the switch I have the same error ?