Setting up light dimming with smartthings button


#1

EDIT: so yeah, i can’t get webcore to see the ‘held’ state even though the ST IDE sees it as ‘held’ and the ST app see it as ‘held’ but the log always says ‘received push’ so I am thinking that is the source of the problem but I have no idea how to fix it.

1) Give a description of the problem
The on/off functionality works just fine in my piston, and I would assume that the double press state is working as well as I just use it to flip a UP/DOWN state for dimming. The default at this point is just to ensure that the variable dimDirection gets a value since I did not assign a value at the beginning.

When I hold the button down I get no change in lighting, i have changed the + and - values to 20 to make sure it wasn’t just switching back and forth in an barely visible way and nothing changed. I am not sure why it doesn’t work because the code is fairly straight forward, I am thinking that it has something to do with the dimDirection variable not being initialized with a value but if I read the docs correctly you only want to set the variable initially if you want it to change back to that value on each run through the piston and I want it to kind of ‘float’ if that makes sense, to carry over the last value from the last run, maybe i don’t understand how to do that correctly.

2) What is the expected behaviour?
For the light to increase in brightness until getting to 100% then setting the dimDirection variable to DOWN so the next held state will decrease the light to 20% or less then switch back.

3) What is happening/not happening?
light switches on/off with ‘pushed’, not sure if double is working, and held is definitely not working.

The logs are such that I do a single press which turns the light on, I do a double press which seems to set the dimDirection variable for me but when I ‘hold’ the button it seems to just execute the single press stuff even though I hold it for at least 2 seconds. I will keep trying to find out whats going on but I need to know if that variable getting set in the logs will stay that way as long as the piston is running or is it getting reset after the piston runs through. If it is being reset do I need to make a global variable or how do I get one that keeps its value? Also why would the ‘held’ state activate the pushed state, they are both natively handled, I didn’t have to make my own device handler or anything, maybe thats needed?

**4) Post a Green Snapshot of the piston!

5) Attach logs after turning logging level to Full
9/8/2019, 4:51:19 PM +363ms
+1ms ╔Received event [Button].button = held with a delay of 104ms
+64ms ║RunTime Analysis CS > 13ms > PS > 31ms > PE > 20ms > CE
+67ms ║Runtime (45871 bytes) successfully initialized in 31ms (v0.3.10f.20190822) (66ms)
+68ms ║╔Execution stage started
+77ms ║║Comparison (enum) held gets (string) pushed = false (0ms)
+78ms ║║Condition #5 evaluated false (4ms)
+79ms ║║Condition group #2 evaluated false (state did not change) (6ms)
+83ms ║║Comparison (enum) held gets (string) double = false (1ms)
+85ms ║║Cancelling condition #6’s schedules…
+85ms ║║Condition #6 evaluated false (5ms)
+86ms ║║Cancelling condition #3’s schedules…
+87ms ║║Condition group #3 evaluated false (state changed) (7ms)
+92ms ║║Comparison (enum) held gets (string) held = true (1ms)
+93ms ║║Condition #22 evaluated true (5ms)
+94ms ║║Condition group #4 evaluated true (state did not change) (6ms)
+97ms ║║Evaluating switch with values [[i:23:null:0, v:[t:string, v:, vt:string]]]
+100ms ║║Comparison (string) is (string) UP = false (1ms)
+103ms ║║Comparison (string) is (string) DOWN = false (1ms)
+106ms ║╚Execution stage complete. (38ms)
+107ms ╚Event processed successfully (107ms)
9/8/2019, 4:51:15 PM +311ms
+2ms ╔Received event [Button].button = double with a delay of 104ms
+93ms ║RunTime Analysis CS > 21ms > PS > 44ms > PE > 27ms > CE
+181ms ║Runtime (45873 bytes) successfully initialized in 44ms (v0.3.10f.20190822) (177ms)
+182ms ║╔Execution stage started
+192ms ║║Comparison (enum) double gets (string) pushed = false (1ms)
+194ms ║║Cancelling condition #5’s schedules…
+195ms ║║Condition #5 evaluated false (6ms)
+196ms ║║Cancelling condition #2’s schedules…
+197ms ║║Condition group #2 evaluated false (state changed) (9ms)
+201ms ║║Comparison (enum) double gets (string) double = true (1ms)
+203ms ║║Cancelling condition #6’s schedules…
+204ms ║║Condition #6 evaluated true (6ms)
+205ms ║║Cancelling condition #3’s schedules…
+206ms ║║Condition group #3 evaluated true (state changed) (8ms)
+209ms ║║Evaluating switch with values [[i:7:null:0, v:[t:string, v:, vt:string]]]
+212ms ║║Comparison (string) is (string) UP = false (2ms)
+215ms ║║Comparison (string) is (string) DOWN = false (2ms)
+217ms ║║Cancelling statement #59’s schedules…
+221ms ║║Executed virtual command setVariable (1ms)
+224ms ║╚Execution stage complete. (42ms)
+225ms ╚Event processed successfully (225ms)
9/8/2019, 4:51:10 PM +492ms
+2ms ╔Received event [Button].button = pushed with a delay of 122ms
+86ms ║RunTime Analysis CS > 21ms > PS > 44ms > PE > 22ms > CE
+89ms ║Runtime (45872 bytes) successfully initialized in 44ms (v0.3.10f.20190822) (86ms)
+90ms ║╔Execution stage started
+100ms ║║Comparison (enum) pushed gets (string) pushed = true (3ms)
+102ms ║║Condition #5 evaluated true (6ms)
+102ms ║║Condition group #2 evaluated true (state did not change) (8ms)
+109ms ║║Evaluating switch with values [[i::6d6c440c3e060bb0c498be099189f911::switch, v:[t:enum, v:off, d::6d6c440c3e060bb0c498be099189f911:, a:switch, i:null, x:false, vt:string, p:a]]]
+112ms ║║Comparison (enum) off is (string) on = false (1ms)
+114ms ║║Cancelling statement #33’s schedules…
+132ms ║║Executed physical command [Smart Bulb 2].on() (15ms)
+133ms ║║Executed [Smart Bulb 2].on (17ms)
+135ms ║╚Execution stage complete. (46ms)
+136ms ╚Event processed successfully (136ms)


#2

Double is probably working, held might be. You need to use a string compare for the buttons to compare the values for held. You don’t need the “on events from”, either.

Here’s a skeleton for the three triggers for a button.

The variables will stay set in between runs EXCEPT if where you declare them at the top you set a value *which it looks like you are doing). Create them with “no value set”, and set the values during the piston run and it’ll keep the value in between runs.


#3

I will give this a try but the only reason i have an on event statement is that the pushed (or anything) would not run without it, it was not in the original code and my light would not turn on or off and then i put it in and it worked just fine. Not sure why that is


#4

ok, so you were spot on with needing to change pushed and held to strings, the logs now show that all 3 states are being recognized but the way I understood ‘held’ is that it will repeat its code every second that it is being held and i held it for at least 4 or 5 seconds but it only ran once, what am I missing here?

here are new logs

9/8/2019, 8:15:45 PM +35ms
+2ms ╔Received event [Button].button = held with a delay of 425ms
+104ms ║RunTime Analysis CS > 21ms > PS > 55ms > PE > 28ms > CE
+107ms ║Runtime (45358 bytes) successfully initialized in 55ms (v0.3.10f.20190822) (104ms)
+108ms ║╔Execution stage started
+117ms ║║Comparison (enum) held gets (string) pushed = false (1ms)
+118ms ║║Condition #5 evaluated false (4ms)
+119ms ║║Condition group #2 evaluated false (state did not change) (6ms)
+123ms ║║Comparison (enum) held gets (string) double = false (0ms)
+125ms ║║Cancelling condition #6's schedules...
+126ms ║║Condition #6 evaluated false (6ms)
+127ms ║║Cancelling condition #3's schedules...
+128ms ║║Condition group #3 evaluated false (state changed) (8ms)
+135ms ║║Comparison (enum) held gets (string) held = true (4ms)
+136ms ║║Cancelling condition #22's schedules...
+137ms ║║Condition #22 evaluated true (8ms)
+138ms ║║Cancelling condition #4's schedules...
+139ms ║║Condition group #4 evaluated true (state changed) (11ms)
+142ms ║║Evaluating switch with values [[i:23:null:0, v:[t:string, v:UP, vt:string]]]
+145ms ║║Comparison (string) UP is (string) UP = true (1ms)
+147ms ║║Cancelling statement #40's schedules...
+159ms ║║Executed virtual command setVariable (4ms)
+164ms ║║Calculating (dynamic) 50 + (integer) 5 >> (dynamic) 55
+170ms ║║Executed virtual command setVariable (3ms)
+173ms ║║Cancelling statement #43's schedules...
+190ms ║║Executed physical command [Smart Bulb 2].setLevel([55]) (13ms)
+192ms ║║Executed [Smart Bulb 2].setLevel (16ms)
+201ms ║║Comparison (integer) 50 is_equal_to (integer) 100 = false (2ms)
+203ms ║║Condition #46 evaluated false (7ms)
+204ms ║║Condition group #45 evaluated false (state did not change) (8ms)
+207ms ║╚Execution stage complete. (99ms)
+208ms ╚Event processed successfully (208ms)
9/8/2019, 8:15:21 PM +782ms
+1ms ╔Received event [Button].button = double with a delay of 83ms
+78ms ║RunTime Analysis CS > 18ms > PS > 39ms > PE > 22ms > CE
+81ms ║Runtime (45356 bytes) successfully initialized in 39ms (v0.3.10f.20190822) (79ms)
+82ms ║╔Execution stage started
+90ms ║║Comparison (enum) double gets (string) pushed = false (1ms)
+92ms ║║Cancelling condition #5's schedules...
+92ms ║║Condition #5 evaluated false (5ms)
+93ms ║║Cancelling condition #2's schedules...
+94ms ║║Condition group #2 evaluated false (state changed) (7ms)
+101ms ║║Comparison (enum) double gets (string) double = true (3ms)
+102ms ║║Cancelling condition #6's schedules...
+103ms ║║Condition #6 evaluated true (8ms)
+104ms ║║Cancelling condition #3's schedules...
+105ms ║║Condition group #3 evaluated true (state changed) (9ms)
+107ms ║║Evaluating switch with values [[i:7:null:0, v:[t:string, v:, vt:string]]]
+110ms ║║Comparison (string) is (string) UP = false (2ms)
+113ms ║║Comparison (string) is (string) DOWN = false (1ms)
+115ms ║║Cancelling statement #59's schedules...
+120ms ║║Executed virtual command setVariable (3ms)
+123ms ║╚Execution stage complete. (42ms)
+124ms ╚Event processed successfully (125ms)
9/8/2019, 8:15:09 PM +622ms
+1ms ╔Received event [Button].button = pushed with a delay of 100ms
+66ms ║RunTime Analysis CS > 14ms > PS > 28ms > PE > 24ms > CE
+69ms ║Runtime (45359 bytes) successfully initialized in 28ms (v0.3.10f.20190822) (67ms)
+70ms ║╔Execution stage started
+81ms ║║Comparison (enum) pushed gets (string) pushed = true (4ms)
+82ms ║║Cancelling condition #5's schedules...
+83ms ║║Condition #5 evaluated true (8ms)
+84ms ║║Cancelling condition #2's schedules...
+85ms ║║Condition group #2 evaluated true (state changed) (10ms)
+92ms ║║Evaluating switch with values [[i::6d6c440c3e060bb0c498be099189f911::switch, v:[t:enum, v:off, d::6d6c440c3e060bb0c498be099189f911:, a:switch, i:null, x:false, vt:string, p:a]]]
+95ms ║║Comparison (enum) off is (string) on = false (1ms)
+98ms ║║Cancelling statement #33's schedules...
+110ms ║║Executed physical command [Smart Bulb 2].on() (9ms)
+110ms ║║Executed [Smart Bulb 2].on (11ms)
+113ms ║╚Execution stage complete. (44ms)
+114ms ╚Event processed successfully (114ms

EDIT: I fully appreciate that the code could be less complicated than it currently is, once i get a better understanding of how held works I will simplify the code quite a bit, this was just a first attempt to do a piston from scratch, its been awhile since I wrote any code and the only language I really know is C++ so I was a lot more verbose than I needed to be. According to the material I read, if you keep holding the button down the ‘held’ state gets sent every 1 seconds, this does not seem to be the case with mine however. Ideas? I’ll keep looking for docs.


#5

Every “held” that I have worked with fires once per long press


#6

hmm, ok well then I think switching my pushed for held would be more reasonable then


#7

Normally, I use “held” commands for the ultra rare commands…
They are the slowest so generally avoided with frequently used commands