📸 Notification with camera snapshot with blocking condition and clickaction

Tags: #<Tag:0x00007fc40df42378>

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 and illuminance (+ 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.

Gist: https://gist.github.com/Stan-Gobien/1d142c91dbddb47879a8b0cc29a2951f

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 }}"

  - 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 }}
     
3 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.