Correct way to check whether $device switch is on


#1

1) Give a description of the problem
I have already written about this elsewhere on this forum, truth be told, but I suppose that from the Meta section, the likelihood o getting answers is small. I noticed recently that some of my chunkier pistons could not be resumed after pausing, and could not be saved whilst running. The problem is documented at Unable to Resume Pistons. I then realised that I was getting this error on the ST IDE when trying to resume:

java.lang.reflect.UndeclaredThrowableException @line 1308 (api_intf_dashboard_piston_resume)

Since there were no replies on my post (on the above link), I went about it the (very) hard way, and tried to determine what was wrong with pistons not resuming… And I found something in common - they all had a For Each ($device) loop, and to determine whether a device switch was on or off, I was using: “if {[$device:switch]} is ‘on’” or “if {[$device:switch]} is ‘off’”. SmartThings seems not to like this, inconsistently, and I noticed that when I changed the code, it always manages to resume. I say inconsistently because sometimes, albeit very rarely, I do manage to resume such pistons. But it’s all very intermittent and unpredictable. See the green snippet below:

Am I doing this wrong? Should I instead use the following:

It just seems wrong to me, maybe because I am a beginner and am not understanding something… If I do the code like this, it seems like $device is an array, not a variable holding a single device, and surely in the FOR EACH loop, it is only putting in that variable one device at a time, and then going through the entire loop once for every single device. Or is my understanding flawed?

2) What is the expected behaviour?
I expected the “if {[$device:switch]} is ‘off’”: Indeed, it actually does work whenever I manage to properly save and resume a piston that contains the said expression, but often the resume fails with the above error - and by often I mean almost always.

3) What is happening/not happening?
As detailed above, it is throwing an error. I am just looking to see whether the alternative, i… to build th statement through the WebCoRE interface which would thn read “if any of {$device}'s switch is on” sounds a littl off to me, implying a list of devices rather than just one device being considered for every pass through the FOR EACH loop.

**4) Post a Green Snapshot of the piston
Uploaded in text above.

5) Attach logs after turning logging level to Full
No logs - just the IDE error above.


#2

When I use a loop on $device, I usually have the list of devices stored in a device variable. In your first image above, I’d proably do:

if
  Any of switch 63, switch 64, switch 65, or switch 66' switch is on
     save matching devices to variable onDevices
  and 
  Switch 112 is off
  and
  Motion sensor 10's...
then
  for each $device in onDevices..
  do
    if
     [$device:switch] is 'on'

#3

Thank you for replying…

I will try that, though I have a feeling that ST just does not like this part:

The fact that I am putting that as an expression - ST just won’t allow me to resume the piston, whereas if I replace that with “if any of {$device}'s switch is on”, then it allows me to resume properly straight away.

Nevertheless I will try that out. Thank you.


#4

Try formatting it like this:

image


#5

Hmmm, great - I will certainly try that, thanks. This looks like a change that may work :slight_smile:


#6

I know what you mean but all device variables are treated like this because they aren’t limited to a single device. It just looks wrong when you know there is only a single device.

It wouldn’t occur to me to do it any other way.


#7

Thanks @orangebucket and @eibyer… Your solutions both worked for me… I opted to go with the latter one, i.e. using “if Any of {$device}'s switch is off…” given it comes out of the box, so to speak, therefore avoiding the need to write an expression - and it works just as well.

Many thanks both.