As pointed out by @joshua_lyon below this code is redundant, Smart Things provides a Simulated Presence Sensor which can be used, however you can still follow the instruction for install the “Simulated Presence Sensor” if you’d like.
ORIGINAL POST:
For anyone who’s looking to emulate and control presence sensors. I found this very hand when developing device code and apps and needed to emulate the location (away or present) to active the apps (including some of ST’s default apps like Door/Locks which can’t be done through the IDE).
Anyways it’s a simple emulator, it will create a Mobile Presence Device on your phone which you can “Tap” to change mode from present to away and vice versa.
This is a DEVICE TYPE (not Smart app) so you’ll need to install it manually through the IDE.
To install this follow these instructions:
- Login to your IDE at https://graph.api.smartthings.com (create a login if you don’t have one)
- Click on “My Device Types”
- Click on “+New SmartDevice” on the top right
- Click “From Code”
- Copy paste the code into the editor and click “Create”
- Click “Publish” and then “For me” on the top right
- Click on “+New Device” on the top right
- Enter a “Name”
- For “Device Network Id *” enter 2000
- Under “Type *” select the new device type you just created - Virtual Mobile Presence (it will show up at the bottom of the list)
- Select your “Location”
- Click “Create”
- Open you SmartThings App on your iPhone, under “Things” look for your new device. You can now control this by touching the cover icon to change the presence. You’re done! Now you have a presence device you can control manually.
The Virtual Mobile Presence Device Type Code:
/* **DISCLAIMER**
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* Without limitation of the foregoing, Contributors/Regents expressly does not warrant that:
* 1. the software will meet your requirements or expectations;
* 2. the software or the software content will be free of bugs, errors, viruses or other defects;
* 3. any results, output, or data provided through or generated by the software will be accurate, up-to-date, complete or reliable;
* 4. the software will be compatible with third party software;
* 5. any errors in the software will be corrected.
* The user assumes all responsibility for selecting the software and for the results obtained from the use of the software. The user shall bear the entire risk as to the quality and the performance of the software.
*/
/*
* Base code has been take from SmartThings as of Feb 2015
*
* Virtual Mobile Presence Device, manually switch modes
*
* Taken from SmartThings base code, enhanced and bugfixed by RBoy
* Change log:
* 2015-2-3 - Initial code
*
*/
metadata {
// Automatically generated. Make future change here.
definition (name: "Virtual Mobile Presence", namespace: "rboy", author: "RBoy") {
capability "Presence Sensor"
capability "Sensor"
command "away"
command "present"
}
simulator {
status "present": "presence: 1"
status "not present": "presence: 0"
}
tiles {
standardTile("presence", "device.presence", width: 2, height: 2, canChangeBackground: true, inactiveLabel: false, canChangeIcon: true) {
state("present", label:'${name}', icon:"st.presence.tile.mobile-present", action:"away", backgroundColor:"#53a7c0")
state("not present", label:'${name}', icon:"st.presence.tile.mobile-not-present", action:"present", backgroundColor:"#ffffff")
}
main "presence"
details "presence"
}
}
def parse(String description) {
def name = parseName(description)
def value = parseValue(description)
def linkText = getLinkText(device)
def descriptionText = parseDescriptionText(linkText, value, description)
def handlerName = getState(value)
def isStateChange = isStateChange(device, name, value)
def results = [
name: name,
value: value,
unit: null,
linkText: linkText,
descriptionText: descriptionText,
handlerName: handlerName,
isStateChange: isStateChange,
displayed: displayed(description, isStateChange)
]
log.debug "Parse returned $results.descriptionText"
return results
}
private String parseName(String description) {
if (description?.startsWith("presence: ")) {
return "presence"
}
null
}
private String parseValue(String description) {
switch(description) {
case "presence: 1": return "present"
case "presence: 0": return "not present"
default: return description
}
}
private parseDescriptionText(String linkText, String value, String description) {
switch(value) {
case "present": return "$linkText has arrived"
case "not present": return "$linkText has left"
default: return value
}
}
private getState(String value) {
switch(value) {
case "present": return "arrived"
case "not present": return "left"
default: return value
}
}
def away() {
sendEvent(name: 'presence', value: 'not present')
}
def present() {
sendEvent(name: 'presence', value: 'present')
}