Virtual Presence Device - Control from SmartPhone


(www.rboyapps.com - Make your home your butler!) #1

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:

  1. Login to your IDE at https://graph.api.smartthings.com (create a login if you don’t have one)
  2. Click on “My Device Types”
  3. Click on “+New SmartDevice” on the top right
  4. Click “From Code”
  5. Copy paste the code into the editor and click “Create”
  6. Click “Publish” and then “For me” on the top right
  7. Click on “+New Device” on the top right
  8. Enter a “Name”
  9. For “Device Network Id *” enter 2000
  10. Under “Type *” select the new device type you just created - Virtual Mobile Presence (it will show up at the bottom of the list)
  11. Select your “Location”
  12. Click “Create”
  13. 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')
}

Presence Manager 1.1 Released
(Joshua Lyon (SharpTools.io Dashboard)) #2

Isn’t there already a Simulated Presence Sensor with similar functionality?



Keeping Water Cooler Off When Empty?
(www.rboyapps.com - Make your home your butler!) #3

You’re absolutely right!! I missed it, instead I found a Presence Sensor and tried using that (ofcourse you can’t change the location) so I started on this one.

Man - what a waste! Thanks for pointing it out anyways. This is a redundant thread!


(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy) #4

You can delete your own posts if you decide the thread is deprecated. :wink:

Or @April might if you ask her… She’s got super admin rights.

But… It is still useful reminder information on Presence, though. Thanks!


(Kevin Tierney) #5

This is actually a useful thread, even if not for the original reason. I didn’t even know there was a simulated prescense sensor. I’d given up on presence/geofencing a long time ago

@joshua_lyon can we control this virtual presence device in SharpTools/Tasker? I’m thinking AutoLoaction + SharpTools + Simulated Presence = much more powerful/accurate presence sensor


(Joshua Lyon (SharpTools.io Dashboard)) #6

@kevintierney absolutely! A few other community members are already using SharpTools + Tasker along with the Simulated Presence Sensor to achieve more powerful/accurate presence.

The Simulated Presence Sensor exposes the methods arrived() and departed() which you can control through SharpTools.

Let me know if additional details are needed.


(Kristopher Kubicki) #7

If it just me or is the Simulated Presence Sensor icon all messed up?

@joshua_lyon - can you show me the Simulated Presence Sensor source? I’d love to see how those commands are implemented.


(Joshua Lyon (SharpTools.io Dashboard)) #8

The Simulated Presence Sensor icon was messed up for me as well. (Stretched out beyond the bounds of its container). I ended up just using a picture as the icon.

I’m not sure where the Simulated Presence Sensor source is as it doesn’t show up under ‘My Device Types’ > ‘New’ > ‘From Template’. Perhaps someone from ST could help with this request?


(Brice; SmartRulesApp.com) #9

Where did you find out the methods arrived() and departed()? I’d like to use some of the other simulated sensors, but I don’t see their code available in the templates, so I’m not sure how to use them.


(Joshua Lyon (SharpTools.io Dashboard)) #10

Through reflection of supportedCommands in a supporting SmartApp.


(Kevin Tierney) #11

Someone created a smartapp that will display device capabilities. Install that and you can see what commands/attributes are available


(Brice; SmartRulesApp.com) #12

That is brilliant! Thanks


(Kristopher Kubicki) #13

OK cool - I pulled that down the same way too. I was hoping the source was out there

Kristopher


(Kristopher Kubicki) #14

Source here:

https://graph.api.smartthings.com/ide/device/example/0d8244b9-e242-4e38-8f30-834b0753a239

So the interesting thing about that is the command is just “instanced” in the definition. I wasn’t really aware you could do that – so that’s pretty sweet.


(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy) #15

That’s the way SmartThings works, … If not exactly, but in effect:

The SmartDevice Type is used by the factory to create instances of the Device Class appropriate for each Thing.


(Kristopher Kubicki) #16

Just an FYI - I noticed that ST recently deprecated this functionality. Intentionally or not.


(Joshua Lyon (SharpTools.io Dashboard)) #17

@Kristopher which functionality are you referring to that may have been unintentionally deprecated? I just tried an existing Simulated Presence Sensor and created a new Simulated Presence Sensor and they both seem to work fine using the arrived and departed commands.

I got a notification from SmartThings that some users are experiencing issues connecting and controlling their devices - might your concern be related to that?

20150317 - Device Control
Incident Report for SmartThings

New Incident Status: Identified
Some users may be experiencing issues connecting and controlling their devices. We have identified the issue and will update when it is resolved.
Mar 17, 08:29 EDT


(Matthijs Hoekstra) #18

This virtual presence sensor is perfect for the nanny/guest in the house scenario. My mom was visiting and when my wife and i left the house it went dark :slight_smile: instead of creating new modes just toggle the presence sensor and i’m done.


#19

Hi

I’ve successfully installed the built-in ST Simulated Presence Sensor. It is currently attached to no other Things or SmartApps; also, for the record, using Android.

The Simulated Presence Sensor thing does NOT appear to be firing the DEPARTED command…

From the Live log:
11bf6995-86a1-44fa-bff0-b430b8253343 10:49:14 AM: trace Executing ‘arrived’ -> Manually set to Not Present, ie. I clicked the button/tile for the 2nd time
11bf6995-86a1-44fa-bff0-b430b8253343 10:49:06 AM: trace Executing ‘arrived’ -> Manually set to Present, ie. I clicked the button/tile for the 1st time.

I tried this with a second instance of another Simulated Presence Sensor, and again, for both the present (on) and not present (off) button press it fires the “arrived” method. It is never firing “departed”…

Thoughts?

Thanks in advance.
J


#20

To answer my own question…

I found the answer in this topic:

Subject to correction, I believe this is the code that gets installed when one installs the built-in ST Virtual Presence Sensor; please correct me if I am wrong.

Based on the above assumption being correct, it looks like the correct commands are indeed firing depending on the present/not present state, BUT that the log reports it incorrectly:

// handle commands
def arrived() {
log.trace "Executing ‘arrived’"
sendEvent(name: “presence”, value: “present”)
}
def departed() {
log.trace "Executing ‘arrived’"
sendEvent(name: “presence”, value: “not present”)

Question: how would I go about fixing the built-in code?

Thanks
J