Calculate percentage in expression for light dimmer level?


#1

1) Give a description of the problem
This piston is hopefully going to control some recessed lighting in my main living area. On overcast days, it’s nice to have a little supplemental lighting for the kids to play/draw/etc. I don’t want the lights to be on a set level for the duration of the day, but think it might be better to adjust them to the level of light needed over the course of the day.

My thought was to take a “sample” of lux readings. 1 reading every minute for 10 mins, and then average them and adjust the dimmer level if needed.
This piston would run while the lux reading is less than 3000 for example. With these two numbers (the smoothed lux reading - lets say 300lux, and the max lux setting - 3000lux), we should be able to calculate a percentage… (300/3000)*100 = 10%, and then to get the level to set the lights, we just say 100- ((300/3000)*100) = 90%

I’m trying to set the level of the dimmers using an expression: 100-((300/3000)*100) (should evaluate to 90).
What I’m finding though, is that the expression doesn’t evaluate.

2) What is the expected behavior?
Expect lights to dim to 90%

3) What is happening/not happening?
Expression will only evaluate to 0 or 100 and set the dimmer level as such.

4) Post a Green Snapshot of the piston
image

5) Attach any logs (From ST IDE and by turning logging level to Full)
Can provide logs if needed, but currently don’t have any…

Thanks in advance!!!


#2

Is it not evaluating in the the box where you write the expression, or is it not evaluating when the piston runs?

If the former, it is probably because the variables don’t have an assigned value yet. If the latter, I just tested something similar and was able to get it to work so I’d have to see a log to help further.


#3

Well, aside from the lights only turning on at 100%, how do I know what it’s evaluating while running?

If the logic is working, the variables should have value when this expression runs.

In my troubleshooting, i replaced the variables with the real numbers the variables should be, and still only seems to evaluate to 0 or 100. I’ll attach logs tomorrow. Thanks!


#4

Logs…

11/17/2017, 9:11:12 AM +711ms
+250ms ╔Stopping piston…
+377ms ╚Piston successfully stopped (131ms)
11/17/2017, 9:11:08 AM +367ms
+1ms ╔Received event [Can Lights].switch = off with a delay of 594ms
+171ms ║RunTime Analysis CS > 12ms > PS > 31ms > PE > 128ms > CE
+200ms ║Runtime (45112 bytes) successfully initialized in 31ms (v0.2.0fd.20171105) (198ms)
+201ms ║╔Execution stage started
+211ms ║║Comparison (string) :4247115e9ad4ebc8666f5d52da13142d: is_any_of (string) :b3537ca37abc9eac45e0cbe7faaac16d:,:4247115e9ad4ebc8666f5d52da13142d: = true (2ms)
+212ms ║║Condition #16 evaluated true (5ms)
+213ms ║║Condition group #1 evaluated true (state did not change) (7ms)
+218ms ║║Comparison (integer) 2 is_less_than (integer) 10 = true (1ms)
+220ms ║║Condition #4 evaluated true (5ms)
+224ms ║║Comparison (integer) 2 is_equal_to (integer) 0 = false (1ms)
+226ms ║║Condition #40 evaluated false (4ms)
+231ms ║║Calculating (dynamic) 2 + (integer) 1 >> (dynamic) 3
+233ms ║║Comparison (integer) 2 is_equal_to (dynamic) 3 = false (1ms)
+235ms ║║Cancelling condition #41’s schedules…
+235ms ║║Condition #41 evaluated false (9ms)
+236ms ║║Cancelling condition #39’s schedules…
+237ms ║║Condition group #39 evaluated false (state changed) (16ms)
+238ms ║║Cancelling condition #2’s schedules…
+239ms ║║Condition group #2 evaluated false (state changed) (24ms)
+244ms ║║Comparison (integer) 2 is_equal_to (integer) 10 = false (1ms)
+245ms ║║Condition #12 evaluated false (4ms)
+246ms ║║Condition group #5 evaluated false (state did not change) (5ms)
+249ms ║║Cancelling statement #23’s schedules…
+252ms ║║Executed virtual command wait (1ms)
+253ms ║║Requesting a wake up for Fri, Nov 17 2017 @ 9:11:28 AM EST (in 20.0s)
+258ms ║╚Execution stage complete. (58ms)
+259ms ║Setting up scheduled job for Fri, Nov 17 2017 @ 9:11:28 AM EST (in 19.995s)
+277ms ╚Event processed successfully (277ms)
11/17/2017, 9:10:54 AM +438ms
+1ms ╔Received event [Home].time = 1510927854997 with a delay of -559ms
+267ms ║RunTime Analysis CS > 43ms > PS > 43ms > PE > 180ms > CE
+281ms ║Runtime (45115 bytes) successfully initialized in 43ms (v0.2.0fd.20171105) (278ms)
+282ms ║╔Execution stage started
+335ms ║║Calculating (integer) 1 + (integer) 1 >> (integer) 2
+349ms ║║Executed virtual command setVariable (11ms)
+360ms ║║Comparison (string) :4247115e9ad4ebc8666f5d52da13142d: is_any_of (string) :b3537ca37abc9eac45e0cbe7faaac16d:,:4247115e9ad4ebc8666f5d52da13142d: = true (6ms)
+362ms ║║Condition #16 evaluated true (10ms)
+363ms ║║Condition group #1 evaluated true (state did not change) (12ms)
+370ms ║║Comparison (integer) 2 is_less_than (integer) 10 = true (1ms)
+371ms ║║Condition #4 evaluated true (5ms)
+380ms ║║Comparison (integer) 2 is_equal_to (integer) 0 = false (5ms)
+382ms ║║Condition #40 evaluated false (9ms)
+389ms ║║Calculating (dynamic) 1 + (integer) 1 >> (dynamic) 2
+392ms ║║Comparison (integer) 2 is_equal_to (dynamic) 2 = true (1ms)
+393ms ║║Condition #41 evaluated true (10ms)
+394ms ║║Condition group #39 evaluated true (state did not change) (21ms)
+395ms ║║Condition group #2 evaluated true (state did not change) (30ms)
+397ms ║║Cancelling statement #2’s schedules…
+411ms ║║Calculating (integer) 196 + (dynamic) 196 >> (integer) 392
+416ms ║║Executed virtual command setVariable (2ms)
+421ms ║║Executed virtual command setVariable (3ms)
+428ms ║║Comparison (integer) 2 is_equal_to (integer) 10 = false (1ms)
+430ms ║║Condition #12 evaluated false (6ms)
+431ms ║║Condition group #5 evaluated false (state did not change) (7ms)
+434ms ║║Cancelling statement #23’s schedules…
+438ms ║║Executed virtual command wait (1ms)
+439ms ║║Requesting a wake up for Fri, Nov 17 2017 @ 9:11:14 AM EST (in 20.0s)
+445ms ║╚Execution stage complete. (163ms)
+446ms ║Setting up scheduled job for Fri, Nov 17 2017 @ 9:11:14 AM EST (in 19.994s)
+465ms ╚Event processed successfully (465ms)
11/17/2017, 9:10:34 AM +449ms
+1ms ╔Received event [Home].time = 1510927835702 with a delay of -1254ms
+308ms ║RunTime Analysis CS > 22ms > PS > 64ms > PE > 222ms > CE
+325ms ║Runtime (45115 bytes) successfully initialized in 64ms (v0.2.0fd.20171105) (323ms)
+329ms ║╔Execution stage started
+407ms ║║Calculating (integer) 0 + (integer) 1 >> (integer) 1
+416ms ║║Executed virtual command setVariable (4ms)
+447ms ║║Comparison (string) :4247115e9ad4ebc8666f5d52da13142d: is_any_of (string) :b3537ca37abc9eac45e0cbe7faaac16d:,:4247115e9ad4ebc8666f5d52da13142d: = true (15ms)
+450ms ║║Condition #16 evaluated true (31ms)
+452ms ║║Condition group #1 evaluated true (state did not change) (33ms)
+460ms ║║Comparison (integer) 1 is_less_than (integer) 10 = true (2ms)
+463ms ║║Cancelling condition #4’s schedules…
+464ms ║║Condition #4 evaluated true (9ms)
+472ms ║║Comparison (integer) 1 is_equal_to (integer) 0 = false (3ms)
+474ms ║║Cancelling condition #40’s schedules…
+475ms ║║Condition #40 evaluated false (9ms)
+483ms ║║Calculating (dynamic) 0 + (integer) 1 >> (dynamic) 1
+487ms ║║Comparison (integer) 1 is_equal_to (dynamic) 1 = true (2ms)
+489ms ║║Condition #41 evaluated true (13ms)
+491ms ║║Condition group #39 evaluated true (state did not change) (24ms)
+492ms ║║Cancelling condition #2’s schedules…
+493ms ║║Condition group #2 evaluated true (state changed) (38ms)
+495ms ║║Cancelling statement #2’s schedules…
+508ms ║║Calculating (integer) 0 + (dynamic) 196 >> (integer) 196
+514ms ║║Executed virtual command setVariable (3ms)
+520ms ║║Executed virtual command setVariable (2ms)
+529ms ║║Comparison (integer) 1 is_equal_to (integer) 10 = false (2ms)
+532ms ║║Cancelling condition #12’s schedules…
+533ms ║║Condition #12 evaluated false (9ms)
+535ms ║║Cancelling condition #5’s schedules…
+536ms ║║Condition group #5 evaluated false (state changed) (12ms)
+541ms ║║Cancelling statement #23’s schedules…
+545ms ║║Executed virtual command wait (1ms)
+547ms ║║Requesting a wake up for Fri, Nov 17 2017 @ 9:10:54 AM EST (in 20.0s)
+555ms ║╚Execution stage complete. (228ms)
+557ms ║Setting up scheduled job for Fri, Nov 17 2017 @ 9:10:54 AM EST (in 19.992s)
+577ms ╚Event processed successfully (577ms)
11/17/2017, 9:10:14 AM +206ms
+2ms ╔Received event [Can Lights].switch = on with a delay of 500ms
+197ms ║RunTime Analysis CS > 16ms > PS > 58ms > PE > 123ms > CE
+233ms ║Runtime (45116 bytes) successfully initialized in 58ms (v0.2.0fd.20171105) (230ms)
+234ms ║╔Execution stage started
+245ms ║║Comparison (string) :4247115e9ad4ebc8666f5d52da13142d: is_any_of (string) :b3537ca37abc9eac45e0cbe7faaac16d:,:4247115e9ad4ebc8666f5d52da13142d: = true (3ms)
+246ms ║║Condition #16 evaluated true (6ms)
+247ms ║║Condition group #1 evaluated true (state did not change) (7ms)
+253ms ║║Comparison (integer) 10 is_less_than (integer) 10 = false (2ms)
+254ms ║║Cancelling condition #4’s schedules…
+255ms ║║Condition #4 evaluated false (6ms)
+256ms ║║Cancelling condition #2’s schedules…
+257ms ║║Condition group #2 evaluated false (state changed) (8ms)
+262ms ║║Comparison (integer) 10 is_equal_to (integer) 10 = true (1ms)
+264ms ║║Cancelling condition #12’s schedules…
+265ms ║║Condition #12 evaluated true (5ms)
+266ms ║║Cancelling condition #5’s schedules…
+266ms ║║Condition group #5 evaluated true (state changed) (7ms)
+269ms ║║Cancelling statement #6’s schedules…
+279ms ║║Calculating (integer) 196 + (dynamic) 196 >> (integer) 392
+284ms ║║Executed virtual command setVariable (3ms)
+289ms ║║Calculating (integer) 392 / (integer) 10 >> (integer) 39.2
+294ms ║║Executed virtual command setVariable (3ms)
+300ms ║║Comparison (integer) 39 is_less_than (integer) 3000 = true (2ms)
+301ms ║║Condition #21 evaluated true (4ms)
+312ms ║║Comparison (dynamic) 196 is_less_than (integer) 3000 = true (2ms)
+313ms ║║Condition #22 evaluated true (11ms)
+314ms ║║Condition group #17 evaluated true (state did not change) (18ms)
+322ms ║║Comparison (enum) on is (string) on = true (2ms)
+323ms ║║Cancelling condition #30’s schedules…
+324ms ║║Condition #30 evaluated true (8ms)
+325ms ║║Cancelling condition #26’s schedules…
+326ms ║║Condition group #26 evaluated true (state changed) (9ms)
+328ms ║║Cancelling statement #18’s schedules…
+340ms ║║Calculating (integer) 39 / (integer) 3000 >> (integer) 0.013
+342ms ║║Calculating (integer) 0 * (integer) 100 >> (integer) 0
+346ms ║║Calculating (integer) 100 - (integer) 0 >> (integer) 100
+356ms ║║Skipped execution of physical command [Can Lights].setLevel([99]) because it would make no change to the device. (6ms)
+361ms ║║Executed physical command [Can Lights].setLevel([100], [delay: 1000]) (4ms)
+367ms ║║Executed physical command [Can Lights].setLevel([100], [delay: 1099]) (4ms)
+368ms ║║Executed virtual command [Can Lights].fadeLevel (19ms)
+373ms ║║Waiting for 1100ms
+1481ms ║║Executed virtual command [Can Lights].setVariable (3ms)
+1487ms ║║Executed virtual command [Can Lights].setVariable (3ms)
+1491ms ║║Cancelling statement #23’s schedules…
+1494ms ║║Executed virtual command wait (1ms)
+1495ms ║║Requesting a wake up for Fri, Nov 17 2017 @ 9:10:35 AM EST (in 20.0s)
+1501ms ║╚Execution stage complete. (1266ms)
+1502ms ║Setting up scheduled job for Fri, Nov 17 2017 @ 9:10:35 AM EST (in 19.994s)
+1522ms ╚Event processed successfully (1521ms)
11/17/2017, 9:10:10 AM +538ms
+2ms ╔Received event [Can Lights].switch = on with a delay of 162ms
+698ms ║RunTime Analysis CS > 21ms > PS > 501ms > PE > 176ms > CE
+707ms ║Piston waited at a semaphore for 260ms
+710ms ║Runtime (45184 bytes) successfully initialized in 501ms (v0.2.0fd.20171105) (707ms)
+711ms ║╔Execution stage started
+721ms ║║Comparison (string) :4247115e9ad4ebc8666f5d52da13142d: is_any_of (string) :b3537ca37abc9eac45e0cbe7faaac16d:,:4247115e9ad4ebc8666f5d52da13142d: = true (2ms)
+723ms ║║Condition #16 evaluated true (6ms)
+724ms ║║Condition group #1 evaluated true (state did not change) (7ms)
+729ms ║║Comparison (integer) 0 is_less_than (integer) 10 = true (1ms)
+731ms ║║Cancelling condition #4’s schedules…
+731ms ║║Condition #4 evaluated true (5ms)
+736ms ║║Comparison (integer) 0 is_equal_to (integer) 0 = true (1ms)
+738ms ║║Cancelling condition #40’s schedules…
+739ms ║║Condition #40 evaluated true (5ms)
+739ms ║║Condition group #39 evaluated true (state did not change) (7ms)
+740ms ║║Cancelling condition #2’s schedules…
+741ms ║║Condition group #2 evaluated true (state changed) (15ms)
+742ms ║║Cancelling statement #2’s schedules…
+755ms ║║Calculating (integer) 0 + (dynamic) 196 >> (integer) 196
+768ms ║║Executed virtual command setVariable (11ms)
+777ms ║║Executed virtual command setVariable (7ms)
+783ms ║║Comparison (integer) 0 is_equal_to (integer) 10 = false (1ms)
+784ms ║║Cancelling condition #12’s schedules…
+785ms ║║Condition #12 evaluated false (5ms)
+786ms ║║Cancelling condition #5’s schedules…
+787ms ║║Condition group #5 evaluated false (state changed) (8ms)
+790ms ║║Cancelling statement #23’s schedules…
+793ms ║║Executed virtual command wait (1ms)
+794ms ║║Requesting a wake up for Fri, Nov 17 2017 @ 9:10:31 AM EST (in 20.0s)
+799ms ║╚Execution stage complete. (89ms)
+801ms ║Setting up scheduled job for Fri, Nov 17 2017 @ 9:10:31 AM EST (in 19.995s)
+827ms ╚Event processed successfully (827ms)
11/17/2017, 9:10:08 AM +539ms
+1ms ╔Received event [Home].time = 1510927809144 with a delay of -606ms
+243ms ║RunTime Analysis CS > 22ms > PS > 51ms > PE > 170ms > CE
+256ms ║Runtime (45112 bytes) successfully initialized in 51ms (v0.2.0fd.20171105) (254ms)
+258ms ║╔Execution stage started
+326ms ║║Calculating (integer) 9 + (integer) 1 >> (integer) 10
+352ms ║║Executed virtual command setVariable (21ms)
+364ms ║║Comparison (string) :4247115e9ad4ebc8666f5d52da13142d: is_any_of (string) :b3537ca37abc9eac45e0cbe7faaac16d:,:4247115e9ad4ebc8666f5d52da13142d: = true (4ms)
+366ms ║║Condition #16 evaluated true (11ms)
+368ms ║║Condition group #1 evaluated true (state did not change) (13ms)
+386ms ║║Comparison (integer) 10 is_less_than (integer) 10 = false (10ms)
+390ms ║║Cancelling condition #4’s schedules…
+392ms ║║Condition #4 evaluated false (19ms)
+394ms ║║Cancelling condition #2’s schedules…
+395ms ║║Condition group #2 evaluated false (state changed) (23ms)
+412ms ║║Comparison (integer) 10 is_equal_to (integer) 10 = true (9ms)
+416ms ║║Cancelling condition #12’s schedules…
+417ms ║║Condition #12 evaluated true (18ms)
+419ms ║║Cancelling condition #5’s schedules…
+420ms ║║Condition group #5 evaluated true (state changed) (21ms)
+426ms ║║Cancelling statement #6’s schedules…
+444ms ║║Calculating (integer) 1960 + (dynamic) 196 >> (integer) 2156
+451ms ║║Executed virtual command setVariable (4ms)
+461ms ║║Calculating (integer) 2156 / (integer) 10 >> (integer) 215.6
+468ms ║║Executed virtual command setVariable (4ms)
+478ms ║║Comparison (integer) 215 is_less_than (integer) 3000 = true (2ms)
+480ms ║║Condition #21 evaluated true (7ms)
+491ms ║║Comparison (dynamic) 196 is_less_than (integer) 3000 = true (2ms)
+493ms ║║Condition #22 evaluated true (12ms)
+495ms ║║Condition group #17 evaluated true (state did not change) (22ms)
+511ms ║║Comparison (enum) off is (string) on = false (3ms)
+513ms ║║Cancelling condition #30’s schedules…
+514ms ║║Condition #30 evaluated false (16ms)
+516ms ║║Cancelling condition #26’s schedules…
+676ms ║║Condition group #26 evaluated false (state changed) (178ms)
+679ms ║║Cancelling statement #27’s schedules…
+716ms ║║Executed physical command [Can Lights].on() (31ms)
+719ms ║║Executed [Can Lights].on (37ms)
+739ms ║║Calculating (integer) 215 / (integer) 3000 >> (integer) 0.0716666667
+747ms ║║Calculating (integer) 0 * (integer) 100 >> (integer) 0
+753ms ║║Calculating (integer) 100 - (integer) 0 >> (integer) 100
+828ms ║║Executed physical command [Can Lights].setLevel([0]) (49ms)
+835ms ║║Executed physical command [Can Lights].setLevel([50], [delay: 500]) (5ms)
+843ms ║║Executed physical command [Can Lights].setLevel([100], [delay: 1000]) (5ms)
+849ms ║║Executed physical command [Can Lights].setLevel([100], [delay: 1099]) (4ms)
+852ms ║║Executed virtual command [Can Lights].fadeLevel (92ms)
+854ms ║║Waiting for 1100ms
+1989ms ║║Executed virtual command [Can Lights].setVariable (4ms)
+2001ms ║║Executed virtual command [Can Lights].setVariable (4ms)
+2006ms ║║Cancelling statement #23’s schedules…
+2010ms ║║Executed virtual command wait (1ms)
+2012ms ║║Requesting a wake up for Fri, Nov 17 2017 @ 9:10:30 AM EST (in 20.0s)
+2020ms ║╚Execution stage complete. (1763ms)
+2022ms ║Setting up scheduled job for Fri, Nov 17 2017 @ 9:10:30 AM EST (in 19.992s)
+2041ms ╚Event processed successfully (2042ms)
11/17/2017, 9:09:48 AM +491ms
+6ms ╔Received event [Home].time = 1510927789154 with a delay of -663ms
+438ms ║RunTime Analysis CS > 25ms > PS > 47ms > PE > 360ms > CE
+455ms ║Runtime (45112 bytes) successfully initialized in 47ms (v0.2.0fd.20171105) (445ms)
+460ms ║╔Execution stage started
+526ms ║║Calculating (integer) 8 + (integer) 1 >> (integer) 9
+543ms ║║Executed virtual command setVariable (10ms)
+554ms ║║Comparison (string) :4247115e9ad4ebc8666f5d52da13142d: is_any_of (string) :b3537ca37abc9eac45e0cbe7faaac16d:,:4247115e9ad4ebc8666f5d52da13142d: = true (5ms)
+555ms ║║Condition #16 evaluated true (10ms)
+557ms ║║Condition group #1 evaluated true (state did not change) (11ms)
+568ms ║║Comparison (integer) 9 is_less_than (integer) 10 = true (5ms)
+570ms ║║Condition #4 evaluated true (9ms)
+579ms ║║Comparison (integer) 9 is_equal_to (integer) 0 = false (5ms)
+581ms ║║Condition #40 evaluated false (9ms)
+587ms ║║Calculating (dynamic) 8 + (integer) 1 >> (dynamic) 9
+590ms ║║Comparison (integer) 9 is_equal_to (dynamic) 9 = true (1ms)
+591ms ║║Condition #41 evaluated true (9ms)
+592ms ║║Condition group #39 evaluated true (state did not change) (20ms)
+593ms ║║Condition group #2 evaluated true (state did not change) (34ms)
+595ms ║║Cancelling statement #2’s schedules…
+611ms ║║Calculating (integer) 1764 + (dynamic) 196 >> (integer) 1960
+617ms ║║Executed virtual command setVariable (3ms)
+622ms ║║Executed virtual command setVariable (3ms)
+630ms ║║Comparison (integer) 9 is_equal_to (integer) 10 = false (2ms)
+632ms ║║Condition #12 evaluated false (6ms)
+633ms ║║Condition group #5 evaluated false (state did not change) (7ms)
+637ms ║║Cancelling statement #23’s schedules…
+646ms ║║Executed virtual command wait (4ms)
+650ms ║║Requesting a wake up for Fri, Nov 17 2017 @ 9:10:09 AM EST (in 20.0s)
+662ms ║╚Execution stage complete. (205ms)
+665ms ║Setting up scheduled job for Fri, Nov 17 2017 @ 9:10:09 AM EST (in 19.991s)
+730ms ╚Event processed successfully (730ms)


#5

Here’s the problem! Your variables are set as integers, so when the math evaluates Average/Luxmax and gets .0716666 it changes that to zero. That makes the outcome of your calculation (100-Average/Luxmax*100) = 100-0, or 100, so you’re only seeing the lights at full brightness.

+739ms ║║Calculating (integer) 215 / (integer) 3000 >> (integer) 0.0716666667
+747ms ║║Calculating (integer) 0 * (integer) 100 >> (integer) 0
+753ms ║║Calculating (integer) 100 - (integer) 0 >> (integer) 100

Set your variables to decimal and try it again - but that should fix it since you’re piston is running through all the steps you want it to, when you want it to.


#6

That’s it! Thanks!