7.6 KiB
weirdtangent/amcrest2mqtt
Expose multiple Amcrest cameras and events to an MQTT broker, primarily designed to work with Home Assistant. Also exposes a webrtc link, if you have one, so a live feed can be viewed from within Home Assistant (on a dashboard, not on the entity page for the camera)
Uses the python-amcrest library.
Forked from dchesterton/amcrest2mqtt
Docker
For docker-compose, use the configuration included in this repository.
Using the docker image, mount your configuration volume at /config (and see the included config.yaml.sample file to include in there as config.yaml) or use the ENV variables if you must. You can also mount a media volume at /media and motion recordings (up to a max size you specify) will be stored there to fill up your disk space!
It supports the following environment variables - but these are a pain, the config file is easier!
-
AMCREST_HOSTS(required, 1+ space-separated list of hostnames/ips) -
AMCREST_NAMES(required, 1+ space-separated list of device names - must match count of AMCREST_HOSTS) -
AMCREST_PORT(optional, default = 80) -
AMCREST_USERNAME(optional, default = admin) -
AMCREST_PASSWORD(required) -
AMCREST_WEBRTC_HOST(optional, webrtc hostname for link, but then link/sources below become required:) -
AMCREST_WEBRTC_PORT(webrtc port, default = 1984) -
AMCREST_WEBRTC_LINK(webrtc stream link, default = 'stream.html') -
AMCREST_WEBRTC_SOURCES(webrtc "Source" param for each camera, same count and order of AMCREST_HOSTS above) -
MQTT_USERNAME(required) -
MQTT_PASSWORD(optional, default = empty password) -
MQTT_HOST(optional, default = 'localhost') -
MQTT_QOS(optional, default = 0) -
MQTT_PORT(optional, default = 1883) -
MQTT_PROTOCOL(optional, default = '5') - MQTT protocol version: '3.1.1' or '5' -
MQTT_TLS_ENABLED(required if using TLS) - set totrueto enable -
MQTT_TLS_CA_CERT(required if using TLS) - path to the ca certs -
MQTT_TLS_CERT(required if using TLS) - path to the private cert -
MQTT_TLS_KEY(required if using TLS) - path to the private key -
MQTT_PREFIX(optional, default = amgrest2mqtt) -
MQTT_DISCOVERY_PREFIX(optional, default = 'homeassistant') -
MEDIA_PATH(optional) - path to store motion recordings (mp4) files -
MEDIA_SOURCE(optional) - HomeAssistant url for accessing those recordings (see config.yaml.sample) -
STORAGE_UPDATE_INTERVAL(optional, default = 900) - how often to fetch storage stats (in seconds) -
SNAPSHOT_UPDATE_INTERVAL(optional, default = 60) - how often to fetch camera snapshot (in seconds)
It exposes through the new 2024 HomeAssistant device discovery a service plus a camera with multiple components for each camera you specify:
homeassistant/device/amcrest2mqtt_service- service confighomeassistant/device/amcrest2mqtt_[SERIAL_NUMBER]per camera, with components:
Snapshots/Eventshots plus Home Assistant Area Cards
The camera snapshots work really well for the HomeAssistant Area cards on a dashboard - just make this MQTT camera device is the only camera for an area and place an Area card for that location on a dashboard.
An "event snapshot" (eventshot) is separately (and specifically, by filename) collected IF the camera automatically records a snapshot because of an event. Note, that if the Amcrest camera is configured to record 3 or 5 snapshots on an event - each of those may be seen and updated by amcrest2mqtt and you will very quickly end up with the last snapshot.
WebRTC
The WebRTC option works with the go2rtc package which is a streaming server that works very well for (my) Amcrest cameras. If you setup the WebRTC config here, there will be a camera.<name> webrtc which you can put on a dashboard with the entity card. It will show a small camera icon and likely say "Idle", but if you click on it (and give it a little time to warm up) you will see the live-streaming feed from the webrtc server.
Device Support
The app supports events for any Amcrest device supported by python-amcrest.
Running the app
To run via env variables with Docker Compose, see docker-compose.yaml or make sure you attach a volume with the config file and point to that directory, for example:
CMD [ "python", "-m", "amcrest2mqtt", "-c", "/config" ]
Healthcheck
There is a simple healthcheck that can be run, as seen in the sample docker-compose. The app simply touches a file in /tmp every 60 seconds, so while the app is functional, that file should keep getting hit. /app/src/healthcheck.py will check that and return true or false.
Mounted Volume Permissions (Synology)
If you mount a host folder into /media for saving recordings, ensure the container has write access. On Synology NAS, shared folders use ACLs that can block Docker containers even when chmod 777 appears open.
To reset permissions and make the volume writable by the container’s default user (uid=1000, gid=1000), run the following via SSH (alter for your path):
sudo synoacltool -del /volume1/photo/Amcrest
sudo chmod 777 /volume1/photo/Amcrest
sudo chown 1000:1000 /volume1/photo/Amcrest
Then verify inside the container:
docker exec -it amcrest2mqtt ls -ld /media
You should see permissions like:
drwxrwxrwx 1 appuser appuser ... /media
Once configured correctly, you should see new recordings appear in your mounted folder with ownership 1000:1000 and a symlink to the latest file.
Also, make sure you have
environment:
- TZ=America/New_York
in your docker-compose if you want the recording filenames to by local time and not UTC.
Out of Scope
Non-Docker Environments
Docker is the only supported way of deploying the application. The app should run directly via Python but this is not supported.