# Generic Presence Detection

**URL:** <https://community.openhab.org/t/generic-presence-detection/17878>\
**Category:** Tutorials & Examples\
**Tags:** presence\
**Created:** [December 7, 2016, 11:14pm UTC](https://community.openhab.org/t/generic-presence-detection/17878 "2016-12-07T23:14:44Z")\
**Posts on this page:** 20\
**Page:** 4

<div class="post-metadata">

**Author:** ![lamero](https://community.openhab.org/letter_avatar_proxy/v4/letter/l/50afbb/32.png) [@lamero](https://community.openhab.org/u/lamero)\
**Post date:** [October 25, 2018, 5:38pm UTC](https://community.openhab.org/t/generic-presence-detection/17878/61 "2018-10-25T17:38:57Z")

</div>

I’m trying to use DSC Alarm sensor with this fantastic rule. But presence never goes off. What’s wrong?

items

```csv
Switch Present
Group:Switch:AND(OFF,ON) gPresent
Switch Present_Timer { expire="15s,command=OFF" }
Switch PresentDSC (gPresent)

```

rule

```php
rule "System started"
when
    System started
then
    Present.sendCommand(OFF) // assume no one is home on startup
end

rule "gPresent updated, at least one sensor changed state"
when
    Item gPresent received update
then
    logInfo("presence.rules","gPresent aggiornato")
    // someone came home
    if(gPresent.state == ON && Present.state != ON) {
        Present_Timer.postUpdate(OFF) // cancel the timer if necessary
        Present.sendCommand(ON)
    }

    // no one is home
    else if(gPresent.state == OFF && Present.state != OFF){
        Present_Timer.sendCommand(ON) // start the timer
    }
end

rule "Present_Timer expired"
when
    Item Present_Timer received command OFF
then
    Present.sendCommand(OFF)
end

rule "DSC"
when
    Item DSCAlarmMotion changed from OPEN to CLOSED
then
    PresentDSC.sendCommand(ON)
end

```

debug

```php
rule "debug"
when
    Item Present received update
then
    sendTelegram("home", "Presence - " + Present.state.toString)
end

```

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [October 25, 2018, 5:59pm UTC](https://community.openhab.org/t/generic-presence-detection/17878/62 "2018-10-25T17:59:58Z")

</div>

Does PresentDSC change state?

---

<div class="post-metadata">

**Author:** ![lamero](https://community.openhab.org/letter_avatar_proxy/v4/letter/l/50afbb/32.png) [@lamero](https://community.openhab.org/u/lamero)\
**Post date:** [October 25, 2018, 8:56pm UTC](https://community.openhab.org/t/generic-presence-detection/17878/63 "2018-10-25T20:56:25Z")

</div>

Yes

```auto
2018-10-25 23:02:17.523 [ome.event.ItemCommandEvent] - Item 'Present' received command OFF
2018-10-25 23:02:17.525 [vent.ItemStateChangedEvent] - Present changed from ON to OFF
2018-10-25 23:02:28.625 [ome.event.ItemCommandEvent] - Item 'PresentDSC' received command ON
2018-10-25 23:02:28.632 [ome.event.ItemCommandEvent] - Item 'Present' received command ON
2018-10-25 23:02:28.638 [vent.ItemStateChangedEvent] - Present changed from OFF to ON
2018-10-25 23:03:24.084 [ome.event.ItemCommandEvent] - Item 'PresentDSC' received command ON

```

(See time)

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [October 25, 2018, 10:26pm UTC](https://community.openhab.org/t/generic-presence-detection/17878/64 "2018-10-25T22:26:49Z")

</div>

Does it generates the error every time the winner runs?

Do the Item names exactly match the way they are defined in PaperUI or your .items files? Case matters. I’m kind of at a loss what we it could be.

---

<div class="post-metadata">

**Author:** ![lamero](https://community.openhab.org/letter_avatar_proxy/v4/letter/l/50afbb/32.png) [@lamero](https://community.openhab.org/u/lamero)\
**Post date:** [October 26, 2018, 9:28am UTC](https://community.openhab.org/t/generic-presence-detection/17878/65 "2018-10-26T09:28:24Z")

</div>

> Does it generates the error every time the winner runs?

No errors into the log

Something wrong with expire binding. Present\_Timer never expires. Reinstalled expire bindings, rebooted OH:

```auto
2018-10-26 11:26:29.533 [ome.event.ItemCommandEvent] - Item 'Present' received command OFF
2018-10-26 11:26:29.535 [vent.ItemStateChangedEvent] - Present changed from NULL to OFF
2018-10-26 11:26:51.936 [ome.event.ItemCommandEvent] - Item 'PresentDSC' received command ON
2018-10-26 11:26:51.939 [vent.ItemStateChangedEvent] - PresentDSC changed from NULL to ON
2018-10-26 11:26:51.940 [GroupItemStateChangedEvent] - gPresent changed from NULL to ON through PresentDSC
2018-10-26 11:26:51.944 [ome.event.ItemCommandEvent] - Item 'Present' received command ON
2018-10-26 11:26:51.945 [vent.ItemStateChangedEvent] - Present_Timer changed from NULL to OFF
2018-10-26 11:26:51.946 [vent.ItemStateChangedEvent] - Present changed from OFF to ON

```

This is the rule. See comment (“are you sure?”)

```php
rule "System started"
when
    System started
then
    Present.sendCommand(OFF) // assume no one is home on startup
end

rule "gPresent updated, at least one sensor changed state"
when
    Item gPresent received update
then
    // someone came home
    if(gPresent.state == ON && Present.state != ON) {
        Present_Timer.postUpdate(OFF) // cancel the timer if necessary
                                     // OFF? Are you sure?
                                     // With ON it works
        Present.sendCommand(ON)
    }

    // no one is home
    else if(gPresent.state == OFF && Present.state != OFF){
        Present_Timer.sendCommand(ON) // start the timer
    }
end

rule "Present_Timer expired"
when
    Item Present_Timer received command OFF
then
    Present.sendCommand(OFF)
end

```

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [October 26, 2018, 3:47pm UTC](https://community.openhab.org/t/generic-presence-detection/17878/66 "2018-10-26T15:47:47Z")

</div>

Yes I’m sure.

With `expire="15s,command=OFF"` the timer gets started when they Item changes state to ON. Is 15 seconds pass with the Item not receiving any other updates the binding will send the OFF command to the Item. When you postUodate(OFF), the binding sees that the Item is already in the OFF state now so it cancels the timer and no OFF command will be sent.

So the Timer expired rule only gets triggered when the Expire binding commands the item to OFF.

I’ve been running with this coffee for years so the concept is sound.

---

<div class="post-metadata">

**Author:** ![vzorglub](https://community.openhab.org/user_avatar/community.openhab.org/vzorglub/32/17166_2.png) [@vzorglub](https://community.openhab.org/u/vzorglub)\
**Post date:** [October 26, 2018, 3:59pm UTC](https://community.openhab.org/t/generic-presence-detection/17878/67 "2018-10-26T15:59:48Z")

</div>

> [@rlkoshak](#):
>
> I’ve been running with this coffee

Rich, you can’t help yourself, can you… 😄

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [October 26, 2018, 4:26pm UTC](https://community.openhab.org/t/generic-presence-detection/17878/68 "2018-10-26T16:26:40Z")

</div>

I was in a meeting typing on my phone. For some reason Google really thinks I mean coffee when I type code. Sigh. Guess I need more code this morning.

😉

---

<div class="post-metadata">

**Author:** ![vzorglub](https://community.openhab.org/user_avatar/community.openhab.org/vzorglub/32/17166_2.png) [@vzorglub](https://community.openhab.org/u/vzorglub)\
**Post date:** [October 26, 2018, 5:43pm UTC](https://community.openhab.org/t/generic-presence-detection/17878/69 "2018-10-26T17:43:45Z")

</div>

I though you came up with another idiomatic pearl!

---

<div class="post-metadata">

**Author:** ![UdoJR](https://community.openhab.org/letter_avatar_proxy/v4/letter/u/898d66/32.png) [@UdoJR](https://community.openhab.org/u/UdoJR)\
**Post date:** [October 31, 2018, 1:27pm UTC](https://community.openhab.org/t/generic-presence-detection/17878/70 "2018-10-31T13:27:32Z")

</div>

@rlkoshak @lamero Mmm, I took a look a your code and I think your

```php
Present_Timer

```

get’s never canceled, because

```php
Present.state

```

is still on when someone comes home before timeout, so you will never reach

```php
Present_Timer.postUpdate(OFF) // cancel the timer if necessary

```

Possible solution would be to leave out

```php
&& Present.state != ON

```

or if you don’t want that

```php
Present.sendCommand(ON)

```

gets triggered twice, you can add an additional _else if_

```php
    else if(gPresent.state == ON && Present.state ==ON){ // someone came home but timer is running
        Present_Timer.postUpdate(OFF) // cancel the timer if necessary
    }

```

and remove

```php
        Present_Timer.postUpdate(OFF) // cancel the timer if necessary

```

in the first _if_.

Didn’t tested it, so maybe I’m wrong?  
Greets Udo

---

<div class="post-metadata">

**Author:** ![lamero](https://community.openhab.org/letter_avatar_proxy/v4/letter/l/50afbb/32.png) [@lamero](https://community.openhab.org/u/lamero)\
**Post date:** [October 31, 2018, 2:24pm UTC](https://community.openhab.org/t/generic-presence-detection/17878/71 "2018-10-31T14:24:29Z")

</div>

I’m confused

**ITEMS**

```csv
Switch Present
Group:Switch:AND(OFF,ON) gPresent
Switch Present_Timer { expire="10s,command=OFF" }
Switch PresentDSC (gPresent)

```

**RULE**

```php
rule "System started"
when
    System started
then
    Present.sendCommand(OFF) // assume no one is home on startup
end

rule "gPresent updated, at least one sensor changed state"
when
    Item gPresent received update
then
    logInfo("present","gPresent.state " + gPresent.state.toString)
    logInfo("present","Present.state " + Present.state.toString)
    // someone came home
    if(gPresent.state == ON && Present.state != ON) {
        logInfo("present","present 1 if")
        Present_Timer.postUpdate(OFF) // cancel the timer if necessary
        Present.sendCommand(ON)
    }

    // no one is home
    else if(gPresent.state == OFF && Present.state != OFF){
        logInfo("present","present 2 if")
        Present_Timer.sendCommand(ON) // start the timer
    }
end

rule "Present_Timer expired"
when
    Item Present_Timer received command OFF
then
    Present.sendCommand(OFF)
end

```

**LOG**

_System start_

```auto
2018-10-31 15:39:23.086 [ome.event.ItemCommandEvent] - Item 'Present' received command OFF
2018-10-31 15:39:23.088 [vent.ItemStateChangedEvent] - Present changed from ON to OFF

```

_Alarm sensor triggered_

```auto
2018-10-31 15:41:02.687 [ome.event.ItemCommandEvent] - Item 'PresentDSC' received command ON
2018-10-31 15:41:02.689 [INFO] [lipse.smarthome.model.script.present] - gPresent.state ON
2018-10-31 15:41:02.690 [INFO] [lipse.smarthome.model.script.present] - Present.state OFF
2018-10-31 15:41:02.690 [INFO] [lipse.smarthome.model.script.present] - present 1 if
2018-10-31 15:41:02.691 [ome.event.ItemCommandEvent] - Item 'Present' received command ON
2018-10-31 15:41:02.692 [vent.ItemStateChangedEvent] - Present changed from OFF to ON

```

_Alarm sensor triggered after 2 minutes_

```auto
2018-10-31 15:43:09.434 [ome.event.ItemCommandEvent] - Item 'PresentDSC' received command ON
2018-10-31 15:43:09.435 [INFO] [lipse.smarthome.model.script.present] - gPresent.state ON
2018-10-31 15:43:09.436 [INFO] [lipse.smarthome.model.script.present] - Present.state ON

```

_Alarm sensor triggered after 7 minutes_

```auto
2018-10-31 15:48:22.300 [ome.event.ItemCommandEvent] - Item 'PresentDSC' received command ON
2018-10-31 15:48:22.303 [INFO] [lipse.smarthome.model.script.present] - gPresent.state ON
2018-10-31 15:48:22.303 [INFO] [lipse.smarthome.model.script.present] - Present.state ON

```

_Full log_

```auto
2018-10-31 15:39:23.086 [ome.event.ItemCommandEvent] - Item 'Present' received command OFF
2018-10-31 15:39:23.088 [vent.ItemStateChangedEvent] - Present changed from ON to OFF
2018-10-31 15:41:02.687 [ome.event.ItemCommandEvent] - Item 'PresentDSC' received command ON
2018-10-31 15:41:02.689 [INFO] [lipse.smarthome.model.script.present] - gPresent.state ON
2018-10-31 15:41:02.690 [INFO] [lipse.smarthome.model.script.present] - Present.state OFF
2018-10-31 15:41:02.690 [INFO] [lipse.smarthome.model.script.present] - present 1 if
2018-10-31 15:41:02.691 [ome.event.ItemCommandEvent] - Item 'Present' received command ON
2018-10-31 15:41:02.692 [vent.ItemStateChangedEvent] - Present changed from OFF to ON
2018-10-31 15:43:09.434 [ome.event.ItemCommandEvent] - Item 'PresentDSC' received command ON
2018-10-31 15:43:09.435 [INFO] [lipse.smarthome.model.script.present] - gPresent.state ON
2018-10-31 15:43:09.436 [INFO] [lipse.smarthome.model.script.present] - Present.state ON
2018-10-31 15:48:22.300 [ome.event.ItemCommandEvent] - Item 'PresentDSC' received command ON
2018-10-31 15:48:22.303 [INFO] [lipse.smarthome.model.script.present] - gPresent.state ON
2018-10-31 15:48:22.303 [INFO] [lipse.smarthome.model.script.present] - Present.state ON

```

_How alarm sensors updates their state_

**ITEMS**

```csv
Group:Contact:OR(OPEN, CLOSED) DSCAlarmMotion <motionDetector>
Contact ZONE1_STATUS "Sensor (Zone 1)" (DSCAlarmZones, DSCAlarmMotion) { channel="dscalarm:zone:be74eaa7:zone1:zone_status" }

```

**RULES**

```auto
rule "DSC"
when
    Item DSCAlarmMotion changed from OPEN to CLOSED
then
    PresentDSC.sendCommand(ON)
end

```

**Present\_Timer** never starts, never expires

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [October 31, 2018, 6:03pm UTC](https://community.openhab.org/t/generic-presence-detection/17878/72 "2018-10-31T18:03:24Z")

</div>

There may be a timing issue here because I’ve been running with this code (or at least code based on it) for years without problem.

The way it is supposed to work is gPresent represents the state of the actual sensors and Present is a proxy for the sensors so we can use the timer to smooth out the flapping.

In my working code at home I’m using changed as the trigger, not received update, so maybe that is part of the issue.

I’m also using a count of the ON sensors instead of just gPresent.state == ON. It looks like I’ve made a number of changes since posting this.

Ah yes, I must have fixed this somewhere along the way and forgot to come back here.

```php
when
    Item gPresent changed
then
    logDebug("present", "gPresent changed to " + gPresent.state)

    // A presence sensor is ON, set Present to ON if it is OFF and cancel the Timer if it exists
    if(gPresent.state == ON && (Present.state != ON || Present_Timer.state == ON)) {
        logDebug("present", "Someone came home")
        if(Present_Timer.state != OFF) Present_Timer.postUpdate(OFF) // cancel the timer
        if(Present.state != ON) Present.sendCommand(ON)
    }

    // All presence sensors are OFF, create a Timer, if one doesn't already exist, to turn Present to OFF if 
    // Present isn't already OFF
    else if(gPresent.state == OFF && Present.state !-= OFF && Present_Timer.state != ON) {
        logDebug("present", "Everyone is away, setting timer")
        Present_Timer.sendCommand(ON) // set timer to turn off Present
    }
end

```

I transcribed this and had to change the names to match the Items above. But I think this might address the issues. Let me know one way or the other and I’ll update the OP.

> [@lamero](#):
>
> I’m confused

In your case it is important to realize that this Rule assumes that there will be one or more sensors that is constantly ON when you are home (e.g. Network binding, iCloud binding, FIND, reelyActive detection of phone or BT beacon). I don’t think it works well with motion timers.

But if I’m reading what you are trying to do incorrectly, try the changes I made above and see if ti helps.

---

<div class="post-metadata">

**Author:** ![UdoJR](https://community.openhab.org/letter_avatar_proxy/v4/letter/u/898d66/32.png) [@UdoJR](https://community.openhab.org/u/UdoJR)\
**Post date:** [October 31, 2018, 7:11pm UTC](https://community.openhab.org/t/generic-presence-detection/17878/73 "2018-10-31T19:11:26Z")

</div>

> [@rlkoshak](#):
>
> There may be a timing issue here because I’ve been running with this code (or at least code based on it) for years without problem.

No timing issue, it is just wrong, I tested it and I’m sure. In your example on the top you will never reach the line that cancels the timer.

Can you edit your first post? Because it will miss leading other people.

```php
rule "Group present updated, at least one sensor changed state"
when
    Item gPresent changed
then
    if(gPresent.state == ON && Present.state != ON) { // someone came home
        Present.sendCommand(ON)
    } else if(gPresent.state == ON && Present.state == ON) { // someone came home but timer is running
        Present_Timer.postUpdate(OFF) // cancel the timer
    } else if(gPresent.state == OFF && Present.state != OFF){ // last person left home
        Present_Timer.sendCommand(ON) // start the timer
    }
end

rule "Present_Timer expired"
when
    Item Present_Timer received command OFF
then
    Present.sendCommand(OFF)
end

```

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [October 31, 2018, 7:14pm UTC](https://community.openhab.org/t/generic-presence-detection/17878/74 "2018-10-31T19:14:14Z")

</div>

> [@UdoJR](#):
>
> No timing issue, it is just wrong, I tested it and I’m sure. In your example on the top you will never reach the line that cancels the timer.

Thanks for reading my full reply to your original post.

---

<div class="post-metadata">

**Author:** ![UdoJR](https://community.openhab.org/letter_avatar_proxy/v4/letter/u/898d66/32.png) [@UdoJR](https://community.openhab.org/u/UdoJR)\
**Post date:** [October 31, 2018, 8:02pm UTC](https://community.openhab.org/t/generic-presence-detection/17878/75 "2018-10-31T20:02:23Z")

</div>

> [@rlkoshak](#):
>
> Thanks for reading my full reply to your original post.

Sorry, if that last post sounds a bit rude, wasn’t meant that way.

---

<div class="post-metadata">

**Author:** ![UdoJR](https://community.openhab.org/letter_avatar_proxy/v4/letter/u/898d66/32.png) [@UdoJR](https://community.openhab.org/u/UdoJR)\
**Post date:** [October 31, 2018, 8:20pm UTC](https://community.openhab.org/t/generic-presence-detection/17878/76 "2018-10-31T20:20:49Z")

</div>

> [@rlkoshak](#):
>
> There may be a timing issue here because I’ve been running with this code (or at least code based on it) for years without problem.

Ok, but it not worked with the original post and it never has, maybe when you changed it for your use.

> [@rlkoshak](#):
>
> The way it is supposed to work is gPresent represents the state of the actual sensors and Present is a proxy for the sensors so we can use the timer to smooth out the flapping.

I already got it, thanks for explaining.

> [@rlkoshak](#):
>
> In my working code at home I’m using changed as the trigger, not received update, so maybe that is part of the issue.

That’s another problem of retriggering, but not the reason for the problem we are talking about.

> [@rlkoshak](#):
>
> Ah yes, I must have fixed this somewhere along the way and forgot to come back here.

It’s very “unreadable” therefor for beginnersnot easy to understand, because of the _or_ and the two _if_ statements in the _if_ statement. But could be working this way. Maybe we find together a more readable version of your OP, thats besides was a good idea.

```auto
Present.state !-= OFF

```

Just the “-” minus is to much.

> [@rlkoshak](#):
>
> I transcribed this and had to change the names to match the Items above. But I think this might address the issues. Let me know one way or the other and I’ll update the OP.

Oh yes, sorry I skipped this one, but as I said, seems to me to unreadable.  
Thanks for your work and your help for the community. 🙂  
Have a nice evening!

Greets Udo

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [October 31, 2018, 9:13pm UTC](https://community.openhab.org/t/generic-presence-detection/17878/77 "2018-10-31T21:13:07Z")

</div>

> [@UdoJR](#):
>
> Sorry, if that last post sounds a bit rude, wasn’t meant that way.

Not that it was rude. Just that you ignored the bulk of what I wrote in response. I said that the code was wrong and supplied a version of what I’ve been successfully running with and said I would update the OP. But you only responded to the first line of the post. It was a little annoying.

> [@UdoJR](#):
>
> Just the “-” minus is to much.

Typo.

---

<div class="post-metadata">

**Author:** ![UdoJR](https://community.openhab.org/letter_avatar_proxy/v4/letter/u/898d66/32.png) [@UdoJR](https://community.openhab.org/u/UdoJR)\
**Post date:** [October 31, 2018, 10:07pm UTC](https://community.openhab.org/t/generic-presence-detection/17878/78 "2018-10-31T22:07:09Z")

</div>

Ahhh, then this was the problem, because for me it was annoying that you sound like you didn’t made an error 😉

---

<div class="post-metadata">

**Author:** ![lamero](https://community.openhab.org/letter_avatar_proxy/v4/letter/l/50afbb/32.png) [@lamero](https://community.openhab.org/u/lamero)\
**Post date:** [November 1, 2018, 2:54pm UTC](https://community.openhab.org/t/generic-presence-detection/17878/79 "2018-11-01T14:54:01Z")

</div>

> [@rlkoshak](#):
>
> Presence\_Sensors

_gPresence\_Sensors_ or _Presence\_Sensors_? 🙄

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [November 1, 2018, 3:24pm UTC](https://community.openhab.org/t/generic-presence-detection/17878/80 "2018-11-01T15:24:25Z")

</div>

Typo. I’ve corrected it above. The name should match what is defined in the .items file, no “g”.

[Previous page](https://community.openhab.org/t/generic-presence-detection/17878.md?page=3)

[Next page](https://community.openhab.org/t/generic-presence-detection/17878.md?page=5)
