NULL means those Items have never been updated. It doesn’t have a state.
How are you updating this Item?
First of all it should be a DateTime Item. Then, if you don’t want to use the NTP add-on just create a rule to update the Item periodically (once a minute or once a second) with the current time.
DateTime CurrentDateTime "Current Date & Time [%1t$tH:%1t$M %1t$A %1t$m %1t%d]" <calendar>
See Items | openHAB and Formatter (Java SE 17 & JDK 17) for details on how to use the formatter to display the date and time how you want.
Then in your rule it’s just:
items.CurrentDateTime.postUpdate(time.toZDT());
This is the full rule? It doesn’t make sense as written.
- This is an Inline Script Action in a UI rule?
- What’s
item? If this is your CurrentDateTime Item?
- You are just comparing Strings.
>= means that the left side is first alphabetically, not that it represents a later date. You need to compare the DateTimes, not strings.
- Are you aware that the Astro binding has a season Channel? Astro - Bindings | openHAB There’s a settings to have it use meterological dates instead of equinox dates.
Season calculation can be switched from equinox based calculation to meteorological based (starting on the first day of the given month). This is done by setting useMeteorologicalSeason to true in the advanced setting of the sun.
The best way to implment this is to:
- install the Astro binding
- create/accept from the inbox the local sun Thing
- click “show advanced” and check the “use meterological season”
- link this Channel to what ever
item is
That’s all you need to do. Note that Astro is calculated 100% locally, no dependency on any internet service. And it handles leap years and doesn’t require any code.
But if for some reason you needed to compare between two dates in a rule you would do it as follows:
// see https://www.openhab.org/addons/automation/jsscripting/#time-tozdt
var year = time.toZDT().year();
var winterStart = time.toZDT(year+"-12-01");
var springStart = time.toZDT(year+"-03-01");
var summerStart = time.toZDT(year+"-06-01");
var autumnStart = time.toZDT(year+"-09-01");
var now = time.toZDT();
// see https://www.openhab.org/addons/automation/jsscripting/#isbetweendates-start-end
var currSeason = items.Season.state;
if(now.isBetweenDates(winterStart, springStart.minusDays(1)) currSeason = "WINTER";
else if(now.isBetweenDates(springStart, summerStart.minusDays(1)) currSeason = "SPRING";
else if(now.isBetweenDates(summerStart, autumnStart.minusDays(1)) currSeason = "SUMMER";
else currSeason = "AUTUMN";
// see https://www.openhab.org/addons/automation/jsscripting/#getitem-name-nullifmissing
items.Season.sendCommandIfDiffernt(currSeason);
But again, you don’t need this for the seasons. This sort of thing is why the Astro binding exists. And you can’t just do complicated date time comparisons using Strings but time.toZDT() can convert Strings into a ZonedDateTime that you can do such comparisons with.