Cameras and Motion

@shep

These look promising. Do they have a battery life indicator in their API? I’d like to be proactive about replacing batteries, rather than realize “oh dang, HASS reports the camera as off, now I need to order batteries.” Battery life would be a big concern for me… and are probably the reason why I’d stay away from this vs something that is POE.

1 Like

According to the linked post, there is a separate battery level sensor for each camera

If you are happy with a DIY solution we have a thread on use of raspberry pi camera for motion detection etc Door bell with pi camera and motion detection

1 Like

Annnd this is what I’ve been looking for. Now to write an AppDaemon app for this :slight_smile:

1 Like

Hey any chance you can post your code? I have the R2 and can’t get it to work.

Here’s what I use (per R2 camera):

Switches:

- platform: command_line
  switches:
    camera_bedroom_infrared:
      command_on: 'curl -k "https://192.168.xxx.xx:443/cgi-bin/CGIProxy.fcgi?cmd=openInfraLed&usr=YOURUSER&pwd=YOURPASSWORD"'
      command_off: 'curl -k "https://192.168.xxx.xx:443/cgi-bin/CGIProxy.fcgi?cmd=closeInfraLed&usr=YOURUSER&pwd=YOURPASSWORD"'
      command_state: 'curl -k --silent "https://192.168.xxx.xx:443/cgi-bin/CGIProxy.fcgi?cmd=getDevState&usr=YOURUSER&pwd=YOURPASSWORD" | grep -oP "(?<=infraLedState>).*?(?=</infraLedState>)"'
      value_template: '{{ value == "1" }}'
    camera_bedroom_motion:
      command_on: 'curl -k "https://192.168.xxx.xx:443/cgi-bin/CGIProxy.fcgi?cmd=setMotionDetectConfig1&isEnable=1&linkage=12&snapInterval=1&triggerInterval=10&schedule0=281474976710655&schedule1=281474976710655&schedule2=281474976710655&schedule3=281474976710655&schedule4=281474976710655&schedule5=281474976710655&schedule6=281474976710655&width1=10000&height1=10000&sensitivity1=2&valid1=1&usr=YOURUSER&pwd=YOURPASSWORD"'
      command_off: 'curl -k "https://192.168.xxx.xx:443/cgi-bin/CGIProxy.fcgi?cmd=setMotionDetectConfig1&isEnable=0&usr=YOURUSER&pwd=YOURPASSWORD"'
      command_state: 'curl -k --silent "https://192.168.xxx.xx:443/cgi-bin/CGIProxy.fcgi?cmd=getMotionDetectConfig1&usr=YOURUSER&pwd=YOURPASSWORD" | grep -oP "(?<=isEnable>).*?(?=</isEnable>)"'
      value_template: '{{ value == "1" }}'

Sensors:

- platform: command_line
  name: "camera bedroom motion"
  command: 'curl -k --silent "https://192.168.xxx.xx:443/cgi-bin/CGIProxy.fcgi?cmd=getDevState&usr=YOURUSER&pwd=YOURPASSWORD" | grep -oP "(?<=motionDetectAlarm>).*?(?=</motionDetectAlarm>)"'
  value_template: >-
    {%- if value == "0" -%}
      Disabled
    {%- elif value == "1" -%}
      None
    {%- elif value == "2" -%}
      Detected
    {%- endif -%}
  scan_interval: 5

Shell_commands:

camera_bedroom_preset_off: 'curl -k "https://192.168.xxx.xx:443/cgi-bin/CGIProxy.fcgi?cmd=ptzGotoPresetPoint&name=Off&usr=YOURUSER&pwd=YOURPASSWORD"'
camera_bedroom_preset_room: 'curl -k "https://192.168.xxx.xx:443/cgi-bin/CGIProxy.fcgi?cmd=ptzGotoPresetPoint&name=Room&usr=YOURUSER&pwd=YOURPASSWORD"'
camera_bedroom_zoom_out: 'curl -k "https://192.168.xxx.xx:443/cgi-bin/CGIProxy.fcgi?cmd=zoomOut&usr=YOURUSER&pwd=YOURPASSWORD"'

Scripts (only so I can show the shell_commands in the UI):

camera_bedroom_preset_off:
  sequence:
    service: shell_command.camera_bedroom_preset_off
camera_bedroom_preset_room:
  sequence:
    service: shell_command.camera_bedroom_preset_room
8 Likes

@fanaticDavid can you share your motion detection curl command i cant seem to get mine to work I keep getting:

curl -k "https://192.168.2.165/cgi-bin/CGIProxy.fcgi?cmd=setMotionDetectConfig&isEnable=1&usr=USER&pwd=PASSWORD"

<CGI_Result>
    <result>-3</result>
</CGI_Result>

all the other foscam ones work like a charm, but this on which is an R2 V3 does not

I found this at the end. Thanks this solved my issue!!!

how you do this?

Using a command_line sensor with a curl command. I shared the code in my previous post in this thread, a couple of posts up from this one.

I did recently switch from a regular command line sensor to a binary command line sensor. That sensor looks like this:

- platform: command_line
  name: "camera bedroom motion"
  command: 'curl -k --silent "https://xxx.xxx.xxx.xx:443/cgi-bin/CGIProxy.fcgi?cmd=getDevState&usr=YOURUSER&pwd=YOURPASSWORD" | grep -oP "(?<=motionDetectAlarm>).*?(?=</motionDetectAlarm>)"'
  value_template: >-
    {%- if value == "2" -%}
      motion
    {%- else -%}
      none
    {%- endif -%}
  payload_on: motion
  payload_off: none
  device_class: motion
  scan_interval: 5

device_class: motion makes it so the off state shows as clear in the UI, and on as detected. Bare in mind the actual state of the binary sensor is still off or on. And don’t forget to replace xxx.xxx.xxx.xx with the ip address of your Foscam camera, as well as YOURUSER and YOURPASSWORD with your correct credentials.

hello, I am new at this and I have a foscam too, could you tell me how can I integrate it to the home assistant thx

Awesome info!! Worked like a charm.
Just one comment in case anyone faces a similar issue:
the getDevState seems to require credentials for an Administrator role. Using an Operator won’t be enough.

1 Like

@ fanaticDavid thank you very much for sharing the curl commands. There’re other projects but the simplicity and practicality of your approach is best. All I wanted was to be able to use the motion sensor and this worked fine on my Foscam C1. I couldn’t get the switches to work but I guess this is due to camera differences. Thank you again.

1 Like

@Maxlandr

What exactly are you trying to do?

@fanaticDavid

Thanks for this!

I had my cameras sending motion detected through Blue Iris on my PC. For some reason my garage camera had a really long delay from the time motion was detected (and Blue Iris started recording) till BI sent the motion detected signal to HA. It was a delay of anywhere from 15 to 30 seconds!

The rest of my cameras (4 others) were pretty much immediate. As soon as BI started recording based on motion it sent the signal to HA. I’m not sure why that camera is so delayed. As far as I can see the config for all of them are exactly the same.

This allows me to get an almost instant response from the camera to HA (based on the polling interval…) and then turn my lights on. And if I have any issues with my Blue Iris/PC I will still have access to the camera motion status.

The Dahua Starlight 5231 IP cam is one of the best bang for the buck POE IP cameras on the market. See extensive review and discussion here: https://ipcamtalk.com/threads/dahua-starlight-varifocal-turret-ipc-hdw5231r-z.14683/

Camera-based motion detection is pretty inaccurate and ends in lots of false positives. New cameras from Hikvision and Dahua and other manufacturers include smarter motion detection with virtual tripwires, zone intrusion, etc. that is significantly more accurate.

User cor35vet on ipcamtalk wrote a small python script to monitor Dahua IVS events. See here: https://ipcamtalk.com/threads/trigger-blue-iris-with-ivs.17303/#post-1631964. I modified it to send MQTT messages so I could incorporate my cameras into HomeAssistant. See here: https://github.com/johnnyletrois/dahua-watch/blob/master/watch.py. It would be awesome if someone smarter than me would make a native component for HomeAssistant.

I use the following binary sensors to monitor the MQTT topics:

binary_sensor:
- platform: mqtt
  state_topic: "home-assistant/cameras/0/IVS"
  name: "Garage Tripwire"
- platform: mqtt
  state_topic: "home-assistant/cameras/1/IVS"
  name: "NWhouse Tripwire"
- platform: mqtt
 state_topic: "home-assistant/cameras/2/IVS"
 name: "SWhouse Tripwire"

One of my automations using the above sensors is to play a dog barking on my patio speakers and turn my office light on if the garage sensor is tripped:

- alias: Garage cam tripwire
  initial_state: true
  trigger:
    platform: state
    entity_id: binary_sensor.garage_tripwire
    to: 'on'
  condition:
    condition: and
    conditions:
      condition: time
      after: '22:30:00'
      before: '06:00:00'
  action:
    - service: notify.ios_john_iphone_app
      data:
         message: "Garage tripwire activated"
        data:
          attachment:
            url: !secret ip_cam_garage_image_login_url
            content-type: jpeg
            hide-thumbnail: false

    - service: light.turn_on
      data:
        entity_id: light.office_ceiling_level
        brightness: 255

    - service: script.dog_bark

    - delay: '00:00:{{ (range(30, 55)|random|int) }}'

    - service: light.turn_off
      entity_id: light.office_ceiling_level
1 Like

So far I’m not seeing any false positives from my Foscam cameras. Or even the one REALLY cheap Tenvis camera that I bought several years ago.

And at double the price of my Foscams I’m not seeing the advantage of that camera for my uses.

I just want to see the live image of the camera in the visu of home assistant, I add it in the configuration. Yaml as the example that comes in the page components (foscam) but in the visu of HA it says error loading image so I don’t know what is wrong thanks for your help

can you post your configuration code so we can take a look for any problems?

@fanaticDavid

So far I have three Foscam cameras working with the command line sensor as above.

I also have older Foscam and Tenvis cameras that don’t use the same CGI binary commands as the one posted.

Here is the cgi command that gets me the status:

http://192.168.1.XX:XXXX/get_status.cgi?user=XXXX&pwd=XXXXX

I’m trying to modify the command to parse out the status result I’m getting but can’t quite seem to figure it out.

Here is the result of the cgi command when I run it from a browser window:

var id=‘00626E52160D’;
var sys_ver=‘11.37.2.65’;
var app_ver=‘2.0.10.15’;
var alias=‘foscam1’;
var now=1511383922;
var tz=18000;
var alarm_status=1;
var ddns_status=500;
var ddns_host=‘XXXXXXXXX’;
var oray_type=0;
var upnp_status=0;
var p2p_status=0;
var p2p_local_port=25940;
var msn_status=0;
var wifi_status=0;
var ppcn=0;
var temperature=0.0;
var humidity=0;
var tridro_error=’’;

The piece I’m trying to parse out and test for is “var alarm_status=1”

Here is what I have as the command line:

'curl -k --silent "http://192.168.1.XX:XXXX/get_status.cgi?user=XXXX&pwd=XXXXX" | grep -oP "(?<=alarm_status=).*?(?=;>)"'

I get an error that says “Command failed”.

I did a little searching and it led me to try the following but it failed as well:

'curl -k --silent "http://192.168.1.XX:XXXX/get_status.cgi?user=XXXX&pwd=XXXXX" | grep -oP "\alarm_status=\K.*(?=\; var ddns_status)"'

I’m very new to linux. Where is my error?

I have an ip camera which use as a ( platform: generic ) . They show up in my HA nicely.
**I would like to put the on-off switch which show in ui. Most of my camera command are not the same as foscam or other cameras. They don’t have “getDevState”

The camera has cgi command by http. However, I couldn’t understand some command such as
command_state: ‘curl -k --silent “https://192.168.xxx.xx:443/cgi-bin/CGIProxy.fcgi?cmd=getDevState&usr=YOURUSER&pwd=YOURPASSWORD” | grep -oP “(?<=infraLedState>).*?(?=)”’
value_template: ‘{{ value == “1” }}’

Can you recommand and show me step by step.

Here is the list of commands for my camera which I put as - platform: command_line
platform: command_line
switches:
office_cam_infrared:
friendly_name: “Infrared”
command_on: ‘curl -k “http://xxx:[email protected]:8300/cgi-bin/hi3510/param.cgi?cmd=setinfrared&-infraredstat=open”’
command_off: ‘curl -k “http://xxx:[email protected]:8300/cgi-bin/hi3510/param.cgi?cmd=setinfrared&-infraredstat=close”’
office_cam_infrared_auto:
friendly_name: “Infrared Auto”
command_on: ‘curl -k “http://xxx:[email protected]:8300/cgi-bin/hi3510/param.cgi?cmd=setinfrared&-infraredstat=auto”’
command_off: ‘curl -k “http://xxx:[email protected]:8300/cgi-bin/hi3510/param.cgi?cmd=setinfrared&-infraredstat=close”’
value_template: ‘{{ value == “1” }}’
office_cam_snapshot_email:
friendly_name: “Snapshot to Email”
command_on: ‘curl -k “http://xxx:[email protected]:8300/cgi-bin/hi3510/param.cgi?cmd=setsnaptimerattrex&-as_enable=1&-as_type=email”’
command_off: ‘curl -k “http://xxx:[email protected]:8300/cgi-bin/hi3510/param.cgi?cmd=setsnaptimerattrex&-as_enable=0&-as_type=email”’
office_cam_snapshot:
friendly_name: “Snapshot to SD”
command_on: ‘curl -k “http://xxx:[email protected]:8300/cgi-bin/hi3510/param.cgi?cmd=setsnaptimerattrex&-as_enable=1&-as_type=snap”’
command_off: ‘curl -k “http://xxx:[email protected]:8300/cgi-bin/hi3510/param.cgi?cmd=setsnaptimerattrex&-as_enable=0&-as_type=snap”’
office_cam_move_left:
friendly_name: “Move left”
command_on: ‘curl -k “http://xxx:[email protected]:8300/cgi-bin/hi3510/ptzctrl.cgi?-step=1&-act=left”’
command_off: ‘curl -k “http://xxx:[email protected]:8300/cgi-bin/hi3510/ptzctrl.cgi?-step=1&-act=home”’
office_cam_move_right:
friendly_name: “Move right”
command_on: ‘curl -k “http://xxx:[email protected]:8300/cgi-bin/hi3510/ptzctrl.cgi?-step=1&-act=right”’
command_off: ‘curl -k “http://xxx:[email protected]:8300/cgi-bin/hi3510/ptzctrl.cgi?-step=1&-act=home”’
office_cam_move_up:
friendly_name: “Move up”
command_on: ‘curl -k “http://xxx:[email protected]:8300/cgi-bin/hi3510/ptzctrl.cgi?-step=1&-act=up”’
command_off: ‘curl -k “http://xxx:[email protected]:8300/cgi-bin/hi3510/ptzctrl.cgi?-step=1&-act=home”’
office_cam_move_down:
friendly_name: “Move down”
command_on: ‘curl -k “http://xxx:[email protected]:8300/cgi-bin/hi3510/ptzctrl.cgi?-step=1&-act=down”’
command_off: ‘curl -k “http://xxx:[email protected]:8300/cgi-bin/hi3510/ptzctrl.cgi?-step=1&-act=home”’
office_cam_alarm:
friendly_name: “Cam Alarm”
command_on: ‘curl -k “http://xxx:[email protected]:8300/cgi-bin/hi3510/param.cgi?cmd=setscheduleex&-ename=md&-week0=PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP&-week1=PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP&-week2=PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP&-week3=PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP&-week4=PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP&-week5=PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP&-week6=PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP&”’
command_off: ‘curl -k “http://xxx:[email protected]:8300/cgi-bin/hi3510/param.cgi?cmd=setscheduleex&-ename=md&-week0=NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN&-week1=NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN&-week2=NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN&-week3=NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN&-week4=NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN&-week5=NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN&-week6=NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN&”’
office_cam_motion:
friendly_name: “Cam Motion”
command_on: ‘curl -k “http://xxx:[email protected]:8300/cgi-bin/hi3510/param.cgi?cmd=setmdattr&-enable=1&-s=50&-name=1&-x=0&-y=0&-w=720&-h=1280”’
command_off: ‘curl -k “http://xxx:[email protected]:8300/cgi-bin/hi3510/param.cgi?cmd=setmdattr&-enable=0&-s=50&-name=1&-x=0&-y=0&-w=720&-h=1280”’