blueprint:
  name: Donetick — announce a due task
  description: >-
    Receive Donetick task.reminder webhooks and run your chosen notification
    actions for due reminders only. No polling or API token. Home Assistant
    generates the secret webhook ID for you. Notifications outside the allowed
    hours are discarded, not delayed. Multiple Donetick recipients can produce
    duplicate events.
    See https://donetick.com/blog/donetick-home-assistant-alexa/ for setup.
  domain: automation
  input:
    webhook_trigger:
      name: Webhook
      description: >-
        Select ADD TRIGGER and choose Webhook. Home Assistant fills in a secret
        webhook ID for you; the copy button beside it copies the full URL to
        paste into Donetick. Keep that URL secret and never reuse an ID from a
        tutorial. Use the cog icon to allow only POST, and to turn off "Only
        accept local requests" if Donetick Cloud or Home Assistant Cloud
        delivers the webhook. Use HTTPS for internet delivery.
      selector:
        trigger:
    start_time:
      name: Allow notifications from
      description: Home Assistant local time. Events outside this window are discarded.
      default: "08:00:00"
      selector:
        time:
    end_time:
      name: Allow notifications until
      default: "21:00:00"
      selector:
        time:
    notification_actions:
      name: What should happen when a task is due?
      description: >-
        Add a notification action. Use {{ donetick_message }} for its message.
        Other variables: donetick_task, donetick_assignee, donetick_due_date,
        donetick_task_id. Default creates a Home Assistant inbox notification.
      default:
        - action: persistent_notification.create
          data:
            title: Donetick
            message: "{{ donetick_message }}"
      selector:
        action:

triggers: !input webhook_trigger

conditions:
  - condition: template
    value_template: >-
      {% set event = trigger.json | default({}, true) -%}
      {% set data = event.get('data', {}) if event is mapping else {} -%}
      {{ event is mapping and event.get('type') == 'task.reminder'
         and data is mapping and data.get('type') == 'due'
         and data.get('name') is string and data.get('name') | trim | length > 0
         and data.get('id') is number
         and data.get('due_date') is string }}
  - condition: time
    after: !input start_time
    before: !input end_time

actions:
  - variables:
      donetick_task: "{{ trigger.json.data.name | trim }}"
      donetick_assignee: "{{ trigger.json.data.assignee | default('', true) | striptags | trim }}"
      donetick_due_date: "{{ trigger.json.data.due_date }}"
      donetick_task_id: "{{ trigger.json.data.id }}"
  - variables:
      donetick_message: >-
        Donetick reminder: {{ donetick_task | striptags }} is due now{% if donetick_assignee %}. Assigned to {{ donetick_assignee }}{% endif %}.
  - sequence: !input notification_actions

mode: queued
max: 20
max_exceeded: warning
