Blueprint to clear notifications that is stuck when the automation fails

A simple blueprint to clear notifications that are “stuck” on android from when the automation stops for some reason.

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

blueprint:
  name: clear_notifications
  description: "Clears notifications that gets stuck when the automations failed. \n
    This automation is default set to only be able to run when no other automations are 
    currently running as the idea of this blueprint is to clear the 'stuck' notifications. \n 
    But you have the option to add automations that you want to be able to run alongside this 
    one under the exclude parameter."
  domain: automation
  input:
    device:
      name: Device
      description: Pick the device that you want to clear notifications on
      selector:
        device:
          integration: mobile_app
    exclude:
      name: Excluded Automations
      description: "Automations that you want to be allowed to run when you send the clear.\nOnly
        entities are supported, devices must be expanded!"
      default:
        entity_id: []
      selector:
        target:
          entity:
            domain: automation
    action_1:
      name: Action one
      description: Do something before the notification is cleared, like turn on/off a bool, wait for a script to execute.
      selector:
        action: {}
      default: []
    action_2:
      name: Action two
      description: Do something after the notification is cleared, like turn off/on a bool.
      selector:
        action: {}
      default: []
variables:
  exclude: !input exclude
# Triggers
trigger:
  - platform: event
    event_type: mobile_app_notification_action
condition:
  - condition: template
    value_template: >-
      {% set automation = namespace(runs=[]) %} {% for automations in states.automation
      | selectattr("attributes.current", "defined")
      | selectattr('attributes.current', '!=', '0')%}
      {% if 0 <= automations.attributes.current and not automations.entity_id in exclude.entity_id %}
      {% set automation.runs = automation.runs + [automations.attributes.current] %}
      {%endif%}
      {% endfor %} 
      {{automation.runs | sum == 0}}
action:
  - choose: []
    default: !input action_1
  - device_id: !input device
    domain: mobile_app
    type: notify
    message: clear_notification
    data:
      tag: "{{trigger.event.data['tag']}}"
  - choose: []
    default: !input action_2
mode: restart