Skip to content

Instantly share code, notes, and snippets.

@joshualyon
Created March 4, 2019 18:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshualyon/1528e827d3eee962a1b750d407e5879e to your computer and use it in GitHub Desktop.
Save joshualyon/1528e827d3eee962a1b750d407e5879e to your computer and use it in GitHub Desktop.
Virtual Values device driver for Hubitat or SmartThings
/*
Virtual Values
Author: @josh (SharpTools.io)
Description: The Virtual Values device driver exposes a variety of methods which can be used to
set values of specific attributes. These attributes can then be used in other Apps within
Hubitat or in SharpTools.io Dashboards by using Hero Attribute tiles.
This serves as a flexible 'utility' device for setting arbitrary values that you want to use
elsewhere.
Command to Attribute Mappings
setText → text
speak → text *1
deviceNotification → text *2
setNumber → number
setLevel → level, [switch] *3
on → switch
off → switch
*1 speak() is a wrapper method for setText to provide compatibility with apps that support Speech Synthesis devices
*2 deviceNotification() is a wrapper method for setText to provide compatibility with apps that support Notify devices
*3 setLevel will issue the off() command if the level is set to 0 and will leave the level set at the previous value
Other Notes:
The on/off commands are useful for reflecting an active/default state in SharpTools.io dashboards which is helpful
for setting the color of your tile based on the device's state.
*/
metadata {
definition (name: "Virtual Values", namespace: "sharptools-io", author: "Josh Lyon") {
capability "Actuator"
capability "Sensor"
capability "Switch"
capability "Switch Level"
capability "Notification"
capability "Speech Synthesis"
command "setText", ["STRING"]
command "setNumber", ["NUMBER"]
attribute "text", "STRING"
attribute "number", "NUMBER"
}
preferences {}
}
def parse(String description) {
}
def on() {
log.trace "Executing 'on'"
turnOn()
}
def off() {
log.trace "Executing 'off'"
turnOff()
}
def setNumber(value){
log.trace "Executing setNumber $value"
Map numberEvent = buildEvent("number", value, null)
sendEvent(numberEvent)
}
def setText(value){
log.trace "Executing setText $value"
Map event = buildEvent("text", value, null)
sendEvent(event)
}
/* Also map speak() and deviceNotification() to setText for convenience */
def speak(value){ setText(value) }
def deviceNotification(value){ setText(value) }
private Map buildEvent(name, value, unit=null) {
Map eventMap = [name: name, value: value, unit: unit, isStateChange: true]
return eventMap
}
/*----- Set Level ---- */
def setLevel(value) {
log.trace "Executing setLevel $value"
def intValue = value as Integer
def newLevel = Math.max(Math.min(intValue, 100), 0)
Map levelEventMap = buildEvent("level", newLevel, "%")
if (levelEventMap.value == 0) {
turnOff()
// notice that we don't set the level to 0'
} else {
implicitOn()
sendEvent(levelEventMap)
}
}
def setLevel(value, duration) {
log.trace "Executing setLevel $value (ignoring duration)"
setLevel(value)
}
private implicitOn() {
if (device.currentValue("switch") != "on") {
turnOn()
}
}
private turnOn() {
sendEvent(name: "switch", value: "on", isStateChange: true)
}
private turnOff() {
sendEvent(name: "switch", value: "off", isStateChange: true)
}
def installed() {
setLevel(100)
setText("Use setText to set me")
setNumber(100)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment