Piston is skipping statements. Energy Monitor


1) Give a description of the problem
I can not for the life of me figure out why this piston isn’t working. I need some webcore minions to help me with the webcore gremlins.

2) What is the expected behavior?
Set variable to power usage. After the variable stays under 30 for 1 minute, turn off TV.

3) What is happening/not happening?
It’s not recognising once the variable amount has gone below the threshold.

4) Post a Green Snapshot of the pistonimage

Even if I set the energy threshold much higher then the TV uses, the bottom IF still never runs.

(Nevermind the Gaming Room Light, I changed it to that just to test it, but the light keeps soldering on.)

This is the old ernegy saving piston I used to run and it worked, for the most part.

It was terribly laggy however and would often glitch and just the TV off for no reason.

Thanks very much to WCmore for helping me discover that I could save 13 lines of code by using local variables instead.


The problem is you have inserted a while loop and as long as the switch is on, you piston will be stuck in the while loop. You are looking for power to go low but even if it goes low, the piston always runs top to bottom so it will just jump back into the while loop and never get to turn the switch off. I think if you put the if statement first, it would work since it would turn off the switch first when that trigger is caught and then never enter the while loop since the switch would be off.

Subscription and Global Variable set problems

You mean like this?


Yes, did that work?


Sorry, I looked too quick. In you new if power <200 for 1 minute, I don’t see it turning the TV off (outlet 4). Not sure where white bulb 2 came from.


Wow that worked. I didn’t realize that was the logic of a while loop? A loop will basically cancel everything else in the piston? Because I also tried setting the 2 to async, but it just broke the entire piston.

The bulb was only for testing, So I could visually see when it turned something off, rather then turning the TV off my wife is using ATM.


Got it. Its not that the while loop cancels everything else, it is that every time a trigger occurs, the piston execution starts from the very top, not from where the trigger is. So, when the second trigger tried to interrupt the while loop, the execution started at the top and , since the outlet was still on, just re-entered the while loop. This is why you couldn’t get past it. By moving it first, the if is false when the power on gets triggered and move past the first if with no problem. Then when you want to turn it off, that check gets execute first in this new arrangement. Hope that makes sense. There was a post I was reading the other night that explains it a lot better. I will try to find it for you.


Here it is. I had it bookmarked:


That is a very informative read. Looking at the way I have set up the piston, do you see another/better way of doing what I want. I prefer a piston that when i click trace and test, I can see when things activate and run, but the while loops seems to make no trace data appear next to the top IF. So if the power does drop below 200, it never changes green so I know it has. I’ve been using webcore for a while now but I have still not been able to make a piston that works flawlessly. They always have a glitch/bug.


Oh actually it doesn’t appear to be working. Once it has done 1 while loop, it checks to see if it’s under the threshold. If it isn’t, it goes back to the while loop and never returns to check if it’s under the threshold. The power level has been about 160 for 2 minutes but it has not turned off the light.

Subscription and Global Variable set problems

It’s strange the if doesn’t appear to have a trigger even though you are using a trigger. I am not sure the interaction with @WCmore who knows way more than me but why are you using a variable and not just checking that TV’s power stays less than 200 directly? It would seem like you could eliminate the while loop completely. Does the TV power not get reported regularly enough?


Well, The way my power sockets work is they will only output a power usage as a decimal (say 12.74) but only when you ask it to. Hence the refresh. My sockets don’t output live data. Either way I would still need to run a repeating script to keep manually updating the power stat. Using (if variable below) or (if devicePower() below) doesn’t really make any difference. The only pro of using a variable to me is I can easily tie it into other things later such as tracking usage data.

I am using TP link HS110 power sockets, in case anyone knows how to change the update frequency of the power consumption.


I may have outdone myself though. I just threw this code together and it seems to work. I just kept doing trial and error (like I normally do) until I got the result I wanted.

It’s incredibly compact too which is a bonus! :smiley:


Excellent! Glad you figured it out. Sorry I wasn’t more help. Still learning myself.


I looked up the socket you are using and looks like it is a wifi socket. How did you connect that with SmartThings/webCoRE?


On GitHub, Davegut has created an API to link the 2 systems together.