A virtual presence sensor for Hubitat that determines its state by trying to access an HTTP endpoint. If it gets good responses, it reports present. If it gets a few bad responses in a row, it reports not present.
Use Cases
Pinging a device (that has an HTTP endpoint) to see if it is on the house's wifi network. If that device is present, then it means someone is home.
When running HomeBridge on a Raspberry Pi, sometimes the homebridge process stops responding to HomeKit and Hubitat. Use this virtual device to regularly check and see if HomeBridge is still up and running. If it isn't, you can use Rule Machine rules to cycle the power on the Raspberry Pi. (I have my RPi plugged into a z-wave outlet for this reason.)
Paste in the contents of httpPresenceSensor.groovy
Click "Save"
Go to the "Devices" page
Click "+ Add Virtual Device"
Set "Device Name" and "Device Network Id" to anything you like. Set "Type" to "HTTP Presence Sensor".
Click "Save Device"
On the device list, click the name of your new sensor
Set "Endpoint URL" to the url that you want the sensor to regularly ping. Example: On my HomeBridge setup, the hubitat plugin exposes an endpoint at "http://192.168.1.37:8005/". This is what I enter for the Endpoint URL.
Nice idea. I do something similar but my pinging is run from my router and uses the maker API to update a presence device. I never thought about doing this with HTTP though.
I notice when my phone (iphone) gives different responses when on/off the network. I wonder if that could be used for presence detection. Not sure how android answers back.
Offline: response:[status:408, errorMessage:No route to host (Host unreachable)]
Online: response:[status:408, errorMessage:Connect to 192.168.1.11:80 [/192.168.1.11] failed: Connection refused (Connection refused)]
Very possibly. I haven't tried this against an iphone yet, because I know they go into sleep modes where they disconnect from the wifi, even if they are at home. But if you can get it working, please let me know!
I didn't do this with an actual "ping" because I first wrote it for SmartThings, which can't do a ping, but can do a local http GET. But when I found that HomeBridge had very nice HTTP endpoints (for both the SmartThings and Hubitat plugins), I went with HTTP.
Yup. I've measured and logged the iphone and have found that it goes into sleep mode for up to 15 minutes. Setting that kind of delay will avoid false positives. This is what I currently use when pinging for both of my phones. I have a fingbox on my network that looks like it also does something similar. It will mark a device offline after 20 minutes. A little more conservative but I've never had it go offline for more than 15 when sleeping.
Ya. I wish we could do a ping. Then fully integrating and lan presence solution would be much easier.
Ok, I'm getting the same responses as you when I do a simple GET to my iPhone's local IP address. Interesting. So I'd need a customized version of this device driver that looked at the different statuses. And I think with the iPhone's wifi going to sleep, you'd need to treat this virtual presence sensor as:
If it is present, then the iPhone is definitely at home
If it is not present, we don't know
Maybe combine it with another presence sensor using @Cobra's Presence Central app. Interesting...
Unfortunately I don't have an Android phone to test with. It probably has some small difference in the http status codes and messages it returns, vs iPhone. This or my other device handler could definitely be adapted to it, but I don't have one to test with.
For you guys out there using an android does this work for you OK?
I have given my phone DHCP reservation on my router and put the address in the device using this Handler but it remains not present.
I have configured the ip address as
192.168.0.x
Tried various options but it just doesn't work.
Any thoughts?