CoRE - Resetting timers & variables etc

core

(Jeff Johnson) #1

Hi, if I’m going about posting this wrong, I apologize in advance - I’ve not really posted on here. I’ll happily post this elsewhere/differently if need be.

Anyway, here goes…

The stuff:
I’ve a closet in the master bedroom with a smart bulb in it. (It has a manual switch as well, but I’ll deal with that at a later date). The door has a contact sensor. The bedroom has a motion sensor in it as well as some other automated stuff and whatnot, but shouldn’t be pertinent to this - just the motion sensor.

The problem:
I’m the only one that closes doors and turns off lights for some reason… So I thought I’d get clever (probably my first mistake) and come up with some automation for this. Obviously when the door opens - light on. Door closes, light off. But I want the light to turn off anyway after ~30 min. That part is easy enough. The tricky part is this, if the door is still open, and the light is off, and motion is detected, the light should come back on. Ok, this is the tricky part… how do I reset the timer. :slight_smile: If the light is on, and has 5 min left, and motion gets detected, I want it to reset the timer, but I didn’t see a way to ‘reset’ ‘stays on for:’. Here’s the pseudo-code that I think represents what I’m trying to do (just ignore my smattering of code syntax…):

if(door changes to open)
{
    do
    {
        if (motion detected)
        {
            if(light == on)
            {
                //some sort of way to reset the light timer
            }
            else
            {
                turn on light    
            }
        }
        else
        {
            if (light STAYS on for 30 min)
            {
                turn off light
            }
            else if (door changes to closed)
            {
                turn off light
                exit (exit while loop)
            }
        }
    } while (door == open)
}

I know its doable, but I’m having difficulty with the variables - in some places I can store variables as ‘time’ but in others ‘time’ isn’t an option, just numbers, decimals, booleans etc. which would be fine except I also don’t know if its counting in millis or what… I’m probaby overcomplicating something… just not sure where, and its a PITA to do on my phone… Is there a web front end for this? (aside from the not-editable (AFAIK) dashboard)


#2

Cancel on truth change should do the trick. You should find that option under task cancellation policy.
So when motion is detected turn light on. But if motion stops for more than 30 minutes, then turn off the light. For the task of “turn off the light” you will need to select the “cancel on piston state change” option. That will reset the timer


(Brian) #3

Use a latching piston and set the option for execute on piston state change and you’llbr fine.

No need for variables for this simple use case.

I believe there are some sample pistons in the CoRE help thread for this.


( I hate Mondays) #4

You are overthinking this. What you want to do is simply done this way, with an ELSE-IF piston:

IF
   Door is open
   OR
   Motion is active
THEN
   Using light...
      > Turn on
      > Wait 30 minutes
      > Turn off

ELSE IF
   Door is closed
THEN
   Using light...
      > Turn off

To explain a bit, there is an action option called TOS (Task Override Scope) that defaults to “Local”. If you ask a device to do something later, but then you ask it to do something else, that later task is automatically canceled, thus cancelling your timer. It just works like that.

However, motion probably works not like you expect it: you’ll get an active event when motion is detected and then an inactive event when motion stops. This means that going into the closet and moving there for 40 minutes will see the light turning off at the 30 minute mark because there will be no “new” motion events to reset your timer. I suggest rethinking this with a latching piston, where door open or motion active turn on the light, whereas door closed or motion inactive turn off the light in 1 minute or so. That will 1) save you electricity and 2) will make sure you can spend hours in the closet without the light turning off on you:

IF
   Door is open
   OR
   Motion is active
THEN
   Using light...
      > Turn on

BUT IF
   Door is closed
   OR
   Motion is inactive
THEN
   Using light...
      > Wait 1 minute
      > Turn off

You will get better results from the latter piston.


(Jeff Johnson) #5

@ncatoffice: Tried the truthiness and it didn’t work - though I may have implemented it wrong.

@bridaus and @ady624: the piston is actually already a Latching type, but @ady624, the motion sensor isn’t in the closet, its in the bedroom - otherwise I’d probably not have bothered with the door sensor. Here’s what the dashboard looks like at the moment:

Am at work so I there’s only so much testing I can do at the moment :stuck_out_tongue:

I should just get a switch instead of a smart bulb, then I don’t have to care about the motion, she can just hit the switch… she likes the old style toggle switches (I’m indifferent - but they’re harder to find in store, and the selection isn’t too great even online - plus there’s not much in the way of interesting functionality…) I really wish someone would make a switch with like, an actuator or something so you could actually toggle the switch into an up/down position…

I digress… too early… :slight_smile: