📸 Notification with camera snapshot with blocking condition and clickaction

Tags: #<Tag:0x00007f3266fe6718>

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.

Gist: https://gist.github.com/Stan-Gobien/4540888f4eb2af42e765bc3307b4856f

blueprint:
  name: Send a notification with camera snapshot when motion is detected with blocking state and URL (or lovelace view) on click 
  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).
    
    
    Required entities:
      - Motion sensor (binary_sensor in motion class)
      - Target entity (camera)
      - Target entity (mobile_app)
    
    Optional features:
      Is is also possible to define a blocking entity, which blocks the automation from running when this entity's state is on.
      If you do not enable the optional entities the automation will skip these conditions.
    
    Optional entities:
      - Blocking entity (any entity with state on/off), example people home or not
      - URI for clickAction on the notification

  domain: automation

  input:
    motion_sensor:
      name: Motion sensor
      description: The sensor wich triggers the snapshot creation
      selector:
          entity:
            domain: binary_sensor
            device_class: motion

    camera:
      name: Camera
      description: The camera which creates the snapshot
      selector:
        entity:
          domain: camera

    notify_device:
      name: Device to notify
      description: Device needs to run 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:

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

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


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
  

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: >
      {% 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", "apns_headers": { "apns-collapse-id": "alarm_motion" }, "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 }}
     
1 Like

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.