Skip to content

Per-device conditions for mobile_push devices (skip devices that aren't in use) #299

Description

@lollox80

Problem

A person's mobile_push delivery fans out to every enabled mobile device: phone, tablet,
laptop, watch. Each one gets its own push, whether or not anyone is using it:

  • macOS laptop asleep / lid closed: APNs queues the push and delivers it on wake,
    often hours later, as a stale alert.
  • Watch not on the wrist: the alert is pointless, and when it's back on it
    duplicates what the phone already showed.
  • Tablet left on a desk: it lights up and chimes in an empty room.

Today the only per-device control is the static enabled flag in mobile_devices
(MOBILE_DEVICE_SCHEMA). Device snoozes (#269) cover manual, time-based muting, but not "only when the device is actually in use". The workaround is to switch off discovery and build one
mobile_push delivery per device, each with its own conditions and explicit target.
That multiplies deliveries, duplicates scenario wiring, and makes the trace harder to read.

The Companion apps already expose the state we need:

Device Entity Meaning
macOS binary_sensor.<mac>_active off on sleep, screensaver, display off, locked
Android binary_sensor.<phone>_interactive screen on
Wear OS binary_sensor.<watch>_on_body watch worn
iOS / iPadOS no direct equivalent could use Focus, battery state or a user template

Proposal

Add optional conditions to MOBILE_DEVICE_SCHEMA, using the same HA condition syntax
already used by deliveries and scenarios:

recipients:
  - person: person.lorenzo
    mobile_devices:
      - mobile_app_id: mobile_app_macbook
        conditions:
          - condition: state
            entity_id: binary_sensor.macbook_active
            state: "on"
      - mobile_app_id: mobile_app_orologio
        conditions:
          - condition: state
            entity_id: binary_sensor.orologio_on_body
            state: "on"

Behaviour:

  1. The conditions are evaluated per notification, at target resolution, with the same
    ConditionVariables the delivery gets. That gives access to priority, so
    priority: critical can bypass a device filter if the user wants.
  2. A device whose conditions fail is dropped from that envelope's mobile_app_ids. The
    enabled flag stays as the static on/off switch.
  3. Fail-open: if evaluation raises or a referenced entity is unavailable/unknown,
    deliver anyway. Losing a notification is worse than an extra one.
  4. Merge with discovery: as with the other mobile_devices keys, conditions on a
    configured entry is overlaid onto the discovered device with the same mobile_app_id,
    so discovery can stay on.
  5. Debug trace / archive: record skipped devices with a reason, e.g.
    mobile_app_orologio: skipped (device conditions), so cards and diagnostics can
    explain why a device got nothing.
  6. If all devices are filtered out, the delivery reports "no targets" as it does
    today for an empty target, rather than "failed" (and, in line with Outcome is partial_delivery (logbook: 'partly sent') whenever any delivery is skipped, even for condition / disabled / no-target skips #232, shouldn't turn the outcome into a partial delivery).

Open questions

  • Skip vs. downgrade. For iOS, sending with interruption-level: passive might be
    better than skipping, since the alert stays in Notification Centre without waking the
    device. Should a device be able to say when_unavailable: skip | passive? This could
    be a follow-up.
  • Auto-mapping. Should SuperNotify detect _active, _interactive and _on_body in
    the device registry and offer them as defaults? I'd keep v1 explicit and opt-in.
  • Phone-only fallback. Should there be a guard so the primary device is never filtered
    out, or is that left to the user's conditions plus priority?

I'm happy to implement this. It touches schema.py (MOBILE_DEVICE_SCHEMA),
people.py (keeping compiled conditions per device, merging on discovery) and the
target resolution for mobile_push, plus tests and docs.


Generated by Claude Code

Activity

  1. jeyrb commented on Oct 24, 2026

    @jeyrb
    Contributor

    Auto-mapping should work, since the device is discovered for the mobile app, and the device has these entities. That could mean no conditions, which makes it simpler for users and easier to move recipient to configflow.

    So could default to only sending to active devices, and use the device's entities to work out which state represents that. Then recipient config, or notification action data, only needed to override that and say send it even if device isn't worn/active or unknown state.

    Other options here, some of which come up before - only target a subset of classes, e.g. phone only or everything except iPad, and the fallback idea you have, where there's an order based on type of device, or user picked out order.

    I don't trust iCloud integration, so haven't had full range of sensor entities on my devices. I do however have the annoyance of occasionally using an iPad and having lots of old notifications appear.

  2. lollox80 commented on Nov 7, 2026

    @lollox80
    CollaboratorAuthor

    Agreed, auto-mapping is better than hand-written conditions: no YAML, and it fits the recipient editor in 2.18.

    A proposal for a first version:

    1. Default: a device is skipped only when its "in use" sensor clearly says no (_active off on macOS, _interactive off on Android, _on_body off on a watch). I'd find it among the device's own entities in the registry, not by name.
    2. When in doubt, send: a device with no such sensor (like iPads without iCloud), or a state that's unknown/unavailable, gets the notification as today. An extra notification is better than a lost one.
    3. Critical always goes through.
    4. Safety net: if every device of a person turns out to be inactive, the phone gets it anyway, so the notification doesn't vanish.
    5. Override: per device on the recipient, "send even when not in use"; plus a field on the action to force it for a single notification.
    6. Trace: skipped devices show up in the trace with the reason.

    I'd keep the other ideas (only some classes of device, fallback order) for a second step. Your iPad case - old notifications when you pick it up again - I think is better solved by "everything except iPad" than by a sensor.

    If that works for you, I'll prepare the PR.

  3. added theissue type on Nov 15, 2026
  4. jeyrb commented on Nov 19, 2026

    @jeyrb
    Contributor

    Sorry, missed this when travelling.

    Yes, please go ahead with PR.

    Related thing is "notify once with best method" which is missing and hard to do well because of fire-and-forget notifications ( although it could use the wait for response type HA action call to make it a bit more reliable ).

  5. lollox80 commented on Nov 23, 2026

    @lollox80
    CollaboratorAuthor

    PR is up: #336, with the change discussed above (Android's is_interactive isn't auto-mapped, since it's only "screen on" and a phone in a pocket would count as idle - the PR description explains).

    On "notify once with best method", I tried the one piece that would make it reliable rather than fire-and-forget: the Android Companion app's receipt confirmation.

    Sending to a phone with data: confirmation: true, the app fires mobile_app_notification_received back to HA when the notification actually arrives on the device. On my phone it came back within a second or two, carrying the tag, title and message that were sent plus the device_id and webhook_id of the device that got it:

    event_type: mobile_app_notification_received
    data:
      tag: prova_confirmation_299
      confirmation: 'true'
      title: Prova #299
      message: Test confirmation (confirmation: true)
      webhook_id: <webhook id>
      device_id: 68c049ac98276675
    origin: REMOTE

    (it's in MessagingManager.kt, only fired when confirmation is set)

    So on Android "notify once" could be: send to the best device with confirmation: true and a tag, wait a few seconds for that event matching tag + device_id, and if it doesn't come, move on to the next device or delivery. That's the same shape as the per-delivery fallback from #256, which today only triggers on an error - it would just add "no receipt" as a trigger.

    iOS has no equivalent: I couldn't find any received-event in the iOS app source, only the action events. So there it stays fire-and-forget, or fallback on error only. An action with a response wouldn't help either, as notify.mobile_app_* returns nothing about delivery.

    Happy to sketch it as a separate issue if you think it's worth it.

  6. reopened this on Nov 24, 2026
  7. jeyrb commented on Nov 24, 2026

    @jeyrb
    Contributor

    Thanks for #336 and the testing.

    Yes please, sketch receipt-confirmation as a separate issue: Android-only is fine as long as iOS degrades to today's behaviour, and it fits with the fallback trigger from #256.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions