Home / Community / Blog

How to detect if there someone is home or if the house is empty.

Prerequisite

  • OpenWRT router
  • MQTT server
  • Something to interpret and display data (Node-RED, Home Assistant, Domoticz, OpenHAB, etc.)

On your OpenWRT router:

Install mosquitto-client. If you don't use encrypted communication use -nossl version.

opkg install mosquitto-client-ssl

Download presence_sensor script:

wget -O /usr/bin/presence_report https://github.com/dersimn/owrtwifi2mqtt/raw/master/presence_report

make it executable:

chmod u+x /usr/bin/presence_report

Now you can check if it works. Run command:

root@OpenWrt:~# /usr/bin/presence_report event 192.168.1.8
presence_report, mode: event, MQTT server: 192.168.1.8

Change 192.168.1.8 to address of your MQTT server. Turn WiFi on one of your devices (my was e6:6a:ca:c8:38:e6).

root@OpenWrt:~# /usr/bin/presence_report event 192.168.1.8
presence_report, mode: event, MQTT server: 192.168.1.8
Mac: e6:6a:ca:c8:38:e6 did del
Mac: e6:6a:ca:c8:38:e6 did new

as you can see there my device "left" (del) and connect (new) to Wi-Fi.

Press Ctrl-C to stop test.

To start this every time router boots I've added this line:

(/usr/bin/presence_report event 192.168.1.8 >/dev/null 2>&1 )&

to /etc/rc.local file. Of course before "exit 0".

Using data

You can subscribe to "owrtwifi/status/#" topics to receive messages in topic like this:

owrtwifi/status/mac-e6-6a-ca-c8-38-e6/event

and payload del or new.

They are also messages in topic

owrtwifi/status/mac-e6-6a-ca-c8-38-e6/dhcp-name

and name of your device in payload.

I'm using Node-RED and check every device like this:

context.devicescount=0;

if (!context.hasOwnProperty("devices")) {
    context.devices={};
}

if (msg.topic === "owrtwifi/status/mac-e6-6a-ca-c8-38-e6/event") {


    if (msg.payload === "new") {
        context.devices.PhoneNo1 = 1;
    }
    if (msg.payload === "del") {
        context.devices.PhoneNo1 = 0;
    }
}
// repeat above to all of your mobile devices
if (msg.topic === "owrtwifi/status/mac-00-11-22-33-44-55/event") {


    if (msg.payload === "new") {
        context.devices.PhoneNo2 = 1;
    }
    if (msg.payload === "del") {
        context.devices.PhoneNo2 = 0;
    }
}


let values = Object.values(context.devices);

values.forEach(function (value,index) {
    if (value == 1) {
        context.devicescount+=1;
    }
    return null;
});

if (context.devicescount>0) {
    node.send({payload:{"devices":context.devicescount},topic:"mydevices"});
}
else {
    node.send({payload:{"devices":context.devicescount,"house is empty":1},topic:"mydevices"});
}

Maybe it's not the best way, but it works for me.

The most important value is devicescount - the house is empty if equal to zero.

Of course this method cannot distinguish if you are in home or you just forgot your phone. It's obvious, but I want to be sure you know this too ;-)

Just my blog...

Mon Tue Wed Thu Fri Sat Sun
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30