Ntfy.sh with actions

Hello,

Just a small post to share my configuration to use ntfy.sh with home assistant and action on notification.

In shell_command :

ntfy: >
    curl
    -X POST
    --url 'https://USERNAME:PASSWORDM@URL_NTFY/{{ topic | default("maison") }}'
    --data "{{ message }}"
    --header 'X-Title: {{ title }}'
    --header 'X-Tags: {{ tags  | default("") }}'
    --header 'X-Priority: {{ priority | default("default") }}'
    --header 'X-Actions: {{ actions | default("") | replace("%ha%","URL_HA/api/events/ntfy,method=POST,headers.Authorization=Bearer LONG_LIFE_TOKEN") }}'
    --header 'X-Click: {{ click  | default("") }}'
    --header 'X-Icon: {{ icon  | default("") }}'

Donā€™t forget to replace :

  • USERNAME : username for ntfy.sh
  • PASSWORD : password for ntfy.sh
  • URL_NTFY : url of your ntfy.sh
  • URL_HA : url of your HA
  • LONG_LIFE_TOKEN : HA long token (on your profils page)

After that you can use action in automation like that :

alias: ask test
description: ""
trigger: []
condition: []
action:
  - variables:
      action_open: "{{ 'OPEN_' ~ context.id }}"
      action_close: "{{ 'CLOSE_' ~ context.id }}"
  - service: shell_command.ntfy
    data:
      topic: loic
      title: coucou
      message: The blinds are half-open. Do you want to adjust this?
      actions: >-
        http,Open,%ha%,body={"action":"{{action_open}}"};
        http,Close,%ha%,body={"action":"{{action_close}}"};
  - wait_for_trigger:
      - platform: event
        event_type: ntfy
        event_data:
          action: "{{ action_open }}"
      - platform: event
        event_type: ntfy
        event_data:
          action: "{{ action_close }}"
  - choose:
      - conditions: "{{ wait.trigger.event.data.action == action_open }}"
        sequence:
          - service: cover.open_cover
            target:
              entity_id: cover.some_cover
            data: {}
      - conditions: "{{ wait.trigger.event.data.action == action_close }}"
        sequence:
          - service: cover.close_cover
            target:
              entity_id: cover.some_cover
            data: {}
mode: single

I hope itā€™s can help user to use action with ntfy.sh.

4 Likes

Good day, thank you for sharing this. Iā€™m using a rest command I found in another post. Please see below:

ntfy_notify:
  url: https://ntfy_url/homeassistant
  method: POST
  headers:
    authorization: "Bearer token"  
    username: "admin"
    password: !secret network_password
    title: "{{ title }}"
    icon: "{{ icon }}"
    priority: "{{ priority | default('default') }}"
    tags: "{{ tags | join(',') }}"
    click: "{{ click }}"
    attach: "{{ attach }}"
    actions: "{{ actions | join(',') }}"
  payload: "{{ message }}"

I was wondering if you could guide me on how to use the actions if using the above rest command.

Iā€™ve tried to use your example:

      actions: >-
        http,Open,%ha%,body={"action":"{{action_open}}"};

by defining ha as a variable in the automation, but im failing. Any guidance is much appreciated.

Edit: never mind. got it. All i had to do is modify my actions definition in the rest command with yours.
Thank you.

Stumbled on this but I seem to be getting a 401 error when i click the action in ntfy is there something specific you did to get this working outside your home network? As Iā€™m looking at the automation action Iā€™m wondering if itā€™s a limitation of the action since it shows HTTP and not HTTPS?

Iā€™m just not sure why the notification comes through and the click action doesnā€™t work (I did include a long-lived token).

Your ntfy instance needs to have a public domain.

Hello did you ever solve this issue?
My setup stop working and i can confirm is an authorization problem.
possibly this section:
ā€“header ā€˜X-Actions: {{ actions | default(ā€œā€) | replace(ā€œ%ha%ā€,ā€œURL_HA/api/events/ntfy,method=POST,headers.Authorization=Bearer LONG_LIFE_TOKENā€) }}ā€™

I use this to send ntfy messages: GitHub - ivanmihov/homeassistant-ntfy.sh: Home Assistant integration for ntfy.sh

configuration.yaml > notify:

- name: ntfy_cameras
  platform: ntfy
  url: !secret ntfy_url
  token: !secret ntfy_token
  topic: hass_ai_cameras
  # icon: <notification_icon_url>

action

action: notify.ntfy_cameras
data:
  title: Alley AI Alert!
  message: "{{generated_content['text']}}"
  data:
    tags:
      - warning
      - video_camera
    attach: https://ha.example.com/local/snapshots/alley_{{trig_ts}}_snapshot2.jpg
    filename: alley_ai.jpg
    click: https://ha.example.com/dashboard-cameras/alley
    actions: 
      - action: "view"
        label: "View Event"
        url: >-
          https://zm.example.com/zm/cgi-bin/nph-zms?mode=jpeg&scale=50&buffer=1000&replay=single&event={{eid}}&user=<user>&pass=<pass>

thank you very much. Iā€™ve seen it and I am able to send notifications with actions with the rest command I posted before.
The issue is the ā€˜actionable notificationsā€™ do not work.
You can/could create an action button in the ntfy message and this button would trigger a script or whatever in home assistant.
This was facilitated using the ā€œactionsā€ argument (ā€“header ā€˜X-Actions: {{ actions | default(ā€œā€) | replace(ā€œ%ha%ā€,ā€œURL_HA/api/events/ntfy,method=POST,headers.Authorization=Bearer LONG_LIFE_TOKENā€) }}ā€™).
It no longer works. ntfy is not able to pass the authorization token to home assistant. Itā€™s probably something to do with the syntax of this command.
Iā€™m not sure, dont know enough.