Unable to Resume Pistons


So I was happily creating new pistons and started writing some pretty complex stuff, until suddenly - as of last week, I couldn’t sav pistons any longer. I found a workaround for that one - pause them, and then I am allowed to save. That did not last long however: soon after, I realised that sometimes my pistons were not getting resumed. Initially it was a rare event, but over the last week it deteriorated slowly from rare event to practically always. It is somewhat similar to this Community Topic: Unable to Save New Piston, but not quite, as the error I get on the IDE is as follows:

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

I tried to work around this by creating a new piston and just paste the code in it (or restore from backup code), but I can see that is not going well either, because when I try to save new pistons now I am getting this in my IDE Live Logging:

java.lang.reflect.UndeclaredThrowableException @line 1166 (api_intf_dashboard_piston_set_save)

I tried clearing my browser cache, but to no avail, rebooting my machine, etc. just in case something local was causing it - all to no avail. What could have gone wrong, and what can I do to resolve this, please?

Correct way to check whether $device switch is on

So, since there were no replies I went about the (very) hard way, and tried to determine what was wrong with pistons not resuming… And I found something in common - thy 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 unpreictable. See 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 cod 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 going through the entire loop once for every single device. Or is my understanding flawed?

Thanks in advance.