Sure. I’m new to ST and don’t have a programming background, so I was actually editing this to learn a bit more about groovy.
In the meantime, I did a couple of additional changes: one - which you should then also do - is remove this bit from the button tile (since it’s not the main one anymore):
canChangeIcon: false, canChangeBackground: true
The other change I made might not suit everyone, but in my set-up I rather prefer to have the ON of the switch associated to AWAY, and OFF to PRESENT (because I interpret the switch as an alarm status, so I want it OFF when someone is present). Maybe this could be defined as a preference (I haven’t looked into how to define those yet)…?
FYI, this is the resulting code I’m currently using:
metadata {
definition (name: "Virtual Presence Plus", namespace: "ajpri", author: "Austin Pritchett") {
capability "Switch"
capability "Refresh"
capability "Presence Sensor"
capability "Sensor"
command "arrived"
command "departed"
}
// simulator metadata
simulator {
}
// UI tile definitions
tiles {
standardTile("presence", "device.presence", width: 2, height: 2, canChangeBackground: true) {
state("present", action: "departed", labelIcon:"st.presence.tile.mobile-present", backgroundColor:"#53a7c0", nextState: "not present")
state("not present", action: "arrived", labelIcon:"st.presence.tile.mobile-not-present", backgroundColor:"#ffffff", nextState: "present")
}
standardTile("refresh", "device.switch", inactiveLabel: false, decoration: "flat") {
state "default", label:'', action:"refresh.refresh", icon:"st.secondary.refresh"
}
standardTile("button", "device.switch", width: 1, height: 1) {
state "off", label: 'OFF', action: "switch.on", icon: "st.Kids.kid10", backgroundColor: "#ffffff", nextState: "on"
state "on", label: 'ON', action: "switch.off", icon: "st.Kids.kid10", backgroundColor: "#53a7c0", nextState: "off"
}
main (["presence", "button"])
details(["presence", "button", "refresh"])
}
}
def parse(String description) {
def pair = description.split(":")
createEvent(name: pair[0].trim(), value: pair[1].trim())
}
// handle commands
def arrived() {
off()
}
def departed() {
on()
}
def on() {
sendEvent(name: "switch", value: "on")
sendEvent(name: "presence", value: "not present")
}
def off() {
sendEvent(name: "switch", value: "off")
sendEvent(name: "presence", value: "present")
}