A really quick one today to update my post about camera snapshot notifications in Home Assistant.
In that post I showed the shell command you could use to take a snapshot from any camera visible to Home Assistant. In the very next release, that functionality was added to the camera component so that you can call a service directly from within Home Assistant.
Instead of using a shell command:
action: - service: shell_command.take_snapshot data_template: url: 'http://localhost:8123{{ states.camera.mjpeg_camera.attributes.entity_picture }}' filename: '/tmp/snapshot.jpg'
You can now simply write:
action: - service: camera.snapshot data: entity_id: camera.mjpeg_camera filename: "/tmp/snapshot.jpg"
And of course you no longer need the shell command defined.
There is one other thing you need to do to configure it. You need to add the path to the whitelist of directories to which Home Assistant can write. This goes into your configuration.yaml under the homeassistant section:
whitelist_external_dirs: - /tmp
I think this is a great change – it just feels cleaner to do it all directly within Home Assistant.
As it’s now a service, it’s also easy to test from the front end using the service call under developer tools:
There is another change in 0.57 that turned out to be a breaking change for my method of showing the snapshot and then hiding it when the notification is dismissed.
Instead of the state of a persistent notification being the full notification text, it is now set to “notifying” and the message and title are available as attributes. That means that instead of the notification dismissal trigger from my example being:
trigger: platform: state entity_id: persistent_notification.doorbell from: "Doorbell rung"
It should be:
trigger: platform: state entity_id: persistent_notification.doorbell from: "notifying"
I like this change as well. I wasn’t really happy with hard coding the text of the notification in the trigger as it forced you to change the trigger if you changed the text of the notification to keep it in sync with the notification. Again, I think the new way is much cleaner and satisfies the DRY principle.
I’ve edited the original blog post to reflect this change but I thought it was worth another quick post about the changes. They’re both changes for the better and highlight how quickly Home Assistant is evolving.
It’s always worth keeping an eye on the changelog for updates – not only are new platforms added with every release, the core functionality of the existing components keeps growing as well.
in Home Automation
Hi, thnx for this post. I’m looking for a automation script which also delete the snapshot from the tmp folder after a couple of days. How can I configure this and do you have an example yaml?
As you can see from my example, I just keep the snapshot file but hide the notification. Each ring of the doorbell overwrites /tmp/snapshot.jpg with a new file. In fact, at the time of writing (and it might still be the case), I think Home Assistant needed a file there for the local_file camera entity to work.
That said, the quickest way to delete old files is probably to set up a cron job outside of HA.
Otherwise you could use a shell command in HA to find snapshot files and delete them. Off the top of my head (and I’m writing this without testing, so use caution…), the shell command (which you could also use in cron) would be:
find /tmp -name ‘snapshot.jpg’ -mtime +2 -exec rm {} \;
Where +2 indicates files older than two days.