Add a delay

Hi everyone! I was wondering if there was a way to add a delay to my program. This is the specific way that I want to use it:

I have made a simple app that turns on a light when motion is detected. I want to add the following features.

  1. Have the user specify a start and end time for this program to work.
    EX: You wouldn’t want lights to turn on at 12:00 p.m., since it’s daylight, so you might say 6 pm -11 pm. The dark hours until you fall asleep.
  2. Leave the lights on for x minutes even after motion has stopped
    EX: If I’m reading on my couch, I want move very much. Instead of having the lights then turn off, I would like them to stay on for 15 minutes after motion stops, so that I don’t have to be constantly moving as I read.

Thanks in advance for anyone that helps :smiley:

For 1) you can see this simple example: https://gist.github.com/jimmyjames/8cde9ae94e0bae9b2db8

For 2) there are a few ways to do this, but one of them is to subscribe to the “inactive” motion state, and then schedule a method using runIn() to execute in the X minutes specified by the user. Then in that scheduled method, see if there has been motion detected within the last X minutes. You can see this example: https://gist.github.com/jimmyjames/7a36e48a0c6dcc280ee3

Note - I have not thoroughly tested these examples, so if you run into/discover issues I apologize (but let us know the issue/fix :slight_smile: )