Piston keeps running over and over again when Testing

I created a piston just to test whether I can successfully record a 'last activity' time on the even runs and report that last activity time on the odd run. I intend on using this function in other pistons.

However, I am very confused by what seems to be happening when I hit the test button. Instead of simply recording the last activity time or reporting it, the piston starts looping between both the cases. I initially tried using if statements and incrementing a run count variable by 1. And the variable kept increasing constantly.

Next I tried it with a switch/case statement and it is still looping through both conditions constantly.

How can I fix this so the code only runs when I hit test and stops after meeting only one of the conditions? And in the next 'test' run goes through the alternate condition and stops.

Are you calling this piston from somewhere else? Where does the $TestInt come from? Full logs and a Trace posted here would be helpful.

Currently I am just running it using the Test button.
The TestInt is a global variable I created.

Here is the full log after I hit test.

8/29/2023, 1:32:01 PM +911ms
+4ms	╔Starting piston... (v0.3.114.20230222_HE)
+49ms	║╔Subscribing to devices...
+72ms	║║subscriptions: no triggers, promoting conditions
+73ms	║║Subscribing to GuptaResidence.:211282cbc83ecab26b2601fbfdc5c1fa:.@TestInt...
+95ms	║╚Finished subscribing (64ms)
+101ms	║Subscribing to GuptaResidence.pistonID...(:b20c68e2f56a54f9e9d6851160015c98:)
+107ms	║Subscribing to GuptaResidence.oldpistonID...(:6d428654fc15d226ac6e01405bdbbaae:)
+115ms	║Evaluating switch with values [[i::211282cbc83ecab26b2601fbfdc5c1fa:.@TestInt, v:[t:integer, v:0, vn:@TestInt, vt:string]]]
+117ms	║Comparison (integer) 0 is (integer) 0 = true (1ms)
+133ms	╚Piston started (130ms)

Here's a screenshot of Trace:

What is happening is your piston has no triggers.

By default, in a piston with no triggers, webcore promotes conditions to triggers.

Your condition is @TestInt, ie every time the global value changes you are getting an event.

pausing the piston in the IDE will stop it...

See:

I'll check if treating the switch as a condition is correct, so far it looks correct as that could have been a device:attribute value

you can adjust the piston setting for subscriptions to set to never so it does not subscribe to anything.

I think you could also create a local integer variable i

set variable {i} = @TestInt

switch (i)

1 Like

Thanks for the tips! I had one more question about the variable being local, will its value from initial run persist the next time it runs? The only reason I made it global was I worried that a local variable will start with 0 each time the piston runs.

Local variables can be manipulated within your piston and will maintain any changes made during execution. But if you initialize the variable to a specific value in the Define section, then that value will be assigned to your variable each time the piston executes.

1 Like

What @Pantheon said

See: