Make rule only run once a day

Hey guys,

I want to create rules which do different things based on the brightness my weatherstation delivers.
Working quite fine but I already knowed that I will have a problem while I was writting the code… but currently it doesn’t make “click” for me to find a solution for it. I bet it’s absolute easy so sorry for asking but it won’t come to my mind :frowning:

First rule turns on a light based on the brightness level. Second rule turns the same light off based on time (which is the way I would like to have it). So after light was turned of by the time trigger the light goes on again. Of course this is because first rule turns it on again because a change happend and brightness is still lower than 500.

rule "Dekobeleuchtung Flur an Dämmerung"
when 
    Item Daemmerung changed
then
    if (Daemmerung.state <= 500){
        sendCommand(EG_Flur_DekoLicht, ON)
    }
end

rule "Dekobeleuchtung Flur aus"
when 
    Time cron "0 55 21 * * ?"
then
    sendCommand(EG_Flur_DekoLicht, OFF)
end

What do you suggest to get the rule not turning on the light again for today after it turned out by the second rule.

Second thing is. That if the brightness goes below 500 after the time defined in rule two the light should not turn on. What do you suggest? I would use a “if” checking with “gettimeoftheday” and only let it run if time is lower as time of second rule. Other / better ideas?

In the first rule, check that the time is before 9:55pm. You can either check the current time or use another Item and rule that will let you know the light should not be on (search for Time of day). This only handles one room though, so you may want to expand to your whole house…

1 Like

Just change the On rule to

import java.text.SimpleDateFormat
import java.util.Date

rule "Dekobeleuchtung Flur an Dämmerung"
when 
    Item Daemmerung changed
then
   var dt = new DateTime().getMillis()/1000 + 60 * 60
   val SimpleDateFormat sdf = new SimpleDateFormat("HH:mm")
   val String strTime = sdf.format(new Date())
    if (Daemmerung.state <= 500 && strTime < "21:55" ) {
        sendCommand(EG_Flur_DekoLicht, ON)
    }
end
1 Like

Or use getMinuteOfDay:

rule "Dekobeleuchtung Flur an Dämmerung"
when 
    Item Daemmerung changed
then
    if (Daemmerung.state <= 500 && now.getMinuteOfDay < (21 * 60 + 55) ) {
        EG_Flur_DekoLicht.sendCommand(ON)
    }
end
1 Like

Thanks.
So my idea for the second “problem” is already serving the first “problem”… sometimes it only needs a little hint

Thanks and stay safe

Or, since Joda is removed in OH 3.0, compare using ZonedDateTime…

rule "Dekobeleuchtung Flur an Dämmerung"
when 
    Item Daemmerung changed
then
    val zoneddatetime_now = new DateTimeType().zonedDateTime
    if (Daemmerung.state <= 500 && zoneddatetime_now.isBefore(zoneddatetime_now.withHour(21).withMinute(55))) {
        EG_Flur_DekoLicht.sendCommand(ON)
    }
end