Only do action only if item state is OPEN for 10 seconds

I have a simple rule that switches my Nest Thermostat to ‘Away’ if one of my main doors is open.

I would like to improve the rule so that a door must have been open for at least 10 seconds before it switches the Nest to Away.

Can anyone point me in the right direction?

Here is the current rule;

 rule "Back Door Open Heating to Eco"
when
Item gMainDoors changed
then
if (gMainDoors.state == OPEN)
        {NestTStat_away_mode.sendCommand("AWAY")}
        else
        {NestTStat_away_mode.sendCommand("HOME")}
end
var Timer timer = null

rule "gMainDoorsn CLOSED"
when
    Item gMainDoors changed to CLOSED
then
    if(timer !== null) {
        timer.cancel()
        timer = null}
end

rule "gMainDoors OPEN"
when
    Item gMainDoors changed to OPEN
then
    if(timer === null) {
        timer = createTimer(now.plusSeconds(10)) [|
            NestTStat_away_mode.sendCommand("AWAY")
            timer = null]}
    else
        {NestTStat_away_mode.sendCommand("HOME")}
end

Many thanks Harry - I’ll give that a shot.

I’m struggling to understand this method for my requirement.

I’m needing to return to this. I created Harry’s rule (Many thanks Harry). It does switch the Nest Thermostat to ‘AWAY’ if the front or back door is open for longer than 10 seconds but it does not seem to be changing back to ‘HOME’ once the open door has been closed.

Here is the log for the door being open;

2018-09-13 10:45:25.678 [GroupItemStateChangedEvent] - gMainDoors changed from CLOSED to OPEN through Security_Door_Back

2018-09-13 10:45:25.688 [GroupItemStateChangedEvent] - gContact changed from CLOSED to OPEN through gMainDoors

2018-09-13 10:45:35.964 [ome.event.ItemCommandEvent] - Item 'NestTStat_away_mode' received command AWAY

2018-09-13 10:45:35.977 [nt.ItemStatePredictedEvent] - NestTStat_away_mode predicted to become AWAY

2018-09-13 10:45:35.986 [vent.ItemStateChangedEvent] - NestTStat_away_mode changed from Away to AWAY

and here is the log for the door being closed. Nothing concerning the Nest Thermostat is logged;

2018-09-13 10:46:27.829 [vent.ItemStateChangedEvent] - Security_Door_Back changed from OPEN to CLOSED

2018-09-13 10:46:27.835 [GroupItemStateChangedEvent] - gMainDoors changed from OPEN to CLOSED through Security_Door_Back

2018-09-13 10:46:27.842 [GroupItemStateChangedEvent] - gContact changed from OPEN to CLOSED through gMainDoors

The script is;

var Timer timer = null

rule "gMainDoors CLOSED"
when
    Item gMainDoors changed from OPEN to CLOSED
then
    if(timer !== null) {
        timer.cancel()
        timer = null}
end

rule "gMainDoors OPEN"
when
    Item gMainDoors changed from CLOSED to  OPEN
then
    if(timer === null) {
        timer = createTimer(now.plusSeconds(10)) [|
            NestTStat_away_mode.sendCommand("AWAY")
            timer = null]}
    else
        {NestTStat_away_mode.sendCommand("HOME")}
end

Any ideas as to why the thermostat is not being returned to ‘Home’ when the door is closed?

rule "gMainDoors CLOSED"
when
    Item gMainDoors changed to CLOSED
then
    if(timer !== null) {
        timer.cancel()
        timer = null}
    NestTStat_away_mode.sendCommand("HOME")
end

Thanks again Harry.

So the Script should be;

var Timer timer = null

rule "gMainDoors CLOSED"
when
    Item gMainDoors changed to CLOSED
then
    if(timer !== null) {
        timer.cancel()
        timer = null}
    NestTStat_away_mode.sendCommand("HOME")
end

rule "gMainDoors OPEN"
when
    Item gMainDoors changed to OPEN
then
    if(timer === null) {
        timer = createTimer(now.plusSeconds(10)) [|
            NestTStat_away_mode.sendCommand("AWAY")
            timer = null]}
    else
        {NestTStat_away_mode.sendCommand("HOME")}
end

It is now sending the ‘HOME’ command but is doing it on every close and not just when the door has been open for 10 or more seconds.
It a busy house with a dog the constanly get let out to the garden so don’t want to go over the Nest threshold for sending commands.

Would this slight change work or should I test for the Thermostat being in the away mode.

rule "gMainDoors CLOSED"
when
    Item gMainDoors changed to CLOSED
then
    if(timer >= 10) {
        timer.cancel()
        timer = null}
    NestTStat_away_mode.sendCommand("HOME")
end

EDIT: Just realised this wouldn’t reset the timer if it was not null but less than 10

Why not just check the current state? If it is not already HOME, then send HOME. Else do nothing, it’s already as you want it.

Yep - I’ve changed it to the following to see if it works;

rule "gMainDoors CLOSED"
when
    Item gMainDoors changed to CLOSED
then
    if(timer !== null) {
        timer.cancel()
        timer = null}
    if(NestTStat_away_mode.state != "HOME") return;
    NestTStat_away_mode.sendCommand("HOME")
end

Why do you have the following in your rule?

when state is not “home” --> return (quit/leave rule)

Wouldn´t be the right way:

when state is already “home” --> return (do nothing - leave rule)

???

So you have to use == and not !=

I was being a muppet - many thanks for spotting that.

I use the expire binding for this kind of rule…

So you don´t need to start/stop/reset timers. You only have an additional switch, which will auto switch to off, when the time is over. When the time is not over yet, you can switch it manually to off.

I think this is easier, but both ways are working.

I use the expire binding on my lights because I couldn’t get my Fibaro motion sensors to keep them on for 10mins when motion is detected.

I’ve not tried virtual switches yet.

Sorry for asking but my brain is not in rules mode…

I have a door with a contact
if contact has been open for 60 seconds or is closed before this cancel sending command
send a command to
Cromecast (pause)

An answer is above

var Timer timer = null



rule "Baderomsdor Underetasje OPEN"
when
    Item Dorsensor_Baddor_Underetasje changed to OPEN
then
    if(timer === null) {
        timer = createTimer(now.plusSeconds(60)) [|
            Dorsensor_Baddor_Underetasje.sendCommand("CLOSED")
            timer = null]}
    else
        {Lucaso.sendCommand("OFF")}
end

I can actually cant understand anything, im quite a copy paste noob

if(timer === null) whats with the three===