Scheduling a recurring event


(tduffy) #1

Hi,
I am having an issue with scheduling in my code, and would love some help from the community.

I want to set a recurring event to trigger some switches, anywhere from every hour to every 6 hours depending on what is input. Basically, I want to control the watering to my greenhouse plants, where they get watered for a certain number of minutes every so many hours. The hours will depend on which plants are getting watered, so hence the ability to set different numbers.

I tried adapting some code I found in another’s app:

if (min >= 2 && min <= 300)
    {
        log.trace "Scheduled for every $min min"
        schedule("0 0/$min * * * ?", WaterPlants) 
}

However, this just results in it running every hour, no matter what $min is. Perhaps I’m being a complete idiot and overlooking something really simple, but I just can’t figure out how to make this do what I want.

Does anyone have any suggestions on how to make this work? Or on a better way to do this?

Thanks


(Tim Slagle) #2

I’m not exactly sure what you are trying to do. Can you give a more clear description of what exactly the schedule needs to be and I can try to design some logic for you.

Provide something like… every x hours xyz plants are watered

So on and so forth.


(tduffy) #3

Well that’s kind of the catch. I want to be able to input the number of hours when I set up the instance of the app. So the user enters the number of hours upon installing, they won’t be hard coded.


(Tim Slagle) #4

ahhhh ok, so you want a serperate app per plant you want watered?


(tduffy) #5

Yep, exactly. :slight_smile: They each need differing amounts of water. In the past I’ve been using many instances of the once-a-day app, but I’d really prefer to be able to just set up one app per plant grouping. Rather than one app per plant grouping per watering time.

It’s a piece of an overarching goal to have a override from my weather station for if it’s going to rain, or already has rained a certain amount in the last 24 hrs. I’ve got the override mostly working, but have had zero luck with this scheduling piece.


(Tim Slagle) #6

The problem here is a cron expression will not work for this scenario.

The way the expression sits now for your cron job is every X minute during any hour of the day it “water plants” will run.

So if you put 24 in for your “min” variable it will run on the 24th minute of evey hour. What we need to do here is do a runIn type logic.

Let me think on this one for a little. There are a couple ways to do this, i am just trying to think of the easiest way.

EDIT:

I’m thinking something like this should work:

def MinuteDelay = 60 * "${min}"
runIn(MinuteDelay, WaterPlants)

This would essentially allow you to run it every 30 minutes… every 300 minutes… etc…

You’d have to bluild something like this into it though…

Run this now then run it again in 300 minutes. If you want to post all your code i can try and take a look at it if im not explaining myself well enough. WHich is known to happen :wink:

OR… we can do something like every hour run these groups… that will allow a cron expression to work.

This will allow any outages to not affect upcoming tasks.


(tduffy) #7

Thank you! That makes sense. I think I’ve got it right in the code now. I’m running a test tomorrow with a light switch to make sure it works correctly in the wild, but the simulator run of it seems good.

I appreciate the quick help!