How to improve my first piston, setting light values based on time


#1

1) Give a description of the problem
I’ve written my first piston, and I am SO PLEASED WITH IT. WebCore is amazing and I’m so grateful I don’t have to much about with contrived virtual switches, scenes, and automation combinations in order to get something to work. However, there are a couple of things I’d like to improve in the code to make it more efficient, and I’m not sure how to do that. Is anyone able to offer me tips on these points?

  1. There’s duplication where I work out what value the dimmers should be set to. the four Set Variable lines at 27 and 49. Can I create a function which performs this calculation and just call on it when required instead? How can I do that?

  2. This is triggered on a timer but also if a device’s switch changes. But if a device’s switch changes the script iterates through all the devices and sets the dimmers on all of them. This works fine as I intend all the lights in the house to be set to the same value, but it’s inefficient iterating through all the devices. Is there a way to change the dimmer only on the device which triggered the event?

**4) Post a Green Snapshot of the piston![image|45x37]

5) Attach logs after turning logging level to Full
28/10/2020, 19:57:55 +418ms
+1ms ╔Received event [Home].test = 1603915075417 with a delay of 0ms
+132ms ║RunTime Analysis CS > 18ms > PS > 76ms > PE > 38ms > CE
+139ms ║Runtime (53825 bytes) successfully initialized in 76ms (v0.3.110.20191009) (136ms)
+140ms ║╔Execution stage started
+176ms ║╚Execution stage complete. (37ms)
+179ms ║Setting up scheduled job for Wed, Oct 28 2020 @ 8:07:51 PM GMT (in 596.403s)
+190ms ╚Event processed successfully (190ms)


#2
  1. Any variables that are defined with an initial value are initialised to that value every time the piston starts up, so you could set the variables in the ‘define’ section.

  2. The variable $currentEventDevice will be the device that triggered the event. It has companion variables $currentEventAttribute and $currentEventValue. If the piston isn’t triggered by a device, they will have still have meaningful values.


#3

Thanks so much for your help, and for the additional information. That’s really helpful.

The piston is now very sleek:


#4

I’ve now managed to break it!

I’ve put in a system where a virtual switch can prevent this from running, if it’s on, and where the virtual switch will be switched off automatically after a set amount of time.

However, I’m getting the error:

An error occurred while executing the event: java.lang.NullPointerException: Cannot get property ‘v’ on null object|

|+1ms|╔Received event [Home].time = 1603978754170 with a delay of -115ms|
|—|---|
|+151ms|║RunTime Analysis CS > 16ms > PS > 69ms > PE > 66ms > CE|
|+155ms|║Runtime (56966 bytes) successfully initialized in 69ms (v0.3.110.20191009) (153ms)|
|+156ms|║╔Execution stage started|
|+175ms|║║Evaluating switch with values [[i::ea49973dd64479a25bb39609f5b89d23::switch, v:[t:enum, v:off, d::ea49973dd64479a25bb39609f5b89d23:, a:switch, i:null, x:false, vt:string, p:a]]]|
|+178ms|║║Comparison (enum) off is (string) on = false (2ms)|
|+183ms|║║Comparison (enum) off is (string) off = true (1ms)|
|+195ms|║║Comparison (boolean) false is (boolean) true = false (1ms)|
|+197ms|║║Cancelling condition #64’s schedules…|
|+198ms|║║Condition #64 evaluated false (6ms)|
|+199ms|║║Condition group #43 evaluated false (state did not change) (7ms)|
|+222ms|║║Comparison (enum) off is (string) on = false (2ms)|
|+224ms|║║Cancelling condition #22’s schedules…|
|+225ms|║║Condition #22 evaluated false (14ms)|
|+226ms|║║Condition group #21 evaluated false (state did not change) (17ms)|
|+248ms|║║Comparison (enum) off is (string) on = false (1ms)|
|+249ms|║║Condition #22 evaluated false (13ms)|
|+251ms|║║Condition group #21 evaluated false (state did not change) (14ms)|
|+273ms|║║Comparison (enum) on is (string) on = true (1ms)|
|+274ms|║║Cancelling condition #22’s schedules…|
|+275ms|║║Condition #22 evaluated true (14ms)|
|+278ms|║║An error occurred while executing the event: java.lang.NullPointerException: Cannot get property ‘v’ on null object|
|+280ms|║╚Execution stage complete. (125ms)|
|+281ms|║Setting up scheduled job for Thu, Oct 29 2020 @ 1:39:44 PM GMT (in 29.834s)|
|+288ms|╚Event processed successfully (288ms)|

It seems to be to do with the variable @manualLighting, which I’ve set in the global variables. What have I done wrong?


#5

Variables are case sensitive, line 81 is @ManualLighting, the others are @manualLighting. I am assuming that it is as it is at the end of the log. If you turn on tracing it might help locate the issue faster.


#6

That’s it! Thank you so much. Well spotted.