Accessing local variable in do loop


#1

1) Give a description of the problem
I’m having a problem with this piston where I’m not sure if it is firing or not. I have a trace, but would like to push some notifications to show whether locks are being locked when alarm set. Some of which are not being locked for some reason.

2) What is the expected behavior?
Do I have the correct variable to show the lock status of each lock?

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

4) Post a Green Snapshot of the pistonimage

5) Attach any logs (From ST IDE and by turning logging level to Full)
(PASTE YOUR LOGS HERE BETWEEN THE MARKS THEN HIGHLIGHT ALL OF THE LOGS SND CLICK ON THE </> ICON TO FORMAT THEM)


#2

Not sure which you want, but you could either put the following in the notification:

“{locks} are now locked”

Or you could remove the notification task from the “with {locks}” statement and add something like this after it if you want to send an individual message showing the exact state of each of the devices. (Note I do not have locks, so I used switches in the example instead.)


#3

Thanks @pnbruckner. Is there a variable that gets loaded when using with {locks} or with {switches}? Yours is simple and I could change the code. I just figured there would be a local variable loaded that I could use.


#4

What exactly do you want the message to say? Give me an example?

Your post mentions a loop but your piston has no loop… I’m confused?


#5

Based on you question though… sounds like you need something like this (I used lights as well coz I have no locks).

This will loop 5 times until all locks (lights) are locked (off). If they all lock successfully you get the success message. If any locks haven’t locked after the 5th loop, you get a warning message detailing which locks failed to lock.


#6

Thanks @Robin. I’ll load what you have and give it a go! I didn’t realize (I guess naively) that smartthings would be so unreliable that I would have to attempt to lock the locks several times. Dang. Here I was looking for simplicity.


#7

It’s normally ok… but when sending loads off commands at once (say you’re trying to turn off all lights at the same time), then the mesh can get a bit overwhelmed and commands get missed.


#8

I was putting 10 seconds of delay between commands in the with locks loop. Still would like to know what the local variable is for the With {locks} do loop …


#9

{locks} is a device variable at the top of the piston, just a way of consolidating the devices into one list instead of repeating the selection throughout the piston.

You could write:

with
Front door, back door, side door, back gate’s lock
Do

for each of line that refers to {locks}, but that would be rather repetitive and less user-friendly.


#10

I’m thinking you’re not understanding what the with statement does with a list of devices. It’s not a loop. For tasks that don’t make a change to the devices - e.g., wait, send a PUSH notification, set a variable, etc. - it just does these tasks once. For tasks that make a change to the device(s) it does that task for each of the devices.

Maybe a concrete example will help. In your original piston, the “with {locks}” statement will send the Lock command to each of the devices, then it will wait 10 seconds (just once), and then it will send the PUSH notification (just once.)

If you want to do a set of tasks, once for each device, then you need the “for each” loop, as I showed in my example. Then the variable you’re looking for is specified by the “for each” statement (in my case I used the default variable named $device, but you can specify your own device type variable if you like.)

Does that help?


#11

I believe it may be sinking in. How does one find this info? I didn’t realize there was a difference between a for each and what I did. I appreciate your outwardly seeming patience …


#12

The wiki page is a bit sparse in regards to explaining statements:

https://wiki.webcore.co/Piston

I might have a stab at it one day!


#13

Appreciate all the help!


#14

How do I query the current state of a lock? Seems like I’m getting stale status when I check right after locking.


#15

It’s very likely (not 100 sure without testing) that device states don’t update in the middle of a for each loop.

Is there any particular reason why you are using ‘for each’… you’re gonna get bombarded with notifications!!!

Maybe try:

define
device locks = x y and z
device garageDoors = a, b and c
device locksOpen = (leave as not set)
device doorsOpen = (leave as not set)

If
Some trigger / condition
THEN

with locks
lock
wait 5 seconds

IF any of locks are unlocked
x2 save matching devices to {locksOpen}
THEN
(leave blank)
end if

with garageDoors
close
wait 5 seconds

IF any of garageDoors are open
x2 save matching devices to {doorsOpen}
THEN
(leave blank)
end if

send push notification {"Alarm has been engaged " (count(locksOpen)==‘0’?‘All locks are locked’ : (count(locksOpen)==‘1’?‘The ’ locksOpen ’ is unlocked’ : The ’ locksOpen ’ are unlocked’)) " and " (count(doorsOpen)==‘0’?‘All garage doors are closed.’ : (count(doorsOpen)==‘1’?‘The ’ doorsOpen ’ is open.’ : The ’ doorsOpen ’ are open.’))

end if