Date/Time of Last On/Off Event


#1

I want to know when the last time a device was turned on/off. How do I do this in a piston? I’ve played around but can’t seem to figure it out.

Thanks


#2

Expressions age() and [deviceName:swiitch] would give you last event and switch status.

Edit: age([deviceName:attribute])


#3

Thank you. I had tried age. That gives me milliseconds since the event right? So I’d need to deduct that from now which is the step I was missing.

Edit: what I just posted doesn’t make sense. Thinking …


#4

Example…

formatDuration(age([Front Door Lock:lock]))

Output:
(expression) formatDuration(age([Front Door Lock:lock])) »»» (string) 00:37:39

Other formats here:
https://wiki.webcore.co/Functions#formatDuration


#5

Got it. I was missing the format part.

Thx


#6

I am using this expression to get the actual date/time of the last on/off event rather than time since:

A
{$now}-(age([device :switch]))

This returns the following format:

Sat, Sep 9 2017 @ 5:55:27 PM CDT

In other places I use the following expression to format a date and time:

B
format('%s %d @ %s', $monthName, $day, $time)

This gives me the format:

September 9, 2017 @ 5:55 P.M.

I would like to transpose the results from A in to the format I get from B.

Can I do this? I cannot figure out how to do it.


EDIT: correction to the output from B above. It’s actually:

September 9 @ 5:55 P.M.

Just correcting the post so I don’t confuse others.


#7

Try this…

formatDateTime($now-(age([Back Porch:switch])), 'MMMMM dd, yyyy @ h:mm a')

Reference: https://docs.oracle.com/javase/tutorial/i18n/format/simpleDateFormat.html


#8

Thank you. You nailed it.

I’ve been sitting here for hours trying different things and just could not make it work.