I’m new to both SmartThings and Arduino and as a starting point to learn/practice I wanted to write a pet feeder Device Type with options to vary the portions amount via a slider and a value tile that shows the portions amount. I am able to get a standardTile to fire off my servo but now I’m a little confused as to how to pass values from the controltileTile/slider and how to update the valueTile (I read the documentation but I’m not that clear as to how to use these tiles)
Question#1: a slider is predefined from 0 to 100. Is there a way to limit it to 0 to 3 (for example - as in: slide the portion amount you want the feeder to release)? For now I’m handling it by defining 0 to 33 as 1 portion, 33-66 2 portions and 66+ three portions, and handle the amounts in the Arduino code.
Next question: when I run my code in the the simulator I have a setLevel Switch Level command where if I enter a value and click the setLevel button it gets correctly sent to my device. In the mobile app, though, I have the slider but I don’t know how to get it to fire the ‘setLevel’ command. So basically I can slide the slider but nothing gets sent to my device. How do I get to fire the setLevel command from my mobile app as well?
Third question: How to I update the valueTile with the portion amount?
Below is the code I’m using with bits and pieces taken from various examples around the web. Again, I’m trying to learn so a) please be nice and b) any suggestion on how should I go about doing this is greatly appreciated.
Thanks!
metadata {
// Automatically generated. Make future change here.
definition (name: “myFeeder”, namespace: “rt”, author: “rt”) {
capability "Actuator"
capability "Switch"
capability "Sensor"
capability “Switch Level”
}
// Simulator metadata
simulator {
status "on": "catchall: 0104 0000 01 01 0040 00 0A21 00 00 0000 0A 00 0A6F6E"
status "off": "catchall: 0104 0000 01 01 0040 00 0A21 00 00 0000 0A 00 0A6F6666"
// reply messages
reply "raw 0x0 { 00 00 0a 0a 6f 6e }": "catchall: 0104 0000 01 01 0040 00 0A21 00 00 0000 0A 00 0A6F6E"
reply "raw 0x0 { 00 00 0a 0a 6f 66 66 }": "catchall: 0104 0000 01 01 0040 00 0A21 00 00 0000 0A 00 0A6F6666"
}
// UI tile definitions
tiles {
standardTile("switch", "device.switch", width: 2, height: 2, canChangeIcon: true, canChangeBackground: true) {
state "on", label: '${name}', action: "switch.off", icon: "st.switches.switch.on", backgroundColor: "#79b821"
state "off", label: '${name}', action: "switch.on", icon: "st.switches.switch.off", backgroundColor: "#ffffff"
}
controlTile("level", "device.level", "slider", width: 2, height: 1, canChangeIcon: false, canChangeBackground: false) {
state "level", action: "setLevel"
}
valueTile("portions", "device.level", width: 1, height: 1) {
state "level", label:'${currentValue}'
}
main "switch"
details "switch", "level", "portions"
}
}
// Parse incoming device messages to generate events
def parse(String description) {
def value = zigbee.parse(description)?.text
if (value == "ping" || value == " ")
{
return
}
def name = value in ["on","off"] ? "switch" : null
def result = createEvent(name: name, value: value, isStateChange: true)
log.debug "Parse returned ${result?.descriptionText}"
return result
}
// Commands sent to the device
def on() {
zigbee.smartShield(text: “on”).format()
}
def off() {
zigbee.smartShield(text: “off”).format()
}
def setLevel(to)
{
zigbee.smartShield(text: ‘portion:’ + to.round()).format()
}