Doubts on how to merge multiple automations into a single one

Hi all.

I have been searching around, but I’m a bit confused on whether I should create a blueprint for this, a template, a script, or something else. I understand HA is rapidly evolving and so 2 year old posts may not reflect the current state of things, so I’d appreciate a little bit of guidance.

In summary, I have 5 reolink cameras, and I have set up an automation for each of them that does exactly the same thing: after detecting a person, it will notify me through the Android app, and optionally turn on the floodlight if it’s dark. Each automation uses the entities, triggers and IDs associated with that particular camera.

Here’s the full yaml for one of the cameras, mostly created through the GUI:

alias: Person detected in garage
description: ""
triggers:
  - type: turned_on
    device_id: DEVICE_ID
    entity_id: ENTITY_ID
    domain: binary_sensor
    trigger: device
conditions: []
actions:
  - action: camera.snapshot
    metadata: {}
    data:
      filename: /config/www/snapshots/garage.jpg
    target:
      device_id: DEVICE_ID
  - delay:
      hours: 0
      minutes: 0
      seconds: 1
      milliseconds: 0
  - action: notify.mobile_app_pixel_9_pro
    metadata: {}
    data:
      message: Person detected in garage
      data:
        image: http://my_local_ip:8123/local/snapshots/garage.jpg
        clickAction: >-
          intent://scan/#Intent;scheme=reolink;package=com.mcu.reolink;action=android.intent.action.VIEW;S.UID=REOLINK_DEVICE_UID;S.DEVNAME=Garage;S.ALMTYPE=PEOPLE;S.ALMCHN=1;S.ALMNAME=Detection;S.ALMTIME={{now().isoformat()}};end
        ttl: 0
        priority: high
  - delay:
      hours: 0
      minutes: 0
      seconds: 30
      milliseconds: 0
  - if:
      - condition: time
        after: sensor.sun_next_dusk
        before: sensor.sun_next_dawn
    then:
      - action: light.turn_on
        metadata: {}
        data:
          brightness_pct: 50
        target:
          device_id: DEVICE_ID
      - delay:
          hours: 0
          minutes: 0
          seconds: 10
          milliseconds: 0
      - action: light.turn_off
        metadata: {}
        data: {}
        target:
          device_id: DEVICE_ID
mode: single

As a result, here are the “variables” that I would need to somehow map to each device:

  • DEVICE_ID: Right now, this is a long UUID that identifies my camera in HA (e.g.: 7989927d494...). Can I perhaps replace this with a string that can be easily mapped?
  • ENTITY_ID: Same as above, but I assume this is the entity ID corresponding to the person detection trigger of this camera.
  • http://my_local_ip:8123/local/snapshots/garage.jpg: I’m keeping it simple and just saving a screenshot to a file with the same name as the camera (+ .jpg). In this case, “garage” would be the string to use.
  • UID=REOLINK_DEVICE_UID: This is a long UID (e.g.: 9527000... that Reolink assigns to each camera. It allows me to add a deep link in the phone notification that will take me straight to that camera’s live view within the Reolink app, which is pretty neat. Unfortunately that means I’d need to somehow map that UID to each camera in the automation.
  • S.DEVNAME=Garage: The name of the camera within the Reolink app. I’ve made this consistent with everything else, so again, just the same string with an initial uppercase.

Could anybody point me to a resource where I can see how to merge all this into a single automation?

I assume most of the variables could be simplified to variants of each camera’s “friendly name”, then accessed through basic string manipulation: garage.light, garage.person, garage.jpg, Garage, etc. So I could probably just pass those friendly names like this:

- garage
- entrance
- porche
etc

However, I’m unsure if I can also somehow map Reolink’s UID to each of those cameras for the deep linking part.

Appreciate any help!

Those are not mutually exclusive options… but you can accomplish what you have described just with a few templates. But, you could also do it without templates, by using branched logic with Choose actions.

If you switch to a State trigger you can cover multiple cameras in a single automation with a few templates and changing the mode…

Automation with Templates
alias: Person detected by camera
description: ""
triggers:
  - trigger: state
    entity_id:
      - binary_sensor.entrance
      - binary_sensor.garage
      - binary_sensor.porche
    to: "on"
    variables:
      device: "{{ device_id(trigger.entity_id) }}"
      object: "{{ trigger.entity_id | replace('binary_sensor.', '') | capitalize }}"
      file: "{{ object|slugify~'.jpg' }}"      
conditions: []
actions:
  - variables:
      camera: |
        {{ device_entities(device) | select('match', 'camera.') | first }}
      reolink_uid_map:
        garage:  9527000
        entrance:  9527001
        porche:  9527002
      reolink_uid: "{{ reolink_uid_map.get(object) }}"  
  - action: camera.snapshot
    metadata: {}
    data:
      filename: "{{ '/config/www/snapshots/'~file}}"
    target:
      device_id: "{{ device }}"
  - delay:
      hours: 0
      minutes: 0
      seconds: 1
      milliseconds: 0
  - action: notify.mobile_app_pixel_9_pro
    metadata: {}
    data:
      message: Person detected in garage
      data:
        image: "{{'http://my_local_ip:8123/local/snapshots/'~file}}"
        clickAction: >-
          intent://scan/#Intent;scheme=reolink;package=com.mcu.reolink;action=android.intent.action.VIEW;S.UID={{reolink_uid}};S.DEVNAME={{object}};S.ALMTYPE=PEOPLE;S.ALMCHN=1;S.ALMNAME=Detection;S.ALMTIME={{now().isoformat()}};end
        ttl: 0
        priority: high
  - delay:
      hours: 0
      minutes: 0
      seconds: 30
      milliseconds: 0
  - if:
      - condition: time
        after: sensor.sun_next_dusk
        before: sensor.sun_next_dawn
    then:
      - action: light.turn_on
        metadata: {}
        data:
          brightness_pct: 50
        target:
          device_id: "{{ device }}"
      - delay:
          hours: 0
          minutes: 0
          seconds: 10
          milliseconds: 0
      - action: light.turn_off
        metadata: {}
        data: {}
        target:
          device_id: "{{ device }}"
mode: parallel

Templates allow us to provide dynamic values for many (but not all) configuration variables within scripts, automations, and blueprints.

Scripts are sequences of actions that can be run from multiple sources such as from multiple automations or other scripts. But they are especially helpful when you have an action sequence that you need to run from both automations and Dashboard input.

I think it’s kind of unlikely that you would be best served by making your own blueprint, but that could be personal bias. Creating a good blueprint is more difficult and nuanced than creating an automation or script.

Thanks a lot! The template you provided sounds a lot like what I want.

How would I go about mapping the Reolink UIDs to each camera, considering those are external to HA?

Is it possible to previously define some custom “global” variables, such as:

camera1_reolinkid: 12345...
camera2_reolinkid: 67890...

So that I can later access them using the same camera1 entity name I’m passing to the blueprint?

Or should I just define those inside the blueprint itself?

There are a couple options…

I would probably go for adding custom attributes in YAML to each of the camera entities. If you did it that way, using reolink_uid as the attribute ID, you could modify the automation above as follows:

...
actions:
  - variables:
      camera: |
        {{ device_entities(device) | select('match', 'camera.') | first }}
      reolink_uid: "{{ state_attr(camera, 'reolink_uid') }}"
....