I have an automation which runs whenever someone presses my video doorbell. Amongst other things, it sends an image to my Galaxy S24 Ultra phone along with a notification. The automation runs the script below.
If I run the automation manually I usually get the notification and image on the phone within a couple of seconds. In “real life” however, I often receive the notification 10-15 minutes after the automation has executed, even when my phone has been connected to the LAN for the entire period.
I have excluded the HA app from battery optimisation on the phone.
Any idea why these delays might be occurring, and what I might do to eliminate them?
Have you ruled out that it’s the automation trigger that’s causing the delay?
Your post mentions running the automation manually, but the code you posted looks like a script (which I assume is being triggered by your automation).
For example, is it possible that the doorbell isn’t working as you expect and triggers late? You can rule this out with a persistent_notification or print-to-log in your automation before you call the script that gets the photo and notifies your phone.
According to the HA log the automation was triggered at 15:16. This in turn called the script at 15:16, but the notification didn’t arrive on my phone until 15:27
You can check that a notification was successfully received via Notification history in the Companion App settings. Here you will see the command and how the notification was received, whether it be Websocket or FCM.
Read more about tests I have done with sending messages to Android Companion App in Browser Mod Wiki article. Android Devices 'Always On' · thomasloven/hass-browser_mod Wiki · GitHub. Delays when sending via FCM can be inevitable but you can try setting message paramaters to minimise delay.
It’s android’s fault, i’d say. I have same experience, too on my S22. I also have notifications set for my Prusa app to get one when 3D printing is finished and i get notification 10, 15+ minutes after print is done, even later. I’ve tried all possible settings on my phone, but nothing helps.
Interesting thing is that the moment i pick up my phone and “wake it up” (with my finger) notification arrives. Which clearly shows that sleeping phone is “dead” for these notifications…
And…yes, i do have ttl and priority set as suggested above.
The Notification History in the app confirms that the notification arrived at 15:31 - 15 minutes after the automation was triggered. The Notification source was FCM,
How would this syntax fit around my current script structure - is this correct?
alias: send_camera_snapshot_doorbell_pressed
sequence:
- variables:
file_time: "{{ now().strftime('%Y%m%d_%H%M%S') }}"
- data:
filename: /config/www/{{ camera_id }}_snapshot_{{ file_time }}.jpg
target:
entity_id: camera.{{ camera_id }}
action: camera.snapshot
- data:
message: "{{ camera_id }} alarm"
title: Doorbell Pressed
data:
image: /local/{{ camera_id }}_snapshot_{{ file_time }}.jpg
channel: Motion
priority: high
visibility: public
data:
ttl: 0
priority: high
actions:
- action: URI
title: Open Image
uri: >-
https://mydomain/local/{{ camera_id
}}_snapshot_{{file_time }}.jpg
- action: URI
title: Open Cameras
uri: https://mydomain/lovelace/cameras
action: notify.mobile_app_ian_galaxy_s24_ultra
mode: single
description: ""
- data:
message: "{{ camera_id }} alarm"
title: Doorbell Pressed
data:
image: /local/{{ camera_id }}_snapshot_{{ file_time }}.jpg
channel: Motion
priority: high
ttl: 0
visibility: public
actions:
- action: URI
title: Open Image
uri: >-
https://myddnsdomain/local/{{ camera_id
}}_snapshot_{{file_time }}.jpg
ttl = time to live, this is the setting that makes the notification instant. Samsung also has additional battery savers that can delay the text. This page is older but I think it’s still applicable to the latest samsung OS.