Presence detection flow

I want to share my presence flow which was inspired from Rob’s and April’s. However I decided it can be simplified by replacing a lot of nodes with a BooleanLogic Ultimate node that performs an OR operation. The idea behind this is that if any of the sensors (in my case my phone’s position and network presence, but there can be as many sensors as you want) is detected, I am at home. If all but one drop the presence status should not change since I’m still probably at home (eg. network presence dissapears at night when the phone goes to sleep).

Just like Rob’s and April’s, my flow has 2 outputs for leaving and arriving and also sets 3 global variables that can be checked any time when needed.

The lower flows are for debugging only and simply display or set the current global variables, or inject geo/wifi presence. Same with the “Debug” function node on the right, it just outputs the calculated presence to the debug panel so I can see a history of changes and check everything worked as it was supposed to. These can all be removed.

For clarity this is what the flow looks without the debug stuff:

The change nodes “Set presence” can also be removed if you don’t want global variables.

Instead of using the included UniFi node I used node-red-contrib-unifi-os because the included didn’t want to work for me and the current (fixed) version required Node Red 3.

Owntracks function node:

const region = 'Home';
let presence = Array.isArray(msg.payload.inregions) 
    && msg.payload.inregions.includes(region);

if (msg.payload.lat !== undefined) {
    node.status({
        fill: presence ? 'green' : 'red',
        shape: 'dot',
        text: presence ? 'Home' : 'Away',
    });
    
    return {
        topic: msg.topic,
        payload: presence,
    };
}

UniFi function node:

const mac = 'AA:BB:CC:DD:EE:FF';
const device = 'phone';
const person = 'mihai';

const lastSeenSeconds = 30;
let presenceCutoff = (new Date() - (lastSeenSeconds * 1000)) / 1000; 

let matches = msg.payload.data.filter(
    device => 
        device.mac.toLowerCase() === mac.toLowerCase()
        && device.last_seen > presenceCutoff
    );
    
let presence = matches.length > 0;

node.status({
    fill: presence ? 'green' : 'red',
    shape: 'dot',
    text: presence > 0 ? 'Home' : 'Away',
});

return {
    topic: `${msg.inputMsg.topic}/${device}/${person}`,
    payload: presence,
};
4 Likes

Nice! It’s always good to have another perspective on things. Have you considered integrating uptime Kuma for presence as well? I was hoping that someone might taking adding that one on to a presence flow, but this one looks nice and very organized.
Could you explain what happened with your unifi node? am I going to have a challenge when i move to NR3?

1 Like

I added uptime Kuma and since it is always saying away for when my phone turns off the wireless, I had to make it only used for present, not away.

The included Unifi library (node-red-contrib-unifi 0.1.8) didn’t work with the latest version of UnifiOS. The author updated it a few days ago but I believe there were some issues so he decided to increase the minimum requirements to Node Red 3 and Nodejs 14.18.0.

For this reason I started using another library, node-red-contrib-unifi-os which works without any issues.

I’ll have a look at Uptime Kuma since it’s the first time I’m hearing of it :slight_smile: What would be the use case? Found it

1 Like

I’ll look at it. I’m still on NR2 because I don’t feel like migrating something that’s just gonna end up being the latest shortly.

That is so weird… my detection never stopped working, on 2 or 3 using that node. Maybe its because I’m on the release/beta channel.

Reminds me of all the times I would share a flow with april and it would have the exact opposite behavior for her that it did for me.