[SOLVED] Grater Equal with DateTime Item in Rule does not work

Hello together,

I have a Problem with a rule with a DateTime Item and Grater Equal Syntax.

I have installed the astro binding on openHAB2.
Then I configured the Things for Sun and Moon.
After that i created this Items.

DateTime 	Sunrise_Time 	"Sunrise [%1$tH:%1$tM]"		<sun>	{ channel="astro:sun:home:rise#start" }
DateTime    Sunrise_Time_Hour   "Sunrise_Hour [%1$tH]"  <sun>   { channel="astro:sun:home:rise#start" }
DateTime    Sunrise_Time_Minute  "Sunrise_Minute [%1$tM]"  <sun>   { channel="astro:sun:home:rise#start" }
DateTime	Sunset_Time	"Sunset [%1$tH:%1$tM]"		<sun>	{ channel="astro:sun:home:set#end" }
String		Season	"Season"					{ channel="astro:sun:home:season#name" }

I created a rule to switch on my lights, like this.

rule "Sunrise - Lights on"
when
    Time cron "0 58 6 ? * MON-FRI *"
then
    if( Sunrise_Time_Hour.state >= 6 && Sunrise_Time_Minute.state >= 40 ) {

      sendCommand(Lampe_1_Schalter,ON)
      sendCommand(Lampe_2_Schalter,ON)
      sendCommand(Lampe_3_Schalter,ON)
      sendCommand(Lampe_4_Schalter,ON)
}
end

But this rule does not work.
Do I have a logical error or a syntax error?

Sorry if this question already has already been asked.
I did not find the right solution to my problem.

Thank you for the help.

A DateTime Item carries the full DateTime. Deep down it actually just stores it as the number of milliseconds that have passed since 1970-01-01T00:00:00.

The label formatting you have in your Sunrise_Time_Hour and Sunrise_Time_Minute only changes how it appears on your sitemap. It doesn’t change the state stored by the DateTime Items. Consequently, Sunrise_Time, Sunrise_Time_Hour, and Sunrise_Time_Minute are all identical.

OK, hopefully confusion #1 is out of the way.

The second confusion is you can not use any of the comparison operations with a DateTime. for what ever reason they are not implemented. In likelihood it’s because a DateTime is really complicated (timezones, daylight savings, different calendar standards, etc.). Instead for DateTimeType you have after() and before methods. See https://www.openhab.org/docs/configuration/rules-dsl.html#using-the-states-of-items-in-rules. Below I’m going to convert the DateTimeType to a Joda DateTime for easier comparison.

Third, if you want the lights to go on at Sunrise, trigger the Rule using the Astro event trigger for Sunrise.

Fourth, put all your Lampe Items into a Group, let’s call it Lampes. Then you can just sendCommand to the Group.

Finally, always use the method on the Item instead of the action for sendCommand and postUpdate. See https://www.openhab.org/docs/configuration/rules-dsl.html#myitem-sendcommand-new-state-versus-sendcommand-myitem-new-state.

So to reproduce your Rule exactly:

rule "Sunrise - Lights on if sunrise is after 06:58"
when
    Time cron "0 58 6 ? * MON-FRI *"
then
    if(new DateTime(Sunrise_Time_Hour.state.toString).isAfter(now)){
        Lampes.sendCommand(ON)
    }
end

Further reading and examples of working with DateTime: Design Pattern: Time Of Day.

1 Like

Wow thank you very much for the explanations and the tips.

Your rule works for me!.