Hi,
I’m trying to setup an automation for all external cameras to send me a push notification with a snapshot of the video.
The code below works fine for just one camera, but I can’t find a way to take the name of the entity and remove “_sensor”, replacing it with “_snapshot” so I can make the code dynamic and add all the cameras in there.
For example:
Entity defined in the trigger: binary_sensor.greenhousecam_motion_sensor
Entity to be used in the same action: camera.greenhousecam_motion_snapshot
For each camera I have the same entities (i.e. drivewaycam_motion_sensor and drivewaycam_motion_snapshot)
I do something similar with my cameras, here are some examples from my camera notifications to my phone
binary_sensor.southwest_motion - this is the trigger
camera.southwest_motion_snap - image retrieved from MQTT
sensor.southwestdetections - AI results of the image
{{trigger.to_state.name[:-7]}} Camera triggered by a {{ states('sensor.'
~trigger.to_state.name[:-7]~'detections')}}
image: /api/camera_proxy/camera.{{trigger.to_state.object_id}}_snap
you can see the [:-7] removes the last 7 characters from the name of the trigger
the truncated name also gets ‘detections’ appended so i can use it in a very similar way to how you will use it i think
The idea would be, if cam2 detects movement (binary_sensor.cam2_motion_sensor) then I’d like to take a snapshot of only cam2 (camera.cam2_motion_snapshot) and not all.
If you have a separate trigger for each, then they can all have different trigger ids. The trigger id’s can be anything you want. So set the trigger id to what you want to put in the spot of dynamic. So if you can not manipulate entity ids to fit your needs, you can use the following trick:
alias: Someone came home
description: ''
trigger:
- platform: state
entity_id: person.edwin
id: beer
to: home
- platform: state
entity_id: person.fleur
id: wine
to: home
- platform: state
entity_id: person.rianne
to: home
id: soda
condition: []
action:
- service: tts.cloud_say
data:
entity_id: media_player.keuken_hub
message: '{{ trigger.to_state.name }} came home and wants some {{ trigger.id }}'
mode: single
Thanks, @chris.huitema! Your solution worked, I just needed to replace name by object_id on it (I was getting the friendly name instead of the entity_id)
alias: Test Motion
description: ''
trigger:
- platform: state
entity_id:
- binary_sensor.greenhousecam_motion_sensor
- binary_sensor.backyardcam_motion_sensor
- binary_sensor.drivewaycam_motion_sensor
from: 'off'
to: 'on'
condition: []
action:
- service: camera.snapshot
data:
filename: /config/www/tmp/snapshot_{{trigger.to_state.object_id}}.jpg
target:
entity_id: camera.{{trigger.to_state.object_id[:-6]}}snapshot
- service: notify.family
data:
title: Motion Detected
message: >-
Motion detected in the
{{trigger.to_state.attributes.friendly_name|replace(" Motion Sensor",
"")}} at {{now().strftime('%H:%M')}}
data:
image: /local/tmp/snapshot_{{trigger.to_state.object_id}}.jpg
channel: Motion
notification_icon: mdi:motion-sensor
url: /lovelace-main/outcams
clickAction: /lovelace-main/outcams
group: motion-detection-group
timeout: 1200
mode: single