what is the best way to publish a camera snapshot for download? It doesn’t have to be open web - local network is ok.
I’m doing camera snapshot every day at noon and want to use that photo on other machine. /local folder is not ok for this because of aggressive caching. The other machine can download image with static url. Can I turn off the cache for specific folder? What are my other options? nginx url rewrite?
i dunno about “best”, but here’s a grab from one of my scripts:
- target:
entity_id: camera.am_garage
data:
filename: /config/www/tmp/garage.jpg
action: camera.snapshot
- delay:
hours: 0
minutes: 0
seconds: 10
milliseconds: 0
- target:
entity_id: camera.am_garage
data:
filename: /config/www/tmp/garage_a.jpg
action: camera.snapshot
- data_template:
message: |
garage door {{ door }}
http://homeassistant.local:8123/local/tmp/garage.jpg
http://homeassistant.local:8123/local/tmp/garage_a.jpg
title: garage snapshot {{ now().strftime('%H:%M:%S')}}
data:
images:
- /config/www/tmp/garage.jpg
- /config/www/tmp/garage_a.jpg
action: notify.homeassistant_gmail
this is invoked when a garage door opens. then it takes 2 snapshots (let’s me see if someone’s coming vs going) and sends them to me both as an attachment as well as the url for download.
Yes ok. But when you open this url the image is cached for 31 days. So next time you open it it will show you old image. That is unless hass was restarted. So this is not good for publishing an image. For push it works ok.
again “best” is subjective but there are numerous Blueprints on the Forum that can do (some of) this…
Have a look at this one for example and you could tweak to suit your needs
‘best’ would be the one that actually work. I have a script that makes a snapshot every day and saves the image. Then it can also be downloaded externally from /local path. Problem is that this method also makes the image cached so next day I cannot see any newer picture. You can even delete the original image and web server will still serve you with cached version. So camera snapshot is ok but then what?
you can change the name from a fixed name to one that encodes the date/time so that it’s not the old image
Sure but then the download side needs to know or calculate proper download filename which is not always possible. So my question is still valid. What is the best way to publish camera snapshot for automated download?
What I’ve tried and failed as not working:
- adding ?v=something to the download url: it just doesn’t work. Image is cached, parameter has no influence on that
- using /api/camera_proxy/camera.mycamera?token= long-lived tokens don’t work here
- using image url from Media Sources browser: url has parameter authSig but the token used there is somehow one-time only and image is still cached with it. Long-lived tokens don’t work there
- nginx rewrite might help with rewriting image url but I can’t figure it out
My blueprint linked above has an option to archive snapshots.
How is this relevant?
Host the image on a webserver you control and can configure however you want (as in, turn caching off). Especially if it’s just for local access.
Set up ftp or a SCP script to send the image to the webserver, hit that webserver with your request for the image.
Ok, this looks like a viable solution. And there is even a tutorial for that Tutorial: Making your first add-on | Home Assistant Developer Docs custom addon with http server. I think this is the way
The person I replied to asked about saving snapshots while changing the name and adding a date/timestamp, hence I replied saying I integrated that. Your latest snapshot is in the same folder, while keeping the same name Last_motion
and when another is saved, that file will be moved into the archive folder and replaced with the current snapshot.
“Sure but then the download side needs to know or calculate proper download filename which is not always possible. So my question is still valid. What is the best way to publish camera snapshot for automated download?”
Seems like your issue if directing a script to a specific file and download it automatically. My blueprint publishes the snap, from there you can send it anywhere, theoretically.
So, to sum up what I have now. In case anybody would be looking for a solution for cacheless webserver within HAss.
- Using this officail tutorial Tutorial: Making your first add-on
- In the /addons folder I’ve created webserver catalog and in there
- Dockerfile
ARG BUILD_FROM
FROM $BUILD_FROM
# Install requirements for add-on
RUN \
apk add --no-cache \
python3
WORKDIR /media/www
# Copy data for add-on
COPY run.sh /
RUN chmod a+x /run.sh
CMD [ "/run.sh" ]
- config.yaml
name: "webserver"
description: "cacheless webserver"
version: "2024.11.6"
slug: "webserver"
init: false
arch:
- aarch64
- amd64
- armhf
- armv7
- i386
startup: services
ports:
8000/tcp: 8000
ingress: true
ingress_port: 8000
map:
- type: media
- type: homeassistant_config
path: /config
- run.sh
#!/usr/bin/with-contenv bashio
echo "Hello world!"
cd /media/www
python3 -m http.server
And that’s it.
This will give you additional local addon which can be installed
This configuration will give you cacheless http on homeassistant.local:8000 with directory listing of /media/www
Is it the safest? No. I cannot recommend this if you have HAss running with public IP.