Presence sensor piston - Commute Times


#1

1) Give a description of the problem
This piston works, but I think it could be more efficient
2) What is the expected behavior?
Log leaving and arrive time for 3 different places, text me a total commute time. Like I said it does work, but I feel like this could be much simpler.

3) What is happening/not happening?
(PUT YOUR INFO HERE)

4) Post a Green Snapshot of the pistonimage

Thanks in advance


[Install Instructions] webCoRE Presence Sensor on your Android (beta)
#2

I’ve got a couple ideas for this but have to get my daughter to bed, then I’ll throw something together later tonight.


#3

Sorry, having trouble getting my sensor to reinstall. Once I get over that hurdle, I’ll post a piston for you. I can’t find any way to fake a device so I can access the arrivingPlace, etc.

In the meantime, here is what I was generally thinking:

ON Events from Presence Sensor
Execute
IF leavingPlace is in ‘Home, CACI, NavyBase’
set variable LastPlace to leavingPlace
set variable LastPlaceTime to $now

IF arrivingPlace is in ‘Home, CACI, NavyBase’ AND is different than LastPlace
send SMS "Your commute time from {LastPlace} to {arrivingPlace} was formatDuration($now-LastPlaceTime,true,m)

END

It may take some tweaking but that should do what you’re after.


#4

Thanks that would be great. I’ve noticed that sometime the presence changing to away does not set the variable, but arriving place (home) or leaving place (Home) seem to work. Of course I’ve only been using this for 24 hours, and have only arrived and left a couple of times to test this out.


#5

I’ve had mixed results with leavingPlace and arrivingPlace. If those aren’t reliable, we’ll change it to this:

ON Events from Presence Sensor
Execute
IF currentPlace changes away from any of 'Home, CACI, NavyBase’
set variable LastPlace to previousPlace
set variable LastPlaceTime to $now

IF currentPlace changes to any of ‘Home, CACI, NavyBase’ AND is different than LastPlace
send SMS "Your commute time from {LastPlace} to {currentPlace} was formatDuration($now-LastPlaceTime,true,m)

END


#6

Thanks, that is much simplier. I think I understood the jist. Does this look right. A couple of the items were new to me. So I did best guess. like on events? I did not see a generic Events.


#7

Nice, almost done!! The “on events from” is exactly what I was talking about, so you got that nailed too.

Is “previousPlace” available from your presence sensor? There’s something like that but I can’t recall the exact wording. Change line 30 to “set variable LastPlace to [Physical Device]Presence Sensor 7 : previousPlace” or something along those lines. Make the same edit on line 35, too.

Let me know if you get any odd behavior and we’ll figure it out, but if that doesn’t work you’re real close!


#8

I think I got it with one exception and one question
So in the SMS text message, can I include the Physical device “Current Place”? I did not know how to include the wording from a physical device.

Also question was for the changes away from should it be
Home,CACI,Navybase
or
Home, CACI, NavyBase

or does it matter?

Thanks again


#9

Good catch! You should be able to just type in [Presence Sensor 7 : currentPlace] with the actual name of your sensor in place of “Presence Sensor 7”.

If that doesn’t work, just save currentPlace to another variable and use that in the message. That will certainly work… but the method above is a sleeker way of doing it if you can get it to work.

Shouldn’t matter, webCoRE filters out the leading spaces. One is just easier for us to read. :slight_smile:


#10

Worked, kinda. Definitely need to tweak. Current Place does not update right away. I was at location for 15 minutes before it updated. Might use arriving at? That came up pretty quick. Since the locations are not close for me it should work. Would be nice if current place updated immediatley though, in case locations were closer.

Also, I’m sure its just the math, but the time for communte was 17562 days… need to verify if $now is correct.

This is very close though, thanks again


#11

Changed to arriving at place. Also changed my variable for time. I had it set to Time Only. But $now is date-time. I suspect that will fix it.

Now to what may be my last issue.
This expression is not pulling the info from my device

'Your Commute today from {LastPlace} to [sensorname : arrivingAtPlace] took ’ formatDuration($now-LastPlaceTime,true,‘m’)

What is returns is

‘Your Commute today from Home to [Sensorname : arrivingAtPlace] took xxxx’

Do I need to surround
[Sensorname : arrivingAtPlace]
with something?


#12

For your expression make sure you don’t have the device included in the quotes. As you write it out you should see it evaluating like this - everything you want in text, including the spaces, needs to be inside your double quotes.

Note: any spaces you include that are NOT inside double quotes won’t show up in your message… it just makes it easier for you to read without everything jammed together. :slight_smile:


#13

I’m having issues with the $now variable. in the expresssion I end up with something like 17000 days, etc.


#14

I created a new variable called currenttime, and made it equal to $now and it worked. Maybe something in the formatting for $now, that I missed?


#15

Interesting. Can you post a green snapshot of the piston that gave you 17000 days? I’m curious to figure out where it fell down.

If you have it working with a variable… I’d call it a wrap and enjoy your new messages!


#16

Sure, I’ll let you know if it works or not. When I went back in to grab the screenshot, the new variable approach also showed 17000 days. Not sure if it will update upon event. ?


#17

What value is stored in “lastplacetime” now? It looks like it’s essentially subtracting zero, or an empty variable.

You can probably just use $now without the variable and see what shows up in the SMS. The values in the expression box can be misleading when your variables are empty… But still let you know if your formatting is correct.


#18

I had $now this morning, but my text was with the 17000 days, which is what caused me to tweak it, I agree its weird. It hard to tell what the value is because I set the variable for curren time to be $now, and thats what it shows

Now $now does have a value, so its annoyng that theres nothing there


#19

I’ll take a look at it in a bit. I’ve had some weird issues with time variables but have always been able to get them to work eventually.


#20

Ok, so I started with a dummy piston to verify this. Using this expression, I get the 17000 days while the variable “LastTime” is empty and I do $now-LastTime.

Subsequent executions showed the same result, even when LastTime had a value. But I noticed the value was only 2:37pm, with no date ($now has a date, so what we’re really doing is subtracting hours from the beginning of epoch time)… so what’s where the 17000 days is coming from!

I changed the variable type to Date and Time, but I was still getting the 17000 days. I tried a few other types… integer, Long, etc. and none of the results were making sense. So I deleted the variable and made a new one that wouldn’t have anything in cache memory associated with it, labeled it Date and Time, and it worked.

in summary, all you have to do is change your variable from Time and Date and Time. If after a couple piston executions that doesn’t get rid of the 17000 days, delete your variable and make a new one with Date and Time, then update your piston commands to the new variable.