Node-RED: Presence Detection

Presence Detection

Huge thanks out to Martin Harizanov ( I don’t know the man but sifting through his very informative blog has allowed me to get this code (and many others) up and running. You can find his original post on the subject here

This is basically his work with a few tweaks. I decided to send all my sensor logs to my home servers MySQL db. The RPi uses an SDCard that has limited write life, and I think there are some performance issues there too (I don’t know, never bothered to look it up, I’m talking out my ass). I have a home server that runs MySQL and Apache so I was going to just offload that work away from the RPi anyway.

To get the presence detecting done I ping for my phone every 30 seconds or so then that result goes to a function to get analyzed (by Mr. Harizanov’s code), then that result is given to another function that UPDATES a MySQL database then finally on to a MySQL node to make the connection.

Ping -> Analyze Ping Function -> Update MySQL Function -> Connect to MySQL Server

I noticed that it took about 2-4 ping results before it would change status, and that it didn’t matter if it was a 30 second or 5 minute delay. So I went with 30 seconds, it doesn’t hammer the Pi too bad and I don’t have to wait too long to test it, hah. I did not thoroughly test this theory, I just noticed it once or twice so I could be way off.

Hopefully this helps someone else in their time of need. One is glad to be of service.

Presence Detection

If you DO NOT have or use a cellphone (or a device that uses WiFi that goes with you everywhere) then this WILL NOT WORK for you.

It works by utilizing the fact that almost everyone has a cellphone and that they usually always have it with them. The idea is simple, Node-RED looks for your cellphone. If it can see your phone it assumes you are home along with it. How do we see your phone? Ping it. Basically ping the IP address of your phone every so often and it the ping comes back you are home, if it times out you are assumed away.

You will need node-red-node-ping installed for this to work.

Nodes Required

  • Ping Node
  • Function (Analyze ping results)
  • Function (Insert data payload into MySQL db)
  • MySQL Node to make the connection

You may want to add a debug node after the Update DB function and two inject nodes before the Update DB function, set as 1 and 0 to simulate your phone home/away.

Ping Node
This one is simple, just add the IP of your cellphone and set the timer. I used 30 seconds, although I will probably change this to 5 minutes. It helps to have control over your router so you can set static IPs.

Function Node (Analyze Ping Result)
Don’t forget the global variable, this is needed in the next step.

Function (Update MySQL db)
You will need to change the db settings to match yours of course

Connect to MySQL node
This is pretty straight forward, setup with your servers credentials

That should get your Node-RED to detect if you are home or not based off your cellphones Wifi. Pretty cool, yeah but what do you do with that info? Log it? Display? Yes! Take the info from my last post and apply it here and we can check the status any time we want!

Check status via HTTP

Here’s the code to check the status of the global variables (make sure they all match!)

Here is the flow for presence detection in all it’s glory!

Discover more from Its_All.Lost

Subscribe to get the latest posts sent to your email.


  1. Shraddha
    March 22, 2017 / 6:21 pm


    Thanks for the blog, but I need tried this flow and I want to implement this for my Modem to check whether up or not.
    Do I set the MAC Address or the IP address of the Modem and Can we do it for the dynamic IP address?

  2. March 22, 2017 / 6:34 pm

    You would set the IP address of the modem. In the script posted there is no setup for detecting a dynamic IP. It just simply pings an IP address. If you want to check your dynamic (public) IP you may run into issues because your IP will most likely be assigned to someone else. So the IP would still respond to a ping. Good luck!

Leave a Reply