this very depends about exact environment, so years of experience might or might not be handy.
I have as well years of experience with programming, yet there is still room for improvements everywhere
let’s see what I’m now running in production to see if it is solid enough
Short explanation, but code is quite easy to follow.
I have introduced two helpers, one holds ON 15mins after last movement occured.
second one holds if marked entrypoints have changed.
Divided into two simple rules. When marked doors changes their state, helper holds ON
Then in “main” rule I’m checking if there is a motion in a group (±2min value) while doorhelper is ON
if so that means, somebody is home even doors have been opened, so let’s reset door helper and set globalpresence to ON
if 15min motion helper expires (changes it’s state) and door helper is ON that means there were no motion and we can assume nobody is home.
If 15min expires but doors are not changed, nothing happening … just nobody is moving - but as doors have not been changed, we can assume somebody is still home.
I’m running it for couple of days now, seems 100% accurate. But again, this depends how sensors are placed and which are used.
Your results may vary.
if you spot some corner case - speak up
I’m always opened to make rules simplier
… and there is no single timestamp at all
// code is in progress so ignore bits here and there
rule "Presense detection / Wasps in the Jar"
when
//System started or
//Item gMotion changed or
Member of gMotion received update or
//Item gContactsDoors changed or
Item holderPresenceMotion changed
then
val helper = holderPresenceMotion.state // this holds movement for 15mins (expire binding)
val motion = gMotion.state // this holds group info about movement +- 2min
val doors = holderPresenceDoors.state // this holds info if doors have been changed since
//val presence = holderPresence.state
// lets check if there is a movement after doors helper have been switched ON
if(motion == ON && doors != OFF){
logInfo("PRESENCE", "Motion after door changed ==> somebody home")
// set doors helper to OFF because there is a movement
holderPresenceDoors.sendCommand(OFF)
// set global presense to ON (somebody is home)
holderPresence.sendCommand(ON)
}
// 15min helper expired and doors have been changed?, assume nobody home
if(helper == OFF && doors == ON){
logInfo("PRESENCE", "No motion after door has changed")
// set global presence to OFF (door were changed and motion expired)
holderPresence.sendCommand(OFF)
}
end
rule "Presense detection: Doors in/out"
when
Item gContactsDoors changed
then
// doors opened/closed
holderPresenceDoors.sendCommand(ON)
logInfo("PRESENCE", "Door opened/closed ==> setting helper to ON ")
end
value of holderPresence (based on doors and motions) I can further combine with mobile phones, running Kodi or specific lights … to make it even more accurate. But for now, let’s keep it simple.