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
}