3 name: "NFC Tag Toggle",
4 namespace: "smartthings",
6 description: "Allows toggling of a switch, lock, or garage door based on an NFC Tag touch event",
7 category: "SmartThings Internal",
8 iconUrl: "https://s3.amazonaws.com/smartapp-icons/Developers/nfc-tag-executor.png",
9 iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Developers/nfc-tag-executor@2x.png",
10 iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Developers/nfc-tag-executor@2x.png")
14 page(name: "pageOne", title: "Device selection", uninstall: true, nextPage: "pageTwo") {
15 section("Select an NFC tag") {
16 input "tag", "capability.touchSensor", title: "NFC Tag"
18 section("Select devices to control") {
19 input "switch1", "capability.switch", title: "Light or switch", required: false, multiple: true
20 input "lock", "capability.lock", title: "Lock", required: false, multiple: true
21 input "garageDoor", "capability.doorControl", title: "Garage door controller", required: false, multiple: true
25 page(name: "pageTwo", title: "Master devices", install: true, uninstall: true)
29 dynamicPage(name: "pageTwo") {
30 section("If set, the state of these devices will be toggled each time the tag is touched, " +
31 "e.g. a light that's on will be turned off and one that's off will be turned on, " +
32 "other devices of the same type will be set to the same state as their master device. " +
33 "If no master is designated then the majority of devices of the same type will be used " +
34 "to determine whether to turn on or off the devices.") {
36 if (switch1 || masterSwitch) {
37 input "masterSwitch", "enum", title: "Master switch", options: switch1.collect{[(it.id): it.displayName]}, required: false
39 if (lock || masterLock) {
40 input "masterLock", "enum", title: "Master lock", options: lock.collect{[(it.id): it.displayName]}, required: false
42 if (garageDoor || masterDoor) {
43 input "masterDoor", "enum", title: "Master door", options: garageDoor.collect{[(it.id): it.displayName]}, required: false
46 section([mobileOnly:true]) {
47 label title: "Assign a name", required: false
48 mode title: "Set for specific mode(s)", required: false
54 log.debug "Installed with settings: ${settings}"
60 log.debug "Updated with settings: ${settings}"
67 subscribe tag, "nfcTouch", touchHandler
68 subscribe app, touchHandler
71 private currentStatus(devices, master, attribute) {
72 log.trace "currentStatus($devices, $master, $attribute)"
75 result = devices.find{it.id == master}?.currentValue(attribute)
80 def value = it.currentValue(attribute)
81 map[value] = (map[value] ?: 0) + 1
82 log.trace "$it.displayName: $value"
85 result = map.collect{it}.sort{it.value}[-1].key
87 log.debug "$attribute = $result"
91 def touchHandler(evt) {
92 log.trace "touchHandler($evt.descriptionText)"
94 def status = currentStatus(switch1, masterSwitch, "switch")
106 def status = currentStatus(lock, masterLock, "lock")
108 if (status == "locked") {
118 def status = currentStatus(garageDoor, masterDoor, "status")
120 if (status == "open") {