Presence Detection Part 2: Improving Presence with Node-Red

In my previous post about presence detection, I showed how you can combine multiple device trackers into one highly accurate Bayesian sensor.

In Home Assistant, the binary_sensor.brad_presence that I created is either on or off. It would be a little nicer if it were a device_tracker entity instead that was either home or not home.

With Node-Red that’s easy enough, but what if we take it a step further and create our own custom device_tracker based on a different set of rules?

CONVERT BINARY SENSOR TO DEVICE TRACKER

The input is an inject node, set to send a message once every minute. It then gets the state of the binary_sensor.brad_presence entity and returns it’s state as the message. The switch evaluates that message: on goes up to set my new device_tracker home, off goes down to a different service call to set it away.

I am using the device_tracker.see service call in the output, configured like so:

This creates a device_tracker.brad_bayesian entity to use like any other.

Copy the JSON below to import this flow into your Node-Red.

[{"id":"763070f9.0c571","type":"switch","z":"4056fc4f.0baa64","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"eq","v":"off","vt":"str"}],"checkall":"true","outputs":2,"x":1316.9999771118164,"y":301.00000381469727,"wires":[["57b903a4.bd2f6c"],["a080246.03d5ed8"]]},{"id":"57b903a4.bd2f6c","type":"api-call-service","z":"4056fc4f.0baa64","name":"Set Presence Home","server":"8ac3cd7f.58d3e","service_domain":"device_tracker","service":"see","data":"{\"dev_id\":\"brad_bayesian\",\"location_name\":\"home\"}","x":1510.3333740234375,"y":272.6667175292969,"wires":[]},{"id":"a080246.03d5ed8","type":"api-call-service","z":"4056fc4f.0baa64","name":"Set Presence Not Home","server":"8ac3cd7f.58d3e","service_domain":"device_tracker","service":"see","data":"{\"dev_id\":\"brad_bayesian\",\"location_name\":\"not_home\"}","x":1520.3333282470703,"y":335.99999380111694,"wires":[]},{"id":"afb02bc5.351e68","type":"comment","z":"4056fc4f.0baa64","name":"Convert Bayesian Sensor into Device Tracker","info":"","x":1015.0000381469727,"y":252.00008296966553,"wires":[]},{"id":"a86ac3f0.00c67","type":"inject","z":"4056fc4f.0baa64","name":"","topic":"","payload":"","payloadType":"date","repeat":"10","crontab":"","once":false,"x":942.3333129882812,"y":300.6665403842926,"wires":[["d43007ca.720b78"]]},{"id":"d43007ca.720b78","type":"api-current-state","z":"4056fc4f.0baa64","name":"Bayesian Presence","server":"8ac3cd7f.58d3e","halt_if":"","entity_id":"binary_sensor.bayesian_presence","x":1142.3333892822266,"y":300.66639733314514,"wires":[["763070f9.0c571"]]},{"id":"8ac3cd7f.58d3e","type":"server","z":"","name":"Home Assistant","url":"http://HASSIP:8123","pass":"XXX"}]

https://pastebin.com/FYA4N3FG

CREATING AN IMPROVED DEVICE TRACKER

I walk my dog over to a park close by a few times a day. This was causing my house to shut down, turn the music off, etc. and then turn everything back on to a blank slate every time I walked out.

I tried a few work arounds with delays and iBeacons, but really I just wanted my bayesian sensor to weight it’s probability slightly more towards home if I was walking to the park.

On the left as inputs, I have 3 different GPS tracking platforms – the Home Assistant iOS app, my Tile tracker, and Owntracks. I format the GPS signal, and then send it into two geofence nodes.

Not my neighborhood, but looks like a good spot for dogwalking

The last function node in the flow marked “GPS” formats the GPS signal into data that Home Assistant can use, so our new device_tracker also contains the GPS data of the original source (our inputs, on the left). Anything sent in the “data” container like this will override the “data” field in the Home Assistant service output node.

Copy the JSON below to import into your Node-Red to see for yourself:

[{"id":"274f638d.ec8f8c","type":"api-call-service","z":"4056fc4f.0baa64","name":"Node Red Presence not_home","server":"8ac3cd7f.58d3e","service_domain":"device_tracker","service":"see","data":"{\"dev_id\":\"you_nodered\",\"location_name\":\"not_home\"}","x":1467.8095474243164,"y":403.9643292427063,"wires":[]},{"id":"b72e2804.d59b08","type":"api-call-service","z":"4056fc4f.0baa64","name":"Node Red Presence home","server":"8ac3cd7f.58d3e","service_domain":"device_tracker","service":"see","data":"{\"dev_id\":\"you_nodered\",\"location_name\":\"home\"}","x":1457.8094329833984,"y":333.2976927757263,"wires":[]},{"id":"be995292.1ddc4","type":"comment","z":"4056fc4f.0baa64","name":"Create Device Tracker","info":"","x":1460.8095932006836,"y":293.9643611907959,"wires":[]},{"id":"e63f1260.978e2","type":"comment","z":"4056fc4f.0baa64","name":"Home or Not?","info":"","x":1031.3452911376953,"y":372.33337688446045,"wires":[]},{"id":"d4e1c95c.1f67e8","type":"function","z":"4056fc4f.0baa64","name":"GPS","func":"newmsg = {};\nnewmsg.payload = { data: {'gps': [msg.payload.lat,msg.payload.lon] } };\nreturn newmsg;","outputs":1,"noerr":0,"x":1258.6432456970215,"y":334.2023983001709,"wires":[["b72e2804.d59b08"]]},{"id":"2d1f97bf.d24a98","type":"function","z":"4056fc4f.0baa64","name":"GPS","func":"newmsg = {};\nnewmsg.payload = { data: {'gps': [msg.payload.lat,msg.payload.lon] } };\nreturn newmsg;","outputs":1,"noerr":0,"x":1257.8097114562988,"y":403.8691101074219,"wires":[["274f638d.ec8f8c"]]},{"id":"739a62d6.c7e47c","type":"geofence","z":"4056fc4f.0baa64","name":"Inside Home Zone","mode":"circle","inside":"true","rad":0,"points":[],"centre":{"latitude":0,"longitude":0},"x":1025.625015258789,"y":322.5000047683716,"wires":[["d4e1c95c.1f67e8"]]},{"id":"f52b241a.4d2f78","type":"geofence","z":"4056fc4f.0baa64","name":"Outside Home Zone","mode":"circle","inside":"false","rad":0,"points":[],"centre":{"latitude":0,"longitude":0},"x":1034.9999961853027,"y":418.7499952316284,"wires":[["2d1f97bf.d24a98"]]},{"id":"8ac3cd7f.58d3e","type":"server","z":"","name":"Home Assistant","url":"http://HASSIP:8123","pass":"XXXXX"}]

https://pastebin.com/BCc3Nc5Z

In this way we create yet another piece of presence data for our Bayesian sensor to evaluate, so that a quick walk around to the nearby park doesn’t arm the house like we’re gone for good.

 

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *