Dynamic entity id on automation

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)

description: ''
trigger:
  - platform: state
    entity_id:
      - binary_sensor.greenhousecam_motion_sensor
    from: 'off'
    to: 'on'
condition: []
action:
  - service: camera.snapshot
    data:
      filename: /config/www/tmp/snapshot_{{ trigger.entity_id }}.jpg
    target:
      entity_id: camera.greenhousecam_motion_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.entity_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

Thanks!
German

Object id is what you are after. entity_iddomain.object_id. Also don’t forget to quote your single line templates.

action:
  - service: camera.snapshot
    data:
      filename: "/config/www/tmp/snapshot_{{ trigger.to_state.object_id }}.jpg"

The problem here is that the entity I capture the snapshot from is not the same one reporting the motion (on the trigger)

Then rename one of them.

But of course, I was taking the complicated route! I don’t know why I haven’t thought of that.

Thanks, @tom_l !

Actually, even if I rename the entity, they’re both on different domains, so how to solve for that?

Trigger (any of these could trigger motion):
binary_sensor.cam1_motion_sensor
binary_sensor.cam2_motion_sensor
binary_sensor.cam3_motion_sensor

Action (save a snapshot):
service: camera.snaphot
entity_id: camera.camX_motion_snapshot

I’m looking to resolve the X above, which has to be taken from a different entity (on a different domain)

You could set the trigger id for each trigger to the entity you want to use for the snapshot, then names don’t even have to match.

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.

The problem child here is this:

action:
  - service: camera.snapshot
    data:
      filename: /config/www/tmp/snapshot_{{ trigger.entity_id }}.jpg
    target:
      entity_id: camera.<<<dynamic>>>_motion_snapshot

try this,

action:
  - service: camera.snapshot
    data:
      filename: /config/www/tmp/snapshot_{{ trigger.to_state.object_id }}.jpg
    target:
      entity_id: camera.{{trigger.to_state.name[:-6]}}snapshot
1 Like

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
1 Like

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
1 Like