Timer to turn something off at 2am

Is there a simple way to use createTimer, or equivalent, to trigger something at a specific time? I have an configuration that, if set, should trigger at 2am. I wanted to avoid creating an entire rule with a cron at that time, and instead use something like createTimer. The problem with doing something like createTimer(now.withTimeAtStartOfDay.plusHours(26)) is what happens if the automation is enabled between 0:00 and 1:59am. I guess ideally I should do math to see if I am before or after midnight, and based on this I could set the right counter, but I’m not sure if I am going about it the right way.

can you create a timer without a rule?
Why not just use a simple cron rule, something like

rule "dostuffattwo"
   Time cron "0 0 2 ? * * *"
   //do stuff

Because it seems wasteful. The rule would exist even if it mostly never gets executed. The context is that I have a bunch of thermostats and a HOLD function. I want the HOLD to cancel after a period of time which is dictated by a menu.

        Selection item=Therm_tmr_minutes icon=time mappings=[0="Off",1="1day",2="2day",3="2am"]

For the first three, it’s simple:

 if ((Therm_tmr_minutes.state==1) || (Therm_tmr_minutes.state==2)) // 1 and 2 are number of days to reset
     myTimer_therm = createTimer(now.plusDays((Therm_tmr_minutes.state as DecimalType).intValue)) [| 
    logInfo("Therm","Canceling existing therm timer.")
    Therm_commands.sendCommand("0")   ]

I want to handle the 2am reset in the same manner and avoid using a different model.

I agree that a cron makes the most sense. With the 2am cron rule, you could just set a condition to cancel the hold if Therm_tmr_minutes.state == 3. It’ll run every day, see that the condition is false most of the time, and do nothing. I don’t think that’s wasteful if it’s fulfilling its purpose and achieving your goals.

Well, you’ve either got a rule or a Timer hanging around for their appointed time. Must have one or the other. There’s no performance advantage either way.

The cron-triggered rule is the simplest, and takes care of after-midnight and rebooted issues by itself. Plus it’s so easy to check again at 3am etc. I wouldn’t resist it any further :smiley:

Simple way:

Switch timer2amActive "Timer @ 2 a.m. [%s]"


var Timer t2amTimer = null

rule "2am timer"
    Item timer2amActive received command
    if(receivedCommand == ON)
        t2amTimer = createTimer(now.withTimeAtStartOfDay.plusHours(2).plusDays(if(now.getHourOfDay > 1) 1 else 0),[ | 
            // do stuff 
        ] )

Of course you could use

now.withTimeAtStartOfDay.plusHours(if(now.getHourOfDay > 1) 26 else 2)


1 Like

Thanks everyone for your help!