How can i compare an astro-time-item with now?


(Hallo Ween) #1

Hi,
how can i compare a given time from the astro-binding with now?

I want to make a cron-job. Then the rule should compare, if now is before or after the astro-time and do different things.

Maybe conjob at 7 o´clock --> look if sunrise was already.

I now do this with 2 separate rules and a proxy item, but i think it should work with a simple compare, too?

This is my astro item:

DateTime Sonnenaufgang_Start "Sonnenaufgang [%1$tH:%1$tM]" <sunrise> (gAstro)	{ channel="astro:sun:home:rise#start" }

How to compare DateTimeType of the AstroBinding with "now"
(Hallo Ween) #2

I made a test rule. It is working, but it says, it is deprecated. Can someone tell me, how i can write this rule in the new format?

rule "System Test 1"
when
    Item System_Test_1 changed
then
    if(now.isBefore((Sonnenaufgang_Start.state as DateTimeType).calendar.timeInMillis)) {
            logInfo("System Test 1", "System Test 1: now is before sunrise " + now + " --- sunrise: " + Sonnenaufgang_Start.state)
    }
    if(now.isAfter((Sonnenaufgang_Start.state as DateTimeType).calendar.timeInMillis)) {
            logInfo("System Test 1", "System Test 1: now is after sunrise " + now + " --- sunrise: " + Sonnenaufgang_Start.state)
    }
end

I got my information from the openhab-docs, but there are only dscribed the old deprecated things:
https://docs.openhab.org/configuration/rules-dsl.html#datetime-item
or


(Hallo Ween) #3

Is this the right way to write the rule in the new style? It is working for me without showing any errors in VS Code Editor.

if(now.isBefore((Sonnenaufgang_Start.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli)) {
            logInfo("System Test 1", "System Test 1: now is before sunrise " + now + " --- sunrise: " + Sonnenaufgang_Start.state)
    }
    if(now.isAfter((Sonnenaufgang_Start.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli)) {
            logInfo("System Test 1", "System Test 1: now is after sunrise " + now + " --- sunrise: " + Sonnenaufgang_Start.state)
    }

(Harry) #4

I think so DateTimeType is deprecated


(Vincent Regaud) #5

This is the right was. Don’t worry about the deprecated message. OH uses Joda time and we need to convert the type to compare with now

See: