Power meter conditions on a Power plug


#1

I have a xiaomi power plug connected to my laptop that measures the power. What i wanted to do is when the laptop is turn off for 30 min (for example), the plug turns off also.
I have two pistons, one for when the plug is on (> 3W) and another one for when i turn off the laptop (< 3W). What am i doing wrong?



#2

Try this bare piston to see if it does what you want… maybe adjust the time for testing. If it works then just expand it.


#3

I haven’t looked closely at the piston as a whole but you seem to be looping on the state of a global variable but not changing that variable in the loop or doing any waits to allow changes to be picked up.

Global variables are read into a cache when a piston instance starts up and any changes are written when the instance exits. If you wait for more than five seconds, or request a shorter wait that would leave the piston with less than ten seconds of execution time when it ends, the piston exits and starts up again using a timer.

So I think you need a wait in that loop.


#4

Thank you very much it worked like a charm. I don’t know why but when i tried the “remains above” in the “only when” condition it didn’t work.
Sorry for the late reply but i wasn’t able to try it before.
By the way is there any difference between “remains above” and “stays less”? Since you used both in different cases.


#5

Hum i see. I thought that in each iteration of the loop it would check the global variable. And if I change it in other piston it would detect the change.
So if i create a loop that lasts 5 min it only would know the variable state when the loop was started?


#6

You would intuitively assume the global variable would be read every time, but actually the piston always uses its own local copy.

When you do a wait (or something similar like a fade) there are two ways a piston can handle it. It can go into a tight loop of code for the duration of the wait, or it can set a timer to wake it up when the wait is over and exit. In the latter case when the timer fires the piston it fast forwards to the end of the wait and continues with what it was doing. If the wait is for less than five seconds, and if the piston would still have at least ten seconds of execution time available after it, then it takes the first option. Otherwise the second. So for a loop to last five minutes the piston must have exited and started up again at least once. That would mean it has written out any changes to the global variables on exit, and read the latest values when it started up again.