Cycle Nighttime lights for home security


#1

I have only been at this for about a week, so I would love input to my first piston of substance.

I wanted to create the appearance of someone being home when either I am asleep or away from home. In addition, I wanted to avoid anything that appears like a pattern. So, I randomized a specified group of lights and randomized the time interval. I have a simulated motion sensor that aggregates all the motion sensors that I want to monitor. I exclude sensors like my bedroom. If there is no motion in the house between the hours specified in the “every” statement and it is dark out (not between sunrise and sunset), the lights will cycle on/off, randomly, one at a time. The time range in the “every” statement is wide enough to include all dark hours during the winter, but I put in the check for darkness so the light activity does not kick off too early during the longer days.

Like I said, I am a newbie so I appreciate any constructive criticism. It seems to work well. Is it as efficient as it could be?


#2

Here’s something similar.


#3

My best advice is to avoid the nested if/then/else statements. It only makes code more difficult to read and especially to troubleshoot when something isn’t working properly.


#4

Thanks for the feedback. Of course, this leads me to some questions.

  1. With the ‘every’ statement, I knew it wasn’t going to change the results, but I was thinking it was going to be more efficient. I am thinking that may be wrong. When I followed @infofiend 's example, it seemed a bit more efficient. This leads to my second question.

  2. How do I know it is more efficient? The only thing that I know to look at is the ‘Memory used:’. Is there anything else I should be looking at?

  3. Does the ‘Always subscribe’ do anything in this case? I have read some on it, but I can’t say I completely understand yet.

  4. @allrak - thanks for the feedback. Seems like good advice.


#5

Unless you’re dealing with a very large system, I really wouldn’t worry about efficiency. The general rule is that if it works for you, then the Piston is fine. You’ll pick up some tricks just by checking out the forum and by playing around with webCoRE. In your script, you have a few redundant conditions, but again, if it works…

Does the ‘Always subscribe’ do anything in this case?

I use it whenever I want to ensure that one or more conditions act as triggers – e.g., when I have an if statement that doesn’t contain a trigger.


#6

Consider that you don’t necessarily want it too random either. People do naturally have patterns.


#7

I was looking for something like this for a long time but then I realized our 3 cats are doing the trick perfectly:))))
Thanks for sharing…
looks great.


#8

Brilliant strategy for true randomness, LOL