I’ve got several camera automations that I want to do the following:
Take a snapshot of the camera when motion is detected
Continue taking snapshots every X seconds while motion continues to be detected
Upload the first snapshot to discord as a notification
While I have something that works I currently have to duplicate these automations for every camera which is tedious and error prone. I’m wondering if there’s any great method for reducing this duplication.
I think in my ideal world I’d just be able to add the camera to a list of entities when adding a new one, and have everything work.
This is what I’m currently duplicating for each camera, although I might make it more complex which is what’s prompting this question:
- alias: 'Camera: Snapshot on Motion (Back Porch)'
id: camera_snapshot_on_motion_back_porch
trigger:
- entity_id: binary_sensor.motion_back_porch
platform: state
to: 'on'
condition: []
action:
- alias: 'snapshot while back porch camera is detecting motion'
repeat:
until:
- condition: state
entity_id: binary_sensor.motion_back_porch
state: 'off'
sequence:
- service: input_text.set_value
entity_id: input_text.back_porch_last_image
data_template:
value: '/media/camera-snapshots/back_porch_{{ now().strftime("%Y%m%dT%H%M%S") }}.jpg'
- service: camera.snapshot
data_template:
entity_id: camera.back_porch
filename: "{{ states('input_text.back_porch_last_image') }}"
- delay: 10
- alias: 'Discord: Upload on Motion (Back Porch)'
id: discord_upload_on_motion_back_porch
trigger:
- entity_id: binary_sensor.motion_back_porch
platform: state
to: 'on'
condition: []
action:
- delay: 5 # ensure camera has had the time to snapshot
- service: notify.discord
data_template:
message: "Motion on Back Porch!"
target:
- "111"
data:
images:
- "{{ states('input_text.back_porch_last_image') }}"
My troubles are:
I feel like I could move the various actions into a script, and call them with the various parameters, but there’s a number of them to manage
camera filename prefix
camera entity id
motion sensor entity id
I’m not sure I can actually parameterize/templatize an entity_id, or at least couldn’t find any examples
I’m using an input_text to store the name of the image that should be uploaded to discord because unlike the snapshots, I only want to do that once, not every 10s
Wonder if anyone’s got some great ideas, I’m sort of stuck with the duplication for now!
Here is an example for an automation I use to send notifications and snapshots of various cameras based on which motion sensor triggered it (I don’t have camera’s for all the sensors hence the “dummy” stuff):
it doesn’t handle the repeat function but you might figure that part out based on this example. It shouldn’t be too bad incorporating that feature to the above.
I haven’t switched any of my automations, etc to fully embrace the newer functionalities yet unless I’m writing new ones. And most of those haven’t been able to necessarily benefit from the “variables” functionality so I usually forget about it.
It looks like this one is definitely one I can see updating to use it.
I haven’t updated anything to use the new trigger “id” function yet either.