# Presence Detection Rule

**URL:** <https://community.openhab.org/t/presence-detection-rule/5574>\
**Category:** Scripts & Rules\
**Created:** [December 31, 2015, 11:49pm UTC](https://community.openhab.org/t/presence-detection-rule/5574 "2015-12-31T23:49:52Z")\
**Posts on this page:** 17\
**Page:** 2

<div class="post-metadata">

**Author:** ![trumee](https://community.openhab.org/letter_avatar_proxy/v4/letter/t/848f3c/32.png) [@trumee](https://community.openhab.org/u/trumee)\
**Post date:** [August 23, 2017, 3:04am UTC](https://community.openhab.org/t/presence-detection-rule/5574/21 "2017-08-23T03:04:06Z")

</div>

```auto
Group:Switch gPresent "Present group" <present>

```

is also not triggering the rule. I wonder if the following is required

```auto
Group:Switch:OR(ON,OFF) gPresent "Present group" <present>

```

---

<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:** [August 23, 2017, 12:54pm UTC](https://community.openhab.org/t/presence-detection-rule/5574/22 "2017-08-23T12:54:53Z")

</div>

I have updated the above code that I payed above which you can find here:

> [@Generic Presence Detection](https://community.openhab.org/t/generic-presence-detection/17878):
>
> Edit: A rewrite to take advantage of the Debounce rule template on the marketplace. This is a tutorial to show a generic and sensor independent way to do presence detection with multiple sensors and implementing anti-flapping (i.e. all sensors must be OFF for a certain amount of time before the home goes to away). Requirements One or more Switches determines whether someone is present. If one or more of these Switches is ON then someone is presumed to be present (more complex algorithms are p…

My understanding is adding a type to the group should be sufficient to get update events on the Group but since I use gPresent’s state elsewhere I’ve always had a function.

---

<div class="post-metadata">

**Author:** ![trumee](https://community.openhab.org/letter_avatar_proxy/v4/letter/t/848f3c/32.png) [@trumee](https://community.openhab.org/u/trumee)\
**Post date:** [August 24, 2017, 12:13am UTC](https://community.openhab.org/t/presence-detection-rule/5574/23 "2017-08-24T00:13:48Z")

</div>

I noticed that there is no ReentrantLock in the new code. Is that not required anymore?

---

<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:** [August 24, 2017, 12:25am UTC](https://community.openhab.org/t/presence-detection-rule/5574/24 "2017-08-24T00:25:45Z")

</div>

That is correct. The ReentrantLock is no longer needed. I’ve been running this code for many months now and I honestly don’t remember why there ReentrantLock lock was in there in the first place.

---

<div class="post-metadata">

**Author:** ![nakh\_Home](https://community.openhab.org/user_avatar/community.openhab.org/nakh_home/32/19923_2.png) [@nakh\_Home](https://community.openhab.org/u/nakh_Home)\
**Post date:** [June 5, 2018, 3:17pm UTC](https://community.openhab.org/t/presence-detection-rule/5574/25 "2018-06-05T15:17:48Z")

</div>

I have a DateTime associated to each Sensor as Rich Koshak as explained in another post.

i want to display the max of all those dates

How can i proceed ?

---

<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:** [June 5, 2018, 3:33pm UTC](https://community.openhab.org/t/presence-detection-rule/5574/26 "2018-06-05T15:33:57Z")

</div>

```
Group:DateTime:MAX SensorUpdates

```

Something like that should work.

If not you will need a Rule and a Proxy Item.

---

<div class="post-metadata">

**Author:** ![nakh\_Home](https://community.openhab.org/user_avatar/community.openhab.org/nakh_home/32/19923_2.png) [@nakh\_Home](https://community.openhab.org/u/nakh_Home)\
**Post date:** [June 6, 2018, 7:21am UTC](https://community.openhab.org/t/presence-detection-rule/5574/27 "2018-06-06T07:21:28Z")

</div>

@rlkoshak Thanks i will try but i have seen another issue

The Sensor\_lastUpdate is update at each reboot or restart service

Rule:

```auto
rule "ZWaveNode2_DoorWindowSensor"
when
    Item ZWaveNode2_DoorWindowSensor changed 
then
     ZWaveNode2_DoorWindowSensor_LastUpdate.postUpdate(new DateTimeType()) 
end

```

log at the boot

```auto
018-06-06 10:06:42.821 [vent.ItemStateChangedEvent] - ZWaveNode2_DoorWindowSensor_LastUpdate changed from NULL to 2018-06-06T09:41:32.001+0300

```

influxdb.persist:

```auto
Strategies{     
        everyMinute : "0 * * * * ?"
        every5Minutes : "0 */5 * * * ?"
        everyHour : "0 0 * * * ?"
        everyDay : "0 0 0 * * ?"

        default = everyChange
}

Items{
    * : strategy = everyChange, everyDay, restoreOnStartup
}

```

i have tried to check if the value is not NULL in my rule (boot time) but it do not work

```auto
rule "ZWaveNode2_DoorWindowSensor"
when
    Item ZWaveNode2_DoorWindowSensor changed 
then
        if (ZWaveNode2_DoorWindowSensor.previousState().state.toString!==NULL){
    
        ZWaveNode2_DoorWindowSensor_LastUpdate.postUpdate(new DateTimeType()) 
        }

end

```

it do not work  
Could you advise?

---

<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:** [June 6, 2018, 8:09am UTC](https://community.openhab.org/t/presence-detection-rule/5574/28 "2018-06-06T08:09:32Z")

</div>

Try that.  
Don’t forget the `;` after `return`

```php
rule "ZWaveNode2_DoorWindowSensor"
when
    Item ZWaveNode2_DoorWindowSensor changed
then
    if (previousState === null) return; # Don't run the rule if the previous state is null
    ZWaveNode2_DoorWindowSensor_LastUpdate.postUpdate(new DateTimeType()) 
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:** [June 6, 2018, 2:13pm UTC](https://community.openhab.org/t/presence-detection-rule/5574/29 "2018-06-06T14:13:14Z")

</div>

Use

```
if(previousState != NULL) {

```

.previousState will give you the most recent value in the database. So what is probably happening is the changed state (i.e. the change that triggered the Rule) is already saved in the DB before the Rule executes giving you the current state of the Item when you call previousState. This is why the implicit variable previousState exists.

Also, only use !== or === when on of the operands is `null` which is not the same thing as `NULL`.

---

<div class="post-metadata">

**Author:** ![nakh\_Home](https://community.openhab.org/user_avatar/community.openhab.org/nakh_home/32/19923_2.png) [@nakh\_Home](https://community.openhab.org/u/nakh_Home)\
**Post date:** [June 6, 2018, 6:50pm UTC](https://community.openhab.org/t/presence-detection-rule/5574/30 "2018-06-06T18:50:11Z")

</div>

> [@vzorglub](#):
>
> (previousState === null) return;

THanks a lot

---

<div class="post-metadata">

**Author:** ![nakh\_Home](https://community.openhab.org/user_avatar/community.openhab.org/nakh_home/32/19923_2.png) [@nakh\_Home](https://community.openhab.org/u/nakh_Home)\
**Post date:** [June 6, 2018, 6:50pm UTC](https://community.openhab.org/t/presence-detection-rule/5574/31 "2018-06-06T18:50:50Z")

</div>

Thanks

The return is like an "Exit rule " or “exit if”?

---

<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:** [June 6, 2018, 9:10pm UTC](https://community.openhab.org/t/presence-detection-rule/5574/32 "2018-06-06T21:10:36Z")

</div>

Exit rule

---

<div class="post-metadata">

**Author:** ![dastrix80](https://community.openhab.org/user_avatar/community.openhab.org/dastrix80/32/20582_2.png) [@dastrix80](https://community.openhab.org/u/dastrix80)\
**Post date:** [February 24, 2019, 9:55am UTC](https://community.openhab.org/t/presence-detection-rule/5574/33 "2019-02-24T09:55:50Z")

</div>

Hi Rich

In your pattern you have an item Garage\_status\_LastUpdate which shows when the garage was opened, it records a value like so:

```csv

smarthome:status Garage_status_LastUpdate
2019-02-24T20:09:00.539+1100
openhab>

```

Am I right in saying a Transform map could somehow conver this rather unusable string to something more like minutes or seconds that the door has been open for use in an TTS Alexa rule?

Regards

---

<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:** [February 24, 2019, 10:06pm UTC](https://community.openhab.org/t/presence-detection-rule/5574/34 "2019-02-24T22:06:25Z")

</div>

No. That item is a DateTime so it records an instant in time, not a duration.

You need a Number Item and a Rule to periodically increment the value while the door is open. Then you need a JS transform to convert the number, let’s say minutes, to hh:mm

---

<div class="post-metadata">

**Author:** ![MechD](https://community.openhab.org/user_avatar/community.openhab.org/mechd/32/34839_2.png) [@MechD](https://community.openhab.org/u/MechD)\
**Post date:** [February 27, 2019, 12:05am UTC](https://community.openhab.org/t/presence-detection-rule/5574/35 "2019-02-27T00:05:52Z")

</div>

Hello,

Where can I learn how to have my openHab raspberry PI home hub detect when a remote raspberry PI that detects my cell phone via Bluetooth for presence detection?

Mike

---

<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:** [February 27, 2019, 12:37am UTC](https://community.openhab.org/t/presence-detection-rule/5574/36 "2019-02-27T00:37:47Z")

</div>

[https://github.com/rkoshak/sensor](https://github.com/rkoshak/sensor) Reporter is one way.

[Integrating reelyActive on a Raspberry Pi 3 with openHAB over MQTT](https://community.openhab.org/t/integrating-reelyactive-on-a-raspberry-pi-3-with-openhab-over-mqtt/14316) is another.

---

<div class="post-metadata">

**Author:** ![MechD](https://community.openhab.org/user_avatar/community.openhab.org/mechd/32/34839_2.png) [@MechD](https://community.openhab.org/u/MechD)\
**Post date:** [February 27, 2019, 12:57am UTC](https://community.openhab.org/t/presence-detection-rule/5574/37 "2019-02-27T00:57:44Z")

</div>

Thank you, I’ll dig in!

Mike

[Previous page](https://community.openhab.org/t/presence-detection-rule/5574.md?page=1)
