I made my first plugin! I'm a .NET developer by trade. I've never written groovy until now. I wanted to have a stock ticker for the company I work for on my dashboard. So, I went and found a free api called Alpha Vantage. They allow 500 calls per day. Like I said, I use this for displaying a single stock on my dashboard. I wouldn't use this for multiple tickers as you only get so many calls for free per day. You could also use it to set alerts for a specific ticker.
I copied heavily from @staze and his gmcmap.
I would link it, but because I am a new user, I am not allowed to link it. I've also posted it in my github and will update when I'm allowed to post links, but until then, here's the code.
/*
* alphavantage ticker device
*/
metadata {
definition(name: "alphavantage.stockticker", namespace: "alphavantage", author: "Leonard Sperry", importUrl: "https://raw.githubusercontent.com/leosperry/hubitat/main/alphavantage")
{
capability "Sensor"
capability "Polling"
attribute "Symbol", "STRING"
attribute "Open", "NUMBER"
attribute "High", "NUMBER"
attribute "Low", "NUMBER"
attribute "Price", "NUMBER"
attribute "Volume", "NUMBER"
attribute "LatestTradingDay", "DATE"
attribute "PreviousClose", "NUMBER"
attribute "Change", "NUMBER"
attribute "ChangePercent", "NUMBER"
attribute "LastestPoll", "DATE"
}
}
preferences {
section("URIs") {
input name: "ApiKey", type: "text", title: "API Key", required: true
input name: "Symbol", type: "text", title: "Symbol", required: true
input name: 'updateMins', type: 'enum', description: "Select the update frequency", title: "Update frequency (minutes)\n0 is disabled", defaultValue: '30', options: ['0','5','10','15','30'], required: true
input name: "logEnable", type: "bool", title: "Enable debug logging", defaultValue: true
}
}
def logsOff() {
log.warn "debug logging disabled..."
device.updateSetting("logEnable", [value: "false", type: "bool"])
}
def updated() {
unschedule()
log.info "alphavantage updated..."
log.warn "debug logging is: ${logEnable == true}"
if (logEnable) runIn(1800, logsOff)
if(updateMins != "0") {
schedule("0 */${updateMins} * ? * *", poll)
}
}
def parse(String description) {
if (logEnable) log.debug(description)
}
def poll() {
if (logEnable) log.debug "alphavantage polling..."
def url = "https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=${Symbol}&apikey=${ApiKey}"
try {
httpGet(url) { resp ->
//if (logEnable) log.debug
resp.getData()
def respValues = resp.data["Global Quote"]
sendEvent(name: "Symbol", value: respValues["01. symbol"])
sendEvent(name: "Open", value: respValues["02. open"])
sendEvent(name: "High", value: respValues["03. high"])
sendEvent(name: "Low", value: respValues["04. low"])
sendEvent(name: "Price", value: respValues["05. price"])
sendEvent(name: "Volume", value: respValues["06. volume"])
sendEvent(name: "LatestTradingDay", value: respValues["07. latest trading day"])
sendEvent(name: "PreviousClose", value: respValues["08. previous close"])
sendEvent(name: "Change", value: respValues["09. change"])
sendEvent(name: "ChangePercent", value: respValues["10. change percent"])
sendEvent(name: "LastestPoll", value: new Date())
}
} catch(Exception e) {
log.debug "error occured calling httpget ${e}"
}
}
Edit: This is the code I copied heavily from:
hubitat-gmcmap/hubitat-gmcmap.groovy at master · staze/hubitat-gmcmap (github.com)
Here is where you can find my code in github:
hubitat/alphavantage at main · leosperry/hubitat (github.com)
To use this driver, you will need to get yourself and API Key from Alphavantage which you can get for free at the time of this writing.