Skip to content

Instantly share code, notes, and snippets.

@igrowing
Last active June 5, 2019 20:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igrowing/06f2eabb78d608153da5dccb9a2a6912 to your computer and use it in GitHub Desktop.
Save igrowing/06f2eabb78d608153da5dccb9a2a6912 to your computer and use it in GitHub Desktop.
People presence detector

The flow uses external python script which generates MQTT messages to the People Presence Detector.

The People Presence Detector flow runs the script as daemon. The script sends the status only on its change (when any user departs or arrives the premises).

The flow

The MQTT JSON parses presence input to decisions per output:

  • Lights;
  • Temperature control;
  • Alarm;
  • Locks;

For advanced use: put status of this and other nodes in flow/global context to take more sophisticated decisions.

The goal of this flow is: Make reliable and cheap user presence detection with no adding hardware. Major assumption is: Every capable person in premises (your home. your office) has mobile phone and connected automatically to the WiFi.

Prerequisites:

  1. In /home/pi folder create people.csv file in format:
user_name,full_or_part_MAC

Example:

joe,AA:BB:FF
bar,12:23:34:45:56
  1. Install node-red-node-daemon in Node-Red.

  2. Install sudo pip install paho-mqtt netaddr on your Linux.

  3. Ensure the MQTT broker is up and running on the Linux where the ptracker is intented to run.

mosquitto_sub -t "#" &
mosquitto_pub -t "my/test/" -m "test message"
  1. Install sudo apt-get install nmap on your Linux.
[
{
"id": "cc69192f.0c3d28",
"type": "debug",
"z": "b30772c2.e1537",
"name": "out3",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"x": 850,
"y": 480,
"wires": []
},
{
"id": "c241c1dc.90bac",
"type": "debug",
"z": "b30772c2.e1537",
"name": "out1",
"active": false,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"x": 270,
"y": 460,
"wires": []
},
{
"id": "ae050b8b.925558",
"type": "daemon",
"z": "b30772c2.e1537",
"name": "poll_users_d",
"command": "/home/pi/ptracker.py",
"args": "",
"autorun": true,
"cr": false,
"redo": true,
"op": "lines",
"closer": "SIGKILL",
"x": 110,
"y": 480,
"wires": [
[
"c241c1dc.90bac"
],
[],
[]
]
},
{
"id": "f029b8ab.fadbc8",
"type": "mqtt in",
"z": "b30772c2.e1537",
"name": "people presence",
"topic": "shm/rpi/presence/",
"qos": "2",
"broker": "2bd2afca.e962e",
"x": 300,
"y": 500,
"wires": [
[
"f8627697.a29498"
]
]
},
{
"id": "a5a8020d.d1168",
"type": "function",
"z": "b30772c2.e1537",
"name": "presence in action",
"func": "/* === Presence example function ===\n === Replace messages with your actions: ===\n === MQTT commands, GPIO controls, etc. ===\nBased on JSON presence input place desisions per output:\n- Lights;\n- Temperature control;\n- Alarm;\n- Locks;\n\nFor advanced use: put status of this and other nodes in \nflow/global context to take more sophisticated decisions.\n*/\nvar count = 0;\nvar open = false;\nfor (var i in msg.payload) {\n // node.status({text: msg.payload[i]})\n count += (msg.payload[i].present.toLowerCase() == \"true\")?1:0;\n if (msg.payload[i].changed.toLowerCase() == \"true\" && msg.payload[i].present.toLowerCase() == \"true\") {\n open = true;\n }\n}\nvar lights = {payload: \"Turn lights \"};\nvar temps = {payload: \"Turn A/C \"};\nvar alarm = {payload: \"Turn alarm \"};\nvar locks = {payload: \"Turn locks \"};\n\nlocks.payload += (open)?\"open\":\"close\";\n\nif (count > 0) {\n lights.payload += \"on\";\n temps.payload += \"on\";\n alarm.payload += \"off\";\n return [lights, temps, alarm, locks];\n}\n\nlights.payload += \"off\";\ntemps.payload += \"off\";\nalarm.payload += \"on\";\nreturn [lights, temps, alarm, locks];\n",
"outputs": 4,
"noerr": 0,
"x": 650,
"y": 480,
"wires": [
[
"cc69192f.0c3d28"
],
[
"cc69192f.0c3d28"
],
[
"cc69192f.0c3d28"
],
[
"cc69192f.0c3d28"
]
]
},
{
"id": "f8627697.a29498",
"type": "json",
"z": "b30772c2.e1537",
"name": "",
"property": "payload",
"action": "",
"pretty": false,
"x": 470,
"y": 500,
"wires": [
[
"a5a8020d.d1168"
]
]
},
{
"id": "2bd2afca.e962e",
"type": "mqtt-broker",
"z": "",
"name": "",
"broker": "localhost",
"port": "1883",
"clientid": "",
"usetls": false,
"compatmode": true,
"keepalive": "60",
"cleansession": true,
"birthTopic": "",
"birthQos": "0",
"birthPayload": "",
"willTopic": "",
"willQos": "0",
"willPayload": ""
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment