How to get Presence Sensor Last activity data into app?

Working on a new app and need to have the 'Last Activity At' data brought in and then made into usable data. I have no idea how to do this! :sunglasses:

What I need from this data is to know how long someone has been 'Present'.

Basically what I want to do is:

If presenceSensor has been Present for less than 10 minutes
.... Do this
else
.... Do that

Could anyone help me out with an outline on how to code this?

Thanks

Are you trying to write your own app in groovy, or would a RM rule do what you want?

One rule to turn on a virtual switch when someone comes home, then turn it off 10 minutes later. Another rule to say when virtual switch is on do this, else do that.

No, needs to be in Groovy (which is why I posted in Developers and not RM :wink:)

Just need to know how to

  1. get that data into the app
  2. turn the data into how many minutes someone has been Present

Thanks

Update: I've think I've figured out how to go about getting the difference between two dates. Thanks to ST forums. :roll_eyes: lol

Still need an example of getting the 'Last Activity At' data into the app. Pretty please! :grin:

One way to do it is to simply have your App subscribe to the presence events from the various sensors. When the App is notified of a change in presence, have it store the Date/Time into a State Variable. That way you always have the latest time that it changed without having to interrogate the device.

Thanks, will have to do some research on how to do that.

Was hoping there was a simple one liner. Something like...

def s = presenceSensor1.currentValue("date")

There very well may be. Hopefully someone with more App development experience will chime in with the answer.

1 Like

Have you tried using the following?

deviceName.getLastActivity()

Where “deviceName” is your device object reference.

https://docs.smartthings.com/en/latest/ref-docs/device-ref.html#getlastactivity

2 Likes

First test looks very good. I should be able to work with that! I've been digging this whole time too on the 'other' board.

Thank you very much!

1 Like

Just to close out my first post. This is what I ended up doing.

def lastActivity = presenceSensor1.getLastActivity()
			
		    LOGDEBUG("lastActivity: ${lastActivity}")
    		long timeDiff
   			def now = new Date()
    		def prev = Date.parse("yyy-MM-dd HH:mm:ss","${lastActivity}".replace("+00:00","+0000"))
    		long unxNow = now.getTime()
    		long unxPrev = prev.getTime()
    
    		unxNow = unxNow/1000
    		unxPrev = unxPrev/1000
    
    		timeDiff = Math.abs(unxNow-unxPrev)
    		timeDiff = Math.round(timeDiff/60)
			def delay1ms = delay1 * 1000
    
			LOGDEBUG("timeDiff: ${timeDiff}")
1 Like

Last activity could be a nice way to quickly look at devices that are possibly offline. A nice visual quick glance. Green for online, Yellow to indicate offline for x period, and Red for period expired, device is offline.

1 Like