šŸ“ø Notification with camera snapshot with blocking condition and clickaction

Hello,

This is my first blueprint and itā€™s pretty much based on the original camera motion blueprint (see šŸ“ø Send camera snapshot notification on motion ). But I expanded it by adding an (optional) condition that can block the automation. Like a binary template sensor that states if people are home or not.
I looked at Turn on light, switch, scene, script or group based on motion, illuminance, sun (+ more conditions) to see how to add optional conditions.

I also added an (optional) clickAction URL that will open when you click on the notification on your smartphone, for example open a lovelace view with a camera stream.

The goal is to only have notifications for movement detection (with camera snapshot) when we are not home. And open the lovelace view with cctv streams when i click on the notification.

Iā€™m still very new at YAML and blueprints so I hope it meets the quality expectations.
Iā€™m open to suggestions and feel free to improve where possible.

UPDATE: 25/01/2021 ā†’ New GIST url and updated the code below to give you more control of the group_name, channel_name and if you want critical/high priority notifications or not.

UPDATE: 24/07/2023 ā†’ Added a 2 sec delay after calling the snapshot service, before sending the notification.

Gist: CriticalCollapseOptionNotHomeMotionCameraSnapshotWithClickAction.yaml Ā· GitHub

Get started

Click the badge to import this Blueprint: (needs Home Assistant Core 2021.3 or higher)

Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

Or import this Blueprint by using the forum topic / Gist URL:

blueprint:
  name: Send a notification with camera snapshot when motion is detected with blocking state and URL (or lovelace view) on click, critical or not, collapse or not.
  description: >
    This automation blueprint creates a camera snapshot if motion is detected 
    and sends a notification to your phone with the picture. 
    Optionally you can define a binary sensor that will block the automation from running when state is ON.
    For Example do not run when somebody is home (you can use the people_home template example).
    Furthermore you have the option to make the notification critical or high priority and to collapse/overwrite or not.
    
    
    Required entities:
      - Motion sensor (binary_sensor in motion class)
      - Camera entity
      - Notify device entity (mobile_app).
    
    
    Optional entities:
      - Blocking entity (any entity with state on/off), example people home or not. Entity in state ON will block the automation. If needed you can create a template sensor to inverse states.
      - Delay before taking a snapshot. You can play with this a little see what values shows the person best in the snapshot picture.
      - Notification title & message
      - URI for clickAction on the notification. For example a lovelace view such as /lovelace/cctv, this will open in the app.
      - Name for the channel, different channels can have different notifications settings, such as sound etc.
      - Name for group (Android) or grouping name (iOS), to group similar notifications together
      - Overwrite (collapse) similar notifications (iOS): Yes/No
      - Should the notification be critical (iOS) or High Priority / TTL 0 (Android)? Yes/No (However on iOS this will defeat the collapse of grouping) 
  
  
  domain: automation


  input:
    motion_sensor:
      name: Motion sensor
      description: The sensor wich triggers the snapshot creation (domain binary_sensor, class motion).
      selector:
          entity:
            domain: binary_sensor
            device_class: motion

    camera:
      name: Camera
      description: The camera to create the snapshot (domain camera).
      selector:
        entity:
          domain: camera

    notify_device:
      name: Device to notify
      description: Device that runs the official Home Assistant app to receive notifications.
      selector:
        device:
          integration: mobile_app

    is_ios:
      name: Is it an iOS device?
      description: Toggle if your selected device runs iOS, default is Android
      selector:
        boolean:
      default: false

    blocker_entity:
      name: '(OPTIONAL) Blocking entity'
      description: If this entity state is on, it will prevent the automation from running. E.g. somebody is home (binary_sensor.people_home).
      default:
      selector:
        entity:

    delay:
      name: Delay (Optional)
      description: Wait before creating camera snapshot
      default: 0
      selector:
        number:
            min: 0
            max: 60
            unit_of_measurement: seconds
            mode: slider

    notification_title:
      name: Notification title (Optional)
      description: 'Default: "Motion detected!"'
      default: "Motion detected!"

    notification_message:
      name: Notification message (Optional)
      description: 'Default: "{{ motion_sensor_name }} detected movement!"'
      default: "{{ motion_sensor_name }} detected movement!"

    clickAction:
      name: ClickAction URI (Optional)
      description: An URI to open when you click on the notification, can be an external URI (https://www.google.com) or an internal URI to a lovelace dashboard or view (/lovelace/cctv).
      default: ""

    group_name:
      name: Name of the group
      description: To group notifications together for 1 automation. For instance if you want to have multiple automations but seperate them use unique names per automation.
      default: "alarm_motion"

    channel_name:
      name: Name of the channel 
      description: Different channels can have different notifications settings, such as sound etc.
      default: "alarm_motion"

    overwrite_similar:
      name: Overwrite similar
      description: Toggle on if you want to a new notification in the same group to overwrite the previous one (iOS).
      selector:
        boolean:
      default: false

    is_critical:
      name: Should the notification be critical (iOS) or High Priority (Android)?
      description: Toggle off it you don't want critical or high priority notifications.
      selector:
        boolean:
      default: true


trigger:
  platform: state
  entity_id: !input motion_sensor
  from: "off"
  to: "on"


variables:
  motion_sensor: !input motion_sensor
  motion_sensor_name: "{{ states[motion_sensor].name }}"
  camera: !input camera
  notify_device: !input notify_device
  is_ios: !input is_ios
  notification_title: !input notification_title
  notification_message: !input notification_message
  delay: !input delay
  snapshot_create_file_path: "/config/www/tmp/snapshot_{{ states[camera].object_id }}.jpg"
  snapshot_access_file_path: "{{ snapshot_create_file_path | replace('/config/www','/local') }}"
  blocker_entity: !input blocker_entity
  clickAction: !input clickAction
  is_critical: !input is_critical
  overwrite_similar: !input overwrite_similar
  channel_name: !input channel_name
  group_name: !input group_name


condition:
  - condition: template
    value_template: >
      {{ (blocker_entity == none) or (states[blocker_entity].state == 'off') }}
action:
  - delay: "{{ delay }}"

  - service: camera.snapshot
    entity_id: !input camera
    data:
      filename: "{{ snapshot_create_file_path }}"

  - delay: 00:00:02

  - device_id: !input notify_device
    domain: mobile_app
    type: notify
    title: "{{ notification_title }}"
    message: "{{ notification_message }}"
    data: >
      {% if is_ios %}
        {% if is_critical %}
          {% set platform_data = { "channel": "%s", "url": "%s", "attachment": {"url": "%s", "content_type": "JPEG"},  "push": { "category": "%s", "sound": { "name": "default", "critical": 1, "volume": 1.0 } } } | format(channel_name, clickAction, snapshot_access_file_path, group_name) %}
        {% else %}
          {% if overwrite_similar %}
            {% set platform_data = { "channel": "%s", "url": "%s", "apns_headers": { "apns-collapse-id": "%s" }, "attachment": {"url": "%s", "content_type": "JPEG"},  "push": { "category": "%s", "sound": { "name": "default", "volume": 1.0 } } } | format(channel_name, clickAction, group_name, snapshot_access_file_path, group_name) %}
          {% else %}
            {% set platform_data = { "channel": "%s", "url": "%s", "attachment": {"url": "%s", "content_type": "JPEG"},  "push": { "category": "%s", "sound": { "name": "default", "volume": 1.0 } } } | format(channel_name, clickAction, snapshot_access_file_path, group_name) %}
          {% endif %}
        {% endif %}
      {% else %}
        {% if is_critical %}
          {% if overwrite_similar %}
            {% set platform_data = { "tag": "%s", "channel": "%s", "group": "%s", "ttl": 0, "priority": "high", "clickAction": "%s", "image": "%s"} | format(group_name, channel_name, group_name, clickAction, snapshot_access_file_path) %}
          {% else %}
            {% set platform_data = { "channel": "%s", "ttl": 0, "priority": "high", "clickAction": "%s", "image": "%s"} | format(channel_name, clickAction, snapshot_access_file_path) %}
          {% endif %}
        {% else %}
          {% if overwrite_similar %}
            {% set platform_data = { "tag": "%s", "channel": "%s", "group": "%s", "clickAction": "%s", "image": "%s"} | format(group_name, channel_name, group_name, clickAction, snapshot_access_file_path) %}
          {% else %}
            {% set platform_data = { "channel": "%s", "clickAction": "%s", "image": "%s"} | format(channel_name, clickAction, snapshot_access_file_path) %}
          {% endif %}
        {% endif %}
      {% endif %}
      {{ platform_data }}
     
13 Likes

Any chance of adding priority to this? My notifications to Android disappear into a black hole without this code.

data:
  ttl: 0
  priority: high

I read about that being replaced with ā€œimportanceā€: ā€œhighā€, but maybe that wasnā€™t correct.
I addedd priority and TTL here in the post and on the Gist.

Can you confirm if itā€™s working better now?

Can someone also confirm if ClickAction is working for them? I seem to have some issues with it.

Nope still not working. I tried this blueprint: Low battery level detection & notification for all battery sensors which didnt work intially. Then I added

data:
  ttl: 0
  priority: high

And that sorted it out. I have also had to do this with all of my notification automations. They just do not come through without it.

i just tested swapping out ā€˜priorityā€™ for ā€˜importanceā€™ in one of my other automations and it didnt come through, changed it back to ā€˜priorityā€™ and it came though instantly.

I removed ā€œimportanceā€ now.
Only ā€œttlā€: ā€œ0ā€, ā€œpriorityā€: ā€œhighā€ is remaining.
Remove and re-add the blueprint, create a new automation and let me know.

Thanks

Still no dice, Iā€™m afraid. This shows in the logs: https://pastebin.com/8UBn7q1h

Hmm I see an error in your logs about the TTL value.
Perhaps I shouldnā€™t put the 0 between double quotes.

I changed it here and in the gist.
Please let me know.
Thanks for your assistance.

is it possible to add conditions to this blueprint??

Yes thats it! Itā€™s working. Thanks ever so much!

What conditions are you thinking about?

Thanks for the confirmation.

How should the url be configured?

Iā€™ve tried my nabiucasa url and a just https://www.google.com in the clickable uri field but I only get this on my phone when I click the notification picture.

Iā€™m struggling with this as well.
According to the documentation it should work with just an URI.

Edit: I think itā€™s because I have to use the format function for an URI, but I have already used it for the URI for the attachment image.

Iā€™ll have to ask some advice on this.

According to the Jinja documentation I can use the function twice.
https://jinja.palletsprojects.com/en/2.11.x/templates/#format

      {% set android_data = { "channel": "alarm_motion", "group": "alarm_motion", "ttl": 0, "priority": "high", "clickAction": "%s", "image": "%s"} | format(clickAction, snapshot_access_file_path) %}
      {% set ios_data = { "channel": "alarm_motion", "url": "%s", "attachment": {"url": "%s", "content_type": "JPEG"}, "push": { "sound": { "name": "default", "critical": 1, "volume": 1.0 } } } | format(clickAction, snapshot_access_file_path) %}
      {{ ios_data if is_ios else android_data }}

I have made a new version of the blueprint here and on the gist.

Please import the new version and try again.

Thanks

Edit: In my tests, it now works. You can just use relative URIā€™s, you donā€™t have to use you full URI. I made a test with /lovelace/cctv and it opens this lovelace view in the Home Assistant app.

hi, great blueprint. Is it possible to add time conditions? For example day and timeframe.

Cheers

Great,

I will try it again.

Also, I receive notifications with the snapshot on my phone when Iā€™m on my wifi network but when Iā€™m on data I only get the notification. Thereā€™s no snapshot with it.

Any ideas as to why I donā€™t get the snapshot when Iā€™m on data?

Thanks

The clickable notification works now. Itā€™s great

I still donā€™t get a snapshot while Iā€™m on data but the notification is still clickable so it does take me to my nabucasa lovelace veiw. Just would be good to see the snapshot on data.

Either way this is very useful to have.

Thanks again for creating this.

Iā€™m guessing the URL for the snapshot does not work when youā€™re not connected to your WiFi network. Do you have the internal & external URL in your HomeAssistant set-up correctly? ā€œConfiguration - General - External URL & Internal URLā€

Iā€™ll have a look at this.

1 Like

This was it. I didnā€™t have the external or internal URL defined.

Everythingā€™s working. I get a snapshot on both wifi and data.

Thanks for all the help.