tl:dr
Here’s an example HA configuration for using the Android IP Webcam app as an IP camera, a switch for the device’s flash, and an example to poll the motion sensor data which is continually generated and published to the app’s HTTP interface. On the Android app side, download and install the app, configure the video settings and sensor data and start the server. HA config:
IP Camera
camera:
- platform: mjpeg
mjpeg_url: http://IP.ADDRESS:8080/video
name: My Android Camera
Switch to toggle the camera flash
Needed to use Command line switch rather than RESTful switch because I could call three different endpoints in the Android IP Webcam app
switch:
platform: command_line
switches:
camera_flash:
command_on: 'curl -s "http://IP.ADDRESS:8080/enabletorch" > /dev/null'
command_off: 'curl -s "http://IP.ADDRESS:8080/disabletorch" > /dev/null'
command_state: 'curl "http://IP.ADDRESS:8080/status.json"'
value_template: '{{ value_json.curvals.torch == "on" }}'
friendly_name: Camera Flash
Sensor for motion detection
binary_sensor:
- platform: rest
name: Camera Motion
sensor_class: motion
resource: http://IP.ADDRESS:8080/sensors.json?sense=motion_active
value_template: '{{ value_json.motion_active.data[0][1][0] | round(0) }}'
Full details
I’ve got quite a few old Android devices sitting in junk drawers around the house which I wanted to make use of in connection with HA. Having used Android IP Webcam app in the past, I configured a few devices as IP cameras and connected to HA. You can use a device with only a front-facing (such as an old Nexus 7 tablet) or a device with an outward facing camera which are typically higher quality and usually include a flash.
Once you’ve installed the app onto your device, configure it to use the video settings you’d like and enable sensor recording with whatever sensors you want to be able to monitor. Start the camera server and then you can access the dedicated HTTP interface for the camera from your computer’s web browser. From here I used Chrome’s inspector to “reverse engineer” the requests that are sent to the camera’s web server when interacting with the various buttons. You can see sensors.json in action by clicking “Open sensor graph” from the app’s web interface. When inspecting this page you can see how when you check/uncheck the sensors it updates the URL to include/exclude each sensor in an array ?sense=SENSOR1,SENSOR2,ETC
. This can help you when configuring HA for any sensors you want to use. I was focused on the “motion” sensors, but you could experiment with any sensors your device supports which the app exposes.
Motion detection
Provided in three different “sensors” from /sensors.json. I’m not 100% sure how each is calculated.
-
motion (Motion amount)
In my testing this one can range from 0 - 1000+, depending on motion and is inconsistent between devices (Nexus 7 front-facing camera vs. Droid Razr MAXX outward facing camera) when no motion is present. On Nexus 7 the value hovers around 110 in a dark environment and around 275 in a lighter environment whereas on the MAXX the value hovers around 290 in a dark environment and 47 in a bright environment. This sensor might work better for more accurate motion detection (on a device by device basis) than motion_active from my example configuration above, just not sure how best to interpret or calculate the data for the HA sensor. Output has the following structure always with 76 timestamp+values pairs (truncated for brevity):
{"motion":{"unit":"","data":[[1485492206293,[72.0]],[1485492206299,[72.0]],[1485492206341,[72.0]],[1485492206403,[72.0]],[1485492206460,[72.0]],[1485492206522,[72.0]],[1485492206585,[72.0]],[1485492206643,[72.0]],[1485492206701,[72.0]],[1485492206761,[72.0]],[1485492206828,[72.0]]...]]}}
-
motion_event (Motion reported)
Can output multiple timestamp+value pairs (counted up to 15), with the most current timestamp last. Only outputs multiple when the value has changed. Output structure:
{"motion_event":{"unit":"","data":[[1485493018681,[1.0]],[1485493019807,[0.0]],[1485493021730,[1.0]],[1485493021891,[0.0]]]}}
-
motion_active (Motion active)
Can output multiple timestamp+value pairs (I’ve seen up to three), with most current timestamp last. I think it only outputs multiple when the value has changed. Output has this structure:
{"motion_active":{"unit":"","data":[[1485492065318,[0.0]],[1485492065482,[1.0]]]}}
status.json
Provides status of various settings and toggles as currently set. Which is how I detect the state of the flash (aka torch) in the switch config. Output looks like this:
{"video_connections":0,"audio_connections":0,"video_status":{"result": "status", "mode": "none"},"curvals":{"scenemode":"auto","torch":"off","coloreffect":"none","focusmode":"auto","antibanding":"auto","whitebalance":"auto","zoom":"100","exposure":"0","flashmode":"off","focus":"off","exposure_lock":"off","whitebalance_lock":"off","focus_region":"-1001,-1001","photo_size":"3264x2448","orientation":"upsidedown","idle":"off","audio_only":"off","overlay":"on","quality":"51","focus_homing":"off","ip_address":"IP.ADDRESS","motion_limit":"250","adet_limit":"200","night_vision":"off","night_vision_average":"2","night_vision_gain":"1.0","video_recording":"off","motion_detect":"on","video_chunk_len":"60","gps_active":"off","video_size":"1280x720","mirror_flip":"none","ffc":"off","ivideon_streaming":"off"}}
There are other various calls/endpoints for enabling/disabling settings and functionality/features. The app can also do audio, but I haven’t tried to integrate that with HA. Not sure how that could be used or output on HA dashboard.
Endpoints
These are most of the endpoints/urls I’ve discovered inspecting the IP Webcam UI.
/disabletorch (application/xml)
/enabletorch (application/xml)
/toggletorch (application/xml)
/focus (application/xml)
/nofocus (application/xml)
/settings/ffc?set=on/off (application/xml)
/settings/night_vision?set=on/off (application/xml)
/settings/overlay?set=on/off (application/xml)
/settings/orientation?set=landscape/upsidedown/portrait/upsidedown_portrait
/settings/quality?set=XX (where XX = quality %, application/xml)
/settings/gps_active?set=on/off
/settings/motion_limit?set=XXX (Motion trigger amount, application/xml)
/settings/adet_limit?set=XXX (Audio trigger amount, application/xml)
/video (video stream)
/audio.wav (audio wav stream)
/audio.opus (audio opus stream)
/ptz?zoom=X (where X = zoom level 0-15, application/xml)
/startvideo?force=1 (?)
/status.json?show_avail=1 (optional show_avail=1 shows all available options, application/json)
/sensor.json?from=TIMESTAMP&sense=SENSOR1,SENSOR2 (application/json)
I’m not a programmer, so this is the best I’ve been able to do with what the IP Webcam app and HA provide out of the box. I hope to create some automations for turning on/off lights based on motion + time.
Note
I used the free version of the Android IP Webcam app but plan on purchasing the pro version, to support the developer and remove the ads, provided I stick with these Android devices as cameras.
References
- Project for interacting with Android IP Camera app from SmartThings clued me in to how to properly format the JSONPath for value_template.
- Excellent JSONPath expression tester helped me figure out the exact value_template I needed to get the specific object from the output provided in status.json and sensors.json.
- IP Webcam JavaScript (
http://IP.ADDRESS:8080/js/ipwebcam.js
) has some information on how it charts the sensors on the sensors graph, including min/max for each sensor. - Extra hidden features cheats documentation for Android IP Webcam app. (Thanks @Zen)