Need help with automation / node red flow for presence detection

Looking for help with an automation flow in Node Red.

I am streaming telemetry data from my Meraki wireless APs via mqtt to Home Assistant and am seeing it come into the mqtt in node when I attach a debug to it.

The automation flow goes like this:

Garage Door opens → check for sun below horizon → check if my iPhone was just seen by the network in the last 30 seconds (some sort of state change to verify that I’ve arrived home rather than am leaving) → turn on entryway lights.

I setup a switch node off of the mqtt listener and used a “contains” and pasted in the MAC of my iphone. Problem is that nothing seems to be coming out of that node. I must be missing something format or structure wise and am stuck.

Below is what I see in MQTT Explorer -

{"mrMac":"98:18:88:C0:0D:F3","clientType":"visible","clientMac":"94:A6:7E:31:28:36","timestamp":"2021-12-16 01:03:43.686","networkId":"N_647955396388010002","rssi":"-79","radio":"wifi"}

Help appreciated!

Are you using an mqtt in node? What’s the topic that put out what you see in MQTT explorer, and did you specify it in the mqtt in node? It also looks like you will want the output as a parsed json object as there are multiple objects put out in the subscribed topic, and this will break them up.

Screenshot from 2021-12-16 10-59-44

Then once the mqtt in node is outputting a JSON object, you then can do a switch node to only pass on messages where msg.payload.clientMac equals your MAC address

I am now seeing the debug output off the switch node. Didn’t realize the msg field was case sensitive.

So now back to making this work. I’m receiving messages every 10 seconds that my phone is associated to the network.

With the goal of using this as a presence sensor to trigger an automation when I arrive home, how can I best leverage this into a flow as I don’t have a “not associated” state to trigger from a state change.

There is a time stamp in UTC… Trying to think how that can be leveraged to perform some sort of calculation.

Thoughts?

Using a flow variable and a 5 minute timer from the stop timer palette should work. The stop timer will continuously reset the countdown until the phone is gone, then after 5 minutes (or whatever time you want) set the flow variable to False, indicating the phone isn’t there anymore.

[{"id":"fff37109e8fb7446","type":"stoptimer","z":"65d4f7b6aa8e19da","duration":"5","units":"Minute","payloadtype":"num","payloadval":"0","name":"","x":440,"y":440,"wires":[["76e3f3c6d34e438f"],[]]},{"id":"76e3f3c6d34e438f","type":"change","z":"65d4f7b6aa8e19da","name":"Phone Present F","rules":[{"t":"set","p":"iphonepresence","pt":"flow","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":630,"y":440,"wires":[[]]},{"id":"10ea2a7e21d9aa0c","type":"change","z":"65d4f7b6aa8e19da","name":"Phone Present T","rules":[{"t":"set","p":"iphonepresence","pt":"flow","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":550,"y":380,"wires":[[]]},{"id":"e11a8cee00ba17de","type":"mqtt in","z":"65d4f7b6aa8e19da","name":"","topic":"zigbee2mqtt/Bedroom Fan","qos":"2","datatype":"json","broker":"1b6b4b7f.996605","nl":false,"rap":true,"rh":0,"inputs":0,"x":130,"y":380,"wires":[["580d5e747b64d694"]]},{"id":"580d5e747b64d694","type":"switch","z":"65d4f7b6aa8e19da","name":"","property":"payload.brightness","propertyType":"msg","rules":[{"t":"eq","v":"254","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":330,"y":380,"wires":[["10ea2a7e21d9aa0c","fff37109e8fb7446"]]},{"id":"1b6b4b7f.996605","type":"mqtt-broker","name":"TimMQTT","broker":"192.168.0.131","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

I managed to do this with a trigger. Took me a while to figure out (not to mention I didn’t use node-red before).

Fact is that there will be no new topics published to MQTT by Meraki if the device is no longer on the network. That can be leveraged.

The MQTT in listens on the mqtt/meraki topic. It then passes through the “payload.clientMac” filter onto the trigger. The trigger sends “home” as payload, waits for 10 seconds, and then there are 2 options
1- it receives a new message (thus the device is still on the network), and then extends the delay.
2- sends “not_home” if it doesn’t receive a new message.