Device Handler - Scheduling a Task

I'm creating a device handler to connect with YouNeedABudget and fetch basic data around my budgets there. I'm then displaying the remaining budget in each category on a dashboard. All of this is working fine for the initial attempt.

However, I've tried a few different methods to get this thing scheduled so the value updates on it's own and I haven't had any luck with it. I can manually update them with refresh, but it only ever runs the once.

I've tried the schedule("0 * * * *","pollYNAB") as well as a runEvery1Hour(pollYNAB)

I've also tried this without quotes, and that doesn't seem to work either. schedule("0 * * * *",pollYNAB)


import groovy.transform.Field

metadata {
    definition (name: "YNAB Category Driver", namespace: "Matthew", author: "raynerprogramming", importUrl: "") {
	capability "Sensor"
    capability "Refresh"
    attribute "balance", "string"
    attribute "category", "string"

    command "pollData"         
    }
    preferences() {
		section("Query Inputs"){
			input "apiKey", "text", required: true, title: "YNAB API Key", defaultValue: null
            input "categoryid", "text", required: true, defaultValue: null, title: "Category id"
            input "budgetid", "text", required: true, defaultValue: null, title: "Budget id"
	  }
    }    
}

void pollYNAB() {
    if( apiKey == null ) {
        log.error "YNAB API Key not found.  Please configure in preferences."
        return
    }

    def requestParams = [ uri: "https://api.youneedabudget.com/v1/budgets/${budgetid}/categories/${categoryid}", headers: ["Authorization": "Bearer ${apiKey}"]  ]
	asynchttpGet("pollCategoryHandler", requestParams)
    return
}

void pollCategoryHandler(resp,data) {
    log.info resp
    log.info resp.getStatus()
    log.info resp.getHeaders()
    log.info resp.getJson()

	if(resp.getStatus() == 200) {
        def cat = parseJson(resp.data)
		doPollYNAB(cat)		
	} else {
		log.error "YNAB Error: ${resp.getStatus()}"
	}
}

void doPollYNAB(Map ynab) {
    updateDataValue("category", ynab.data.category.name)
    updateDataValue("balance", ynab.data.category.balance.toString())
    updateDataValue("currTime", new Date().format("HH:mm", TimeZone.getDefault()))    
    sendEvent(name: ynab.data.category.name, value: ynab.data.category.balance.toString())
    sendEvent(name: "balance", value: "\$${(ynab.data.category.balance/1000).toString()}")
}

public void refresh() {
	unschedule("pollYNAB")   
	initialize()
}

void updated()   {
	unschedule("pollYNAB")   
	initialize()
}
void initialize() {
    pollYNAB()
    schedule("0 * * * *","pollYNAB")
	return
}

public void pollData() {
	pollYNAB()
    return
}

I use http://cronmaker.com to get my cron expressions.

For example;

schedule("0 1 0 1/1 * ? *", myMethod)

Would run at 1 min past midnight every day and is the correct syntax to use.
Just put it in your ‘initialize’ or ‘updated’ method in your driver

So I would use...

Updated(){
unschedule()
schedule("0 1 0 1/1 * ? *", myMethod)
}

Andy

2 Likes

try:

runEvery1Hour("pollYNAB")

@cobra @cybrmage both of those worked great. Thank you. I tried to find some documentation on this before posting but couldn't, do you have any ideas where I could check before posting next time?

I'm making use of your driver @sarez - it's seems like the pollData command isn't executing for me for on-demand pulling down my budget info. Nothing is being logged in the events for the device.

Care to share your updated driver code here again?

My bad - had a wrong category ID set. Working great!