Hi,
I would like to share my new configuration for use motion and sound sensors built in foscam cameras (like FOSCAM C1) as binary sensors in HA.
In my old setup (https://community.home-assistant.io/t/foscam-motion-sound-detection-and-ir-led-status-in-ha/) I was using a polling method and the problem was the latency between updates in sensors, it could take about 2 or 3 seconds (for polling and updating interval) and this way wasn’t the best for automations like turn on lights.
In my new config i’m changing from a polling method to a push method, achieve this with a foscam camera is a bit complex because foscam doesn’t provide something like a “push url” for notify changes in motion or sound sensors so … the way that i choose is intercept the http request that the camera uses for push notifications (in official foscam app).
For this setup we need a domain name local server and a web server (like apache) with PHP support.
1. Resolve the domain push-access.myfoscam.com to your local apache server (192.168.1.31 for me) instead of foscam public one
I’m using bind9 so i added a new zone (in named.conf.default-zones for ubuntu):
zone "http://push-access.myfoscam.com." {
type master;
file "/etc/bind/db.fakeroot";
};
and this is the content for /etc/bind/db.fakeroot:
;
; BIND reverse data file for broadcast zone
;
$TTL 604800
@ IN SOA localhost. root.localhost. (
2 ; Serial
604800 ; Refresh
86400 ; Retry
2419200 ; Expire
604800 ) ; Negative Cache TTL
;
@ IN NS localhost.
push-access.myfoscam.com. IN A 192.168.1.31
You can test your Domain name server with dig, for example:
$ dig +short push-access.myfoscam.com @192.168.1.101
192.168.1.31 <-- your local IP where your apache server is listening
(where 192.168.1.101 is your local dns)
2. Config your foscam camera (in settings->network->IP configuration) for use your domain name server and enable the push notifications in Detector -> Action Detection and Sound Detection.
Foscam will send a POST request on every push notification (every 60 seconds) to http://push-access.myfoscam.com/gateway, the most important variables are msgType (1 for motion or 2 for sound) and senderTag (the camera ID or mac address).
3. Create a gateway.php in /var/www/html for example:
<?php
define('BROKER', '192.168.1.34'); //Your mosquitto server ip
define('PORT', 1883);
define('CLIENT_ID', "pubclient_" + getmypid());
//Get type (1 motion, 2 sound)
$alert_type = $_POST['msgType'];
//Get camera ID
$cam_id = $_POST['senderTag'];
//Get time
$alert_time = $_POST['msgTime'];
$client = new Mosquitto\Client(CLIENT_ID);
$client->Credentials('myuser', 'mypass');
$client->connect(BROKER, PORT, 60);
$topic = "ipcamera/$cam_id/$alert_type";
$client->publish($topic, 'on', 0, false);
$client->disconnect();
?>;
This PHP will receive the foscam request and will publish a message to mqtt server.
4. Configure apache for rewrite calls from /gateway to /gateway.php
<Directory /var/www/html>;
AllowOverride All
Require all granted
RewriteEngine on
RewriteRule "gateway$" /gateway.php
</Directory>;
EDIT: After updating my foscam firmware, now the camera attempts to connect using HTTPS (TCP 443) instead of HTTP (isn’t validating the ssl certificate yet) so you also need to add this configuration to apache using https (and configure SSL certificates, etc … )
5. Create binary sensors in HA
- platform: mqtt
state_topic: 'ipcamera/<your camera ID>/1'
name: cam_plantabaja_motion
qos: 0
payload_on: 'on'
payload_off: 'off'
sensor_class: motion
- platform: mqtt
state_topic: “ipcamera/<your camera ID>/2"
name: cam_plantabaja_sound
qos: 0
payload_on: 'on'
payload_off: 'off'
sensor_class: sound
6. Create automation and scripts for turn off sensor after 50 seconds for every sensor, this is an example for motion:
alias: Turn off motion sensor for plantabaja
hide_entity: True
initial_state: 'on'
trigger:
- platform: state
entity_id: binary_sensor.cam_plantabaja_motion
to: 'on'
action:
- service: script.timed_motion_plantabaja
And create the scripts:
timed_motion_plantabaja:
sequence:
- service: script.turn_off
data:
entity_id: script.timer_off_motion_plantabaja
- service: script.turn_on
data:
entity_id: script.timer_off_motion_plantabaja
timer_off_motion_plantabaja:
sequence:
- delay:
seconds: 50
- service: mqtt.publish
data:
topic: 'ipcamera/<your camera ID>/1'
payload: 'off'
NOTE: The php script uses mosquito php so you should install it