My goal with Presence detection is turn the alarm off (and on) when nobody is home and on again before I enter my house. So no need for a manual action when leaving/homecoming.
To achieve that I use mainly BLE which recognizes my Watch and determines distance to a number of Beacons around the house. I use ESPresense(.com) as a solution and the distance is sent to OH4.2 via MQTT. So far so good, this all works fine.
I have a rule determining if I’m at home or not. But, there’s always a but , if I’m out of reach of the beacon, it stops sending distances, so no update of the Item in OH and no trigger of the rule and I can’t determine anything. I check distance for instance more than 5 metres and the beacon keeps on sending until 6m. But sometimes the last distance is 4.9m and after that nothing more, out of range.
I currently solved it (I hope) by checking in the rule if there’s no update for 10sec and than set the variable to 5.5 m so the rule does have value and sets my presence to ‘Away’.
Is this the way to go? Or are there other methods/algoritmes of doing this? The rule is big and runs almost every second. ESPresense itself can’t do this either.
You could use one of the timestamp profiles to determine when the last update was received, then you can use this value in you rule to determine if enough time has passed for you to be considered away. But is it that crucial for you to have the alarm activated exactly 10 seconds after you leave? Otherwise I would have reduced the frequency of the rule running. If you set it to 10 second interval, the worst case would be 20 s until the alarm is activated.
And as a side note, have you considered the consequence of false positives/negatives? E.g., what would happen if your watch would run out of battery (or something similar) and the alarm gets activated when you are home?
I would set an expire timer on the Item with the distance to set it to UNDEF, NULL, -1 or something like that if it doesn’t receive an update for a certain amount of time. Then you can just look for changes to that “away” state.
This requires that the Item not change once you are way though as well as receiving relatively regular updates when you are home.
This is how I currently made it. And it seems to work fine. I can’t use the last update timer Anders suggested because once I’m out of BLE reach, there’s no trigger anymore for the rule and therefor the timer option seems to be the best choice.
That could be solved by re-/starting a timer in the rule that sets the status to away when it times out. But I would say @rlkoshak’s solution is the better one.
I take the average distance of the past 10 secs in 2 variables (AvgNDist and AvgNDist2), these variables are used to calculate the Home or Away status.
The timer runs for 10 sec, if it’s not retrgitriggered within the 10secs, than there’s obviously no update in distance and I set both variables to 6 (out of range). If the timer is retriggered, I cancel it.